eval '(exit $?0)' && eval '[ -f /usr/local/bin/perl ] && exec /usr/local/bin/perl -S $0 ${1+"$@"}; exec perl -S $0 ${1+"$@"};'
& eval 'if ( -f /usr/local/bin/perl ) exec /usr/local/bin/perl -S $0 $argv:q ; exec perl -S $0 $argv:q'
     if 0;

# @(#)[Hyper-G] [INS] hginsttar	1.11 [server inst. script] [Gerald Pani]

# 
#<copyright>
# 
# Copyright (c) 1995
# Institute for Information Processing and Computer Supported New Media (IICM),
# Graz University of Technology, Austria.
# 
#</copyright>
  
#<file>
#
# Name:       hgtarinst.pl
#
# Purpose:    Installation and update of the Hyper-G Server 
#
# Created:    17 Oct 95    Gerald Pani
#
# Description:
# 
#    This script installs the Hyper-G server or tools into current directory.
#    Invoke it from your homedir for server installations.
# 
#</file>
# 
# $Id: hginsttar.pl,v 1.4 1996/02/07 15:55:12 gpani Exp $
# 
# $Log: hginsttar.pl,v $
# Revision 1.4  1996/02/07 15:55:12  gpani
# hginsttar 1.11
# o bug fixed: syntax error when using perl 4.036
#
# Revision 1.3  1996/01/25 16:44:50  gpani
# hginsttar 1.10
# o OFFHOSTNAME, DIRdbs, DIRhgs
# o IBMAIX supported
#

@cpuSupp = ('SUN4', 'PMAX', 'HPUX9', 'SUN5', 'SGI', 'ALPHA_OSF1', 'LINUX', 'IBMAIX');	# supported cpu types

@limCore = ('SUN4', 'PMAX');		# limit core size

$tartmp = 'tartmp';
$doLog = 1;
$myName = &basename( $0);
$exact = 0;
$confirm = 0;
$nochanges = 0;
$changelog = 0;

select(STDOUT); $| = 1;

# parse any switches
{
    local($arg);
    while ($arg = shift) {
	($arg =~ /^-h$/)    && (die &help());	    # help
	($arg =~ /^-help$/) && (die &help());	    # help
	($arg =~ /^-exac/) && ($exact = 1, next); # fetch exact versions
	($arg =~ /^-conf/) && ($confirm = 1, next); # confirm installations
	($arg =~ /^-noch/) && ($nochanges = 1, next);
	($arg =~ /^-chan/) && ($changelog = 1, next); # creates hginstserver.clog
	($arg =~ /^-tard/) && (($tartmp = shift) ? next : die "no argument for -tardir\n");
	die &Usage();
    }
}

$instServer = (-e "$tartmp/.ServerFileTable") ? 1 : 0;
$instTools = (-e "$tartmp/.ToolsFileTable") ? 1 : 0;

die "no FileTables\n" unless $instServer + $instTools;

# check install directory
$instdir = &Pwd();
chdir() || die "can't find homedirectory\n";
$homedir = &Pwd();
chdir($instdir) || die "can't change to $instdir\n";

$instServer && ($instdir ne $homedir) && die "Current directory is not homedirectory.\n";

if ($changelog) {
    rename( "$myName.clog", "$myName.clog.old");
    open( CLOG, "> $myName.clog") || die "Couldn't open $myName.clog: $!\n";
}

# check platform
($CPU = &readcpu()) || exit(1);
$ENV{'CPU'} = $CPU;

#set umask
umask(022);

# check /usr/local/Hyper-G
$hypergHome = $ENV{'HYPERG_HOME'} || '/usr/local/Hyper-G';
$hypergHome =~ s/\/$//;

# check hostname
$hostname = `hostname` || die "Couldn't find hostname\n";
chop($hostname);

$instHyperG = 1;
-e "$hypergHome" 
    || ((print "Warning: $hypergHome doesn't exist\n") && ($instHyperG = 0));
!$instHyperG || -d "$hypergHome" 
    || ((print "Warning: $hypergHome must be a directory\n") && ($instHyperG = 0));
