#!/usr/bin/perl
#
# You need to change the above line to the location of Perl on your machine.
# And yes, I realized that I am reinventing the functionality of make here - 
# but make sucks...  If you disagree, then tell me why we have tools like
# GNU configure, imake, xmkmf, and perl's makemaker...

$PERL     = "/usr/bin/perl";

$PREFIX   = "/usr/local/spong";
$BINDIR   = "$PREFIX/bin";
$ETCDIR   = "$PREFIX/etc";
$LIBDIR   = "$PREFIX/lib";
$WWWDIR   = "$PREFIX/www";
$CGIDIR   = "$PREFIX/cgi-bin";
$VARDIR   = "$PREFIX/var";      # Directory where spong will put data
$TMPDIR   = "$PREFIX/tmp";      # update $SPONGTMP var in spong.conf also

$WEBUSER  = "nobody";

# That is typically all you need to change.  You can change the values below
# if you use a system like depot where you install to one located, but those
# files get moved into their final location via some program.

$IPREFIX  = $PREFIX;
$IBINDIR  = "$IPREFIX/bin";
$IETCDIR  = "$IPREFIX/etc";
$ILIBDIR  = "$IPREFIX/lib";
$IWWWDIR  = "$IPREFIX/www";
$ICGIDIR  = "$IPREFIX/cgi-bin";
$IVARDIR  = "$IPREFIX/var";      # Directory where spong will put data
$ITMPDIR   = "$IPREFIX/tmp";      # update $SPONGTMP var in spong.conf also


# -----------------------------------------------------------------------------
# You really really shouldn't need to change anything below this point (I hope)
# -----------------------------------------------------------------------------

@bin    = qw( spong spong-server spong-network spong-client spong-message
	      spong-ack spong-cleanup spong-status);
@lib    = qw( Ack AckList History HistoryList Host HostList Info Service
	      ServiceList );
@cgibin = qw( www-spong www-spong-ack wap-spong);

# Make sure that we are running build in the directory where things are
# unpacked.  This is kind of a lame check, but good enough for now...

if( `cat ./.version 2>&1` ne "spong 2.7\n" ) {
   print "Error: You need to run build from the directory that you unpacked ";
   print "spong in.\nPlease \"cd\" into that directory and type \"./build\"\n";
   exit(0);
}

($VER) = (`cat ./.version 2>&1` =~ /spong (.*)/);

# Compute a list of operating systems that we know about.

opendir( DIR, "./config" ) || die "Can't open ./config dir: $!";
while( $file = readdir( DIR ) ) {
   if( $file =~ /spong\.conf\.(.*)$/ ) { $known_os{$1} = 1; }
}
closedir( DIR );

# Find out the arguments that are passed in and call the appropriate function

$run = 1;
if( $ARGV[0] eq "-n" )      { $run = 0; shift @ARGV; }

if( $ARGV[0] eq "help" )    { &help();              exit(); }
if( $ARGV[0] eq "clean" )   { &clean();             exit(); }
if( $ARGV[0] eq "install" ) { &install();           exit(); }
if( $ARGV[0] eq "upgrade" ) { &upgrade();           exit(); }
if( $known_os{$ARGV[0]} )   { &build( $ARGV[0] );   exit(); }

&help(); exit();


# This routine takes and builds all the stuff that we can.  Based on the OS
# provided, it creates an etc directory that contains the spong.conf and 
# spong.hosts file.  It then converts the tokens in the .pl and .pod files
# into their appropriate values, finally it generates some man pages and html
# documents.

