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

BEGIN { print(qq[\n\nproc $$ beginning now.\n\n]) }
END { print(qq[\n\nproc $$ exiting now.\n\n]) }

my($reminder)  = '';
my($debugging) = 0;
my($deamon)    = 0;
my($remaining) = 0;
my($attempted) = 0;
my($alarm)     = 0;
my($wait)      = 0;
my($inc)       = 5;
my($now)       = time();
my($n)         = qq[\n];

# %=%=%=%=%=%=%=%=%=%=%= FORKING =%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=
{
   # spawn child process, exit; deamonize child
   my($pid) = fork; ++$|;
   die(qq[Can't spawn daemon! $!])
      if
         (!defined($pid)); exit if ($pid);

   # spawn subdeamon process, exit
   my($piid) = fork; ++$|;
   die(qq[Can't spawn subdaemon! $!])
      if
         (!defined($piid)); exit if ($piid);

   # at this point we are the daemon

   # we just wait around and do nothing until time is up
   while (time < $now + ($alarm)) {

      $remaining = $alarm - $wait;

      my($snooze) = ($remaining < $inc) ? $remaining : $inc;

      $snooze = sprintf('%.0f', $snooze);

      print(<<__snooze__) if $debugging;
      $remaining seconds remaining.  going to snooze for $snooze seconds...
__snooze__

      if ($snooze) { sleep( $snooze ) } else { last }

      $wait += $inc;
   }

   # time is up now, make something happen here.
   {
      next
   }
}
# %=%=%=%=%=%=%=%=%=%=%= DONE FORKING =%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=