!$instHyperG || -r "$hypergHome" 
    || ((print "Warning: no read access to $hypergHome\n") && ($instHyperG = 0));
!$instHyperG || -w "$hypergHome" 
    || ((print "Warning: no write access to $hypergHome\n") && ($instHyperG = 0));

if ($instServer) {
    # check serverstring
    $servstring = $ENV{'HGServerString'};
    defined($servstring) || ($servstring = &readservstring()) || die "No HGServerString defined\n";
    $ENV{'HGServerString'} = $servstring;

    # check offHostName
    $offHostName = $ENV{'OFFHOSTNAME'} || $ENV{'GOPHHOST'};
    defined($offHostName) || ($offHostName = &readOffHostName()) 
	|| die "No full internet host name defined\n";
    $ENV{'OFFHOSTNAME'} = $offHostName;

    # check ~/dbserver
    $useOldDIRdbs = 0;
    if (! -d "dbserver") {
	if (-d "server") {
	    print( "Warning: using $homedir/server instead of $homedir/dbserver for DIRdbs\n");
	    print( "\tTo avoid this message: stop the server, rename this directory,\n");
	    print( "\tinvoke hginsttar, 'source ~/.hgrc' and restart the server.\n");
	    $useOldDIRdbs = 1;
	}
	else {
	    -f "dbserver" && die "dbserver must be a directory\n";
	    mkdir("dbserver", 0700) || die "Can't create directory dbserver\n";
	}
    }

    # check ~/HTF
    $useOldDIRhgs = 0;
    if (! -d "hgserver") {
	if (-d "HTF") {
	    print( "Warning: using $homedir/HTF instead of $homedir/hgserver for DIRhgs\n");
	    print( "\tTo avoid this message: stop the server, rename this directory,\n");
	    print( "\tinvoke hginsttar, 'source ~/.hgrc' and restart the server.\n");
	    $useOldDIRhgs = 1;
	}
	else {
	    -f "hgserver" && die "hgserver must be a directory\n";
	    mkdir("hgserver", 0700) || die "Can't create directory hgserver\n";
	}
    }

    # check ~/ftserver
    -f "ftserver" && die "ftserver must be a directory\n";
    -d "ftserver" || mkdir("ftserver", 0700) || die "Couldn't make directory ftserver\n";
    # chmod(0700, "ftserver");

    # check ~/dcserver
    -f "dcserver" && die "dcserver must be a directory\n";
    -d "dcserver" || mkdir("dcserver", 0700) || die "Couldn't make directory dcserver\n";
    # chmod(0700, "dcserver");

    &MakePath('dcserver/local');
    &MakePath('dcserver/cache');
}
    
# check ~/bin
&MakePath('sgml');
&MakePath('samples');
&MakePath('contrib');
&MakePath('man/man1');
&MakePath('doc');
&MakePath("bin/$CPU");
&MakePath('bin/scripts');

if ($instHyperG) {
    chdir( "$hypergHome") || die "Can't change to $hypergHome: $!\n";
    local( $pwd) = &Pwd();
    chdir( $instdir);
    if ($instServer && $instdir ne $pwd) {
	&hgLinkDir('bin');
	&hgLinkDir('sgml');
	&hgLinkDir('samples');
	&hgLinkDir('contrib');
	&hgLinkDir('man');
	&hgLinkDir('doc');
    }
  
    if ( !-f "$hypergHome/hgtv.rc") {
	if (&confirmation( "create $hypergHome/hgtv.rc")) {
	    open(TVRC, "> $hypergHome/hgtv.rc") || die "Couldn't open $hypergHome/hgtv.rc\n";
	    print TVRC &tvrcdata();
	    close (TVRC);
	}
    }
}
else {
    if ( !-f "$homedir/.hgtv.rc") {
	if (&confirmation( "create $homedir/.hgtv.rc")) {
	    open(TVRC, "> $homedir/.hgtv.rc") || die "Couldn't open $homedir/.hgtv.rc\n";
	    print TVRC &tvrcdata();
	    close (TVRC);
	}
    }
    if ( !-f "hgtv.rc") {
	if (&confirmation( "create hgtv.rc")) {
	    open(TVRC, "> hgtv.rc") || die "Couldn't open hgtv.rc\n";
	    print TVRC &tvrcdata();
	    close (TVRC);
	}
    }
}