sub build {
   my( $os ) = @_;
   my $file;

   ssystem( "mkdir ./etc" ) if ! -d "./etc";
   ssystem( "cp ./config/spong.conf ./etc/spong.conf" );
   ssystem( "cp ./config/spong.hosts ./etc/spong.hosts" );
   ssystem( "cp ./config/spong.groups ./etc/spong.groups" );
   ssystem( "cp ./config/spong.message ./etc/spong.message" );
   ssystem( "cat ./config/spong.conf.$os >> ./etc/spong.conf" );

   ssystem( "mkdir ./etc/default") if ! -d "./etc/default";
  
   ssystem( "mkdir ./bin" ) if ! -d "./bin";
   foreach $file ( @bin )  { 
      &replace( "./src/$file.pl", "./bin/$file" ); }
   ssystem( "chmod +x ./bin/*" ); 

   ssystem( "mkdir -p ./lib/Spong" ) if ! -d "./lib/Spong";
#   foreach $file ( @lib ) {
#      &replace( "./src/lib/Spong/$file.pm", "./lib/Spong/$file.pm" ); }
   ssystem( "cp -rp ./src/lib/Spong/* ./lib/Spong/"); 
   ssystem( "chmod 644 ./lib/Spong/*" ); 
   ssystem( "chmod 755 ./lib/Spong/Client ./lib/Spong/Message ./lib/Spong/Network");

      
   ssystem( "mkdir ./cgi-bin" ) if ! -d "./cgi-bin";
   foreach $file ( @cgibin )  { 
      &replace( "./src/$file.pl", "./cgi-bin/$file" ); }
   ssystem( "chmod +x ./cgi-bin/*" ); 

   # Need to do something about replacing the configurable tags in the HTML
   # documentation.

   ssystem( "touch .built" );
}


# After everything is all built, this just copies the various files to their
# final location

sub install {
   if( ! -f ".built" ) { &help(); }

   ssystem( "mkdir -p $IBINDIR" ) if ! -d $IBINDIR;
   ssystem( "cp bin/* $IBINDIR/" );

   ssystem( "mkdir -p $IETCDIR" ) if ! -d $IETCDIR;
   ssystem( "cp etc/* $IETCDIR/" );

   ssystem( "mkdir -p $IVARDIR" ) if ! -d $IVARDIR;
   ssystem( "mkdir -p $IVARDIR/database" );
   ssystem( "mkdir -p $IVARDIR/archives" );
   ssystem( "mkdir -p $IVARDIR/misc" );

   ssystem( "mkdir -p $ITMPDIR" ) if ! -d $ITMPDIR;

   # TODO - Clean this up...

   ssystem( "mkdir -p $ILIBDIR" ) if ! -d $ILIBDIR;
   ssystem( "cp -rp lib/* $ILIBDIR/" );
   ssystem( "chmod -R ugo+rwX,go-w $ILIBDIR/*" );

   # Your web server needs to be able to access these files
   ssystem( "chmod 751 $IETCDIR" );
   ssystem( "chmod 644 $IETCDIR/spong.*" );

   ssystem( "mkdir -p $IWWWDIR" ) if ! -d $IWWWDIR;
   ssystem( "cp -rp ./www/* $IWWWDIR/" );
   ssystem( "chmod -R ugo+rwX,go-w $IWWWDIR" );
   ssystem( "chown -R $WEBUSER $IWWWDIR" );

   # Put copies of the CGI program in the cgi-bin directory...

   ssystem( "mkdir -p $ICGIDIR" ) if ! -d $ICGIDIR;
   ssystem( "cp ./cgi-bin/www-spong $ICGIDIR/www-spong" );
   ssystem( "cp ./cgi-bin/www-spong-ack $ICGIDIR/www-spong-ack" );
   ssystem( "cp ./cgi-bin/wap-spong $ICGIDIR/wap-spong" );
   ssystem( "chmod u=rx,go=r $ICGIDIR/*spong*" ); # Let's be conservative
   ssystem( "chown $WEBUSER $ICGIDIR/*spong*" );


   # Remove this once I'm done testing...

#   ssystem( "cp $ICGIDIR/www-spong /local/www/cgi-bin/www-spong2" );
#   ssystem( "cp $ICGIDIR/www-spong-ack /local/www/cgi-bin/www-spong2-ack" );
}

# This does the same thing as install, except it doesn't override your 
# configuration files.  Instead it copies its new configuration files to the 
# install directory - but calls them *.dist, and displays a message telling
# you about it.

