#!/usr/local/bin/perl

#This script is public domain. It is maintained by C.Parisel
#(cparisel@hotmail.com)
#
#check out announcements on www.hec.ca/~cparis/gog
#
#Use and modify at your own risk.
#Please notify me for bug reports and suggestions

$version="Gog Configure version 1.3 (07-Apr-99)\n\n";

$lgth=200;
$servl="";
$magog_version="2.6";
$gog_version="2.2";
$ze_http_user_dir="/tmp";
$ze_http_user_name="nobody";
$ze_http_user_group="nobody";
$ze_http_doc_root="/home/httpd";
$ze_gogview_root="/home/httpd/GogView";
$ze_http_cgi_bin="/home/httpd/cgi-bin";
$ze_profile_dir="/var/local/integrity";
$ze_login=ftp;
$ze_os="Linux";

sub usage {
  $msg="Usage: configure [client|server]\n\n";
  syswrite STDOUT,$msg,length($msg);
  exit(1);
}

sub set_os {
  $ze_os=qx{ uname };
  chop($ze_os);
  SWITCH_OS: for ($ze_os) {
    /Linux/ && do {$ze_flag="-n";$ze_rc="";$ze_shell="sh";last;};
    /SunOS/ && do {$ze_flag="";$ze_rc="\\c";$ze_shell="ksh";last;};
    /HP-UX/ && do {$ze_flag="";$ze_rc="\\c";$ze_shell="sh";last;};
    /AIX/ && do {$ze_flag="";$ze_rc="\\c";$ze_shell="ksh";};
  }
}

sub set_shell {
  $ze_shell=qx{ which $ze_shell };
  chop($ze_shell);
  SET_SHELL:
  $msg="Where is your Bourne/Posix/Korn shell? [$ze_shell]";
  syswrite STDOUT,$msg,length($msg);
  $l=sysread STDIN,$r,$lgth;
  if ($l>1) {chop($r);
    if (-f $r) {$ze_shell=$r;} else {
      $msg="**Shell not found! ";
      syswrite STDOUT,$msg,length($msg);
      goto SET_SHELL;
    }
  }
}

sub set_profile {
  $msg="You can place server profiles on a local disk or on a removable media
(/mnt/floppy) for better security. Where do you want to store them? [$ze_profile_dir]";
  syswrite STDOUT,$msg,length($msg);
  $l=sysread STDIN,$r,$lgth;
  if ($l>1) {chop($r);$ze_profile_dir=$r;}
}

sub set_sum {
  $ze_sum=qx{ which md5sum };
  chop($ze_sum);
  if ($ze_sum) {
    $msg="MD5 signature is supported on your system. Do you want to use it (recommended, but not backward compatible with gogmagog-2)? [y]";
    syswrite STDOUT,$msg,length($msg);
    $l=sysread STDIN,$r,$lgth;
    chop($r);
    if ($r=~/^[nN]/) {$ze_sum="";}
  }
  if (!$ze_sum) {
    $ze_sum=qx{ which cksum };
    chop($ze_sum);
  }
  SET_SUM:
  $msg="Where is your signature utility? [$ze_sum]";
  syswrite STDOUT,$msg,length($msg);
  $l=sysread STDIN,$r,$lgth;
  if ($l>1) {chop($r);
    if (-f $r) {$ze_shell=$r;} 
    else {
      $msg="**Checksum not found! ";
      syswrite STDOUT,$msg,length($msg);
      goto SET_SUM;
    }
  }
}

sub set_http_user {
  $msg="Who is httpd running as? [$ze_http_user_name]"; 
  syswrite STDOUT,$msg,length($msg);
  $l=sysread STDIN,$r,$lgth;
  if ($l>1) {$ze_http_user_name=$r;chop($ze_http_user_name);}
  $msg="What is the group of user $ze_http_user_name? [$ze_http_user_group]"; 
  syswrite STDOUT,$msg,length($msg);
  $l=sysread STDIN,$r,$lgth;
  if ($l>1) {$ze_http_user_group=$r;chop($ze_http_user_group);}
  HTTP_USER_DIR:
  $msg="Where is $ze_http_user_name home? [$ze_http_user_dir]"; 
  syswrite STDOUT,$msg,length($msg);
  $l=sysread STDIN,$r,$lgth;
  if ($l>1) {chop($r);
    if (-d $r) {$ze_http_user_dir=$r;} else {
      goto HTTP_USER_DIR;
    }
  }
}

