#!/usr/bin/perl
#
# install-timesheet
#
# Copyright 1999 -- 2001, onShore Development Inc. <URL:http://www.onshore-devel.com/>
#
#
# This program is free software under the terms of the GNU General Public
# License (GPL). A copy of the GPL, "COPYING", should have been made
# available with this software.  If not, a copy may be obtained at 
# http://www.fsf.org/copyleft/gpl.html
#
# $Id: install-timesheet,v 1.21 2001/08/31 06:07:42 adam Exp $ 
use Getopt::Long; 

&GetOptions( 
	     "libdir=s" => \$libdir, 
	     "approot=s" => \$approot, 
	     "perlinc=s" => \$perlinc, 
	     "wwwuser=s" => \$wwwuser, 
	     "appowner=s" => \$appowner, 
	     "appgroup=s" => \$appgroup, 
	     "imagedir=s" => \$imagedir, 
	     "htmldir=s" => \$htmldir, 
	     "utildir=s" => \$utildir, 
	     "logdir=s" => \$logdir, 
	     "htaccess=s" => \$htaccess, 
	     "htpasswd=s" => \$htpasswd,
	     "defuser=s" => \$defuser, 
	     "config=s" => \$config, 
	     "instprefix=s" => \$instprefix,
	     "h" => \$help, 
	     "help" => \$help, 
	     "v" => \$verbose,
	     "verbose" => \$verbose,
	     "create_guest=s" => \$create_guest,
	); 

$debug = 0;
$debug and $verbose = 1;
	
if (! $approot
    || ! $imagedir
    || ! $perlinc 
    || ! $wwwuser 
    || ! $libdir
    || ! $appowner 
    || ! $appgroup 
    || $help )
{
    usage(); 
    exit(1);
}



$src{libdir}{dest}="$libdir"; 
$src{libdir}{default}="lib"; 
$src{libdir}{src}="lib"; 


$src{imagedir}{default}="images"; 
$src{imagedir}{src}=$src{imagedir}{default};
$src{imagedir}{dest}=$imagedir;

$src{htmldir}{default}="html"; 
$src{htmldir}{src}=$src{htmldir}{default}; 
if ( ! $htmldir ) { 
    $src{htmldir}{dest}="$approot/html"; 
} else { 
    $src{htmldir}{dest}=$htmldir;
}

$src{utildir}{default}="utils"; 
$src{utildir}{src}=$src{utildir}{default}; 
if (! $utildir ) { 
    $src{utildir}{dest}="$approot/utils"; 
} else { 
    $src{utildir}{dest}=$utildir;
}

$src{logdir}{default}="logs"; 
$src{logdir}{src}=$src{logdir}{default}; 
if ( ! $logdir ) { 
    $src{logdir}{dest}="$approot/logs"; 
} else { 
    $src{logdir}{dest}="$logdir"; 
}

if (! $config ) { 
    $src{config}{dest}="$approot/etc/timesheet.conf"; 
} else { 
    $src{config}{dest}="$config"; 
}
$src{config}{default}="etc/timesheet.conf"; 

$src{perlinc}{dest}="$perlinc"; 
$src{perlinc}{src}="perlmods"; 

# FIXME: deprecate .htaccess, use apache config snippet instead?
if ( ! $htaccess ) { 
    $src{htaccess}{src} = "install/htaccess-default"; 
} else { 
    $src{htaccess}{src} = "$htaccess"; 
} 

$src{htaccess}{dest} = "$approot/.htaccess"; 


if ( $create_guest eq "yes" ) {
    $src{htpasswd}{src} = "install/htpasswd-default"; 
} else {
    $src{htpasswd}{src} = "install/htpasswd-default-noguest"; 
}

if ( ! $htpasswd ) { 
    $src{htpasswd}{dest} = "$approot/.htpasswd"; 
} else { 
    $src{htpasswd}{dest} = "$htpasswd"; 
}


$src{approot}{dest} = "$approot"; 
$src{approot}{src} = "."; 

if ( ! $instprefix ) {
    $instprefix = "";
}

