@rem = '-*- Perl -*-';
@rem = '
@echo off
c:\utils\perl %0 %1 %2 %3 %4 %5 %6
goto eof
';

##########################################################################
##########################################################################
###
###  OK BILL GATES, /take this!/ See what a determined Perl Hacker Can Do!
###
###              backup.cmd ::: Jed Reynolds ::: 9/15/95
###              written for the National Orange Show,
###              San Bernardino, California
###
###  This command file is a perl script to accomplish regular or
###  full backups. In fact, it will do both, if you tell it to.
###
###  USAGE: ntb.cmd (full/inc/both) wait (hours)
###        where full starts a full backup
###              inc starts a regular series of incrimental backups
###              both starts off with a full backup and then
###                continues into the regular incrimental backup schedule
###              wait (hours ) is the delay for how long you want to
###                delay your incrimental backups in hours.
###
###  This script (with some homework) took me about three hours to make.
###  Dammit, this is the third programming language in three weeks, and
###  THIS ONE ACTUALLY WORKS! Go fsf/perl!!!!!!
###
###  I would like to thank Clifton Kiser for presenting me with this
###  challenge, since he's my boss. This was a good challenge, and I hope
###  he likes (this) the results.
###
###########################################################################
###########################################################################

######################################################################
######################################################################
##
## COMMAND FORMAT for ntbackup (you microsoft bastards!)
## ntbackup [operation] [path] [switches]
##
## Where [operation] is backup  and path is the root
## of the directory tree you want to backup
##
## ** switches
## /a - append
## /v - verify
## /d "[text]" - adds a description to archive, text is in quotes
## /b backup local registry
## /hc:on or /hc:off - hardware compression
## /t (option) - backup type
##          normal - copy all files, set archive bit
##          copy - copy all files, don't set archive bit
##          incrimental - only modified files, set archive bit
##          differential - only modified files, don't set archive bit
##          daily - only today's files, dont' set archive bit
## /l "filename" - backup log file
## /e - backup log to include only exceptions
## /tape:N - N in [0...9] specifying tape drive
##
##
##
## ntbackup backup c:\ /a
## will backup all files starting at \ on c:, appending them to the tape
##
## ntbackup backup c:\ /a /d "regular backup $d" /hc:on /t incrimental
## will backup all previously archived files, with the a title and date
## (I assume), with hardware compression
##
######################################################################
######################################################################

######################################################################
######################################################################
##
##                    #### BACKUP PARAMETERS ####
## delay seconds
## 172800 is 48 hours
##  86400 is 24 hours
##  43200 is 12 hours
##  21600 is 6 hours
##
######################################################################
######################################################################

$test = "no";

if ($test eq "yes")
  {
    $DELAYTIME=60;
  }
if ($test eq "no")
  {
    $DELAYTIME=86400;
  }

$OPERATION="backup ";
$COMPRESSION="/hc:on ";
$TAPE="/tape:0 ";

if ( $ARGV[0] eq "full")
   {
      $continue = "no";
      &fullb
   }
elsif( $ARGV[0] eq "inc")
  {
     $continue = "yes";
     &incb;
  }
elsif ($ARGV[0] eq "both")
  {
    $continue = "yes";
    &fullb;
  }
else
  {
    print" * * * NO ARGUMENTS * * * * \n";
    &error;
  }

if ($ARGV[1] eq "wait")
  {
    $waithowlong = $ARGV[2] * 3600;
  }


##
## backup loop is here!!!!
##
$stop = 0;

print" -- NTbackup options below --\n";
print" $OPTIONS\n";

while ($stop != 1)
  {
   if($test eq "yes")
     {
        print"a!\n";
        $execute = "echo ntbackup " . $OPTIONS;
     }
   else
     {
        print" b!\n";
        $execute = "ntbackup " . $OPTIONS;
     }
    if ($test eq "yes")
      {
        print"TEST BACKUP BEGINNING...\n";
      }
    else
      {
         print "Backup Beginning...\n";
      }

    system($execute);

    # print" ### continue: $continue";
    if($continue eq "no")
      {
        $stop = 1;
      }
    else
      {
        &incb;
        &wait;
      }
  }
## end of program ##
exit;
## end of program ##

########################################################################
sub wait {
        $waithowlong = $DELAYTIME;
        print"\n\n delaytime: $waithowlong.\n";
        $nowitis = time;
        if ($waithowlong == 0)
          {
            $waithowlong = 84600;
          }
        print "I'm going to be waiting $waithowlong seconds...";

        $findit = ntb.cmd;

        ##
        ## for each line of input from the "ps" command, do...
        ##

        foreach $_ (`ps`)
          {
             ##
             ## This finds the process id of every running process.
             ## You wouldn't /believe/ how long it took me to figure this out.
             ## Perl is not easy!
             ##

             ($pid)=/.\s+(\S+)/;

             if ($_ =~ /$findit/)
               {
                 print" I FOUND IT! Changing niceness...";
                 $execute="nice $pid idle";
                 print "executing $execute\n";
                 system($execute);
                 print"done.\n";
               }
          }

           ##
           ## This is actually why you're looking at it.
           ##

        sleep $waithowlong;
}

########################################################################
sub error {
    print"\n";
    print" USAGE: tbu.bat (inc / full)\n";
    print"        where inc starts the incrimental backup script\n";
    print"        and full starts a one time full backup.\n";
    print"\n";
    exit;
}

########################################################################
sub fullb {

    $DRIVES="c:\ e:\ f:\ g:\ h:\ ";
    $BACKUP_TYPE="/t normal ";
    $COMMENT="/d \"FULL BACKUP by Jed's script of  $DRIVES \" ";
    $OPTIONS=$OPERATION . $DRIVES . $BACKUP_TYPE . $COMPRESSION . $COMMENT . $TAPE;
}

########################################################################
sub incb {

     $DRIVES="c:\ e:\ f:\ g:\ h:\ ";
     $BACKUP_TYPE="/a /t incremental ";
     $COMMENT="/d \"INCRIMENTAL BACKUP by Jed $DRIVES\" ";
     $OPTIONS=$OPERATION . $DRIVES . $BACKUP_TYPE . $COMPRESSION . $COMMENT . $TAPE;
     if ($test eq "yes")
       {
         print" TEST BACKUP BEGINNING...\n";
       }
     else
       {
         print" Backup Beginning...\n";
       }
}

__END__
:eof
