#!/local/bin/perl
#          
# Written by: Ove Ruben R Olsen (ruben@uib.no) 
#
# To configure the ircstat package
# 
################################################################################
#
# Copyright (c) Ove Ruben R Olsen (Gnarfer from hell)
#
# Permission to use, copy, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and that
# both that copyright notice and this permission notice appear in
# supporting documentation.
# This also apply your own incarnations of any or all of this code.
# If you use portions of this code, you MUST include a apropriate notice
# where you "stole" it from.
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# Email:  Ruben@uib.no 
#
#
# This work is free software, but if you find it usefull I would appreciate
# if you donated aprox 10 USD to your country's cancer research.
#
#
# This work is entierly dedicated to Johanne B Revheim
#
################################################################################
# There is no need going beyond this line unless you are very eager to
# debug and destroy this work :-)
################################################################################
################################################################################

$logfile = "/local/irc/userlog";
$idffile = "/local/irc/ircstat.idf";
$perl = "/local/bin/perl";

print "\nThis is IRCSTAT CONFIG.\n",
       "Written by Ove Ruben R Olsen - Copyright (C) 1995.\n" ; 

print "
This program will configure the various bits and parts of the 
ircstat package. You will then be asked if you want to install it.

Checking to see if we have all files:

";
@EXEFILES= ("dycount", "farnear", "ircstat", "logexc", "newdoms", "totdays",
	    "timecount") ;
@MANFILES=( "farnear.1l", "ircstat.1l", "logexc.1l", "newdoms.1l",
            "timecount.1l", "totdays.1l");
print "Finding files: " ;
foreach (@EXEFILES,@MANFILES) {
  if (-e $_) {
    print ".";
    $kitfiles{$_} = 1;
  } else {
    print "\n$_ NOT found or readable. Will not configure.\n";
    $err ++ ;
    delete $kitfiles{$_};
    print "Finding files: ";
  }
}
if ($err) {
  print "\nFond $err errors. Maybe you do not have all files present?\n";
} else {
  print "\n\nAll files present.\n";
}

foreach (@MANFILES) { delete $kitfiles{$_} ; }
print "

Values inside [ ] are default. Just hit the <enter> key if you want to 
keep these values.

The only thing you ned to know in advance is where your perl-interpreter
is. To get to know this, type   where perl  in your shell. This should
work for the c-shell at least.

";

print "Where is your perl interpreter?\n";
$perl = &ReadLn($perl,"/local/bin/perl","~directory is ok: ", 50,0); 
print "\r\033Mperl is found in $perl              \n\n";

print "Where do you want to put the executables?\n";
$exec = &ReadLn($exec,"/local/bin/","~directory is ok: ", 50,0); 
print "\r\033MPutting files in $exec              \n\n";


print "Where do you want to put the manpages?\n";
$man = &ReadLn($man,"/local/man/man1/", "~directory is ok: ", 50,0); 
print "\r\033MPutting manual files in $man         \n\n";

print "Give the full path and name of the IDF file.\n";
$idffile = &ReadLn($idffile,$idffile, "~directory is ok: ", 50,0); 
print "\r\033MPutting the IDF in $idffile         \n\n";

print "Give the full path and name of the ircd.userlogfile.\n";
$logfile = &ReadLn($logfile,$logfile, "~directory is ok: ", 50,0); 
print "\r\033Mircd userlog is $logfile          \n\n";


foreach $fil (sort keys %kitfiles) {
  print "Configuring $fil ";
  open (OFIL,">tmp.$$");
  if (open (FIL,"$fil")){
    while (<FIL>) {
      if (/^\$logfile\s+= /) {
	print OFIL "\$logfile = \"$logfile\"\;",
	    "\t# Your userlogfile.\n";
	print "logfile ";
	next ;
      }
      if (/^\$ddfil = "/) {
	print OFIL "\$ddfil = \"$idffile\"\;",
	    "\t# Your domain description file.\n";
	print "ddffile ";
	next ;
      }

      if (/^#!\/local\/bin\/perl/) {
	print OFIL "#!$perl\n";
	print "perl ";
	next ;
      }

      print OFIL;
    }
    close (FIL);
    close (OFIL);
    rename ("tmp.$$","$fil");
    print "OK!\n";
  } else {
    print "An error has occured with the $_ file when trying to open it.\n",
	  "The error is ($!) - Skipping this file.\n";
  }
}
unlink("tmp.$$");

print "\nDo you want to install the programs now? (Y/N) ";
chop ($ans = <STDIN>);
if ($ans =~ /y/i) {
  system("/bin/cp @EXEFILES $exec");
  foreach (@EXEFILES ) { $_ = "$exec/$_"; }
  chmod 0700, @EXEFILES ;
  print "Exectuables are now installed in $exec with mode 700.\n",
	"If you are going to use the logexec program remember\n",
	"to configure the email-address within the program.\n";
} else {
  print "Ok! Nothing installed!\n";
}

print "\nDo you want to install the manpages now? (Y/N) ";
chop ($ans = <STDIN>);
if ($ans =~ /y/i) {
  system("/bin/cp @MANFILES $man");
  foreach (@MANFILES ) { $_ = "$man/$_"; }
  chmod 0644, @MANFILES ;
  print "Manpages are now installed in $man with mode 644.\n";
} else {
  print "Ok! Nothing installed!\n";
}

print "\n\n\nConfigure and install are now complete. Have a nice day.\n",
      "There is a irc-stat mailinglist. To join send a request to\n",
      "ircstat-request@vestnett.no with your email adress. There is\n",
      "a human reading the request thus it can take some time to get\n",
      "on the list - but not more than 48 hours.\n";



############################################################################
sub ReadLn{ #  $var,$str,$size  ==> $var
  $OK = 1 ;
  local ($var,$def,$str,$size,$err) = @_;
  local ($tmp);
  while ($OK) {
    $var = $var || $def ;
    printf ("%s [%-${size}s]",$str,$var);
    print "\b" x ($size +1);
    chop ($tmp = <STDIN>);
    $var = $tmp || $var;
    if ( (($var =~ /^\s+$/) || ($var eq "")) && ($err == 1)) {
      print "ERROR: Blank field not allowed.\n";
      next ;
    }
    $var = $tmp || $var;
    $OK = 0 ;
  }
  $str = "$str $var";
  print "\r\033M$str"," " x (80 - length($str)),"\n";
  substr($var,0,$size);
}

