#!/usr/bin/perl -w

#
# "SystemImager" 
#
#  Copyright (C) 1999-2001 Brian Elliott Finley <brian.finley@baldguysoftware.com>
#  Copyright (C) 2002 Bald Guy Software <brian.finley@baldguysoftware.com>
#
#  $Id: rmimage,v 1.12 2003/11/24 22:16:06 brianfinley Exp $
#

# set some variables
$version_number="SYSTEMIMAGER_VERSION_STRING";
$program_name="rmimage";
$get_help = "         Try \"$program_name -help\" for more options.";

# declare modules
use lib "USR_PREFIX/lib/systemimager/perl";
use File::Copy;
use File::Path;
use Getopt::Long;
use SystemImager::Server;
use SystemImager::Common;
use SystemImager::Config;

### BEGIN parse the config file ###
my $autoinstall_script_dir = $config->autoinstall_script_dir();
my $rsyncd_conf = $config->rsyncd_conf();
my $rsync_stub_dir = $config->rsync_stub_dir();

if (!$autoinstall_script_dir) {
    die "AUTOINSTALL_SCRIPT_DIR not defined in the config file.";
}

if (!$rsync_stub_dir) {
    die "RSYNC_STUB_DIR not defined in the config file.";
}

if (!$rsyncd_conf) { die "RSYNCD_CONF not defined in the config file."; }

### END parse the config file ###

### BEGIN functions ###
sub trim {
  my @out = @_;
  for (@out) {
    s/^\s+//;
    s/\s+$//;
  }
  return wantarray ? @out : $out[0];
}
sub check_if_root{
    unless($< == 0) { die "$program_name: Must be run as root!\n"; }
}
### END functions ###

# set version information
$version_info = <<"EOF";
$program_name (part of SystemImager) version $version_number

Copyright (C) 1999-2001 Brian Elliott Finley <brian.finley\@baldguysoftware.com>
Copyright (C) 2002 Bald Guy Software <brian.finley\@baldguysoftware.com>
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
EOF

# set help information
$help_info = $version_info . <<"EOF";

Usage: $program_name [OPTION]... IMAGE

Options: (options can be presented in any order)

 -help                    Display this output.

 -version                 Display version and copyright information.

 -verbose                 Explain what is being done.

 -force                   Continue on error (default is to exit on error).

Tip: Use \"lsimage\" to get a list of available images.

Download, report bugs, and make suggestions at:
http://systemimager.org/
EOF

# interpret command line options
GetOptions( 
  "help" => \$help,
  "version" => \$version,
  "force" => \$force,
  "verbose" => \$verbose
) or die qq($help_info);

# if requested, print help information
if($help) {
  print qq($help_info);
  exit 0;
}

# if requested, print version and copyright information
if($version) {
  print qq($version_info);
  exit 0;
}

# Take the arguments left after Getopt::Long parses it's stuff out 
# as the source and destination image names.
$image=$ARGV[0];

unless($image) { die "\n$program_name: Must specify IMAGE.\n$get_help\n\n"; }

# be sure program is run by root
check_if_root();

$imagedir = SystemImager::Server->get_image_path($rsync_stub_dir, $image);

unless(($imagedir) or ($force)) { 
  print "FATAL: Can't get path to image $image from image's stub file!\n";
  print "       Nothing has been done.\n";
  exit 1;
}

# remove image
if($verbose) {print "  Removing image $image.\n";}
$file=$imagedir;
if($force) {
  if($file) { rmtree($file, 0, 0); }
} else {
  rmtree($file, 0, 0) or die "FATAL: Can't remove $file!\n";
}

# remove master autoinstall script
if($verbose) {print "  Removing master autoinstall script $image.master.\n";}
$file="$autoinstall_script_dir/$image.master";
if($force) {
  if($file) { unlink($file); }
} else {
  unlink($file) or die "FATAL: Can't remove $file!\n";
}

# remove soft links
if($verbose) {print "  Removing softlinks that point to $image.master.\n";}
$cmd="cd $autoinstall_script_dir; find . -lname $image.master -exec rm -f \\{\\} \\;";
system($cmd);
if($? != 0) { die "FATAL: couldn't remove softlinks that point to $image.master!"; }

### BEGIN Remove entries in $rsyncd_conf ###

SystemImager::Server->remove_image_stub($rsync_stub_dir, $image);
SystemImager::Server->gen_rsyncd_conf($rsync_stub_dir, $rsyncd_conf);

### END Remove entries in $rsyncd_conf ###

# remove image from flamethrower.conf file
my $entry_name = $image;
my $flamethrower_conf = "/etc/systemimager/flamethrower.conf";
if(-e $flamethrower_conf) {
    SystemImager::Common->add_or_delete_conf_file_entry($flamethrower_conf, $entry_name) or
        die "$program_name: Cannot remove entry from $flamethrower_conf";

    # remove override entry from flamethrower.conf file
    $entry_name = "override_" . $image;
    SystemImager::Common->add_or_delete_conf_file_entry($flamethrower_conf, $entry_name) or
        die "$program_name: Cannot remove entry from $flamethrower_conf";
}



exit 0;
