#!/usr/bin/env perl
# BEGIN COPYRIGHT BLOCK
# This Program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; version 2 of the License.
# 
# 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.
# 
# Copyright (C) 2007 Red Hat, Inc.
# All rights reserved.
# END COPYRIGHT BLOCK
#

use lib qw(/usr/lib/x86_64-linux-gnu/dirsrv/perl);

use strict;

use File::Basename;
use File::Path;
use DSUtil;
use Resource;
use DSCreate qw(removeDSInstance);
use AdminServer qw(removeAdminServer);

sub usage {
        print(STDERR "Usage: $0 [-f] [-d -d ...]\n\n");
        print(STDERR " Opts: -f            - force removal\n");
        print(STDERR "       -a            - remove all\n");
        print(STDERR "       -d            - turn on debugging output\n");
        print(STDERR "       -y            - actually do the removal\n");
        print(STDERR "WARNING: This command is extremely destructive!\n");
        print(STDERR "         It will remove all of the data and configuration\n");
        print(STDERR "         of all directory servers and admin servers, with\n");
        print(STDERR "         no chance of recovery.  Therefore, in order to actually\n");
        print(STDERR "         do this, you must give the -y option.\n");
}

my $res = new Resource("/usr/share/dirsrv/properties/setup-ds.res",
                       "/usr/share/dirsrv/properties/setup-ds-admin.res");

my $i = 0;
my $force = "";
my $all = 0;
my $seeny;

# load args from the command line
while ($i <= $#ARGV) {
    if ( "$ARGV[$i]" eq "-f" ) { 
        $force = 1;
    } elsif ("$ARGV[$i]" eq "-a") {
        $all = 1;
    } elsif ("$ARGV[$i]" eq "-d") {
        $DSUtil::debuglevel++;
    } elsif ( "$ARGV[$i]" eq "-y" ) { 
        $seeny = 1;
    } else {
        &usage; exit(1);
    }
    $i++;
}

if (!$seeny) {
    &usage; exit(1);
}

my $baseconfigdir = $ENV{DS_CONFIG_DIR} || "/etc/dirsrv";
my @instances = ();
my @errs;

if ( ! -d $baseconfigdir )
{
    print STDERR "Error: $baseconfigdir does not exist\n";
    exit 1;
}

# get all of our directory server instances
for my $dir (glob("$baseconfigdir/slapd-*")) {
    next if ($dir =~ /\.removed/);
    if (-d $dir) {
        $dir =~ s,$baseconfigdir/,,; # strip off dir part
        $dir =~ s/slapd-//; # strip off slapd part
        push @instances, $dir;
    }
}

# remove all of the directory servers
for my $inst (@instances) {
    my $configdir = "$baseconfigdir/slapd-$inst";
    if ( ! -d $configdir )
    {
        print STDERR "Error: $configdir does not exist\n";
        if (!$force) {
            exit 1;
        }
    }
    @errs = removeDSInstance($inst, $force, $all);
    if (@errs) {
        print STDERR "The following errors occurred during removal of $inst:\n";
        for (@errs) {
            print STDERR $res->getText($_);
        }
        print STDERR "Error: could not remove directory server $inst\n";
        if (!$force) {
            exit 1;
        }
    }
}

# remove the admin server
if (@errs = removeAdminServer($baseconfigdir, $force, $all)) {
    print STDERR "The following errors occurred during removal of the admin server:\n";
    for (@errs) {
        print STDERR $res->getText($_);
    }
    print STDERR "Error: could not remove admin server\n";
    if (!$force) {
        exit 1;
    }
}

# if we got here, report success
print "Removed admin server and all directory server instances\n";
exit 0;

# emacs settings
# Local Variables:
# mode:perl
# indent-tabs-mode: nil
# tab-width: 4
# End:
