#!/bin/csh
#################################################
#                                               #
#  MAX+PLUS II Advanced Synthesis 1.01          #
#  Workstation Install Script                   #
#                                               #
#  Copyright (c) Altera Corportation 2003       #
#  All Rights Reserved                          #
#                                               #
#################################################

unalias *

if ( $?QINST_VERBOSE ) then
	set expert_mode = 1
else
	set expert_mode = 0
endif

if ( $?QINST_DEBUG ) then
	set debug = 1
else
	set debug = 0
endif

if ( $?QINST_AUTO ) then
	set auto_install = 1
else
	set auto_install = 0
endif

if (! $?TMP) then
	setenv TMP /tmp
endif


start_install:

set default_mp2_install

if ("$1" != "") then
	set default_mp2_install = $1
else
	if ( $?MAXPLUS2_DIR ) then
		set default_mp2_install = $MAXPLUS2_DIR
	endif
endif 

if ("$2" != "") then
	set default_install = $2
endif


set product_version = "1.01"
set curr_dir=`pwd`

# First determine cd directory

set script_path = `dirname $0`


if ("${script_path}" == "") then
	# in case the dirname doesn't work, use "which" (slower)
	set script_path = `which $0 |& tail -1`
endif

if ("${script_path}" == "." || "${script_path}" == "") then
	set script_path = `pwd`
endif