if ($instServer) {
    # adjust or create .hgrc
    if (-f ".hgrc") {
	if (!&readHGRC( ".hgrc")) {
	    &createHGRC( ".hgrc.recommended");
	    if (&confirmation( "change .hgrc")) {
		rename( ".hgrc", ".hgrc.old");
		rename( ".hgrc.recommended", ".hgrc");
	    }
	}
    }
    else {
	&createHGRC( ".hgrc.recommended");
	if (&confirmation( "create .hgrc")) {
	    rename( ".hgrc.recommended", ".hgrc");
	}
    }
    
    # adjust or make .cshrc
    if (open(CSHRC, "< .cshrc")) {
	open(NCSHRC, "> .cshrc.new") || die "Couldn't create .cshrc.new\n";
	local( $inserted) = 0;
	while (<CSHRC>) {
	    if (/^\s*#\s*Hyper-G\s+resourcefile\s*$/) {
		next;
	    }
	    elsif (/^\s*#\s*@\(#\)\[Hyper-G\] .*$/) {
		next;
	    }
	    elsif (/^\s*source\s+~\/.hgrc\s*$/o) {
		print NCSHRC '# Hyper-G resourcefile', "\n";
		print NCSHRC '# @','(#)[Hyper-G] [HGS-CF] .cshrc	1.00 [ADDITION csh conf.] [Gerald Pani]', "\n";
		$inserted = 1;
	    }
	    print NCSHRC $_;
	}
		     
	if (! $inserted) {
	    print NCSHRC '# Hyper-G resourcefile';
	    print NCSHRC "\n";
	    print NCSHRC '# @','(#)[Hyper-G] [HGS-CF] .cshrc	1.00 [ADDITION csh conf.] [Gerald Pani]', "\n";
	    print NCSHRC 'source ~/.hgrc';
	    print NCSHRC "\n";
	}
	close(NCSHRC);
	close(CSHRC);
	
	open(CSHRC, "< .cshrc");
	local( $checksum, $newchecksum);
	{ local($/); $checksum = unpack("%16C*", <CSHRC>); }
	close(CSHRC);
	open(CSHRC, "< .cshrc.new");
	local( $newchecksum);
	{ local($/); $newchecksum = unpack("%16C*", <CSHRC>); }
	close(CSHRC);
	if ($checksum != $newchecksum) {
	    if (&confirmation( "change .cshrc")) {
		rename( ".cshrc", ".cshrc.old");
		rename( ".cshrc.new", ".cshrc");
	    }
	}
    }
    else {
	open(NCSHRC, "> .cshrc.new") || die "Couldn't create .cshrc.new\n";
	print NCSHRC "\n";
	print NCSHRC '# Hyper-G resourcefile';
	print NCSHRC "\n";
	print NCSHRC 'source ~/.hgrc';
	print NCSHRC "\n";
	close(NCSHRC);
	if (&confirmation( "create .cshrc")) {
	    rename(".cshrc.new", ".cshrc");
	}
    }
}
if ($doLog) {
  open(ILOG, "> $myName.log.new") ||  die "Couldn't open $myName.log.new\n";
  select(ILOG); $| = 1; select(STDOUT);
}

$catfiles = "";
$catfiles .= "$tartmp/.ServerFileTable " if $instServer;
$catfiles .= "$tartmp/.ToolsFileTable " if $instTools;

open(FT, "cat $catfiles|") 
    || die "can't open $catfiles\n";
