#!/bin/sh
#--------1---------2---------3---------4---------5---------6---------7---------8
################################################################################
#                                                                              #
#       ETA/DYNAFORM Platform Independent Software Installation                #
#                                                                              #
#       File name:       df_install                                            #
#                                                                              #
#       Revision:        5.8 (14 Jul 2011)                                     #
#                                                                              #
#       Description: This script installs DF58  software on a workstation      #
#                    and retrieves Host ID for licensing.                      #
#                                                                              #
#       Usage:       To execute the script, type "./df_install"                #
#                                                                              #
#       Licensing:   Complete the online license request form at               #
#                    www.ETA.com or contact support@eta.com.                   #
#                                                                              #
################################################################################

#
# Internal input varivables
#
if test -d "ETA_DF58"
then
	PACKAGE="ETA_DF58"
	PRODUCT="DYNAFORM"
	RELEASE="5.8"
	BLDVERS="5.8 14.Jul.11"
	PURPOSE="Workstation" 
#
	TESTFILE="testfile.df.$$"
	VREGISTRY="registry"
	VR_PRODSTR="${PRODUCT}"
	PRODUCTDIR="ETA_DF58"
	PRODUCTFILE="dynaform3d"
	EXECNAME="df58"
	SUPPORTOS="Linux"
	SUPPORTBIT="64"
#
else

echo ""
echo "Untar product file prior to run DF installation!"
echo ""

exit 1
      
#
fi
echo ""
echo "   ========================================================================"
echo ""
echo "        ETA/DYNAFORM Platform Independent Software Installation Script"
echo ""
echo "             This script installs ETA software on a workstation"
echo "                and retrieves Host ID for licensing."
echo ""
echo "             Product: ${PRODUCT} ${RELEASE}"
echo "             Purpose: ${SUPPORTOS} ${SUPPORTBIT}bit installation"
echo ""
echo "             ETA Software, Inc. - www.dynaform.com"
echo ""
echo "   ========================================================================"
echo ""

#
# See if we have to use the lame SYS-V echo command flags.
#
if test "`echo 'blah\c'`" = "blah\c"
then
      ECHO="echo -n"
      ENDR=""
else
      ECHO="echo"
      ENDR="\c"
fi

# Verify if user has correct package

	echo ""
	echo ""
	flag=1
	while test ${flag} -eq 1
	do	
		echo "This package is for ${PRODUCT} ${RELEASE} ${SUPPORTOS} ${SUPPORTBIT}bit installation."
		${ECHO} "Is that correct? (y/n)[y] ${ENDR}"		
		read CHECK
		if test "${CHECK}" = "n" -o "${CHECK}" = "N"
	           then
			echo "ERROR: wrong installation package."
			flag=0
		   exit 1
		elif test "${CHECK}" = "y" -o "${CHECK}" = "Y" -o "${CHECK}" = ""
		   then
			echo "Correct installation package."
			flag=0
		else
			echo "Error input."
			flag=1
		fi
	done	
#
# Check machine Platform
#
# echo ""
# echo ""
# echo "Now trying to get your machine Platform..."
if test ! -z "`ls /bin/uname`"
then
        case `/bin/uname` in
                OSF | OSF1 | OSF4)
                        PLATFORM="OSF"
				PLATLIST="1"
                ;;
                HP-UX)
                        PLATFORM="HP-UX"
				PLATLIST="2"
                ;;
                AIX)
                        PLATFORM="AIX"
				PLATLIST="3"
                ;;
                IRIX | IRIX64)
                        PLATFORM="IRIX"
				PLATLIST="4"
                ;;
                SunOS | Solaris)
                        PLATFORM="SOLARIS"
				PLATLIST="5"
                ;;
                Linux | LINUX)
                        PLATFORM="LINUX"
				PLATLIST="6"
                ;;
                *)
                        PLATFORM=""
				PLATLIST=""
                ;;
        esac
fi

#
#check the platform of user's machine is right
#

SUPPORTOS=`echo "${SUPPORTOS}" | tr '[a-z]' '[A-Z]'`
if test ${SUPPORTOS} != ${PLATFORM}
then
	echo "ERROR: This package is for ${SUPPORTOS}, please check it!"
	exit 1
fi

if test ${SUPPORTOS} = "LINUX"
then
	BIT=`getconf LONG_BIT`
	if test ${BIT} = "32" -a ${SUPPORTBIT} = "64"
	then
		echo "ERROR: This package is for ${SUPPORTBIT}bit platform, please check it!"
		exit 1
	fi
fi

if test ${SUPPORTOS} = "SOLARIS"
then
	BIT=`isainfo -b`
	if test ${BIT} = "32" -a ${SUPPORTBIT} = "64"
	then
		echo "ERROR: This package is support ${SUPPORTBIT}bit platform, please check it!"
		exit 1
	fi
fi

#
# Make sure the package we expect is where we expect it.
#
if test ! -d ${PACKAGE}
then
	echo "ERROR: ${PACKAGE}: no such directory or file"
	exit 1