if ($script_path !~ /*) then
	set script_path = `pwd`/$script_path
endif

# Next check to see what system this is
set os_type=`uname -s`
set os_version=`uname -r`

if ($os_type == "SunOS") then
	if ($os_version =~ 5.*) then
		# This is solaris 2.x
		set host_platform="solaris"
		set df_command = /usr/ucb/df
		set whoami_command = /usr/ucb/whoami
		@ df_offset = 2
	else if ($os_version =~ 4.1.*) then
		echo "SunOS 4.x is not supported by this script"
		exit 1
	else
		# Unknown SunOS
        echo "Cannot determine whether operating system is compatibile"
        echo "with this installation script"
		exit 1
	endif
else if ($os_type == "HP-UX") then
	# This is an HP
	set host_platform="hp"
	set df_command = /usr/bin/bdf
	set whoami_command = /usr/bin/whoami
	@ df_offset = 2
else if ($os_type == "AIX") then
	# This is an RS/6000
	set host_platform="rs6000"
	set df_command = /usr/bin/df
	@ df_offset = 4
else if ($os_type == "Linux") then
   	# This is a Linux box
   	set host_platform="linux"
   	set whoami_command = /usr/bin/whoami
    set df_command = /bin/df
	bindkey "^[[3~" delete-char
	set KBS=`tput kbs`
	if (${%KBS} == 1) then 
		stty erase $KBS
	endif
    @ df_offset = 4
else
	# Unknown
	echo "Cannot determine whether operating system is compatibile"
	echo "with this installation script"
	exit 1
endif

set default_mount_dir=$script_path

clear 
if ( $auto_install ) echo "Running automated install"

set mount_dir=$default_mount_dir

goto check_mount_directory

get_mount_directory:

echo    ""
echo    "Type the full pathname of the directory where this MAX+PLUS II Advanced Synthesis"
echo -n "CD-ROM is mounted (default: $default_mount_dir): "
set mount_dir=$<

if ($mount_dir == "") then
	set mount_dir=$default_mount_dir
endif

if (! -d $mount_dir) then
	echo ""
	echo "Sorry, '$mount_dir' is not a valid directory name"
	goto get_mount_directory
endif

check_mount_directory:

set disk_type = ""
set ext="gz"
if ( -f $mount_dir/solaris.$ext ) then
	set disk_type = "solaris"
else if ( -f $mount_dir/hp.$ext ) then
	set disk_type = "hp"
else if ( -f $mount_dir/hp11.$ext ) then
	set disk_type = "hp11"
else if ( -f $mount_dir/linux.$ext ) then
	set disk_type = "linux"
else if ( -f $mount_dir/common.$ext ) then
	set disk_type = "common"
else
	echo "$mount_dir is not a valid MAX+PLUS II Advanced Synthesis cdrom image."
	goto get_mount_directory
endif

# always use gzip, look for platform specific gzip provided on CD
set uncompress_cmd="${mount_dir}/gzip/${host_platform}/gzip -c -d"
set default_mount_dir = $mount_dir

start:

echo    ""
echo    "MAX+PLUS II Advanced Synthesis $product_version Installation Script for UNIX Workstations"
echo    "Copyright (c) Altera Corporation 2003"
echo    ""
echo    "Type Ctrl+C <Return> to quit this installation script at any time." 
echo    ""
echo -n "Press <Return> to continue..."
if ( ! $auto_install ) then
	set foo=$<
else
	echo ""
endif



get_mp2_directory:

set mp2_dir="";
echo    ""
echo    "Installation of the MAX+PLUS II Advanced Synthesis software requires an"
echo    "existing installation of the MAX+PLUS II software."
echo    ""
echo    "Type the full pathname of the directory of the existing MAX+PLUS II software"
echo	"installation."
echo    ""
echo -n "(default: $default_mp2_install): "

if (! $auto_install) then
	set mp2_install_dir=$<
else
	set mp2_install_dir = $default_mp2_install
endif

if ($mp2_install_dir == "") then
	set mp2_install_dir = $default_mp2_install
endif

if ((-d $mp2_install_dir) && (-d $mp2_install_dir/mw) && (-f $mp2_install_dir/bin/maxplus2)) then
	if (! $?default_install) then
		set default_install = "${mp2_install_dir}/max2syn"
	endif
else
	echo ""

	if ((! -d $mp2_install_dir) ) then
		echo "${mp2_install_dir} does not exist or is not a directory."
	else
		if (! -d $mp2_install_dir/mw) then
			echo "${mp2_install_dir} does not contain a valid MainWin directory."
		else
			echo "${mp2_install_dir} does not contain a valid MAX+PLUS II installation."
		endif
	endif
	echo "You must provide a path to an existing MAX+PLUS II installation."
	if ($auto_install) then
		echo "automated install has failed."
		exit 1
	endif
	goto get_mp2_directory
endif	



get_install_directory:

set install_dir=""
echo    ""
echo    "Type the full pathname of the directory where you want to"
echo	"install the MAX+PLUS II Advanced Synthesis $product_version software."
echo    ""
echo -n "(default: $default_install): "
if (! $auto_install) then
	set install_dir=$<
endif
if ($install_dir == "") then
	set install_dir=$default_install
endif
if (-d $install_dir) then
	if ((! -w $install_dir) || (! -x $install_dir)) then
		echo ""
		echo "Sorry, you do not have access to the '$install_dir' directory"
		goto get_install_directory
	endif
	if ( -f $install_dir/bin/max2syn ) then
		if ( $disk_type != "common") then
			set answer=""
			while ($answer !~ [Yy]* && $answer !~ [Nn]*)
				echo ""
				echo "An installation of MAX+PLUS II Advanced Synthesis already exists in '$install_dir'"
				echo -n "Do you want to remove it before installing MAX+PLUS II Advanced Synthesis $product_version? (y/n): "
				if ( ! $auto_install ) then
					set answer=$<
				else
					set answer="n"
					echo ""
				endif
			end
			if ($answer =~ [Yy]*) then
				echo ""
				echo "Removing all files in '$install_dir'"
				rm -rf $install_dir/*
			endif
		endif
   else
		set answer=""
		while ($answer !~ [Yy]* && $answer !~ [Nn]*)
		echo    ""
		echo    "The $install_dir directory already exists."
		echo    "OK to delete all existing files in $install_dir before"
		echo -n "installing MAX+PLUS II Advanced Synthesis $product_version? (y/n): "
		if ( ! $auto_install ) then
			set answer=$<
		else
			set answer="n"
			echo ""
		endif
		end
		if ($answer =~ [Yy]*) then
			echo ""
			echo "Removing all files in '$install_dir'"
			rm -rf $install_dir/* >&/dev/null
		endif
   endif
else
	if (! { mkdir $install_dir } ) then
		echo ""
		echo "Cannot create the '$install_dir' directory"
		goto get_install_directory
	endif
endif	

set default_install="$install_dir"

echo ""
echo "------------------------------------------------------------"
echo ""
echo "Calculating disk space requirements..."

@ common_req_space = `grep common $mount_dir/space.txt | awk '{printf $2}'`
@ required_space = `grep "$disk_type " $mount_dir/space.txt | awk '{printf $2}'`
if ($disk_type !=  "common") then
	@ required_space = `expr $required_space + $common_req_space`
endif
if (($required_space != "") && ($required_space != 0)) then
	echo ""
	echo "The requested installation requires $required_space kbytes."
	set df_result = `$df_command $install_dir | tail +2`
	set disk_tmp = `echo $df_result | awk '{n = split($0,a); printf a[n-offset]}' offset=$df_offset`
	if ("$disk_tmp" == "" || $disk_tmp =~ *[A-Za-df-z]*) then
		echo ""
		echo "------------------------------------------------------------"
		echo ""
		echo "Unable to determine disk space available in $install_dir."
		echo "Please verify that you have at least $required_space kbytes"
		echo "available in $install_dir, otherwise this installation"
		echo "will fail."
		echo ""
		set answer=""
		while ($answer !~ [YyNn]*)
			echo    "Is there at least $required_space kbytes available in"
			echo -n "$install_dir (type n to stop this installation)? (y/n): "
			set answer=$<
		end
		if ($answer =~ [Nn]*) then
			set answer=""
			while ($answer !~ [YyNn]*)
				echo -n "Would you like to restart the installation? (y/n): "
				set answer=$<
			end
			if ($answer =~ [Yy]*) then
				goto get_mount_directory
			else
				echo ""
				echo "Exiting MAX+PLUS II Advanced Synthesis UNIX Workstation Installation...."
				echo ""
				exit 1
			endif
		else
			@ diskspace = $required_space
		endif
	else
		@ diskspace = $disk_tmp
	endif
	if ($required_space > $diskspace) then
		echo ""
		echo "------------------------------------------------------------"
		echo ""
		echo "You do not have enough disk space to install the requested items."
		echo "There are only $diskspace kbytes in $install_dir."
		echo ""
		
		set answer=""
		while ($answer !~ [YyNn]*)
			echo -n "Would you like to restart the installation? (y/n): "
			set answer=$<
		end

		if ($answer =~ [Yy]*) then
			goto get_mount_directory
		else
			echo ""
			echo "Exiting MAX+PLUS II Advanced Synthesis UNIX Workstation Installation...."
			echo ""
			exit 1
		endif
	endif
	echo ""
	echo "------------------------------------------------------------"
else
	echo ""
	echo "------------------------------------------------------------"
	echo ""
	echo "Unable to determine the disk space necessary to install MAX+PLUS II Advanced Synthesis."
	echo "Please verify that you have sufficient disk space in "
	echo "$install_dir, otherwise this installation will fail."
	echo -n "Press <Return> to continue..."
	if ( ! $auto_install ) then
		set foo=$<
	endif
endif

echo ""
cd $install_dir

if ( -f $mount_dir/stub.csh ) then
	if ( ! -d ./bin ) then
		mkdir ./bin
	endif
	echo "Creating wrapper shell scripts in the '$install_dir/bin' directory..."
	cp -f $mount_dir/stub.csh ./bin/max2syn
	cp -f $mount_dir/stub.csh ./bin/max2synw
	chmod 555 ./bin/*
endif

if ( -f $mount_dir/adm.$ext ) then
	echo "Installing contents of 'adm.$ext' to the '$install_dir/adm' directory..."
	cat $mount_dir/adm.$ext | $uncompress_cmd | tar xof -
endif

if ( -f $mount_dir/solaris.$ext ) then
	echo "Installing contents of 'solaris.$ext' to the '$install_dir/solaris' directory..."
	cat $mount_dir/solaris.$ext | $uncompress_cmd | tar xof -
	if ( -f $mount_dir/sunmath.so1 ) cp -pf $mount_dir/sunmath.so1 solaris/libsunmath.so.1
endif

if ( -f $mount_dir/hp.$ext ) then
	echo "Installing contents of 'hp.$ext' to the '$install_dir/hp' directory..."
	cat $mount_dir/hp.$ext | $uncompress_cmd | tar xof -
	cp -pf hp/qcmd bin/qcmd
endif

if ( -f $mount_dir/hp11.$ext ) then
	echo "Installing contents of 'hp11.$ext' to the '$install_dir/hp11' directory..."
	cat $mount_dir/hp11.$ext | $uncompress_cmd | tar xof -
	cp -pf hp11/qcmd bin/qcmd
endif

if ( -f $mount_dir/libraries.$ext ) then
	echo "Installing contents of 'libraries.$ext' to the '$install_dir/libraries' directory..."
	cat $mount_dir/libraries.$ext | $uncompress_cmd | tar xof -
endif

if ( -f $mount_dir/lmf.$ext ) then
	echo "Installing contents of 'lmf.$ext' to the '$install_dir/lmf' directory..."
	cat $mount_dir/lmf.$ext | $uncompress_cmd | tar xof -
endif

if ( -f $mount_dir/eda.$ext ) then
	echo "Installing contents of 'eda.$ext' to the '$install_dir/eda' directory..."
	cat $mount_dir/eda.$ext | $uncompress_cmd | tar xof -
endif

if ( -f $mount_dir/tcl.$ext ) then
	echo "Installing contents of 'tcl.$ext' to the '$install_dir/tcl' directory..."
	cat $mount_dir/tcl.$ext | $uncompress_cmd | tar xof -
endif

if ( -f $mount_dir/readme.txt ) then
	cp -f $mount_dir/readme.txt .
endif

if ( -f $mount_dir/license.txt ) then
	cp -f $mount_dir/license.txt .
endif

if ( -f $mount_dir/tlslic.txt ) then
	cp -f $mount_dir/tlslic.txt .
endif

if ( -f $mount_dir/bwidget.txt ) then
	cp -f $mount_dir/bwidget.txt .
endif

if ( -f $mount_dir/common.$ext ) then
	echo "Installing contents of 'common.$ext' to the '$install_dir/common' directory..."
	cat $mount_dir/common.$ext | $uncompress_cmd | tar xof -
endif

if ( -d ./common ) then
	if ( -d ./solaris ) then
		cd solaris
		foreach i (../common/*)
			rm -f `basename $i` >&/dev/null
			ln -s $i .
		end
		cd ..
	endif
	if ( -d ./hp ) then
		cd hp
		foreach i (../common/*)
			rm -f `basename $i` >&/dev/null
			ln -s $i .
		end
		cd ..
	endif
	if ( -d ./hp11 ) then
		cd hp11
		foreach i (../common/*)
			rm -f `basename $i` >&/dev/null
			ln -s $i .
		end
		cd ..
	endif
	if ( -d ./linux ) then
		cd linux
		foreach i (../common/*)
			rm -f `basename $i` >&/dev/null
			ln -s $i .
		end
		cd ..
	endif
endif


echo "Setting default environment variables in $install_dir/adm/qenv.csh"
chmod +w $install_dir/adm/qenv.csh
cat $install_dir/adm/qenv.csh | sed -e "s#---MAXPLUS2_ROOT_DEFAULT---#${mp2_install_dir}#g" > $install_dir/adm/qenv.tmp
cat $install_dir/adm/qenv.tmp | sed -e "s#---MAX2SYN_ROOT_DEFAULT---#${install_dir}#" > $install_dir/adm/qenv.csh
rm -f $install_dir/adm/qenv.tmp
chmod -w $install_dir/adm/qenv.csh

rm -f $install_dir/bin/max2syn_patch_log.txt >& /dev/null

echo ""
echo "Installation complete. You can now run the MAX+PLUS II Advanced Synthesis software."

