#!/usr/bin/perl -w

#
# "SystemImager" 
#
#  Copyright (C) 2002 Bald Guy Software 
#                     <brian.finley@baldguysoftware.com>
#
#  $Id: mkclientnetboot,v 1.7 2002/09/19 17:01:45 brianfinley Exp $
#

# set some variables
$VERSION="SYSTEMIMAGER_VERSION_STRING";
my $program_name="mkclientnetboot";
my $get_help = "  Try \"$program_name -help\" for more options.";

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

use vars qw($config $VERSION);

### BEGIN parse the config file ###

my $tftp_dir = $config->tftp_dir();
if (!$tftp_dir) { die "TFTP_DIR not defined in the config file."; }

#
### END parse the config file ###

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

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
my $help_info = $version_info . <<"EOF";

Usage: $program_name [OPTION]...

Options: (options can be presented in any order)

 -help                   Display this output.
                         
 -version                Display version and copyright information.
                         
 -verbose                Show filenames that are being removed.
                         
 -clients "HOST1 HOST2"  A space seperated list of host names and/or dotted
                         quad IP addresses.  This server (assuming it is a 
                         boot server) will be told to let these clients net
                         boot from this server, at least until they've 
                         completed a successful SystemImager autoinstall.

 -all-clients            Tell this server to let any and all clients net
                         boot from this server, at least until they've 
                         completed a successful SystemImager autoinstall.

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

# interpret command line options
GetOptions( 
    "help"              => \my $help,
    "version"           => \my $version,
    "verbose"           => \my $verbose,
    "clients=s"         => \my $clients,
    "all-clients"     => \my $all_clients,
) 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;
}

unless( ($clients) or ($all_clients) ) {
    print qq(FATAL: Please specify one or more clients with -clients or -all-clients.\n);
    print qq($get_help\n);
    exit 1;
}

SystemImager::Common->check_if_root();

if($clients) {
    # Make array from -clients "hostnames and ip addresses" -BEF-
    $_ = $clients;
    my @array = split;

    foreach my $client (@array) {

        # IP or Hostname?
        if(SystemImager::Common->valid_ip_quad($client)) {
            my $ip = $client;
            remove_boot_file($ip);
        } else {
            my @ips = get_ips($client);
            foreach my $ip ( @ips ) {
                remove_boot_file($ip);
            }
        }
    }
} elsif($all_clients) {
    # Remove all HEXBASED IP ADDRESS NAMED FILES in tftp_dir
    local *DIR;
    my @files;
    my $dir = "$tftp_dir/pxelinux.cfg";
    opendir(DIR, $dir) or die "Couldn't open $dir! $!";
        while( defined(my $file = readdir(DIR)) ) {
            remove_boot_file($file, "literal_file") if($file =~ m/^[[0-9A-F]{8}$/o);
        }
    close(DIR);
}


################################################################################
#
#   Subroutines
#
################################################################################


################################################################################
#
# Description:
# Removes a pxe boot config for the specified IP address.
#
# Usage:
# remove_boot_file($ip_address);
# remove_boot_file($file, "literal_file");
sub remove_boot_file {

    my ($ip_address, $literal_file) = @_;
    my $config_file;

    unless($literal_file) {
        $config_file = SystemImager::Server->ip_quad_2_ip_hex($ip_address);
    } else {
        $config_file = $ip_address;
    }

    my $file = $tftp_dir . "/pxelinux.cfg/" . $config_file;
    if ($verbose) { print "Removing $file.\n"; }
    if(-e $file) {
        unlink($file) or die ("FATAL: Couldn't remove $file!\n");
    }

    return 1;

}


################################################################################
#
# Description:
# Produce a list of IP addresses from a host name, and call remove_boot_file
# for each IP address.
#
# Usage:
# my @ips = get_ips($hostname);
sub get_ips {

    my $host = $_[0];

    my ($hinfo, @ips);
    if ( $hinfo = gethost($host) ) { 
        foreach my $addr ( @{$hinfo->addr_list} ) {
            push @ips, inet_ntoa($addr);
        }
    } else {
        die "Can't find an IP address for $host!\n";
    }

    return @ips;

}