sub set_gogview {
  HTTP_DOC_ROOT:
  $msg="Where is the httpd document root? [$ze_http_doc_root]"; 
  syswrite STDOUT,$msg,length($msg);
  $l=sysread STDIN,$r,$lgth;
  if ($l>1) {chop($r);}
  if (-d $r) {$ze_http_doc_root=$r;} else {
    $msg="**Document root directory not found! ";
    syswrite STDOUT,$msg,length($msg);
    goto HTTP_DOC_ROOT;
  }
  $ze_http_cgi_bin="/cgi-bin";
  HTTP_CGI_BIN:
  $msg="What is the relative path to the cgi-bin directory? $ze_http_doc_root\[$ze_http_cgi_bin]";
  syswrite STDOUT,$msg,length($msg);
  $l=sysread STDIN,$r,$lgth;
  if ($l>1) {chop($r);
    if ($ze_gogview_root=~/^\//) {;} else {
      $ze_gogview_root="/".$ze_gogview_root;
    }
    if (-d $r) {$ze_http_cgi_bin=$r;} else {
      $msg="**Directory does not exist! ";
      syswrite STDOUT,$msg,length($msg);
      goto HTTP_CGI_BIN;
    }
  }
  $ze_gogview_root="/GogView";
  $msg="Where do you want me to create the GogView directory? $ze_http_doc_root\[$ze_gogview_root]";
  syswrite STDOUT,$msg,length($msg);
  $l=sysread STDIN,$r,$lgth;
  if ($l>1) {chop($r);$ze_gogview_root=$r;}
  if ($ze_gogview_root=~/^\//) {;} else {
    $ze_gogview_root="/".$ze_gogview_root;
  }
  &set_http_user;
  $msg="Installing Configuration panel...\n";
  syswrite STDOUT,$msg,length($msg);
  if (-d "$ze_http_doc_root$ze_gogview_root" ) {;} else {
    system "mkdir $ze_http_doc_root$ze_gogview_root";
  }
  if (-d "$ze_http_doc_root$ze_gogview_root/maps") {;} else {
    system "mkdir $ze_http_doc_root$ze_gogview_root/maps";
  }
  if (!open(RFILE,"<server/GogView/conf-panel.html.skel")) {
    print "Error! Cannot find skeleton file. Get a new package!\n";exit 1;
  } 
  @m_in=<RFILE>;
  close RFILE;
  if (open(WFILE,">$ze_http_doc_root$ze_gogview_root/conf-panel.html")) {
    foreach $ll (@m_in) {
      $ll=~s/\%\%CGI_BIN_DIR\%\%/$ze_http_cgi_bin/og;
      syswrite WFILE,$ll,$lgth;
    }
    close WFILE;
  } else {
    $msg="Error! Cannot create configuration panel. Check R/W permissions!\n";
    syswrite STDOUT,$msg,length($msg);
    exit 1;
  }
  if (!open(RFILE,"<server/GogView/select-panel.html.skel")) {
    print "Error! Cannot find skeleton file. Get a new package!\n";exit 1;
  } 
  @m_in=<RFILE>;
  close RFILE;
  $msg="Installing Selection panel...\n";
  syswrite STDOUT,$msg,length($msg);
  if (open(WFILE,">$ze_http_doc_root$ze_gogview_root/select-panel.html")) {
    foreach $ll (@m_in) {
      $ll=~s/\%\%CGI_BIN_DIR\%\%/$ze_http_cgi_bin/og;
      syswrite WFILE,$ll,$lgth;
    }
    close WFILE;
  } else {
    $msg="Error! Cannot create selection panel. Check R/W permissions!\n";
    syswrite STDOUT,$msg,length($msg);
    exit 1;
  }
  if (!open(RFILE,"<server/GogView/GVconfigure.pl.skel")) {
    print "Error! Cannot find skeleton file. Get a new package!\n";exit 1;
  } 
  @m_in=<RFILE>;
  close RFILE;
  $msg="Installing Gogview configuration (cgi script)...\n";
  syswrite STDOUT,$msg,length($msg);
  if (open(WFILE,">$ze_http_doc_root$ze_http_cgi_bin/GVconfigure.pl")) {
    foreach $ll (@m_in) {
      $ll=~s/\%\%HTTP_ROOT\%\%/$ze_http_doc_root/og;
      $ll=~s/\%\%GOGVIEW\%\%/$ze_gogview_root/og;
      syswrite WFILE,$ll,$lgth;
    }
    close WFILE;
    system "chmod 755 $ze_http_doc_root$ze_http_cgi_bin/GVconfigure.pl";
  } else {
    $msg="Error! Cannot create configuration CGI. Check R/W permissions!\n";
    syswrite STDOUT,$msg,length($msg);
    exit 1;
  }
  if (!open(RFILE,"<server/GogView/GVselect.pl.skel")) {
    print "Error! Cannot find skeleton file. Get a new package!\n";exit 1;
  } 
  @m_in=<RFILE>;
  close RFILE;
  $msg="Installing GogView selection (cgi script)...\n";
  syswrite STDOUT,$msg,length($msg);
  if (open(WFILE,">$ze_http_doc_root$ze_http_cgi_bin/GVselect.pl")) {
    foreach $ll (@m_in) {
      $ll=~s/\%\%HTTP_ROOT\%\%/$ze_http_doc_root/og;
      $ll=~s/\%\%GOGVIEW\%\%/$ze_gogview_root/og;
      $ll=~s/\%\%CGI_BIN_DIR\%\%/$ze_http_cgi_bin/og;
      syswrite WFILE,$ll,$lgth;
    }
    close WFILE;
    system "chmod 755 $ze_http_doc_root$ze_http_cgi_bin/GVselect.pl";
  } else {
    $msg="Error! Cannot create selection CGI. Check R/W permissions!\n";
    syswrite STDOUT,$msg,length($msg);
    exit 1;
  }
  system "chown -R $ze_http_user_name:$ze_http_user_group $ze_http_doc_root$ze_gogview_root;chmod -R 777 $ze_http_doc_root$ze_gogview_root";
  $msg="Installing icons...\n";
  syswrite STDOUT,$msg,length($msg);
  system "cp server/GogView/*.gif $ze_http_doc_root$ze_gogview_root/";
}