while(<FT>) {
    local($lExact) = $exact;
  # make symbolic link
  if (/^\s*<SLINK>\s*(\S+)\s*(\S+)\s*$/o) {
    local( $slink) = $1;
    local( $linkName) = $2;
    $linkName = &substPath( $linkName);
    if ((! -l $linkName) || (readlink($linkName) ne $slink)) {
	if (&confirmation( "create symbolic link $linkName -> $slink")) {
	    unlink( $linkName);
	    symlink( $slink, $linkName);
	    print "\tsymlink\t$slink $linkName\n";
	}
    }
    next;
  }
  # make a directory
  if (/^\s*<DIR>\s*(\S+)\s*$/o) {
    local( $newDir) = $1;
    $newDir = &substPath( $newDir);
    if (!-d $newDir) {
      &MakePath( $newDir);
      print "\tmakedir\t$newDir\n";
    }
    next;
  }
  # unlink a file
  if (/^\s*<UNLINK>\s*(\S+)\s*$/o) {
    local( $file) = $1;
    $file = &substPath( $file);
    if (-f $file) {
	if (&confirmation( "remove $file")) {
	    print "\tunlink\t$file\n";
	    unlink( $file);
	}
    }
    next;
  }
  local($path, $rsize, $rmode, $rmtime, $instMode) = split;
  defined($path) && defined($rsize) && defined($rmode) && defined($rmtime) || last;
  # line is ok
  # create localpath
  local($localpath) = $path;
  $localpath = &substPath( $localpath);
  if ($localpath =~ /^HYPERG_HOME\./) {
      if ($instHyperG) {
	  $localpath =~ s/^HYPERG_HOME\./$hypergHome\//;
      }
      else {
	  $localpath =~ s/^HYPERG_HOME\.//;
      }
  }

  local($old) = 'old';
  if ($instMode) {
      if ($instMode =~ /^<SINGLE_REQUIRED_(\d+\.\d+)>$/) {
	  local($required) = $1;
	  if (-e $localpath) {
	      local($ver) = &whatVersion($localpath);
	      $ver = 0.00 unless $ver;
	      if ($required > $ver) {
		  local( @timearr) = localtime( time);
		  local( $mday, $mon, $year) = splice(@timearr, 3, 3);
		  $old = sprintf( "old.%02d%02d%02d", $year, $mon +1, $mday);
		  $lExact = 1;
	      }
	      else {
		  next;
	      }
	  }
      }
      elsif ($instMode =~ /^<SINGLE_RECOMMENDED_(\d+\.\d+)>$/)  {
	  local($recommended) = $1;
	  if (-e $localpath) {
	      local($ver) = &whatVersion($localpath);
	      $ver = 0.00 unless $ver;
	      if ($recommended > $ver) {
		  if (-e "$localpath.recommended") {
		      local($ver) = &whatVersion("$localpath.recommended");
		      $ver = 0.00 unless $ver;
		      if ($recommended > $ver) {
			  $localpath = "$localpath.recommended";
		      }
		      else {
			  next;
		      }
		  }
		  else {
		      $localpath = "$localpath.recommended";
		  }
	      }
	      else {
		  next;
	      }
	  }
      }
      elsif ($instMode =~ /^<SINGLE>$/) {
	  next if (-e $localpath);
      }
      else {
	  next;
      }
  }
  if (-l $localpath && &confirmation( "remove $localpath")) {
      unlink( $localpath);
  }
  local($mtime, $size, $mode) = &filetime($localpath);

  local( $upToDate) = 0;
  if (defined($mtime)) {
      if (!$lExact && ($mtime > $rmtime)) {
	  # need not install;
	  $upToDate = 1;
      }
      elsif (($mtime == $rmtime) && ($size == $rsize)) {
	  # need not install;
	  $upToDate = 1;
      }
  }

  if ($upToDate) {	# need not install;
      print "    \tup to date\t$localpath\n";
  }
  else {
      if ($changelog) {
	  local($file) = &basename($path);
	  local($ver) = &whatVersion($localpath);
	  local($info) = &changeLogInfo( $file, $ver);
	  print CLOG "$file $ver -> $info";
      }
      if ($confirm) {
	  while( 1) {
	      local( $resp);
	      print "install $localpath? ([y]/c/n) ";
	      $resp = <STDIN>;
	      if ($resp =~ /^[Cc]/) {
		  local($file) = &basename($path);
		  local($ver) = &whatVersion($localpath);
		  local($info) = &changeLogInfo($file, $ver);
		  print $info;
	      }
	      else {
		  if ($resp =~ /^[^Yy\n]/ || $nochanges) {
		      $upToDate = 1;
		  }
		  last;
	      }
	  }
      }
      elsif ($nochanges) {
	  print "    \tto install\t$localpath\n";
	  $upToDate = 1;
      }
      if (!$upToDate) {
	  local($d) = &dirname($localpath);
	  die "Error: no directory $d\n" unless (!$d || -d $d);
	  print "    \tpreparing\t$localpath\r";
	  &FetchFile($localpath, $path, $rsize, $rmode, $old) || die "Couldn't fetch $localpath\n";
	  ($mtime, $size, $mode) = &filetime($localpath);
      }
  }

  if (!$upToDate) {
      $mode = $mode & 07777;
      if ($mode != oct($rmode) & 07777) {
	  chmod oct($rmode) & 07777, "$localpath";
      }
      if ($mtime != $rmtime) {
	  utime($rmtime, $rmtime, $localpath);
      }
  }
  &log( $localpath);
}
close(FT);
close(ILOG) if $doLog;
close(CLOG) if $changelog;