#
# dirs
#
foreach $dir ($approot, 
	      $src{libdir}{dest}, 
	      $src{htmldir}{dest}, 
	      $src{utildir}{dest}, 
	      $src{imagedir}{dest}, 
	      $src{logdir}{dest} )
{ 
    if ( ! -d $instprefix . $dir ) { 
	print "Creating $instprefix$dir\n"; 
	`mkdir -p $instprefix$dir`; 
    }
}

($name, $junk, $uid, $gid, @crap) = getpwnam($appowner);

if ( $name != $appowner ) { 
    die "ERROR: application owner '$appowner' not a valid user\n";
}

($gname, $junk, $gid, $junk) = getgrnam($appgroup); 

if ( $gname != $appgroup) { 
    die "ERROR: application group '$appgroup' not a valid group\n";
}

$wwwuid = getpwnam($wwwuser);

if ( ! defined($wwwuid) ) {
    die "ERROR: web user '$wwwuser' not a valid user\n";
}

foreach $dir ($approot, 
	      $src{libdir}{dest}, 
	      $src{htmldir}{dest}, 
	      $src{utildir}{dest}, 
	      $src{imagedir}{dest}, 
	      $src{logdir}{dest})
{ 
    print "Setting ownership and permissions for $instprefix$dir\n"; 
    chown($uid, $gid, $instprefix . $dir) or
	die "ERROR: couldn't change ownership of '$instprefix$dir': $!\n";
    chmod(0755, $instprefix . $dir) or
	die "ERROR: couldn't change ownership of '$instprefix$dir': $!\n"; 
}

# The log directory has to be writable to by the group
print "Setting group writable permissions for $instprefix$src{logdir}{dest}\n";
chmod(0775, $instprefix . $src{logdir}{dest}) or
	die "ERROR: couldn't change mode of '$instprefix$src{logdir}{dest}': $!\n";