fi
#
INSTALL_DIRU=`echo ~`
INSTALL_DIR=$INSTALL_DIRU/${PRODUCTDIR}
#
# Routine to check if input path start with "/"
#
FilterPathName ()
{
  newpathname="$1"
  case "$newpathname" in
    ~*)
      if [ -f /bin/csh -a -x /bin/csh ] ; then
        newpathname=`/bin/csh -c "echo $newpathname"`
      fi
      ;;
    ../*|./*)
      newpathname=`pwd`/"${newpathname}"
      ;;
    *)
      ;;
  esac
  eval newpathname=\"${newpathname}\"
  echo "${newpathname}"
}
#
# Inquire user for input path
#
flag=1
while test ${flag} -eq 1
do
	echo "Location for ${PRODUCT} software:"
#	echo "   (1)[${HOME}/${PRODUCTDIR}]"
# Verify user is root
	  echo "   (1)[${INSTALL_DIR}]"
	${ECHO} "Make selection or Input path: [1] ${ENDR}"
	read TARGET
	if test -z "${TARGET}" -o "${TARGET}" = "1"
	then
		TARGET="${INSTALL_DIR}"
	else
  		newpathname="${TARGET}"
  		case "$newpathname" in
    		~*)
      		if [ -f /bin/csh -a -x /bin/csh ] ; then
        		newpathname=`/bin/csh -c "echo $newpathname"`
      		fi
      		;;
    		../*|./*)
      		newpathname=`pwd`/"${newpathname}"
      		;;
    		/*)
      		;;
    		*)
		newpathname=`pwd`/"${newpathname}"
      		;;
  		esac
  		eval newpathname=\"${newpathname}\"
		TARGET="${newpathname}"
	fi
#
	flag=0
	if test ! -d ${TARGET}
	then
		echo ""
		echo "${TARGET}: No such directory."
		${ECHO} "Do you wish to create it? (y/n)[y] ${ENDR}"
		read check
		if test "${check}" = "n" -o "${check}" = "N"
		then
			flag=1
		else
			#
			# The -p flag may cause an error on some HP-UX systems.
			# Removing the -p will work for 'basename TARGET' as long
			# as 'dirname TARGET' exists.
			#
			mkdir -p ${TARGET}
			if test $? -ne 0
			then
				echo "ERROR: Problem creating ${TARGET}"
				exit 2
			fi
		fi
	else
		echo ""
		echo "Existing '${TARGET}' directory found."
		echo ""
		echo "The existing contents may be modified or replaced!!!"
		${ECHO} "Do you really want to install in this directory? (y/n)[y] ${ENDR}"
		read check
		if test "${check}" = "n" -o "${check}" = "N"
		then
			flag=1
		fi
	fi
#
	if test ${flag} -eq 0
	then
		(echo "Write test." 2>/dev/null > ${TARGET}/${TESTFILE}) 2>/dev/null
		if test $? -ne 0
		then
			echo ""
			echo "You do not have write permission in ${TARGET}."
			echo "Change the permissions, or select another directory, and try again."
			echo ""
			flag=1
		else
			/bin/rm -f ${TARGET}/${TESTFILE}
		fi
	fi
done
#
# Begin the installation.
#

LOGFILE="${PRODUCT}-install.log-$$"
date > ${LOGFILE}

#for i in ${PLATLIST}
#do
#   case ${i} in
#   1) PLATINSTALL="OSF" ;;
#   2) PLATINSTALL="HP-UX" ;;
#   3) PLATINSTALL="AIX" ;;
#   4) PLATINSTALL="IRIX" ;;
#   5) PLATINSTALL="SOLARIS" ;;
#   6) PLATINSTALL="LINUX" ;;
#   esac
#
#
# Check the product
#
PLATINSTALL=${PLATFORM}
if test -d ${PACKAGE}
then
#
# Keep a log of this installation for reference.
#
echo "Files installed:" >> ${LOGFILE}
echo "================" >> ${LOGFILE}
echo ""
echo "Installing ${PLATINSTALL}/${PRODUCT} files."
echo "Installing ${PLATINSTALL}/${PRODUCT} files." >> ${LOGFILE}
# Check for existing files with the same names and save them.
if test -d ${TARGET}
then
      /bin/rm -rf ${TARGET}
fi
  echo "copying files..."
  echo "copying files..." >> ${LOGFILE}
  cp -R ${PACKAGE} ${TARGET}
  ls -1a ${PACKAGE} >> ${LOGFILE}
else
      echo ""
      echo "ERROR: no ${PACKAGE} directory or file"
fi

#done

#
echo ""
echo "The ETA ${PRODUCT} file transfer is completed."
echo "==================================================" 
echo "================" >> ${LOGFILE}
echo "The ETA ${PRODUCT} file transfer is completed." >> ${LOGFILE}
echo ""
#remove temp files
    /bin/rm -rf ${PACKAGE}
    echo "Please see file ${LOGFILE} for detail information of installation."
#
# Write executable script to launch application (test if Solaris)
# REM: SunOS request extra enviroment sets
#
	echo ""
	echo ${TARGET}/df58>${EXECNAME}
	chmod 755 ${EXECNAME}
ed - ${TARGET}/df58  <<-END_OF_CAT
/DYNAFORM_PATH=/c
LD_LIBRARY_PATH=${TARGET}:${TARGET}/lib; export LD_LIBRARY_PATH;
DYNAFORM_PATH=${TARGET}; export DYNAFORM_PATH;
.
w
q
END_OF_CAT
rm df58

ed - ${TARGET}/etapost  <<-END_OF_CAT
/ETAPOST_PATH=/c
ETAPOST_PATH=${TARGET}; export ETAPOST_PATH;
.
w
q
END_OF_CAT

USERNAME=`echo $USER`
`chown -fR ${USERNAME} ${TARGET}`

#alias df58 into shell rouce
CALLPATH=${TARGET}/df58
POSTPATH=${TARGET}/etapost
JSPATH=${TARGET}/dynajs_p
ALIASFLOG=1

if test "$SHELL" = "/bin/csh"
then
	RCFILENAME=$HOME/.cshrc
    LINE1="alias df58 \"$CALLPATH\""
    LINE2="alias etapost58 \"$POSTPATH\""
	LINE3="alias etajs58 \"$JSPATH\""
elif test "$SHELL" = "/bin/bash"
then
	RCFILENAME=$HOME/.bashrc
    LINE1="alias df58='$CALLPATH'"
    LINE2="alias etapost58='$POSTPATH'"
	LINE3="alias etajs58='$JSPATH'"
elif test "$SHELL" = "/bin/zsh"
then
	RCFILENAME=$HOME/.zshrc
    LINE1="alias df58='$CALLPATH'"
    LINE2="alias etapost58='$POSTPATH'"
	LINE3="alias etajs58='$JSPATH'"
else
	ALIASFLOG=0
fi
echo "$LINE1" >> $RCFILENAME
echo "$LINE2" >> $RCFILENAME
echo "$LINE3" >> $RCFILENAME

#create a short cut on desktop for Linux don't use now

if test ${SUPPORTOS} = "LINUX11111"
then
	echo ""
	echo ""
	flag=1
	while test ${flag} -eq 1
	do	
		${ECHO} "Do you want to create a short cut on your desktop? (y/n)[y] ${ENDR}"		
		read CHECK
		if test "${CHECK}" = "n" -o "${CHECK}" = "N"
	        then
			flag=0
		elif test "${CHECK}" = "y" -o "${CHECK}" = "Y" -o "${CHECK}" = ""
		    then
		    USERNAME=`echo $USER`
			CPATH=`/bin/sh -c "echo ~$USERNAME"`
			DESKPATH=${CPATH}/Desktop
			if [ -d "$DESKPATH" ]
			then
				ICONPATH=$DESKPATH/df58.desktop
				if [ -f "${ICONPATH}" ]
				then
					`rm ${ICONPATH}`
				fi
				echo "[Desktop Entry]" >> $ICONPATH
				echo "Encoding=UTF-8" >> $ICONPATH
				echo "Version=5.7.1" >> $ICONPATH
				echo "Type=Application" >> $ICONPATH
				echo "Terminal=false" >> $ICONPATH 
				echo "Icon[en_US]=${TARGET}/dficon.png" >> $ICONPATH
				echo "Name[en_US]=df571" >> $ICONPATH
				echo "Exec=${TARGET}/df571" >> $ICONPATH
				echo "Name=df571" >> $ICONPATH
				echo "Icon=${TARGET}/dficon.png" >> $ICONPATH				
				`chmod 775 $ICONPATH`
				`chown $USERNAME $ICONPATH`
			fi
			flag=0
		else
			echo "Error input."
			flag=1
		fi
	done	

fi

# Added for Lstc Server Manager
	${ECHO} "Do you want to install the Lsdyna ServerManager? (y/n)[y] ${ENDR}"
	read check
	if test "${check}" = "n" -o "${check}" = "N"
	then
		`rm -rf ${TARGET}/LsdynaServerManager` 
	else
		echo ""
	fi



	echo "${PRODUCT} installation completed."
if test $ALIASFLOG = 1 
then
	echo "Please start a new terminal to type '${EXECNAME}' to run ${PRODUCT} ${RELEASE} after you get the license."
fi
	echo "===================================================================" 
#
# Write the eta.log file
#

#if [ -h "${TARGET}/eta_log" ]
#then
	dmy=`date +%b%d,%Y`
	username=`echo $USER`
	upath=`/bin/sh -c "echo ~$NAME"`
	dpath=$upath/Desktop
	if [ -d "${dpath}" ]
	then
		`${TARGET}/eta_log $PRODUCT $RELEASE $dmy $dpath/eta.log`
	fi 
#fi

#
# Try to get eta.log file for licensing
#
echo ""
echo ""
	echo "Please complete the online license request form at"
	echo "www.eta.com  and send the eta.log file to support@eta.com"
	echo "for a valid license."
	echo ""
#		./${EXECNAME} 
exit 0
