#!/usr/bin/perl -w

#
# "SystemImager" - Copyright (C) 1999-2001 Brian Elliott Finley <brian@systemimager.org>
#
#   $Id: mkautoinstallscript,v 1.7.4.1 2001/11/30 04:19:00 dannf Exp $
#
#   Written by Brian Elliott Finley <brian@systemimager.org>
#

use lib "/usr/lib/systemimager/perl";
use lib "/usr/local/lib/systemimager/perl";
use Getopt::Long;
use AppConfig;
use SystemImager::Server;
use SystemImager::Common;

### 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();
my $config_dir = $config->config_dir();

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."; }
if (!$config_dir) { die "CONFIG_DIR not defined in the config file."; }
### END parse the config file ###

$version_number="2.0.1";
$program_name = "mkautoinstallscript";
$version_info = <<"EOF";
$program_name (part of SystemImager) v$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

$get_help = "\n       Try \"-help\" for more options.";

$help_info = $version_info . <<"EOF";

Usage: $program_name [OPTION]... -image IMAGENAME

Options: (options can be presented in any order and may be abbreviated)
 -help                    Display this output.

 -version                 Display version and copyright information.

 -quiet                   Don\'t print any output, just provide an 
                          appropriate exit code.

 -image IMAGENAME         Where IMAGENAME is the name of the image for 
                          which you want to create a new autoinstall 
			  script.

 -update-script [YES|NO]  Update the \$image.master script?  Defaults to 
                          NO if -quiet.  If not specified you will be
                          prompted to confirm an update.


The following options affect the autoinstall client after autoinstalling:

 -ip-assignment METHOD    Where METHOD can be:

                          static_dhcp -- A DHCP server will assign the
			    same static address each time to clients 
			    installed with this image.  Also see the
			    \"makedhcpstatic\" command.  
			    (This is the default)

			  dynamic_dhcp -- A DHCP server will assign IP
			    addresses dynamically to clients installed
			    with this image.  They may be assigned a
			    different address each time.

                          static -- The IP address the client uses
			    during autoinstall will be permanently
			    assigned to that client.

			  replicant -- Don\'t mess with the network
			    settings in this image.  I\'m using it as a
			    backup and quick restore mechanism for a 
			    single machine.

 -post-install ACTION     ACTION can be:

                          beep -- Clients will beep incessantly after 
			    succussful completion of an autoinstall.
			    (This is the default)

			  reboot -- Clients will reboot themselves
			    after successful completion of an 
			    autoinstall.

			  shutdown -- Clients will halt themselves
			    after successful completion of an 
			    autoinstall.


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

$ip_assignment_method = "static_dhcp";
$post_install = "beep";     # the default
$update_script = "no";      # the default

GetOptions( 
    "help" => \$help,
    "version" => \$version,
    "quiet" => \$quiet,
    "image=s" => \$image,
    "ip-assignment=s" => \$ip_assignment_method,
    "update-script=s" => \$update_script,
    "post-install=s" => \$post_install
) || die "$help_info";


### BEGIN evaluate options ###
# if requested, print help information
if($help) { 
  print "$help_info";
  exit 0;
}

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

# be sure $image is set
unless ($image) {
  die "\n$program_name: -image IMAGENAME must be specified.\n$get_help\n\n";
}

# be sure $image doesn't start with a hyphen
if ($image =~ /^-/) {
  die "\n$program_name: Image name can't start with a hyphen.\n$get_help\n\n";
}

# be sure $update_script was passed a proper option
if($update_script) {
  $update_script = lc $update_script;
  unless(
       ($update_script eq "yes")
    or ($update_script eq "no" )
  ) { die "\nERROR: -update-script must be YES or NO.\n$get_help\n\n"; }
}

### END evaluate options ###

# be sure program is run by root
SystemImager::Common->check_if_root();

my $base_imagedir=SystemImager::Server->get_full_path_to_image_from_rsyncd_conf( $rsyncd_conf, $image );
unless ($base_imagedir) { die "\nFATAL: can't find image entry in $rsyncd_conf!\n"; }

$imagedir="$base_imagedir$image";

$master_script="$autoinstall_script_dir/$image.master";

if(( -e "$master_script" ) && ( $update_script eq "no" )) {
  die qq(\nWARNING: "$master_script" already exists.\n         Use "-update-script YES" to overwrite.\n$get_help\n\n);
}

SystemImager::Server->validate_ip_assignment_option( $ip_assignment_method );
SystemImager::Server->validate_post_install_option( $post_install );

### BEGIN create a fresh $autoinstall_script_dir/$image.master ###
SystemImager::Server->create_autoinstall_script(
	$master_script,
	$config_dir,
	$image,
	$imagedir,
	$ip_assignment_method,
	$post_install
);
### END create a fresh $autoinstall_script_dir/$image.master ###
unless ($quiet) { print qq(\nYour new autoinstallscript has been created:\n\n); }
unless ($quiet) { print qq(  "$master_script"\n); }

exit 0;