# dirs are done,  now files
foreach $typedir (keys %src) { 
  # these are handled seperatly
  next if ( $typedir eq "perlinc" || 
	    $typedir eq "htpasswd" || 
	    $typedir eq "htaccess" || 
	    $typedir eq "config" ||
	    $typedir eq "logdir" ); 
  print "\n\n";
  print "$typedir: Installing files from $src{$typedir}{src} to\n";
  print "          $instprefix$src{$typedir}{dest}\n"; 
  print "=============================================================================\n"; 
  opendir(DIR, $src{$typedir}{src})
      or die "ERROR: cannot open dir $src{$typedir}{src}: $!\n"; 
  @files=readdir(DIR);
  closedir DIR;
  foreach (@files) { 
      next if ( -d $_ );
      if (  $typedir eq "utildir" ) { 
	  next if ( /^\.|~$/); 
      } else { 
	  next if ( ! /(\.cgi|\.html|\.pl|\.gif|\.jpg|dump|load|tmpl)$/ ); 
      } 
      $file=$_; 
      $verbose or print "."; 
      $verbose and
	  print "$src{$typedir}{src}/$file -> $instprefix$src{$typedir}{dest}/$file\n"; 
      open(SRC, "<$src{$typedir}{src}/$file") 
	  or die "ERROR: cannot open $src{$typedir}{src}/$file: $!\n"; 
      open(DEST, ">$instprefix$src{$typedir}{dest}/$file") 
	  or die "ERROR: cannot write to $instprefix$src{$typedir}{dest}: $!\n";
      while (<SRC>) {
	  $line = $_;
				# FIXME: HTML files will require special handling
				# WARNING: assumes one hit per line only
	  if ( $file !~ /(\.gif|\.jpg|\.html)$/ && $line !~ /<(IMG|INPUT|A|BODY)/i ) {
	      foreach $key (keys %src) {
		  if ( length($src{$key}{default}) and 
		       $src{$key}{default} ne $src{$key}{dest} ) {
		      # FIXME: screwed for HTML tags and stuff
		      # replace the path; some paths have ../ in the front
		      if ( $line =~ s/([\"\'])(\.\.\/)?$src{$key}{default}/$1$src{$key}{dest}/ ) { 
			  $debug and warn("SUBSTHIT $key: $line" . 
					  " FROM $src{$key}{default} -> $src{$key}{dest}\n");
			  next;
		      }
		  }
	      }
	  }
	  print DEST $line;
      }
      close SRC; 
      close DEST; 
      if ( $file =~ /(.pl|.cgi)$/ || $typedir eq "utildir" ) { 
	  chmod(0755, "$instprefix$src{$typedir}{dest}/$file"); 
      } 
      else { 
	  chmod(0644, "$instprefix$src{$typedir}{dest}/$file");
      }
  }
}
print "\n\n"; 

#
# perlmod
#
print "$src{perlinc}{src}/ADB.pm -> $instprefix$perlinc/ADB.pm \n";
open(SRC, "<$src{perlinc}{src}/ADB.pm")
    or die "ERROR: cannot open $src{perlinc}{src}/ADB.pm: $!"; 
open(DEST, ">$instprefix$perlinc/ADB.pm")
    or die "ERROR: cannot open $instprefix$perlinc/ADB.pm for write: $!"; 
while (<SRC>) { 
  print DEST; 
}
close SRC;
close DEST; 
chmod(0644, "$instprefix$perlinc/ADB.pm"); 

#
# .htaccess and .htpasswd
#
if ( ! -f $instprefix . $src{htaccess}{dest} ) {
    print "Creating default .htaccess and setting ownership for $wwwuser\n"; 
    print "$src{htaccess}{src} -> $instprefix$src{htaccess}{dest}\n"; 
    open(SRC, "<$src{htaccess}{src}")
	or die "ERROR: cannot open '$src{htaccess}{src}': $!\n";
    open(DEST, ">$instprefix$src{htaccess}{dest}")
	or die "ERROR: cannot write to '$instprefix$src{htaccess}{dest}': $!\n"; 
    while (<SRC>) {
	if ( /(^AuthUserFile)/ ) { 
	    $_ = "AuthUserFile $src{htpasswd}{dest}\n"; 
	}
	print DEST; 
    }
    close SRC; 
    close DEST; 
    chown($wwwuid, $gid, $instprefix . $src{htaccess}{dest}) or
	die "ERROR: couldn't change ownership: $!\n";
    chmod(0644, $instprefix . $src{htaccess}{dest}) or
	die "ERROR: couldn't change permissions: $!\n";
}
else {
    print "Skipping .htaccess install -- it is already installed\n";
}

if ( ! -f $instprefix . $src{htpasswd}{dest} ) {
    print "Creating default .htpasswd and setting ownership for $wwwuser\n"; 
    print "$src{htpasswd}{src} -> $instprefix$src{htpasswd}{dest}\n"; 
    open(SRC, "<$src{htpasswd}{src}")
	or die "ERROR: cannot open '$src{htpasswd}{src}': $!\n";
    open(DEST, ">$instprefix$src{htpasswd}{dest}")
	or die "ERROR: cannot write to '$instprefix$src{htpasswd}{dest}': $!\n";
    while (<SRC>) {
	# reset default user account if needs be -- perhaps this
	# "admin" spec should be moved up to top of file? 
	if ( $defuser ) { 
	    s/admin/$defuser/g; 
	} 
	print DEST;
    }
    close SRC; 
    close DEST; 
    chown($wwwuid, $gid, $instprefix . $src{htpasswd}{dest}) or
	die "ERROR: couldn't change ownership: $!\n";
    chmod(0600, $instprefix . $src{htpasswd}{dest}) or
	die "ERROR: couldn't change permissions: $!\n";
}
else {
    print "Skipping .htpasswd install -- it is already installed\n";
}

sub usage { 
  print "Usage:\n"; 
  print "\tinstall-timesheet -libdir path -approot path -imagedir path -perlinc path -wwwuser user -appowner user -appgroup group\n\n"; 
  print "\t\tlibdir:  Application library path\n"; 
  print "\t\tapproot: Application root\n";
  print "\t\tperlinc: system default perl include path\n";
  print "\t\twwwwuser: user account the webserver runs as \n"; 
  print "\t\tappowner: user who should own the installed files\n"; 
  print "\t\tappgroup: group who shoul own the installed files\n\n";
  print "install-timesheet will attempt to create approot and libdir if they do\n"; 
  print "not already exist, but will fail if perlinc does not exist.\n"; 
}

	

