#!/usr/bin/perl -w

#
# "SystemImager" - Copyright (C) 1999-2001 Brian Elliott Finley <brian@systemimager.org>
#
#   Others who have contributed to this code (in alphabetical order):
#
# This file is: rmimage
#

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

# declare modules
use Getopt::Long;
use File::Copy;
use File::Path;
use AppConfig;

### BEGIN parse the config file ###

my $config = AppConfig->new(
			    'autoinstall_script_dir' => { ARGCOUNT => 1 },
			    'autoinstall_boot_dir' => { ARGCOUNT => 1 },
			    'default_imagedir' => { ARGCOUNT => 1 },
			    'rsyncd_conf' => { ARGCOUNT => 1 },
			    'config_dir' => { ARGCOUNT => 1 },
			    );

$config->file('/etc/systemimager/systemimager.conf');

my $autoinstall_script_dir = $config->autoinstall_script_dir();
my $rsyncd_conf = $config->rsyncd_conf();

if (!$autoinstall_script_dir) {
    die "AUTOINSTALL_SCRIPT_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\@systemimager.org>
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                   Don\'t ask for confirmation before 
                          overwriting the destination image or master
			  autoinstall script (if they exist).

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();

# get full path to image from $rsyncd_conf
$file=$rsyncd_conf;
open (FILE, "<$file") or die "FATAL: Couldn't open $file for reading!\n";
  while (<FILE>) {
    if (/^\s*path\s*=.*$image/) { 
      (my $junk, $imagedir) = split (/=/);
      $imagedir =~ s/^\s+//;
      $imagedir =~ s/\s+$//;
    }
  }
close FILE;

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

# 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 ###
$file=$rsyncd_conf;
if($verbose) {print "  Removing entries from $file.\n";}
open (FILE, "<$file") or die "FATAL: Couldn't open $file for reading!\n";

$tmp_file="/tmp/tmp_file.$$";
open (TMP_FILE, ">$tmp_file") or die "FATAL: Couldn't open $tmp_file for writing!\n";
  while (<FILE>) {
    if (/\[$image\]/) {
      next;
    }
    elsif (/^\s*path\s*=.*$image/) { 
      next;
    }
    else {
      print TMP_FILE;
    }
  }
close TMP_FILE or die "FATAL: Can't close $tmp_file: $!";
close FILE or die "FATAL: Can't close $file: $!";

move($file, "$file.orig") or die "FATAL: Can't move $file to $file.orig: $!";
move($tmp_file, $file) or die "FATAL: Can't move $tmp_file to $file: $!";
### END Remove entries in $rsyncd_conf ###

exit 0;
