#!/bin/csh
##############################################################
#                                                            #
#  Quartus Workstation Patch Install Script
#                                                            #
#  Copyright (c) Altera Corportation 2005	             #
#  All Rights Reserved                                       #
#                                                            #
##############################################################

start_install:

setenv PATH /bin:/usr/bin:$PATH

set product_version = "12.0"
set patch_version = "2.24"
set incompatible_db = "0"
set target_os_type = "Linux"
set product_name = "Quartus"
set product_dir = "quartus"
set patch_number = `echo $patch_version | sed -e's/[0-9]*\./\./'`
set curr_dir=`pwd`
set patch_file = "${product_dir}_patch.tar.gz"
set curr_year = `date '+ %Y'`
set auto_install = "0"

if ("$1" != "") then
	set default_install_dir = $1
	set auto_install  = "1"
else
	set default_install_dir = /opt/altera${product_version}
endif

unalias rm

# 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 != $target_os_type) then
    # The patch is not for the current OS
    echo "Error: You're installing the patch on a wrong platform. The patch can only be installed on $target_os_type."
    exit 1
endif

if ($os_type == "SunOS") then
	if ($os_version =~ 5.*) then
		# This is solaris 2.x
		set host_platform="solaris"
	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="hp11"

else if ($os_type == "Linux") then
	# This is an Linux
	set host_platform="linux"

else
	# Unknown
        echo "Cannot determine whether operating system is compatibile"
        echo "with this installation script"
	exit 1

endif

start:

clear 
echo    ""
echo    "$product_name $product_version Patch $patch_version Installation Script for $target_os_type Workstations"
echo    "Copyright (C) Altera Corporation $curr_year All right reserved."
echo    ""
echo    "Type Ctrl+C <Return> to quit this installation script at any time." 
echo    ""
echo    "Use this installation script to install patches to the $product_name software"
echo    ""

if ("$auto_install" == "0") then
    echo "Press <Return> to continue..."
    set foo=$<
endif

set default_patch_dir = $script_path
if ( -f $default_patch_dir/$patch_file ) goto get_install_directory

get_patch_directory:

echo    "Type the full pathname of the directory where the $product_name"
echo -n "patch file ($patch_file) is located (default: $default_patch_dir): "
set patch_dir=$<

if ($patch_dir == "") then
	set patch_dir=$default_patch_dir
endif

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

if (! -f $patch_dir/$patch_file ) then
	echo ""
	echo "Sorry, '$patch_file' is not located in '$patch_dir'"
	goto get_patch_directory
endif

set default_patch_dir=$patch_dir

get_install_directory:

set install_dir=""
if ("$auto_install" == "0") then
    echo    "Type the full pathname of the directory where the $product_name $product_version"
    echo -n "software is installed (default: $default_install_dir): "
    set install_dir=$<
endif

if ($install_dir == "") then
	set install_dir=$default_install_dir
endif

if (! -d $install_dir) then
	echo ""
	echo "Sorry, '$install_dir' is not a valid directory name"
	set auto_install = "0"
	set default_install_dir = /opt/altera${product_version}
	goto get_install_directory
endif

if (! -d $install_dir/$product_dir) then
	echo ""
	echo "Sorry, you don't have '$product_dir' installed under '$install_dir'. You need to specify a path that is one-level above the '$product_dir' directory."
	set auto_install = "0"
	set default_install_dir = /opt/altera${product_version}
	goto get_install_directory
endif

set default_install_dir=$install_dir

install_files:

if (! -w $install_dir ) then
	echo ""
	echo "You do not have write permission to '$install_dir'"
	echo "Cannot install service patch!"
	exit 1
endif

cd $install_dir

set files = (`cat $script_path/$patch_file | gzip -c -d | tar tf - | awk '{printf "%s ", $0}'`)

echo ""
echo "Moving existing files to '<filename>.$product_version'..."
foreach i ($files)
	if ( -f $i ) then
		if ( ! -f $i.$product_version) then
			mv -f $i $i.$product_version
		endif
    endif
end

echo ""
echo "Installing patch files..."
if ($os_type == "Linux") then
    cat $script_path/$patch_file | gzip -c -d | tar xf -
else
    cat $script_path/$patch_file | gzip -c -d | tar xof -
endif

echo "$product_name $product_version patch $patch_version" >>! patch_log.txt

if ($product_dir == "quartus") then

    echo "$patch_number;$incompatible_db" >>! $product_dir/$host_platform/quartus_patch_log.txt

    set host_platform64="${host_platform}64"
    if ( -d $product_dir/$host_platform64 ) then
	echo "$patch_number;$incompatible_db" >>! $product_dir/$host_platform64/quartus_patch_log.txt
    endif
endif

echo ""
echo "Patch $patch_version has been successfully installed."
echo ""
