#!/net/local/bin/perl
#=======================START OF CONFIGURATION=========================
# What kind of directory slash?
# unix 
$SLASH = '/';
# windows/dos
#$SLASH = '\';

# Set the following line to where your perl lives and put in flags so that
# in can include the various .pm 
# NOTE THIS MUST BE PERL VERSION 5.0 or above.
$PERL = '#!/net/local/bin/perl -I/net/zeus/facstaff/cdyreson/public_html/cgi-bin/perl/pm';
#$PERL = '#!/usr/local/bin/perl -IC:\Inetpub\wwwroot\CDyreson\cgi-bin\perl\pm';
 
# Set the following line to where the URL ROOT of the SAMaker cgi-bin scripts
$ROOT = "http://www.eecs.wsu.edu/cgi-bin/cgiwrap/~cdyreson/cgi-bin/perl/SAMaker";
#$ROOT = "http://www.it.bond.edu.au/cdyreson/cgi-bin/perl/SAMaker";
 
# Where will the cgi-bin scripts live?
# WARNING, WARNING, WARNING!!! when you run install it will overwrite 
# the scripts that are located in this directory (for scripts marker, 
# stats, hint, lookat, and explanation) so make sure to
# put all the SAMaker cgi-bin scripts in their own directory.
$SCRIPTDIR = '/net/zeus/facstaff/cdyreson/public_html/cgi-bin/perl/SAMaker';
#$SCRIPTDIR = "./SAMaker";
 
# Does your server only understand scripts with a certain extension?
#$SCRIPTEXTENSION = "";  # assume no
$SCRIPTEXTENSION = ".pl";
 
# Where will SAMaker live?
# WARNING, WARNING, WARNING!!! when you run install it will overwrite 
# the script SAMaker that is located in this directory (which should be OK)
#$SAMAKERDIR = './bin';
$SAMAKERDIR = '/net/zeus/facstaff/cdyreson/bin/perl';
 
# Where will the SAMaker Formats live?
#$SAMAKERFORMATDIR = "./SAMakerFormats";
$SAMAKERFORMATDIR = '/net/zeus/facstaff/cdyreson/bin/perl/SAMakerFormats';
 
# chmod and chgrp to set the protections appropriately
#$CHMOD= 'chmod a+x';
#$CHGRP = '';

# Absolute path - this is the place where the data files are written
$ABSOLUTEPATH = '/net/zeus/facstaff/cdyreson/prweb/SAMaker';

# copy commands
$CP = 'cp ';          # to copy the things in the SAMakerFormats directory
#=======================END OF CONFIGURATION=========================
configure("marker.proto", "$SCRIPTDIR/marker$SCRIPTEXTENSION");
configure("stats.proto", "$SCRIPTDIR/stats$SCRIPTEXTENSION");
configure("SAMaker.proto", "$SAMAKERDIR/SAMaker");
configure("LessonMaker.proto", "$SAMAKERDIR/LessonMaker");
configure("germanexample.tex.proto", "germanexample.tex");
print "Copying SAMakerTags.pm to $SAMAKERDIR.\n";
system("$CP SAMakerTags.pm $SAMAKERDIR/");
print "Copying LessonMakerTags.pm to $SAMAKERDIR.\n";
system("$CP LessonMakerTags.pm $SAMAKERDIR/");
print "Making $SAMAKERFORMATDIR directory.\n";
mkdir "$SAMAKERFORMATDIR", 0755 unless -e "$SAMAKERFORMATDIR";
print "Copying SAMakerFormats to $SAMAKERFORMATDIR.\n";
system("$CP SAMakerFormats/* $SAMAKERFORMATDIR/");

print "\nI'm ALL DONE!!!!!\n";
print "But you should please check for any errors above!\n";
print "IMPORTANT: You should manually copy the pm/ directory!\n";
print "And set permissions appropriately for them too!\n";

sub configure {
  local ($filein, $fileout) = @_;

  print "reading $filein\n";
  open(PROTO, "<$filein") || die "no $filein\n";
  
  # slurp input 
  @lines = <PROTO>;
  close(PROTO);
  $text = join("",@lines);
  $text =~ s/&&&ABSOLUTEPATH&&&/$ABSOLUTEPATH/g;
  $text =~ s/&&&SLASH&&&/$SLASH/g;
  $text =~ s/&&&PERL&&&/$PERL/g;
  $text =~ s/&&&ROOT&&&/$ROOT/g;
  $text =~ s/&&&EXTENSION&&&/$SCRIPTEXTENSION/g;
  $text =~ s/&&&FORMATDIR&&&/$SAMAKERFORMATDIR/g;
  $text =~ s/&&&SAMAKERDIR&&&/$SAMAKERDIR/g;

  open(OUT, ">$fileout") || die "can't open $fileout\n";
  print "creating $fileout\n";
  print OUT $text;
  close(OUT);

  if (defined $CHMOD) {system("$CHMOD $fileout");}
  if (defined $CHGRP) {system("$CHGRP $fileout");}
}
