#!/bin/csh -f
#***********************************************************************\
#									*
# %W%	%E%								*
#									*
# Copyright (C) 1993, ElectroGIG Nederland BV, The Netherlands		*
# 									*
# ElectroGIG Nederland							*
# Amstel 222								*
# 1017   AJ Amsterdam							*
# tel:   020-6233495							*
# fax:   020-6226801							*
# email: cm@gig.nl							*
#									*
#***********************************************************************/

# check if user is root
set user=`whoami`

if ( $user == root ) then
  echo do not run this script as root
  exit 1
endif

setenv TMPDIR /usr/tmp/gig$$
onintr cleanup

switch(`uname -m`)
   case IP[0-9]:
   case IP[0-9][0-9]:
   case IP[0-9][0-9][0-9]:
      set m = iris
      breaksw
   case MIPS:
   case RISC:
      set m = dec
      breaksw
   case sun:
   case sun4:
   case sun4[a-z0-9]:
      set m = sun
      breaksw
   default:
      echo unknown machine
      exit 1
endsw

if ( ! $?DISPLAY ) then
    setenv DISPLAY `hostname`':0.0'
endif

if ( $?LG && $?GM && $?GB ) then
   set bin=$LG/$GM/$GB
else if ($?GIG_D_BIN) then
   set bin=$GIG_D_BIN
else
   set bin=.
endif

set testfiles = ""
set demofiles = ""
/bin/rm -f $GIG_D_BIN/Demo_gt
/bin/rm -f $GIG_D_BIN/Nurbs_gt
/bin/rm -f $GIG_D_BIN/ProjectManager_gt
/bin/rm -f $GIG_D_BIN/Converter_gt
/bin/rm -f $GIG_D_BIN/'Flow Motion_gt'
/bin/rm -f $GIG_D_PROJECTS/*_prj/intenv/tmpscrnlay.txt >& /dev/null
set converters = ""

set gig = $bin/gig
if ($m == iris) then
  hinv -t cpu | fgrep -s R4 
  if( ! $status && -e $bin/gig.mips2 ) then
    set gig = $bin/gig.mips2
  else
    set gig = $bin/gig
  endif
endif

if ($m == sun) then
  xdpyinfo -display :0.0 | fgrep -s X3D-PEX 
  if( $status && -e $bin/gigX ) then
    set gig = $bin/gigX
  else
    #use pexlib version
    set gig = $bin/gig
  endif
endif

set binargs=

while ($#argv > 0)
  set tmp=$1
  shift argv
  if ("--" == "$tmp") then
    break
  else if ("-t" == "$tmp") then
    set testfiles = "$testfiles $1"
    shift
  else if ("-i" == "$tmp" ) then
    # intdebug option for GIG/3D-GO
    set binargs = ($binargs -d)
  else if ("-a" == "$tmp" ) then
    set binargs = ($binargs $1)
    shift
  else if ("-d" == "$tmp" ) then
    set demofiles = "gigdemoopen gigdemo "
    touch $GIG_D_BIN/Demo_gt
  else
    set gig = $bin/gig$tmp
  endif
end
if ($#argv > 0) then
  set binargs = ($binargs $argv)
endif

foreach f (`/bin/ls $GIG_D_INTERCOL/*_conv.int $GIG_D_INTERCOL/*_out.int`)
  set sf = $f:t
  set sf = $sf:r
  if( -e $GIG_D_TOOLUTILS/$sf) then
    set converters = "$converters $sf"
  endif
end

if ( -e $GIG_D_TOOLUTILS/startProjectManager && -e $GIG_D_TOOLUTILS/tkui) then
  touch $GIG_D_BIN/ProjectManager_gt
endif

if ( -e $GIG_D_TOOLUTILS/startConverter && -e $GIG_D_TOOLUTILS/tkui) then
  touch $GIG_D_BIN/Converter_gt
endif

#initialise nurbs-module
if ( -e $GIG_D_INTERCOL/nurbs.int && -e $GIG_D_BIN/Nurbs_modeler) then
  set testfiles = "nurbs $testfiles"
  $GIG_D_BIN/Nurbs_modeler check_license
  if ( ! $status ) then
    touch $GIG_D_BIN/Nurbs_gt
  endif
endif

#initialise flowmotion-module
if ( -e $GIG_D_INTERCOL/fminit.int ) then
  touch $GIG_D_BIN/'Flow Motion_gt'
endif

#initialise mapfactory
if ( -e $GIG_D_INTERCOL/mapfinit.int ) then
  set testfiles = "$testfiles mapfinit"
endif

if ( !(-f $gig)) then 
   echo file $gig does not exist 
   exit
endif

if ( !(-x $gig) ) then 
   echo file $gig is not executable 
   exit
endif

if ($?GM) then
 if ($GM != sgi_X && $GM != dec_X && $GM != sunhc ) then
  if ($m != $GM) then
    echo '\$GM ($GM) is not equal to current machine type ($m) !'
    exit 1
  endif
 endif
endif


# create temp directory for evaluation and remove when done

mkdir $TMPDIR


$gig	$binargs gigfiles \
	$demofiles $converters gigx $testfiles gigmain


# flush temp directory
cleanup:
    /bin/rm -fr $TMPDIR
