#!/usr/bin/perl
# Copyright (C) 2012-2013 Zentyal S.L.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
use strict;
use warnings;

use EBox;
use EBox::Global;

my $force = grep {
  ($_  eq '-f') or ($_ eq '--force')
} @ARGV;
my $help = grep {
  ($_  eq '-h') or ($_ eq '--help')
} @ARGV;

if ($help) {
    usage();
}

if (not $force) {
    print "WARNING!!\n";
    print "This will erase the mnesia database used by Zentyal jabber module. Chat-rooms, off line message and other setting will be lost. The only thing which would remain are the user accounts since they reside in Zentyal LDAP\n";
    while (1) {
        print "Do you want to continue? (y/n)\n";
        my $key;
        read(STDIN, $key, 1);
        if ($key eq 'y') {
            last;
        } elsif ($key eq 'n') {
            exit 1;;
        }
    }
}

EBox::init();
my $jabber = EBox::Global->modInstance('jabber');
$jabber->_clearDatabase();
$jabber->save();

sub usage
{
    print "$0 [-f|--force]\n";
    exit 0;
}

1;