sub upgrade {
   if( ! -f ".built" ) { &help(); }

   ssystem( "mkdir -p $IBINDIR" ) if ! -d $IBINDIR;
   ssystem( "cp bin/* $IBINDIR/" );

   ssystem( "mkdir -p $IETCDIR" ) if ! -d $IETCDIR;
   ssystem( "cp etc/spong.conf $IETCDIR/spong.conf.dist" );
   ssystem( "cp etc/spong.hosts $IETCDIR/spong.hosts.dist" );
   ssystem( "cp etc/spong.groups $IETCDIR/spong.groups.dist" );
   ssystem( "cp etc/spong.message $IETCDIR/spong.message.dist" );

   # Your web server needs to be able to access these files
   ssystem( "chmod 751 $IETCDIR" );
   ssystem( "chmod 644 $IETCDIR/spong.*" );

   # TODO - Clean this up...

   ssystem( "mkdir -p $ILIBDIR" ) if ! -d $ILIBDIR;
   ssystem( "cp -rp lib/* $ILIBDIR/" );
   ssystem( "chmod -R ugo+rwX,go-w $ILIBDIR/*" );

   ssystem( "mkdir -p $IWWWDIR" ) if ! -d $IWWWDIR;
   ssystem( "cp -rp ./www/* $IWWWDIR/" );
   ssystem( "chmod -R ugo+rwX,go-w $IWWWDIR" );
   ssystem( "chown -R $WEBUSER $IWWWDIR" );

   # Put copies of the CGI program in the cgi-bin directory...

   ssystem( "mkdir -p $ICGIDIR" ) if ! -d $ICGIDIR;
   ssystem( "cp ./cgi-bin/www-spong $ICGIDIR/www-spong" );
   ssystem( "cp ./cgi-bin/www-spong-ack $ICGIDIR/www-spong-ack" );
   ssystem( "cp ./cgi-bin/wap-spong $ICGIDIR/wap-spong" );
   ssystem( "chmod ugo+rwx,go-w $ICGIDIR/*spong*" );
   ssystem( "chown $WEBUSER $ICGIDIR/*spong*" );
}

# This removes all the generated files in the spong distrib directory.

sub clean {
   ssystem( "rm -rf ./lib" );

   foreach( qw( etc bin cgi-bin ) ) {
      if( -d "./$_" ) {
	 ssystem( "rm -rf ./$_/*" );
	 ssystem( "rmdir ./$_" );
      }
   }
   ssystem( "rm ./.built" ) if -f "./.built";
}

   
# This prints out a help message, it explains how this program can be called,
# what options are apropriate, etc...

sub help {
   print "You need to type ./build <os> first, where <os> is one of the ";
   print "following:\n\n";
   
   foreach( sort keys %known_os ) { print "    * $_\n"; }

   print <<'_EOF_';

If your OS is not listed above, then you will need to create a
config/spong.conf.<os> file for your OS before building this package.  Once
you have done that, please send that file to ed-hill@uiowa.edu so that I can
include it with the distribution.

_EOF_
   exit(0);
}


# This takes a source file and replaces some keywords in the file with values
# that the user has defined.  It's a sed replacement...

sub replace {
   my( $in, $out ) = @_;

   print "replacing tokens in $in, creating $out\n";
   if( $run ) {
      open( IN, $in ) || die "can't open $in: $!";
      open( OUT, ">$out" ) || die "can't create $out: $!";
      while( <IN> ) {
	 s/\@\@PERL\@\@/$PERL/g;	 s/\@\@ETCDIR\@\@/$ETCDIR/g;
	 s/\@\@BINDIR\@\@/$BINDIR/g;	 s/\@\@WWWDIR\@\@/$WWWDIR/g;
	 s/\@\@LIBDIR\@\@/$LIBDIR/g;     s/\@\@VER\@\@/$VER/g;
	 print OUT $_;
      }
      close( OUT );
      close( IN );
   }
}

# This takes a shell command, prints it and then passes it off to the normal
# perl system function. It also checks to see if it should run the command.

sub ssystem { 
   if( $run ) { 
      print $_[0], "\n"; 
      system( $_[0] ); 
   } else { 
      print $_[0], "\n"; 
   } 
}


