#!/usr/local/bin/perl
#
# This is the Configure Script.
# 
# You will only have to change the above line to point at a working
# version of perl and it should run just fine.
#
# Paul Walker Jan 95
#


print <<ENDOFINTRO;
===============================
=   COCOBOARD CONFIGURATION   =
===============================

You will be asked a series of questions.  Each question will have a 
default answer in parentheses.  Hitting return at the prompts will 
set the default value, while entering a value will change the default.  
Note, some changes may be subject to a sanity check.

*** IMPORTANT ***
You should NOT use configure to create or remove projects or 
postings.  Rather, you should consult the information in the
~/PF/tools directory, where there are scripts for doing these
sort of maintenance functions.

ENDOFINTRO

$yesno = "n";
$yesno = &promptuser("Do you want to continue with Installation [Y/N]",$yesno);
$yesno =~ tr/[a-z]/[A-Z]/;
if (!($yesno =~ /Y/)) {
   print "Exiting Configuration\n";
   exit;
}

# Get our current directory
$cdir = `pwd`;
chop $cdir;
$me = "pwalker@ncsa.uiuc.edu (Note: Release 1.1 beta2, May 22 95)";

# Set perl to the correct thing in each of the modifiable files
@fixperl = ("$cdir/projectsfilter", "$cdir/tools/makeproject",
	    "$cdir/tools/removepost", "$cdir/tools/removeproject");

$defperl = `which perl`;
chop $defperl;
$whereisperl = &promptuser("Where is your installation of Perl",$defperl);

$definst = `pwd -l`;
chop $definst;
$installdir =  &promptuser("Where is your Projects Page installed",$definst);

$hthome = "/www/Projects";
$hthome =  &promptuser("Where should incoming mail be written",$hthome);

foreach $FIXME (@fixperl) {
    open (IFP, "< $FIXME") || 
	die "Configuration error 1, $FIXME.  Report to $me\n"; 
    open (OFP, "> $$.tmp") || 
	die "Configuration error 2, $$.tmp.  Report to $me\n";
    while (<IFP>) {
	if (/^\#\!/) {
	    print OFP "\#\!$whereisperl\n";
	} elsif (/^\$installdir/) {
	    print OFP "\$installdir = \"$installdir\"\;\n";
	} elsif (/^\$hthome/) {
	    print OFP "\$hthome = \"$hthome\"\;\n";
	} else {   
	    print OFP;
	}
    }
    close IFP;
    close OFP;
    unlink ("$FIXME");
    link ("$$.tmp","$FIXME");
    unlink("$$.tmp");
    system "chmod 700 $FIXME";
}

# Scan through the conf file asking for a setting on everything which doesnt
# contain a $.

print <<EOM;

-----------------------------------------------------------------------
You will now be asked for a series of configuration options such as the
location of your server (both on local disk and on the WWW), email 
addresses, default domains, etc...  Each will have a short blurb of
explanatory text followed by  a prompt to set the variable with a
default value.  Note the projects page is currently set up to work
for No Where University, so you should replace no.where.edu and the
like with your domain, and No Where U with the name of your Organization.


EOM

open (CONF, "< Projects.conf") || 
    die "Configure Error 3, Projects.conf.  Please contact $me\n";
open (OUT,"> $$.tmp") ||
    die "Configure Error 4, $$.tmp.  Please contact $me\n";

while (<CONF>) {
    print OUT;
    last if (/End of Preamble/);
}

$keepongoin = 0;
while (<CONF>) {
    if ($keepongoin && !(/^\s*$/)) {
	print OUT;
	next;
    }
    if (/^\s*$/) {
	print OUT;
	$keepongoin=0;
	next;
    }
    if (/^\# ConfSkip/) {
	print OUT;
	$keepongoin = 1;
	next;
    }
    if (/^\#\s+(.*)$/) {
	print "$1\n";
	print OUT;
	next;
    }
    # Only thing left is name value pair.
    /^(\S+) (.*)$/;
    $variable = $1;
    $value = $2;
    print "\n";
    $tmp = &promptuser("Please set $variable",$value);
    print OUT "$variable $tmp\n";
    eval "\$$variable = \"$tmp\"";
    if ($variable =~ /uncompface/) {
 	$xfskip = 1;
    }
    print "\n\n";
}

