#!/usr/bin/perl
#
# config-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
#
#

use Getopt::Long; 
&GetOptions ( 
	"url=s" => \$url, 
	"image_url=s" => \$image_url,
	"export_dir=s" => \$export_dir, 
	"backup_dir=s" => \$backup_dir,
	"dbaddr=s" => \$dbaddr, 
	"sqldb=s" => \$sqldb, 
	"passfile=s" => \$passfile, 
	"download_user=s" => \$download_user, 
	"confdir=s" => \$confdir, 
	"conffile=s" => \$conffile, 
	"export_log=s" => \$export_log,
        "instprefix=s" => \$instprefix,
	); 

if ( ! $confdir || ! $conffile ) { 
    print "ERROR: config directory or file not supplied\n"; 
    &usage; 
    exit 1; 
} 


if ( ! -d $instprefix . $confdir ) { 
    print "Creating config directory '$instprefix$confdir'\n"; 
    $i = system("mkdir $instprefix$confdir 2>&1"); 
    if ($i) { 
	die "ERROR: creating directory '$instprefix$confdir'\n"; 
    } 
}

%options = ( 
	     "TIMESHEET_URL" => $url,
             "IMAGE_URL" => $image_url,
	     "EXPORT_DIR" => $export_dir, 
	     "EXPORT_LOG" => $export_log, 
	     "BACKUPDIR" => $backup_dir,
	     "DBADDR" => $dbaddr, 
	     "SQLDB" => $sqldb, 
	     "PASSFILE" => $passfile, 
	     "DOWNLOAD_USER" => $download_user, 
	     );


if ( ! -f "$instprefix${confdir}/${conffile}") {
    open(OLDCONF, "etc/timesheet.conf") or
	die "ERROR: cannot open lib/timesheet.conf: $!\n"; 
    open(NEWCONF, ">$instprefix${confdir}/${conffile}") or
	die "ERROR: cannot open $instprefix${confdir}/${conffile} for write: $!\n";

    print "Creating config file $instprefix${confdir}/${conffile}\n"; 

    while (<OLDCONF>) { 
	foreach $key (keys %options) { 
	    if (/(^\*$key)/ )  {
		$_ = "$1 = \\'$options{$key}'; #'\n"; 
	    }
	}
	print NEWCONF; 
    }
    close OLDCONF;
    close NEWCONF;
    print "  done.\n"; 

    print "Setting permissions on $instprefix${confdir}/${conffile}\n";
    chmod(0644, "$instprefix${confdir}/${conffile}") or
	die "ERROR: chmod $instprefix${confdir}/${conffile}: $!\n";
}
else {
    print "Skipping installation of already existing \n  " .
	"$instprefix${confdir}/${conffile}${conffile}";
}

exit(0);
