#!/usr/local/bin/perl

	$VERSION		= "1.6";
	$MOD_DATE		= "13.Sep.94";
	$AUTHOR			= "Paul Hulford";

# 13.Sep.94 PDH - a couple of commands need HGHOST set 
# 17.Jul.94 PDH - test length of ARGV[0] otherwise option 0 fails 
# 13.Jul.94 PDH - order of path arguments was wrong 

# This file sets up the Hyper-G environment to allow any user to
# use the Hyper-G commands and viewers supported by any particular
# platform. After setting up the enviroment which is machine
# dependent it calls /usr/local/Hyper-G/bin/local/hgcmd to offer
# a menu of commands if no arguments appear on the command line.
# If there are arguments these are exec'd under the new env.

$mach = `uname -m`;
chop $mach;
 
$MACH = "";
$MACH = $mach if( $mach =~ /sun4m|RISC|IP22/ );
 
$CPU = "";
$CPU = "PMAX" if $MACH =~ /RISC/;
$CPU = "SGI" if $MACH =~ /IP22/;
$CPU = "SUN4" if $MACH =~ /sun4m/;

$HGHOST = "hmu1.cs.auckland.ac.nz";

return if ! $CPU;

$HGDIR = "/usr/local/Hyper-G";

$spath = $ENV{'PATH'}; 
@path = ( 	"$HGDIR/bin/$CPU",
		"$HGDIR/bin/scripts",
		"$HGDIR/bin/local"		);

while( defined( $p = shift(@path) ) )
{
	if( ! ( $spath =~ /(^|.*:)$p(:.*$|$)/ ) )
	{
		$ns_path = "$p".":$ns_path" if -d $p;
	}
}

$mpath = $ENV{'MANPATH'};
@path = ( "$HGDIR/man" );

while( defined( $p = shift(@path) ) )
{
	if( ! ( $mpath =~ /(^|.*:)$p(:.*$|$)/ ) )
	{
		$nm_path = "$p".":$nm_path" if -d $p;
	}
}

chop($ns_path) unless $spath;
$spath = $ns_path.$spath;
chop($nm_path) unless $mpath;
$mpath = $nm_path.$mpath;

$ENV{'MANPATH'} = $mpath;
$ENV{'PATH'} = $spath;
$ENV{'CPU'} = $CPU;
$ENV{'HGHOST'} = $HGHOST;

while( length($ARGV[0]) )
{
	$_ = shift;
	$cmd .= '"'.$_.'" ' if( /\s/ ); 
	$cmd .= $_.' ' if( ! /\s/ );
}

system( "hgcmd $cmd" );