if ($doLog) {
  rename("$myName.log", "$myName.log.old");
  rename("$myName.log.new", "$myName.log");
}

exit;

sub help {
  return '
    This script installs the Hyper-G server or tools into current directory.
    Invoke it from your homedir for server installations.

'.&Usage();
}

sub Usage {
  return "
Usage: $0 [options]

        -h[elp]         help
        -conf[irm]      confirm each installation ([y]/c/n)
                        c ... retrieves a changelog for this file
        -noch[anges]    don't update installation
        -chan[gelog]    create changelog file \'$myName.clog\'
        -tard[ir] dir   dir is the directory containing the expanded
                        tar archive. Default is \'tartmp\'.
";
}

sub substPath {
   local( $name) = @_;
   return $name;
}
    
sub readservstring {
  local($ss);
  print "Hyper-G ServerString:";
  $ss = <STDIN>;
  print "\n";
  chop($ss);
  return $ss;
}

sub readOffHostName {
  local($ss);
  print "Full internet host name of this machine:";
  $ss = <STDIN>;
  print "\n";
  chop($ss);
  return $ss;
}

sub tvrcdata {
  return "# Resource file for hgtv
# 1. homedir/.hgtv.rc or 2. HYPERG_HOME/hgtv.rc or 3. /usr/local/Hyper-G/hgtv.rc or default.
# For interpretation see hgtv -v
 
# startcollection
# <ROOT> rootcollection 

# hostname and port of Hyper-G-server 
<HGHOST>$hostname
<HGPORT>418

# Supported Languages: english, german, styrian
<LANGUAGE>english

# Sort order is an ordered list of letters:
#         -       on 1st position: sort descending (default ascending)
#         A       Author
#         C       Creation time
#         E       Expiration time
#         O       Opening time
#         P       Parent (Search only)
#         S       Score (WAIS only)
#         T       Title
#         t       Type (Document, Collection, Anchor...)
<SORTORDER>

# file to log errors
<ERRLOG>

# directory of sgml-config files
<SGMLDIR>$instdir/sgml
# path for scripts and binaries used by the client  
<PATH>$instdir/bin/scripts $instdir/bin/$CPU
";
}

# common functions

sub FetchFile {
  local($localpath, $path, $size, $mode, $old) = @_;

  print "    \tinstalling\t$localpath\r";

  rename("$localpath", "$localpath.$old") if -e "$localpath";
  `cp $tartmp/$path $localpath`;
  chmod oct($mode) & 07777,  "$localpath";
  
  print "    \tinstalled \t$localpath\n";

  return 1; 
}

