#!/usr/bin/perl -w
# 14 mars 2004
#
# PARTIE DAEMON
# 3 partie:
#     + Ecriture du pid fork dans /var/run/atscript.pid
#     + Ouverture d un pipe en lecture/ecriture
#     + sleep indefini pour ne pas quitter et garder un oeil sur le pipe.
# les fichiers se nomment en fonction du temps en seconde a la quelle ils vont etre lances.
# Script test, pour voir ou ca couillleeeeeeeeeeee....
##############################################################################

# INITIALISATION
use Fcntl;
require POSIX;
use strict;
use Proc::Daemon;
Proc::Daemon::Init;

my ($realpid,$forkpid,$logfile,$oldpid,$sleep,$current_time,$sec,$min,$hour,$mday,$mon,$year,$c_sec,$c_min,$c_hour,$c_mday,$c_mon,$c_year,$file
,$pid,$pipe,@users,$user,@times);
$|= 1;

$logfile = "/var/log/atscript";
$SIG{'CHLD'} = \&zombies;



sub log_send($)
{
    $_ = shift;
    open(LOG,">>$logfile");
    select LOG;
    print $_;
    close LOG;
}

sub pid_send($)
{
    $user = shift;
    if (open(PID,"/var/run/atscript.pid"))
    {
	$pid = <PID>; 
	log_send("$$ va tuer (peut etre?) le pid: $pid");	
	if (kill 0,$pid and $pid > $realpid) {log_send " Killing... "; kill 'KILL',$pid; }
	log_send(" OK !\n");	
    }
    close PID;
    log_send("New pid dans le fichier: $user\n");
    open(SPID,">/var/run/atscript.pid") or log_send("ERROR");
    select SPID;
    print $user;
    close SPID;
return 1;
}



#############################################################################

log_send "\n\n".gmtime(time)."\n********************************************\nRuning $0\n";

    if ($forkpid = fork)  
	{
	log_send("Fork reussi\n");	
        pid_send("$forkpid");	
	log_send("Fin log_send\n");
	}
    elsif(defined($forkpid))
	{
	&mise_a_jour;
	}
    log_send("Fin\n\n");
    select(undef,undef,undef,2);
    undef($forkpid);
    if ($forkpid = fork)  
	{
	log_send("Fork reussi\n");	
        pid_send("$forkpid");	
	log_send("Fin log_send\n");
	}
    elsif(defined($forkpid))
	{
	&mise_a_jour;
	}
    log_send("Fin\n\n");
    select(undef,undef,undef,2);
	
    undef($forkpid);
    if ($forkpid = fork)  
	{
	log_send("Fork reussi\n");	
        pid_send("$forkpid");	
	log_send("Fin log_send\n");
	}
    elsif(defined($forkpid))
	{
	&mise_a_jour;
	}

    log_send("Fin\n\n");
    select(undef,undef,undef,2);
	
    undef($forkpid);
    if ($forkpid = fork)  
	{
	log_send("Fork reussi\n");	
        pid_send("$forkpid");	
	log_send("Fin log_send\n");
	}
    elsif(defined($forkpid))
	{
	&mise_a_jour;
	}




















log_send("Fin 3 mise a jour !\n");


log_send  "Fin mise a  jour initiale...\n";
log_send("\n\n PID ATD: $$\n\n");
$realpid = $$;
close LOG;

$pipe = "/var/run/atscript";
if (-e $pipe) {    unlink $pipe;    }
POSIX::mkfifo($pipe,0666) or log_send "mknod failed $!\n";
chmod 0666, $pipe or log_send $!;
sysopen(FIFO,$pipe,O_RDWR) or log_send "LEcture failed\n";


#while(1)
# 
    while (<FIFO>)
    {
        if (/Update/) {   log_send "RECU: update !\n";    &mise_a_jour;log_send "\n  /\|\\Fin mise a jour (apres avoir recu update)  /\!\\\n\n";  }
        elsif (/Show/) {   log_send "RECU: show !\n"; print FIFO "Recu !n\n\nRECU !!\n";}
	else { log_send  "Message inconnu recu !\n"; }
    }
#
log_send(" FIN DU PROG R A M M E  E E E E E E??????????????\n");

        








sub mise_a_jour
{
	log_send("Dans le fork: ($$)\n");
	select(undef,undef,undef,30000000000);
	return 1;	
log_send("DONE!\n")
}


#########################################################################



sub zombies { 1 until (waitpid(-1 , 'WNOHANG') == -1); }
