#!/usr/bin/perl use Getopt::Std; use Time::Local; use ShellColor; use POSIX; $pfad_zu_toenen="/home/lionstarr/Musik"; # ohne / am Ende sub usage{ print "$0 10 Sekunden Countdown\n"; print "$0 -s Zeit Countdown mit der Länge Zeit. Zeit z.B. 5m3s\n"; print "$0 -t Zeitpunkt Countdown bis zu Zeitpunkt. Zeitpunkt z.B. \"10.11.2007 00:00\"\n"; print "$0 -c Befehl Befehl, der beim Ende des Countdowns ausgeführt werden soll!\n\t(§i§ wird durch den Parameter i ersetzt, §l§ durch die Laufzeit, §n§ durch die Nummer des Aufrufs)\n"; print "$0 -n Zahl Wie oft Befehl ausgeführt werden soll\n"; print "$0 -d Zeitspanne Wie lange zwischen den einzelnen Ausführungen gewartet werdens soll!\n"; print "$0 -i Name Name der Eieruhr (in Command benutzbar)\n"; print "\t\t -s und -t sind nicht kombinierbar\n"; exit 1; } sub date_to_secs{ $string=shift; @date_time=split(/ /, $string); @date=split(/\./,$date_time[0]); @time=split(/:/,$date_time[1]); if(!(defined($time[2]))){ $time[2]= "00" }; $finish = timelocal($time[2],$time[1],$time[0],$date[0],$date[1]-1,$date[2]); return $finish - time; } sub time_to_secs{ $str = shift; if($str =~ /(\d+)y/){ $secs += $1*60*60*24*365; } if($str =~ /(\d+)d/){ $secs += $1*60*60*24; } if($str =~ /(\d+)h/){ $secs += $1*60*60; } if($str =~ /(\d+)m/){ $secs += $1*60; } if($str =~ /(\d+)s/){ $secs += $1; } return $secs; } sub secs_to_time{ $secs = shift; $years = $secs / (60*60*24*365); $years = floor($years); $secs -= $years *60*60*24*365; $days = $secs / (60*60*24); $days = floor($days); $secs -= $days *60*60*24; $hours = $secs / (60*60); $hours = floor($hours); $secs -= $hours *60*60; $minutes = $secs / (60); $minutes = floor($minutes); $secs -= $minutes *60; $string = ""; if($years > 0){ $string.= "${years}y "; } if($days > 0){ $string.= "${days}d "; } if($hours > 0){ $string.= "${hours}h "; } if($minutes > 0){ $string.= "${minutes}m "; } if($secs > 0){ $string.= "${secs}s "; } if($string == ""){ $string = "0s"; } return $string; } sub my_sleep{ @list=('|','/','-','\\'); $all=shift; $zeitpunkt=shift; $|=1; $i=$all; for(1..$all){ $i--; print "\r"; if($i<4){ print ShellColor::yellow(secs_to_time($i)); } else{ print secs_to_time($i); } if($zeitpunkt != ""){ print "bis zum $zeitpunkt"; } if($all<=10){ print " "; print ShellColor::green("-"x$i); print "_"x($all-$i); if($i<=5){ ` mplayer $pfad_zu_toenen/ton_sehr_hoch.mp3 2>&1 1>/dev/null &`; } else{ ` mplayer $pfad_zu_toenen/Musik/ton_hoch.mp3 2>&1 1>/dev/null &`; } } print " ",$list[$i%4], " "; if($all<=10){ select(undef,undef,undef,0.5); } else{ select(undef,undef,undef,1); } if($i==10){ print "\r"; print " "x100; my_sleep(10); return; } } print " - Fertig\n"; } my $optstring = "i:s:t:c:d:n:h"; getopts( $optstring, \%opt ); if($opt{h}){ usage(); } if($opt{s} && $opt{t}){ usage(); } if($opt{s}){ $sleep=time_to_secs($opt{s}); } elsif($opt{t}){ $sleep=date_to_secs($opt{t}); } $sleep ||= 10; $cmd=$opt{c}; $cmd ||="mplayer '$pfad_zu_toenen/bang.mp3' 2>&1 1>/dev/null && zenity --info --text '§i§ (Der Countdown lief §l§) Aufruf Nr. §n§' "; $opt{d} ||= 0; $opt{n} ||= 1; $opt{i} ||= "Unbenannter Countdown wurde beendet!"; $laufzeit=secs_to_time($sleep); $cmd =~ s/§i§/$opt{i}/; $cmd =~ s/§l§/$laufzeit/; if($opt{t}){ my_sleep($sleep,$opt{t}); } else{ my_sleep($sleep); } $delay=time_to_secs($opt{d}); if($opt{d} != 0){ foreach(0..$opt{n}-1){ $cmd_now=$cmd; $n = $_ +1; $cmd_now =~ s/§n§/$n/; system($cmd_now); select(undef,undef,undef,$delay); } } else{ foreach(0..$opt{n}-1){ $cmd_now=$cmd; $n = $_ +1; $cmd_now =~ s/§n§/$n/; system($cmd_now); } }