sub readcpu {
  local($CPU);

  $CPU = $ENV{'CPU'};
  if (!defined($CPU)) {
      $CPU = (&machine())[0];
      print "\nYour machine type is $CPU\n";
  }
  return $CPU if (grep( /^$CPU$/, @cpuSupp));
  die "$myName: Architecture $CPU not yet supported!\n";
}

sub filetime {
  local($file) = @_;
  local(@arr) = stat("$file");	# ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev,
			      	# $size, $atime, $mtime, $ctime, $blksize, $blocks)
  local(@result) = ($arr[9], $arr[7], $arr[2]);	# ($mtime, $size, $mode)
  @result;
}

sub MakePath {
  local($path) = @_;
  local(@parts) = split(m;/;, $path);
  local($onpath) = "";
  local($nextdir);
  while(defined($nextdir = shift(@parts))) {
    $onpath .= $nextdir;
    &MakeDir($onpath);
    $onpath .= '/';
  }
}

sub MakeDir {
  local($dir) = @_;
  -f $dir && die "$dir must be a directory\n";
  return if (-d $dir);
  if (&confirmation( "create directory $dir")) {
      mkdir($dir, 0755) || die "Couldn't make directory $dir\n";
  }
}

sub basename {
  local($path) = @_;
  
  return '' unless $path =~ m,(^|/)([^/]+)$,;
  return $2;
}

sub dirname {
  local($path) = @_;
  
  return '' unless $path =~ m,(^|^.*/)([^/]+)$,;
  return $1;
}

sub log {
    local($path) = @_;
    return unless $doLog;
    local($info) = 0;
    if (open(WHAT, "< $path")) {
	local($/) = '@(#)';
	local($x);
	$x = <WHAT>; 
	while(<WHAT>) {
	    if (/([^\n\0]*)/) {
		$x = $1;
		($x =~ /^\s*\[Hyper-G\]/) && (print(ILOG $path, "\t$x\n"), ($info = 1));
		($x =~ /^\s*\[GDS\]/) && (print(ILOG $path, "\t$x\n"), ($info = 1));
	    }
	}
	close(WHAT);
    }
    print ILOG $path, "\tNOINFO\n" unless $info;
}
 
sub hgLinkDir {
  local($dirName) = @_;
  local($src,$dst) = ("$hypergHome/$dirName", "$instdir/$dirName");

  if (-l "$src") {
      if (readlink("$src") eq "$dst") {
	  return(1);
      }
      else {
	  if (!&confirmation("unlink invalid symbolic link $src")) {
	      return(1);
	  }
	  unlink( "$src") || die "unlink invalid symbolic link $src: $!\n";
      }
  }

  -f "$src" && die "$src must be a directory\n";

  return(1) if (-d "$src");
  if (!&confirmation( "create symbolic link $src -> $dst")) {
      return(1);
  }
  symlink("$dst", "$src") 
      || die "create symbolic link $src --> $dst: $!\n";
  return(1);
}
  
