#!/usr/bin/perl
##
## Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
## Creation Date: Mon Apr 27 13:50:17 PDT 2009
## Last Modified: Sun Jun 14 19:06:55 PDT 2009 (copied from humextra.pl)
## Filename:      /var/www/cgi-bin/improv
## Syntax:        Perl 5; CGI
## $Smake:        cp -f %f /var/www/cgi-bin/%b
##
## Description:   This program zips the improv files in their current state.
##    

use strict;

# Configuration variables:
my $improvaddr    = "http://improv.sapp.org";
my $cgiaddr       = "$improvaddr/cgi-bin";
my $imgaddr       = "$improvaddr/img";

my $improvdir     = "/project/improv";
my $downloaddir   = "$improvdir/web/website/download";
my $compressdir   = "$improvdir/web";
my $logdir        = "$improvdir/web/log";
my $prefix        = "improv";

my $datestring    = getDateString();

##########################################################################

# filterSpiders();

# use CGI;
# my $cgi_form = new CGI;
# my $options    = $cgi_form->param('options');

createUnstableArchive();
sendData();
writelog();

exit(0);

##########################################################################


##############################
##
## sendData -- send requested information to user.
##

sub sendData {

   my $datafile = "$prefix-$datestring.tar.bz2";
   my $contenttype = "application/x-bzip2";

   open (DATAFILE, "$downloaddir/$datafile");
   my @filestats = stat DATAFILE;
   my $filesize = $filestats[7];
   my $contents;
   read DATAFILE, $contents, $filesize;
   close DATAFILE;
   print <<"EOT";
Content-Type: $contenttype
Content-Disposition: inline; filename=$datafile

EOT
   print "$contents";
}



##############################
##
## createUnstableArchive --
##

sub createUnstableArchive {
   #if (-r "$downloaddir/$prefix-$datestring.tar.bz2") {
   #   return;
   #}
   
   `(cd $compressdir; tar chjf website/download/$prefix-$datestring.tar.bz2 $prefix)`;
}



##############################
##
## writelog -- write the log of data transfer
##

sub writelog {
   open (LOGFILE, ">> $logdir/unstable.log");
   my $entry_time = localtime(time);
   print LOGFILE "$datestring [$entry_time] $ENV{'REMOTE_HOST'} ($ENV{'REMOTE_ADDR'})\n";
   close LOGFILE;
}



##############################
##
## filterSpiders -- prevent spiders from accessing the script
##

sub filterSpiders {
   my $agent = $ENV{'HTTP_USER_AGENT'};
   if (($agent =~ /spider/i) || 
       ($agent =~ /crawler/i) ||
       ($agent =~ /slurp/i) ||
       ($agent =~ /bot/i)) {
      print <<"EOT";
Content-Type: text/plain

Sorry, automatic download of this webpage is not allowed since
the files are generated automatically.  

EOT

      open (LOGFILE, ">> $logdir/unstable.log");
      my $entry_time = localtime(time);
      print LOGFILE "ERROR $datestring [$entry_time] $ENV{'REMOTE_HOST'} ($ENV{'REMOTE_ADDR'}) illegal spidering <<$agent>>\n";
      close LOGFILE;
   
      exit(0);
   } 
}



##############################
##
## getDateString();
##

sub getDateString {
   my $year;
   my $month;
   my $day;
   my $hour;
   my $min;
   my $sec;
   ($year, $month, $day, $hour, $min, $sec) = getDate();
   return "$year$month$day";
}



##############################
##
## getDate --
##

sub getDate {
   my $cyear;   # current year
   my $cmonth;  # current month
   my $cday;    # current day
   my $chour;   # current hour
   my $cmin;    # current minute
   my $csec;    # current second
   my $weekday; # current weekday
   my $dayofyear;       # current day of year
   my $isdst;   # current timezone

   ($csec, $cmin, $chour, $cday, $cmonth, $cyear,
         $weekday, $dayofyear, $isdst) = localtime(time);

   $cmonth += 1;     # fix month so that it is in the range [
   $cyear += 1900;   # fix year so that it is actual year.

   if ($cmonth < 10) {
      $cmonth = int($cmonth);
      $cmonth = "0$cmonth";
   }
   if ($cday < 10) {
      $cday = int($cday);
      $cday = "0$cday";
   }
   if ($chour < 10) {
      $chour = int($chour);
      $chour = "0$chour";
   }
   if ($cmin < 10) {
      $cmin = int($cmin);
      $cmin = "0$cmin";
   }
   if ($csec < 10) {
      $csec = int($csec);
      $csec = "0$csec";
   }

   return ($cyear, $cmonth, $cday, $chour, $cmin, $csec);
}


