1: This is wackyass, a webcam archiving system.  No, it's not
   2: commented, and no there's no POD.  This was a little 
   3: project I did at home to archive single frame pics on a 24 
   4: hour rotation.  
   5: 
   6: use FileHandle;
   7: use LWP::Simple;
   8: 
   9: if ($ARGV[0] eq 'stop')
  10: { $kill = 1; }
  11: if (-e 'pidfile')
  12: { $running = 1; }
  13: if ($kill and $running)
  14: {
  15:     $pfh = new FileHandle 'pidfile', O_RDONLY;
  16:     chomp($pid = $pfh->getline);
  17:     chomp($stime = $pfh->getline);
  18:     undef $pfh;
  19:     $skill = kill 9, $pid;
  20:     $srm = unlink 'pidfile';
  21:     if ($skill and $srm)
  22:     {
  23: 	($sec, $min, $hour, $mday, $mon, $year,
  24: 	 $wday, $yday, $isdst) = localtime();
  25: 	$etime = '0' x (2 - length($hour)).$hour.':'.'0' x (2 - length($min)).$min.':'.'0' x (2 - length($sec)).$sec.' '.'0' x (2 - length($mon + 1)).($mon + 1).'/'.'0' x (2 - length($mday)).$mday.'/'.($year + 1900);
  26: 	$lfh = new FileHandle '../log/logfile', O_CREAT|O_APPEND;
  27: 	print $lfh "wackyass:$pid start time: $stime\n";
  28: 	print $lfh "wackyass:$pid end time: $etime\n";
  29: 	undef $lfh;
  30:     }
  31:     elsif ($skill)
  32:     { print "\nUnable to remove the pidfile.\n"; }
  33:     elsif ($srm)
  34:     { print "\nUnable to kill $pid.\n"; }
  35:     exit(0);
  36: }
  37: elsif ($kill)
  38: {
  39:     print "\nWackyass is not currently running.\n";
  40:     exit(0);
  41: }
  42: elsif ($running)
  43: {
  44:     print "\nWackyass is currently running.\n";
  45:     exit(0);
  46: }
  47: 
  48: ($sec, $min, $hour, $mday, $mon, $year,
  49:  $wday, $yday, $isdst) = localtime();
  50: $stime = '0' x (2 - length($hour)).$hour.':'.'0' x (2 - length($min)).$min.':'.'0' x (2 - length($sec)).$sec.' '.'0' x (2 - length($mon + 1)).($mon + 1).'/'.'0' x (2 - length($mday)).$mday.'/'.($year + 1900);
  51: $ppid = $$;
  52: $pfh = new FileHandle 'pidfile', O_CREAT|O_WRONLY;
  53: print $pfh "$$\n$stime\n";
  54: undef $pfh;
  55: 
  56: if ($pid = fork)
  57: {
  58:     $lmin = -1;
  59:     $i = 0;
  60:     for (;;)
  61:     {
  62: 	($sec, $min, $hour, $mday, $mon, $year,
  63: 	 $wday, $yday, $isdst) = localtime();
  64: 	$ssec = $sec;
  65: 	if ($lmin != $min)
  66: 	{
  67: 	    $i = 0;
  68: 	    $lmin = $min;
  69: 	}
  70: 	if ($sec > 50)
  71: 	{
  72: 	    $i = 0;
  73: 	    $min++;
  74: 	    $lmin = $min;
  75: 	}
  76: 	if ($i > 5)
  77: 	{ $i = 0; }
  78: 	if ($min == 60)
  79: 	{
  80: 	    $min = 0;
  81: 	    $lmin = $min;
  82: 	    $hour++;
  83: 	}
  84: 	if ($hour == 24)
  85: 	{ $hour = 0; }
  86: 	$filename = '/usr/local/apache/sites/wackyass/htdocs/'.'0' x (2 - length($hour)).$hour.'/'.'0' x (2 - length($min)).$min.$i.'.jpg';
  87: 	$ifh = new FileHandle "$filename", O_CREAT|O_WRONLY;
  88: 	($sec, $min, $hour, $mday, $mon, $year,
  89: 	 $wday, $yday, $isdst) = localtime();
  90: 	if ($sec < $ssec)
  91: 	{ $sec += 60; }
  92: 	$stime = 10 - ($sec - $ssec);
  93: 	sleep $stime;
  94: 	print $ifh get("http://192.168.0.108:8080");
  95: 	undef $ifh;
  96: 	$i++;
  97:     }
  98: }
  99: elsif (defined $pid)
 100: {
 101:     for (;;)
 102:     {
 103: 	($sec, $min, $hour, $mday, $mon, $year,
 104: 	 $wday, $yday, $isdst) = localtime();
 105: 	$min++;
 106: 	if ($min == 60)
 107: 	{
 108: 	    $hour++;
 109: 	    if ($hour == 24)
 110: 	    { $hour = 0; }
 111: 	    $min = 0;
 112: 	}
 113: 	for ($i = 0; $i < 6; $i++)
 114: 	{
 115: 	    $filename = '/usr/local/apache/sites/wackyass/htdocs/'.'0' x (2 - length($hour)).$hour.'/'.'0' x (2 - length($min)).$min.$i.'.jpg';
 116: 	    if ((-M $filename) >= 0.5)
 117: 	    { unlink $filename; }
 118: 	}
 119: 	if ($ppid != getppid)
 120: 	{ kill 9, $$; }
 121: 	sleep 60;
 122:     }
 123: }