sub machine {
  local( $uname, $sysname, $release);
  $uname = &which('uname') || die "uname not found\n";
  
  chop( $sysname = `$uname -s`) || die "no sysinfo from uname -s\n";
  chop( $release = `$uname -r`) || die "no release from uname -r\n";

  if ($sysname =~ /^HP-UX$/) {
    local($major, $minor) = ($release =~ /^A.([0-9]+)\.([0-9]+)/);
    if ($major < 8) {
      print "Major OS release must be at least 8 for HP-UX.\n";
      return ();
    }
    return ('HPUX', 8) if $major == 8;
    return ('HPUX9', 9) if $major == 9;
    print "Major OS release greater than 9 is not supported for HP-UX.\n";
  }
  elsif ($sysname =~ /^SunOS$/) {
    local($major, $minor) = ($release =~ /^([0-9]+)\.([0-9]+)/);
    if ($major < 4) {
      print "Major OS release must be at least 4 for SunOS.\n";
      return ();
    }
    return ('SUN4', 4) if $major == 4;
    return ('SUN5', 5) if $major == 5;
    print "Major OS release greater than SunOS 5 not supported.\n";
  }
  elsif ($sysname =~ /^ULTRIX$/) {
    local($major, $minor) = ($release =~ /^([0-9]+)\.([0-9]+)/);
    if ($major < 4) {
      print "Major OS release must be at least 4 for ULTRIX.\n";
      return ();
    }
    return ('PMAX', 4) if $major == 4;
    print "Major OS release greater than ULTRIX 4 not supported!\n";
  }
  elsif ($sysname =~ /^IRIX$/) {
    local($major, $minor) = ($release =~ /^([0-9]+)\.([0-9]+)/);
    print "Warning: Hyper-G may not work properly on $sysname $release.\n" if $major == 4;
    return ('SGI', $major);
  }
  elsif ($sysname =~ /^OSF1$/) {
    local($major, $minor) = ($release =~ /^V([0-9]+)\.([0-9]+)/);
    print "Warning: Hyper-G may not work properly on $sysname $release.\n" if $major < 2;
    return ('ALPHA_OSF1', $major);
  }
  elsif ($sysname =~ /^Linux$/) {
    local($major, $minor) = ($release =~ /^([0-9]+)\.([0-9]+)/);
    print "Warning: Hyper-G may not work properly on $sysname $release.\n" if $major != 1;
    return ('LINUX', $major);
  }
  elsif ($sysname =~ /^AIX$/) {
      local($major, $minor);
      chop( $major =  `$uname -v`) || die "no sysinfo from uname -v\n";
      chop( $minor =  `$uname -r`) || die "no sysinfo from uname -r\n";
      print "Warning: Hyper-G may not work properly on $sysname $major.$minor.\n" if $major < 3;
      return ('IBMAIX', $major);
  }
  else {
    print "$sysname not yet supported.\n";
  }
  return ();
}

sub which {
  local( $prog) = @_;
  local(@path) = split( /:/, $ENV{'PATH'});
  while(defined($p = shift(@path))) {
    $p .= '/' . $prog;
    return $p if (-x $p) && (-f $p);
  }
  return '';
}

sub Pwd {
    local($pwd);
    local($dd,$di) = stat('.');
    chop($pwd = `pwd`);
    die "Pwd:pwd empty\n" if (!$pwd);
    chdir( $pwd) || die "Pwd:chdir current:$!\n";
    local($pd,$pi) = stat('.');
    die "Pwd:dev or ino not equal\n" if ($di != $pi || $dd != $pd);
    return $pwd;
}

sub whatVersion {
  local($file) = @_;
  local($version) = '';
  return $version unless -f $file;
  if (open(WHAT, "< $file")) {
      local($/) = '@(#)';
      local($x);
      $x = <WHAT>; 
      while(<WHAT>) {
	  if (/([^\n\0]*)/) {
	      $x = $1;
	      ($x =~ /\[Hyper-G\]\s+\[[^\]]+\]\s+\S+\s+(\S+)/) && ($version = $1);
	  }
      }
      close(WHAT);
  }
  return $version;
}

sub confirmation {
    local( $out) = @_;
    local( $resp);
    return(0) if ($nochanges);
    return(1) if (!$confirm);
    print "$out? ([y]/n) ";
    $resp = <STDIN>;
    return(($resp =~ /^[^Yy\n]/) ? 0 : 1);
}

