#!/usr/bin/perl -w
use strict;

$|=1;
my ($pid,$sec,$c_min,$min,$hour,$c_hour,$mday,$mon,$year,$clavier,@contenu);
if (! -d $ENV{'HOME'}."/.atscript") { mkdir ($ENV{'HOME'}."/.atscript") or die $!; }

$clavier = " ";

if ($ARGV[0] and $ARGV[0] =~ /^\d+:\d+/)
{
    ($sec,$c_min,$c_hour,$mday,$mon,$year) = localtime;
    $year+=1900;
    ($hour,$min) = split(/:/,$ARGV[0]);
    if ($c_hour > $hour) { $mday +=1; }

    
    if ($sec == 0) { $sec = "00"; } elsif (1<$sec and $sec<10) { $sec = "0".$sec; }
    if ($min == 0) { $min = "00"; }     elsif (1<$min and $min<10) { $min = "0".$min; }
    if ($hour == 0) { $hour = "00"; }     elsif (1<$hour and $hour<10) { $hour = "0".$hour; }
    if ($mday == 0) { $mday = "00"; }     elsif (1<$mday and $mday<10) { $mday = "0".$mday; }
    if ($mon == 0) { $mon = "00"; }     elsif (1<$mon and $mon<10) { $mon = "0".$mon; }    
    print "Exectution a $hour : $min\n";
    print "Execution le $mday $mon $year a $hour $min : $sec\n";
    if (-e $ENV{'HOME'}."/.atscript/".$year.$mon.$mday.$hour.$min."00") 
	    { die("Il y a deja un travai pour cette heure la\n"); }
    open(INFO,">>".$ENV{'HOME'}."/.atscript/".$year.$mon.$mday.$hour.$min."00") or die $!;
    print "Ecriture dans ~/.atscript/".$year.$mon.$mday.$hour.$min."00\n";
    print "Entrer les commande, (quit pour arreter):\033[1m\n";
    while ($clavier !~ /^quit/i)
    {
    print "\t";
    push @contenu,$clavier;    
    $clavier=<STDIN>;
    }
    print "\033[0m";
    foreach (@contenu) {print INFO $_; }
    print "Envoie de signal!\n";
    &send_sig;

}
    

elsif ($ARGV[0] and $ARGV[0] =~ /^-?-l(?:ist)?$/)
{	
    $clavier=0;
    print "Liste en cours:\n";
    opendir(DIR,$ENV{'HOME'}."/.atscript") or die("Repertoire inexistant! $!\n");
    foreach(grep !/\.$/,readdir(DIR))
    {
    $clavier++;
    /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/ && do {print "\033[1m($clavier)\033[0m Prevu pour le ".$3."/".($2+1)."/".$1." a: \033[1m$3:$4:$5\033[0m \n"; };
    open(INFO,$ENV{'HOME'}."/.atscript/$_") or die $!;
    while(<INFO>)
	{
	print "\t$_\n";
	}
    close INFO;
    }
    close DIR;
}



elsif (!$ARGV[0] or $ARGV[0] =~ /^-?-h(?:elp)?$/)
{
    &show_commands;
    die "\n";
}


elsif ($ARGV[0] && $ARGV[0] =~ /^-?-d(?:el)?$/i && $ARGV[1] && $ARGV[1]=~/\d/) 
    {
     print "Supprime $ARGV[1]\n";  &del($ARGV[1]);
     }
    

else { print $ARGV[0].": commande inconnu\n"; &show_commands; }




sub show_commands
{
    print "\033[1mCommandes disponibles:\033[0m 
	heure:minutes	: Ajoute une nouvelle serie de commandes a executer.
	-h (help)	: Affiche cet aide.
	-l (list)	: Affiche toutes les commandes demandees.
	-d number	: Supprime la serie de commandes selectionnee.
	-dall		: Supprime toutes les series de commandes.\n\n";
}

sub del($)
{
    print "REcu: $_[0]\n\n";
    opendir(DIR,$ENV{'HOME'}."/.atscript") or die("Repertoire inexistant! $!\n");
    @contenu = grep !/\.$/,readdir(DIR);
    if ($contenu[$_[0]-1])
    {
        unlink($ENV{'HOME'}."/.atscript/".$contenu[$_[0]-1]) or die $!;
    }
    else { print "Job inexistant!\n"; }
    close DIR;
    &send_sig;    
}





sub send_sig {
    require POSIX;
    use Fcntl;
    sysopen(FIFO,"/var/run/atscript",O_RDWR) or die "No pipe ! $!";
    print FIFO " Update \n";
    select(undef,undef,undef,0.2);
    close FIFO;
    print "Message envoye\n";
}