if ($usexface && (!$xfskip)) {
    $uncompface = &promptuser("Where is Uncompface Installed",
			      "/usr/local/bin/uncompface");
    $ikon2xbm = &promptuser("Where is ikon2xbm Installed",
			      "/usr/local/bin/ikon2xbm");
    print OUT "\# ConfSkip\n";
    print OUT "\# Executable for uncomping X-Face\n";
    print OUT "uncompface $uncompface\n\n";
    print OUT "\# ConfSkip\n";
    print OUT "\# Executable for converting an icon to xbm\n";
    print OUT "ikon2xbm $ikon2xbm\n\n";
}

if ($uselynx) {
    $lynx = &promptuser("Where is lynx Installed",
			      "/usr/local/bin/lynx");
    print OUT "\# ConfSkip\n";
    print OUT "\# Lynx\n";
    print OUT "lynx $lynx\n";
}

close CONF;
close OUT;


unlink ("Projects.conf");
link ("$$.tmp","Projects.conf");
unlink("$$.tmp");

# Try and build mmencode
print <<EOM;

-------------------
Now lets try and compile mmencode.  mmencode is part of Meta-Mail, but since
not everyone seems to have that installed, and its pretty darn crucial to
the whole functioning of this systems, we've included it.  If this make fails,
you will have to figure out why yourself.  It seems fairly bulletproof, though.
Of course, if you don't have cc or something awful like that, you're in
trouble.  

If you really can't get over this next step (eg, you get errors ...) please
mail me, but try and get it working yourself first ...
 - Paul ($me)

EOM

# too lazy to write a makefile for something this simple ...
system "cd $installdir/c_code\; cc -c codes.c\; cc mmencode.c -o ../bin/mmencode codes.o\; /bin/rm codes.o\; cc qp.c -o ../bin/qp; cd ..";
print "\nBuild Done\n";
print "\n";
$nothing = &promptuser("Pausing","Press any key to continue");


# Install the Icons
system "mkdir -p $hthome/Icons; cp Icons/* $hthome/Icons";

# Start up the Log File
system "mkdir -p $hthome/LogFile";
if (!(-e "$hthome/LogFile/LogFile")) {
    open (LOG, "> $hthome/LogFile/LogFile") ||
	die "Configure Error 5, $hthome/LogFile/LogFile. Please contact $me\n";
    $today = `date`;
    print LOG "NEW LOG\nStarted $today\n";
    close LOG;
}

# Make an index.html in $hthome as 644
if (!(-e "$hthome/index.html")) {
    open (IDX, "> $hthome/index.html") ||
	die "Configure Error 6, $hthome/index.html. Please contact $me\n";
    print IDX <<EOIDX;
<title>$organization Projects Page</title>
<h1>$organization Projects Page</h1>
This page is the list of projects for $organization.  Click on a project for 
posting summaries.
<hr>
<UL>
</UL>
<hr>
<address>$originator / $organization Projects Page</address>
EOIDX
    close IDX;
}

# Make a .forward file
open (FWD, "> $ENV{'HOME'}/.forward") ||
    die "Configure Error 7, $HOME/.forward.  Please contact $me\n";
print FWD "|$installdir/projectsfilter";

# Tell em we're done and go on home.
print <<EOM;

------------------------
The installation process is now complete, and your projects page,
$originator, is now ready to accept Email. 

You should now use the tools/makeproject command to add projects 
to your list.  Please see the online documentation on

http://jean-luc.ncsa.uiuc.edu/Codes/CoCoBoard

and the prompts given in the command.

EOM

# Utility Routines

sub promptuser {
    ($promptstring, $defval) = @_;
    print "$promptstring ($defval) :"; 
    $useranswer = <STDIN>;
    chop $useranswer;
    if ($useranswer =~ /^$/) {
	return $defval;
    } else {
	return $useranswer;
    }
}				