sub changeLogInfo {
    local( $filePatt, $version) = @_;
    local( $info, $lastVersion) = ('', '');
    local( $sameVersionInfo, $sameVersionInfoBeta) = ('', '');
    if (open(CL, "< $tartmp/.ChangeLog")) {
	select(CL);
	# local($/,$*) = ("", 1);
	local($/) = "";
	select(STDOUT);
	while(<CL>) {
	    if (/^(\S+) (\S+)\n/) {
		local($file, $ver) = ($1,$2);
		if ($file eq $filePatt) {
		    if ($ver gt $version) {
			s/^[^\n]+\n//;
			$info .= $_;
			$lastVersion = $ver;
		    }
		    elsif ($ver eq $version) {
			s/^[^\n]+\n//;
			$sameVersionInfo = $_;
			$lastVersion = $ver;
		    }
		}
	    }
	}
	close (CL);
    }
    if (open(CL, "< $tartmp/.ChangeLog.beta")) {
	select(CL);
# 	local($/,$*) = ("", 1);
	local($/) = "";
	select(STDOUT);
	while(<CL>) {
	    if (/^(\S+) (\S+)\n/) {
		local($file, $ver) = ($1,$2);
		if ($file eq $filePatt) {
		    if ($ver gt $version) {
			s/^[^\n]+\n//;
			$info .= $_;
			$lastVersion = $ver;
		    }
		    elsif ($ver eq $version) {
			s/^[^\n]+\n//;
			$sameVersionInfoBeta = $_;
			$lastVersion = $ver;
		    }
		}
	    }
	}
	close (CL);
    }
    if (!$info) {
	$info .= $sameVersionInfoBeta;
    }
    if (!$info) {
	$info .= $sameVersionInfo;
    }
    if (!$info) {
	$info .= "no info for this topic\n";
    }
    if ($lastVersion) {
	$info = "$lastVersion\n$info";
    }
    return $info;
}

sub createHGRC {
    local( $hgrc) = @_;
    open(HGRC, "> $hgrc") || die "Couldn't create $hgrc\n";
    print HGRC '# hgrc Hyper-G resourcefile', "\n";
    print HGRC '# @','(#)[Hyper-G] [HGS-CF] .hgrc	1.10 [server configuration file] [Gerald Pani]', "\n";
    print HGRC 'setenv HGRC true', "\n";
    print HGRC "umask 022\n";
    print HGRC "unset noclobber\n";
    print HGRC "setenv CPU $CPU\n";
    print HGRC 'set path=(~/bin/scripts ~/bin/$CPU $path)', "\n";
    if ($useOldDIRdbs) {
	print HGRC 'setenv DIRserver $HOME/server', "\n";
    }
    else {
	print HGRC 'setenv DIRdbs $HOME/dbserver', "\n";
    }
    if ($useOldDIRhgs) {
	print HGRC 'setenv DIRhtf $HOME/HTF', "\n";
    }
    else {
	print HGRC 'setenv DIRhgs $HOME/hgserver', "\n";
    }
    print HGRC 'setenv DIRfts $HOME/ftserver', "\n";
    print HGRC 'setenv DIRdcs $HOME/dcserver', "\n";
    print HGRC "setenv DBHost $hostname\n";
    print HGRC "setenv HGHOST localhost\n";
    print HGRC "setenv HGServerString \"$servstring\"\n";
    print HGRC "setenv OFFHOSTNAME \"$offHostName\"\n" if $offHostName;
    print HGRC "limit coredumpsize 0\n" if (grep( /^$CPU$/, @limCore));
    close(HGRC);
}

sub readHGRC {
    local( $hgrc) = @_;
    if (open(RC, "< $hgrc")) {
	local(%hgenv);
	while (<RC>) {
	    next if /^\s*#.*$/;
	    (/^\s*setenv\s*(\S+)\s*$/) && ($hgenv{$1} = '') && next;
	    (/^\s*setenv\s*(\S+)\s+(.*)$/) && ($hgenv{$1} = $2) && next;
	}
	close (RC);
	return 0 if ($hgenv{'CPU'} ne $CPU);
	return 0 if (!$useOldDIRdbs && !$hgenv{'DIRdbs'});
	return 0 if ($useOldDIRdbs && !$hgenv{'DIRserver'});
	return 0 if (!$useOldDIRhgs && !$hgenv{'DIRhgs'});
	return 0 if ($useOldDIRhgs && !$hgenv{'DIRhtf'});
	return 0 if (!$hgenv{'DIRfts'});
	return 0 if (!$hgenv{'DIRdcs'});
	return 0 if ($hgenv{'DBHost'} ne $hostname);
	return 0 if ($hgenv{'HGHOST'} ne $hostname && $hgenv{'HGHOST'} ne 'localhost');
	return 0 if (!$hgenv{'OFFHOSTNAME'});

	return 1;
    }
    return 0;
}
