#! /bin/csh -f

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




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 ) setenv IA_CWD $my_cwd

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 plat = ""
set plat_temp = `uname -a`

if ( `echo $plat_temp | grep -ic OSF` ) then
    #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 ( `echo $plat_temp| grep -ic aix ` ) then
  set plat = 'ibm'
else if ( `echo $plat_temp| grep -ic OSF`) then
  set  plat = 'u_alpha'
else if ( `echo $plat_temp| grep -ic irix `) then
  set  plat = 'origin2000'
else if ( `echo $plat_temp| grep -i linux | grep -ic ia64`) then
  set  plat = 'linux64'	
else if ( `echo $plat_temp| grep -ic linux`) then
  set  plat = 'linux32'
else if ( `echo $plat_temp| grep -i hp-ux | grep -ic ia64`) then
  set  plat = 'hpi'
else if ( `echo $plat_temp| grep -ic hp-ux`) then
  set  plat = 'hp64'
else if ( `echo $plat_temp| grep -ic sunos`) then
  set  plat = 'sun64'
endif


if ( $plat == "" ) then
  #contains ALL 6.4 UNIX platforms
  set plats = ( )
  set plats = ( $plats IBM_64-bit_release_for_AIX_5.1:ibm )
  set plats = ( $plats Compaq_64-bit_release_for_Tru64_5.1:u_alpha )
  set plats = ( $plats SGI_64-bit_release_for_IRIX_6.5:origin2000 )
  set plats = ( $plats Intel_Pentium_32-bit_for_Linux:linux32 )
  set plats = ( $plats Intel_Itanium_64-bit_for_Linux:linux64 )
  set plats = ( $plats HP_64-bit_release_for_HP-UX_11:hp64 )
  set plats = ( $plats Intel_Itanium_64-bit_for_HP-UX_11:hpi )
  set plats = ( $plats Sun_UltraSparc_release_with_Solaris_2.8:sun64 )

  #create the choices for the user for this CD
  @ numplats = 0
  @ platnum = 0   # number of platforms on this CD
  set choices = ( )
  foreach entry ( $plats )
    set dir = `echo $entry | awk -F: '{print $2}'`
    if  ( -d $setup_dir/JVM/$dir ) then
      @ platnum++
      set choices = ( $choices $entry )
    endif
  end
  #obtain the user's selection

  set choice = "-1"
  
 while ( $choice < 0 || $choice > $platnum )
   echo ""
   echo "********************"
   echo "Please choose the system that you wish to install:"
   echo ""
   @ num = 0
   foreach entry ( $choices )
     @ num++
     set desc = `echo $entry | awk -F: '{print $1}' | sed 's/_/ /g'`
     set dir = `echo $entry | awk -F: '{print $2}'`
     set udir = `echo $dir | tr '[a-z]' '[A-Z]'`
     echo "   ${num}. $desc"
   end
   echo "   0. exit"
   echo ""
   echo -n "Enter the number: "
   set choice=$<
   if ( $choice == "0" ) exit
   if ( "$choice" != "`echo $choice | tr '[A-Z][a-z]' '[\ *]'`" ) set choice = "-1"
 end
 set plat = `echo $choices[$choice] | awk -F: '{print $2}'`
endif

setenv ABAQUS_PLAT "$plat"

if ($plat == "hp64" ) then
  if ( ! -e $setup_dir/UNIX/install.bin ) then
    echo "***ERROR: $setup_dir/UNIX/install.bin not found."
    echo "          Please ensure that you have mounted the installation CD"
    echo "          with the correct mount command, which is:"
    echo "                   mount -rt cdfs device /cdrom "
    echo "          where device is the CD-ROM device on your system.  Also,"
    echo "          make sure that the cdcase option has not been included"
    echo "          in your mount command in the sytem file /etc/fstab."
    echo ""
    exit 1
  endif
endif

if ( -d $setup_dir/JVM/$plat/jre/bin ) then
    set untar_jre = '0'
    set path = ( $setup_dir/JVM/$plat/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


#Get TMPIR if one is not already set, only hpi
if ( $untar_jre ) then
    if ( ! $?TMPDIR ) then
        echo ""
        echo "A scratch directory is required to extract the java runtime "
        echo "environment that is required to execute the installer."
        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 $tmpdir 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 ( $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/UNIX/install.bin ) then
  echo "***ERROR:  $setup_dir/UNIX/install.bin not found"
  exit 1
endif


#Compaq JVM is too verbose
if ( $plat == "u_alpha" ) then
  set status = `$setup_dir/UNIX/install.bin >& /dev/null`
else
  set status = `$setup_dir/UNIX/install.bin`
endif
