#!/usr/bin/perl -w
#
# "SystemImager"
#
#  Copyright (C) 2002 Bald Guy Software 
#                     Brian E. Finley <brian.finley@baldguysoftware.com>
#  Copyright (C) 2003 Brian E. Finley <finley@mcs.anl.gov>
#
#    $Id: si_netbootmond 3177 2005-07-13 00:16:47Z finley $
#
#    2005.07.12 Brian Elliott Finley
#    - apply umask patch for Daniel Widyono
#

use lib "USR_PREFIX/lib/systemimager/perl";
use strict;
use File::Copy;
use POSIX qw(setsid);
use SystemImager::Config;
use SystemImager::Server;
use vars qw($config $VERSION);

$0 = "si_netbootmond";

my $net_boot_default = lc $config->net_boot_default();
if("$net_boot_default" eq "net") {
    print STDERR qq(\nNET_BOOT_DEFAULT set to "$net_boot_default" in systemimager.conf.\n);
    print STDERR qq(No need for me here, so I'm exiting.\n);
    exit 0;
}

my $tftp_dir = $config->tftp_dir();
my $pxe_conf_dir;

unless($tftp_dir) {
    die "TFTP_DIR not specified in systemimager.conf file!";
} else {
    $pxe_conf_dir = "$tftp_dir" . "/pxelinux.cfg";
}

my $syslinux_cfg_localboot = "/etc/systemimager/pxelinux.cfg/syslinux.cfg.localboot";
my $pid_file = "/var/run/si_netbootmond.pid";
my $log_file = "/var/log/systemimager/rsyncd";


################################################################################
#
# Misc. Daemon stuff
#
################################################################################
chdir '/'                   or die "Can't chdir to /: $!";
open STDIN, '/dev/null'     or die "Can't read /dev/null: $!";
open STDOUT, '>>/dev/null'  or die "Can't write to /dev/null: $!";
open STDERR, '>>/dev/null'  or die "Can't write to /dev/null: $!";
umask 0;
sub REAPER {
    $SIG{CHLD} = \&REAPER;
    my $waitedpid = wait;
    print STDERR "Dead child: $waitedpid\n";
}
$SIG{CHLD} = \&REAPER;


################################################################################
#
# Fork the watching process
#
################################################################################
my $pid;
if ($pid = fork) {
} elsif (defined $pid) { # send the forked child off

    setsid or die "Can't start a new session: $!";

    my $file = $pid_file;
    local *FILE;
    open(FILE,">$file") or die ("FATAL: Can't open file: $file\n");
        print FILE "$$\n";
    close(FILE);

    # Lurk
    tail_rsync_log_file();

} else {
        die "Can't fork: $!\n";
}


################################################################################
#
# BEGIN subroutines 
#
sub tail_rsync_log_file {

    my $cmd = "tail -n 0 --follow=name $log_file";
    local *LOG_FILE;
    open(LOG_FILE,"$cmd |") || die("Can't $cmd!");
        while(<LOG_FILE>) {
       
            # Get individual field values for this line
            my @array = split(/\s+/);

            if ("$array[5]" eq "scripts/imaging_complete") {

                my $client_ip = $array[8];
                $client_ip =~ s/\(//g;
                $client_ip =~ s/\)//g;

                # diagnostic output
                #print "Configuring $client_ip for local booting.\n";
                create_no_boot_symlink($client_ip);
            }
        }
    close(LOG_FILE);
    exit 0;
}


# Usage: create_no_boot_symlink($client_ip);
sub create_no_boot_symlink {

    my ($client_ip) = @_;

    my $ip_hex = SystemImager::Server->ip_quad_2_ip_hex($client_ip);

    # Diagnostic output
    #print "ip_hex: $ip_hex\n";

    unlink("$pxe_conf_dir/$ip_hex");
    umask(0022);
    copy("$syslinux_cfg_localboot", "$pxe_conf_dir/$ip_hex");
    
}
#
# END subroutines
#
################################################################################

