#!/bin/csh -f

set scrdir = ""
set jre_scrdir = ""
set plat = ""
set plat_temp = `uname -a`
set jre_tar = 'jre.tar'

if ( `echo $plat_temp | grep -ic OSF` ) then
    set plat = 'u_alpha'
    #workaround v64_1882
    @ ss = `limit | grep stacksize | awk '{print $2}'`
    if ( $ss >= 4000000 ) then
      echo "Lowering stacksize to 8192 for ABAQUS installation..."
      limit stacksize 8192
    endif    
endif

if ( ! $?DISPLAY ) then
  echo ""
  echo "In order to run the installation GUIs, the environment variable"
  echo "DISPLAY must be set. Please set DISPLAY as desired and run this"
  echo "setup script again."
  echo ""
  exit 1
endif


set system_jre = "false"
set nochecks = "false"

if ( $#argv >= 1 ) then
   if ( "$argv" =~ *-j* && "$argv" =~ *142* ) then
       set jre_tar = 'jre142.tar'
   else if ( "$argv" =~ *-j* ) then
       set system_jre = "true"
   endif
   if ( "$argv" =~ *-n* ) then
       setenv NOCHECKS true
       set nochecks = "true"
   endif
endif


set cmd = $0
set setup_dir = $cmd:h
set my_cwd = $cwd
cd $setup_dir
set setup_dir = $cwd
cd $my_cwd

if ($setup_dir =~ *\#* ) then
    echo "***ERROR: The CDROM has been mounted to an invalid directory name. The "
    echo "          character # cannot exist in the path, as it is known to "
    echo "          cause problems with the installation GUI.  Please mount the "
    echo "          CD to a different path and rerun this installation procedure."
    echo "          This issue has been documented as bug number v64_1976."
    exit 1
endif


set untar_jre = '0'

if ( "$system_jre" != "true" ) then
    if ( -d $setup_dir/jre/bin ) then
        set path = ( $setup_dir/jre/bin $path )
    else if ( -e $setup_dir/../$jre_tar ) then
        set untar_jre = '1'
    else
        echo "***ERROR:  The java runtime environment was not found on this CD."
        echo "           Please contact ABAQUS support."
        exit 1
    endif
endif


if ( "$setup_dir" == "$my_cwd" ) then
  echo "***ERROR. The installation should not be executed from "
  echo "          the directory that contains the setup files."
  echo "          Please change your current working directory and"
  echo "          rerun the installation."
  exit 1
endif

#cwd is default installation directory
if ( `echo "$my_cwd" | grep -c ^\/` && -d "$my_cwd" ) then
    setenv IA_CWD $my_cwd
endif

if ( $untar_jre  || ! $?NOCHECKS ) then
    if ( ! $?TMPDIR ) then
        echo ""
        echo "A scratch directory is required for the execution of the" 
        echo "ABAQUS installation procedure."
        echo ""
        echo 'TMPDIR is not defined on this system.  Provide the full path'
        echo "to the scratch directory."
        echo ""
        echo -n "Scratch directory is: "
        set tmpdir=$<
    else
        set tmpdir = $TMPDIR   
    endif
        # ensure a full path is entered
    while ( ! `echo $tmpdir | grep -c ^\/` )
        echo ""
        echo 'Please enter a FULL pathname starting with /.'
        echo -n "Scratch directory is: "
        set tmpdir = $<
    end
    if (! -w $tmpdir ) then 
        echo ""
        echo "***ERROR: The directory $scrdir does not have proper"
        echo "          write permissions. Check this directory and"
        echo "          restart this procedure."
        exit 1
    endif
endif

set username = ""
if ( $?USER ) then
    set username = $USER
else if ( $?LOGNAME ) then
    set username = $LOGNAME
endif 



if ( (! $?NOCHECKS ) && -e $setup_dir/sysinfo.tar ) then
    #run sysinfo in temporary location

    if ( -e $tmpdir/TEMP_ABAQUS_sysinfo_$username ) then
        echo "...Removing old ABAQUS sysinfo scratch directory"
        rm -rf $tmpdir/TEMP_ABAQUS_sysinfo_$username
    endif
    mkdir $tmpdir/TEMP_ABAQUS_sysinfo_$username
    set scrdir = $tmpdir/TEMP_ABAQUS_sysinfo_$username
    chmod 755 $scrdir

    cp $setup_dir/sysinfo.tar $scrdir
    cd $scrdir
    chmod 755 sysinfo.tar
    tar -xf sysinfo.tar
    if (! -d sysinfo ) then
        echo "***ERROR: sysinfo directory not found."
        rm sysinfo.tar
        exit 1
    endif
    cd sysinfo
    set syspass = "0"
    echo ""
    echo "Checking system requirements for installation.  This will "
    echo "take just a moment..."
    echo ""

    ./sysinfo.csh -product Installation -notAllProducts >& ./sysinfo.out
    set sysstatus = $status
    set sysfail = `cat sysinfo.out | grep -ic fail`
    #sysstatus:  0 is good, 2 is warning

    if ( $sysfail == "0" &&  $sysstatus == "0" ) then
        echo ""
        echo "    All installation requirements are met.  Continuing with"
        echo "    ABAQUS installation procedure..."
        echo ""
    else if ( $sysfail == "0" &&  $sysstatus == "2" ) then
        echo "    All installation requirements are met, although at least"
        echo "    one tested generated a warning.  Please review the output"
        echo "    below before continuing with the ABAQUS installation "
        echo "    procedure..."
        echo ""
        echo ""
        echo "***************************************"
        cat ./sysinfo.out
        echo "***************************************"
        echo ""
        echo ""
        echo ""
        echo "Hit <Enter> to continue with the ABAQUS installation."
        set ans = $<
        echo ""
    else
        echo ""
        echo "***ERROR:  One or more system tests have failed the minimum installation"
        echo "           requirements. The installation procedure will not continue."
        echo -n "           Hit <Enter> to view the output from this system check. "
        set ans = $<
        echo ""
        cat ./sysinfo.out 
        goto CLEANUP
    endif
endif # NOCHECKS



if ( $untar_jre ) then
    if ( -e $tmpdir/TEMP_ABAQUS_jre_$username ) then
        echo "Removing old ABAQUS jre scratch directory... "
        rm -rf $tmpdir/TEMP_ABAQUS_jre_$username
    endif
    echo "Extracting temporary java runtime environment to $tmpdir... " 
    mkdir $tmpdir/TEMP_ABAQUS_jre_$username
    set jre_scrdir = $tmpdir/TEMP_ABAQUS_jre_$username
    chmod 755 $jre_scrdir

    cp $setup_dir/../$jre_tar $jre_scrdir
    cd $jre_scrdir
    chmod 755 $jre_tar
    tar -xf $jre_tar
    if (! -d jre ) then
        echo "***ERROR: jre directory not found."
        rm $jre_tar
        exit 1
    endif
    echo "Executing the installation GUI..."
    set path = ($jre_scrdir/jre/bin $path)
endif



if ( ! -e $setup_dir/intro/UNIX/install.bin ) then
    if ( -e $setup_dir/intro/unix/install.bin ) then
        if ( `echo $plat_temp | grep -ic hp-ux` ) then
           echo "***ERROR: The file $setup_dir/intro/UNIX/install.bin is not found."
           echo "          Please ensure that you have mounted the installation CD"
           echo "          with the correct mount command, which is:"
           echo "                   mount -rF cdfs device /cdrom "
           echo "          where device is the CD-ROM device on your system.  Also,"
           echo "          make sure that you have not included the cdcase option "
           echo "          to your mount command in your /etc/fstab file."
           echo ""
        else
           echo "***ERROR: The file $setup_dir/intro/UNIX/install.bin is not found but"
           echo "          the file $setup_dir/intro/unix/install.bin is.  This indicates"
           echo "          that you have mounted the CD in such a way as to make all "
           echo "          characters lower case. You can find the correct mount commands"
           echo "          for all systems at"
           echo ""
           echo "          http://www.abaqus.com/support/v64/v64_sysRqmts.html"
        endif
    else
        echo "***ERROR: The file $setup_dir/intro/UNIX/install.bin does not exist."
        echo "          Please make sure that you have mounted your CD correctly." 
        echo "          You can find the correct mount commands for all systems at"
        echo ""
        echo "          http://www.abaqus.com/support/v64/v64_sysRqmts.html"
        echo ""
    endif
    goto CLEANUP
endif
    


if ( $plat == "u_alpha" ) then
    $setup_dir/intro/UNIX/install.bin >& /dev/null
else
    $setup_dir/intro/UNIX/install.bin
endif


CLEANUP:
# cleanup scratch directory used for installation
cd $setup_dir


if ( $scrdir != "" ) then
    if ( -e $scrdir) rm -rf $scrdir
endif

if ( $jre_scrdir != "" ) then
    if ( -e $jre_scrdir) then
        echo "Removing temporary java runtime environment..."
        rm -rf $jre_scrdir
    endif
endif


if ( $scrdir != "" || $jre_scrdir != "" ) then
  echo ""
  echo ""
  echo "...finished."
endif