if ($#ARGV<0) {
  &usage; }
else {
  $action=$ARGV[0];
}

$selection=0;

$msg=$version;
$msg.="This script will configure and install gogmagog for your system.\n";
$msg.="You can exit at any time by pressing ^C (Control-C) and start over if\n";
$msg.="you wish to change your settings.\n\n";
$msg.="Whenever asked for something, the default answer is shown between\n";
$msg.="[brackets]. Pressing Enter at that point will take the default answer.";
$msg.="\n\n";
syswrite STDOUT,$msg,length($msg);
&set_os;

if ($action=~/client/) {
  $selection=1;
  $msg="You have chosen a CLIENT (gog version $gog_version) installation.\n";
  syswrite STDOUT,$msg,length($msg);
  &set_shell;
  &set_sum;
  $msg="Building gog...\n";
  syswrite STDOUT,$msg,length($msg);
  if (!open(RFILE,"<client/gog-$gog_version.skel")) {
    print "Error! Cannot find skeleton file. Get a new package!\n";exit 1;
  } 
  @m_in=<RFILE>;
  close RFILE;
  $msg="Installing gog...\n";
  syswrite STDOUT,$msg,length($msg);
  if (open(WFILE,">/sbin/gog")) {
    foreach $ll (@m_in) {
      $ll=~s/\%\%SHELL\%\%/\#\!$ze_shell/og;
      $ll=~s/\%\%SUM\%\%/$ze_sum/og;
      $ll=~s/\%\%FLAG\%\%/$ze_flag/og;
      $ll=~s/\%\%CARRIAGE_RETURN\%\%/$ze_rc/og;
      syswrite WFILE,$ll,$lgth;
    }
    close WFILE;
  } else {
    $msg="Error! Cannot create /sbin/gog. Check R/W permissions!\n";
    syswrite STDOUT,$msg,length($msg);
    exit 1;
  }
  system "chmod 700 /sbin/gog";
  $msg="\nDo not forget to add an entry to the root crontab and customize the template configuration file (client/config.tmpl) for gog.\n\n";
  syswrite STDOUT,$msg,length($msg);
}

