#!/bin/csh -f 
#
#	%W% %E%
#
# setupgig:  SGI VERSION!
#   this script should be called after the GIG tar files
#   have been untarred, and after the gig account has been created.
#   It creates some dirs and adds some dotfiles
#	
# usage:
#   setupgig username dirname
#   (this is run as root by ui_go)
#
# 960202 Added the moving of the nodelock file from /var/netls to /etc, BSt
#


# make sure path contains necessary directories
set path = (/usr/bsd /usr/ucb /bin /usr/bin /etc /usr/etc $path)
rehash 

set cd_root = $cwd
set machine = 'sgi'

set usr_name   = $1
set usr_dir    = $2

set gigvrbos   = 1

#
#list of dirs in a project and list of standard projects
#
set libdirs=(lib/intdump lib/intenv lib/intmap lib/intlib lib/intpixmap)

set rootfiles=(utils/setgamma)
foreach i ( $rootfiles ) 
  if ( -e ~$usr_dir/$i ) then
    chown $usr_name $usr_dir/$i
  endif
end

#set .rhosts for channels
if (-d $usr_dir/utils) then
  su $usr_name -c "echo `hostname` $usr_name >! $usr_dir/utils/rhosts"
  if ($gigvrbos) echo "created $usr_dir/utils/rhosts"
endif

if ($gigvrbos) echo "installing dotfiles:"

# move dotfiles to home dir after saving old files
set dotfiles = (login cshrc rhosts Xdefaults)
if ( $machine == sun ) then
  set dotfiles = ($dotfiles openwin-init)
endif
if ( $machine == sgi ) then
  set dotfiles = ($dotfiles sgisession Sgiresources)
endif
foreach f($dotfiles)
  if (-f $usr_dir/utils/$f) then
    if (-f $usr_dir/.$f) then
      set newf = ${f}.$$
      while (-f $usr_dir/$newf)    # just in case ${f}.$$ exists
        set newf = ${newf}.$$
      end
      su $usr_name -c "/bin/mv $usr_dir/.$f $usr_dir/$newf"
    endif
    su $usr_name -c "/bin/mv $usr_dir/utils/$f $usr_dir/.$f"
    if ($gigvrbos) echo "$f installed"
  endif
end
unset dotfiles

# check if the library project exists. If not create it and link
# it to the old intdump etc.
#if (! -e $usr_dir/projects/library_prj) then
#  su $usr_name -c "mkdir -p $usr_dir/projects/library_prj"
#  foreach l ($libdirs)
#    su $usr_name -c "ln -s $usr_dir/$l $usr_dir/projects/library_prj"
#  end
#endif

# install gig logo on sgi machine
chkconfig -f visuallogin on
chkconfig -f noiconlogin off

# change owner of setgamma and make it set-owner-executable
foreach i ( $rootfiles ) 
  if ( -f $usr_dir/$i ) then
    #chown root $usr_dir/utils/setgamma
    chown root $usr_dir/$i
    chmod 4555 $usr_dir/$i
  endif
end

#
# Move the nodelock file to where we want it
#
if (-f /usr/netls/nodelock) then
  if (! -f /etc/nodelock ) then
    echo copying nodelock file from /usr/netls to /etc
    cp /usr/netls/nodelock /etc/nodelock
  endif
endif
if (-f /var/netls/nodelock) then
  if (! -f /etc/nodelock ) then
    echo copying nodelock file from /var/netls to /etc
    cp /var/netls/nodelock /etc/nodelock
  endif
endif

set mainmem = `hinv | grep Main\ m | awk '{ print $4 }'`
echo detected $mainmem Mb of main memory.
if ($mainmem == 32) then
  echo Using small confile
  cp $usr_dir/lib/intlib/confile.sma $usr_dir/lib/intlib/confile
endif

su $usr_name -c "$usr_dir/utils/getid"

exit 0

problem:
echo ""
echo -n "installation problem: "
switch ($probl_no)
  case "1"
    echo "you should be logged in as 'root' to run $0"
    breaksw
  case "2"
    echo "$0 running on unknown machine $MACHINE, bye"
    breaksw
  case "3"
    echo "$0 needs help ($machine)"
    breaksw
  case "4"
    echo "cannot find passwd file"
    breaksw
  case "5"
    echo "cannot find group file"
    breaksw
  case "6"
    echo "cannot obtain home dir for $usr_name from $pwd_file"
    breaksw
  case "7"
    echo "cannot obtain group name for $grp_number from $grp_file"
    breaksw
  case "8"
    echo "cannot obtain gid for $usr_name from $pwd_file"
    breaksw
  case "9"
    echo "cannot obtain uid for $usr_name from $pwd_file"
    breaksw
  case "10"
    echo "cannot obtain gid for $grp_name from $grp_file"
    breaksw
  case "11"
    echo "existing $usr_dir is not a directory"
    breaksw
  case "12"
    echo "cannot not create directory $usr_dir"
    breaksw
  case "13"
    echo "required space not properly initialised"
    breaksw
  case "14"
    echo "there is not enough disk space to install GIG at $usr_dir"
    switch ($machine)
      case "sgi":
        echo " ($space_reqd blocks required)"
        breaksw
      case "sun":
      case "dec":
        echo " ($space_reqd Kb required)"
        breaksw
    endsw
    breaksw
  case "15"
    echo "cannot find group number $grp_number in $grp_file"
    breaksw
  case "16"
    echo "cannot find group name $grp_name in $grp_file"
    breaksw
  case "17"
    echo "cannot find services file"
    breaksw
  default
    breaksw
endsw
echo ""
echo "$0 aborted"

# undo as much as possible
# remove newly created directory
if ($?newdir) then
  /bin/rm -rf $usr_dir
endif
# restore passwd and group files
if ($?pwdfile_chgd) then
  /bin/rm -f $pwd_file
  /bin/mv $old_pwd_file $pwd_file
endif
if ($?grpfile_chgd) then
  /bin/rm -f $grp_file
  /bin/mv $old_grp_file $grp_file
endif

exit $probl_no

#-----------------------------------------------------------------------
