#!/usr/bin/perl -w
# 14 mars 2004
#
# PARTIE Lancement
# 	* Calcul le sleep a effectuer pour le prochain job
#	* Execute le job, et passe au suivant
# tfe@fallen-sun.com
##############################################################################


# INITIALISATION
use Fcntl;
require POSIX;
use strict;

my ($runed,$time,$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";
undef($forkpid);



sub log_send($)
{
$_ = shift;
open(LOG,">>/var/log/atscript");
print LOG $_;
close LOG;
}



if ($forkpid = fork){ log_send("Forkage...\n"); 
    if (open(INFO,"/var/run/atscript.pid"))  { 
	    $pid = <INFO>; 
	    if (kill 0,$pid) { log_send("ERROR , PID REACTIF!\n"); }
	    close INFO;
    }	
    open(WRITE,">/var/run/atscript.pid");
    print WRITE $forkpid;
    close WRITE;
    }
elsif (defined($forkpid))
    {
	$|=1;
	log_send("Dans le fork: ($$)\n");
        $current_time = &make_time;    
        ($c_year,$c_mon,$c_mday,$c_hour,$c_min,$c_sec) = $current_time =~ /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
        log_send "Time: $c_year $c_mon $c_mday $c_hour $c_min $c_sec\n";
        @users = &config() or die $!;
	log_send "USERS: "; log_send @users; log_send "\n";
	undef @times;	
        foreach $user(@users){
	    log_send "Lecture de $user\n";
	    if ($user ne 'root') {  opendir(DIR,"/home/$user/.atscript/") or next; }
	    else {  opendir(DIR,"/$user/.atscript/") or next; }
	    push (@times,"$_ $user")  foreach ( grep(! /^\./, readdir(DIR))); 
            close DIR;
        }	
	log_send scalar @times." fichiers dans le .atscript\n";
        @times = sort @times;

	log_send "Sleeping !\n";	
	
	
	
        foreach $time(@times) { 
	    log_send "Etude de $_\n";
	    ($file,$user) = split / /,$time; # user $min , #stop at $sec secondes.
            ($year,$mon,$mday,$hour,$min,$sec) = $file =~ /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
            log_send "Execution: $year $mon $mday $hour $min $sec\n";	
   	    $sleep =(($year-$c_year)*60*60*24*365 + ($mday-$c_mday)*60*60*24 + ($hour-$c_hour)*60*60 + ($min-$c_min)*60  +($sec-$c_sec));

    if ($sleep > -5 ) {
		if ($sleep > 0) { 
		           log_send "\t+ Sleep de $current_time a $file: soit une attente de $sleep secondes\n"; 
			   select(undef,undef,undef,$sleep);
			   }
		else { log_send("Pas de sieste, temps d attente negatif... mais execution\n"); }
		log_send "Apres le sleep:\n";
       		open(JOB,"/home/$user/.atscript/$file") or open(JOB,"/root/.atscript/$file")
       		    or log_send ("Erreur de lecture du fichier /home/$user\|/root\|/.atscript/$file\n");
       		foreach(<JOB>)    	
			    { 
			    s/\'//;  
			    chomp; 
			    $runed = "su $user -c \"$_\" &";
			    log_send "run: $runed\n"; 
			    system($runed);
			    }
       		close JOB;	    
    }
    else { log_send("Old job... pas de raison d execution\n");
   log_send "Suprression du fichier: $file\n";
   if ($user ne 'root'){unlink("/home/$user/.atscript/$file")	}		
   else {    unlink("/root/.atscript/$file"); }
   log_send "Nouveaux temp: $c_year,$c_mon,$c_mday,$c_hour,$c_min,$c_sec\n";
    $current_time = &make_time;    
    ($c_year,$c_mon,$c_mday,$c_hour,$c_min,$c_sec) = $current_time =~ /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
    }
    log_send("job fini... $$\n");
    } 
}


#########################################################################
sub make_time
{
    ($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
    if ($sec==0){ $sec="00";} elsif ($sec<10) { $sec = "0".$sec; }
    if ($min==0){ $min="00";} elsif ($min<10) { $min = "0".$min; }    
    if ($hour==0){ $hour="00";} elsif ($hour<10) { $hour = "0".$hour; }    
    if ($mday==0){ $mday="00";} elsif ($mday<10) { $mday = "0".$mday; }    
    if ($mon==0){ $mon="00";} elsif ($mon<10) { $mon = "0".$mon; }    
    $year = $year+1900;
    return $year.$mon.$mday.$hour.$min.$sec;
}

sub config {
    open(CONF,"/etc/atscript.conf") or die "Fatal: no config found /etc/atscript.conf\n";
    while(<CONF>)   {
        if (/^users:(.*)/) { @users = split / /,$1;  }
    }
    close CONF;
    if (!@users) { die "Fatal: no config users set in /etc/atscript.conf\n"; }
    return @users;
}


print STDOUT "Fin du lancement.\n";    
close LOG;