if ($action=~/server/) {
  $selection=1;
  $msg="You have chosen a SERVER (magog version $magog_version) installation.\n";
  syswrite STDOUT,$msg,length($msg);
  &set_shell;
  &set_profile;
  $install_gogview="yes";
  $msg="Do you want to install the GogView GUI? [$install_gogview]";
  syswrite STDOUT,$msg,length($msg);
  $l=sysread STDIN,$r,$lgth;
  if ($l>1) {$install_gogview=$r;}
  if ($install_gogview=~/^[y|Y]/) {$install_gogview=1;} else {
    $install_gogview=0;
  }
  if ($install_gogview==0) {
    $msg="Default FTP login name ? [$ze_login]"; 
    syswrite STDOUT,$msg,length($msg);
    $l=sysread STDIN,$r,$lgth;
    if ($l>1) {$ze_login=$r;}
    chop($ze_login);
  } else {
    &set_gogview;
  }
  $msg="If you want me to retrieve some profiles, enter a space-sparated list of host names\n(optional, you can do that yourself later): "; 
  syswrite STDOUT,$msg,length($msg);
  $l=sysread STDIN,$r,$lgth;
  $msg="Building magog...\n"; 
  syswrite STDOUT,$msg,length($msg);
  if ($l>1) {chop($r);$servl=$r;}
  if (!open(RFILE,"<server/magog-$magog_version.skel")) {
    print "Error! Cannot find skeleton file. Get a new package!\n";exit 1;
  } 
  @m_in=<RFILE>;
  close RFILE;
  @m_in_copy=@m_in;
  $msg="Installing magog in /sbin directory...\n"; 
  syswrite STDOUT,$msg,length($msg);
  if (open(WFILE,">/sbin/magog")) {
    foreach $ll (@m_in) {
      $ll=~s/\%\%SHELL\%\%/\#\!$ze_shell/o;
      $ll=~s/\%\%HTTP_USER_DIR\%\%/\"\/tmp\"/o;
      $ll=~s/\%\%LOGIN\%\%/\"$ze_login\"/o;
      $ll=~s/\%\%PROFILE_DIR\%\%/$ze_profile_dir/o;
      syswrite WFILE,$ll,$lgth;
    }
    close WFILE;
  } else {
    $msg="Error! Cannot create /sbin/magog. Check R/W permissions!\n";
    syswrite STDOUT,$msg,length($msg);
    exit 1;
  }
  system "chmod 700 /sbin/magog";
  if ($install_gogview==1) {
    $msg="Installing a copy of magog in GogView directory...\n"; 
    syswrite STDOUT,$msg,length($msg);
    if (open(WFILE,">$ze_http_doc_root/$ze_http_cgi_bin/magog")) {
      foreach $ll (@m_in_copy) {
        $ll=~s/\%\%SHELL\%\%/\#\!$ze_shell/o;
        $ll=~s/\%\%HTTP_USER_DIR\%\%/$ze_http_user_dir/o;
        $ll=~s/\%\%LOGIN\%\%/$ze_http_user_name/o;
        $ll=~s/\%\%PROFILE_DIR\%\%/$ze_profile_dir/o;
        syswrite WFILE,$ll,$lgth;
      }
      close WFILE;
    } else {
      $msg="Error! Cannot create $ze_http_doc_root/$ze_http_cgi_bin/magog. Check R/W permissions!\n";
      syswrite STDOUT,$msg,length($msg);
      exit 1;
    }
    system "chmod 755 $ze_http_doc_root/$ze_http_cgi_bin/magog";
  }
  if (-d $ze_profile_dir) {;} else {
    $msg="Creating $ze_profile_dir...\n";
    syswrite STDOUT,$msg,length($msg);
    system "mkdir $ze_profile_dir";
    if ($install_gogview==1) {system "chmod 755 $ze_profile_dir";}
    else {system "chmod 700 $ze_profile_dir";}
  }
  if (length($servl)>0) {
    $msg="Filling in $ze_profile_dir...\n";
    syswrite STDOUT,$msg,length($msg);
    $msg="Enter a login name, so I can retrieve the profiles: ";
    syswrite STDOUT,$msg,length($msg);
    $l=sysread STDIN,$r,$lgth;
    chop($r);$login=$r;
    $msg="Now enter the password for account $login: ";
    syswrite STDOUT,$msg,length($msg);
    system "stty -echo";
    $l=sysread STDIN,$r,$lgth;
    chop($r);$pass=$r;
    system "stty echo";
    system "magog -bot $login $pass $servl";
    if ($install_gogview==1) {system "chmod 555 $ze_profile_dir/*";}
  } else {
    $msg="All done.\nDont forget to fill in $ze_profile_dir.\n\n";
    syswrite STDOUT,$msg,length($msg);
  }
}

if ($selection==0) {&usage;}
