#!/bin/csh -f
#
#      $Id: perf_install,v 1.18 2003-05-16 14:53:12 haley Exp $
#
#########################################################################
#									#
#			   Copyright (C)  1992				#
#	     University Corporation for Atmospheric Research		#
#			   All Rights Reserved				#
#									#
#########################################################################
#
#	File:		perf_install
#
#	Author:		John Clyne
#			National Center for Atmospheric Research
#			PO 3000, Boulder, Colorado
#
#	Date:		Tue Nov 17 14:01:17 MST 1992
#
#	Description:	Perform the installation. If no arguments are given
#			perf_install attempts to use the default directory
#			paths. Otherwise perf_install expects the bin, lib
#			include, and man paths to be given on the command
#			line in that order. If a path is set to "/dev/null"
#			the coresponding software component is not installed.
#
#	Usage:		perf_install [ bin_path inc_path lib_path man_path ]
#
#	Environment:	LOCALDIR	path to installation system
#			INSTALL_SOURCE	one of (tape,cdrom,tarfile)
#			SYSTEM		system type the INSTALL is being run from
#			SYSTEM_TO_INSTALL system type of binaries we want
#			INSTALL_TAPE_DEV	tape dev path for tape installs
#			INSTALL_CD_DEV		cdrom dev path for cd installs
#			INSTALL_FILE_DEV	tar file path for file installs
#
#	Files:
#
#
#
#
#	Options:
#
#
#
#

onintr cleanup

if ($#argv == 0) then
	set paths = `$LOCALDIR/get_paths -default`
	if ($status != 0) then
		exit 1
	endif
	set bin_path = $paths[1]
	set inc_path = $paths[2]
	set lib_path = $paths[3]
	set man_path = $paths[4]

else if ($#argv == 4) then
	set bin_path = $argv[1]
	set inc_path = $argv[2]
	set lib_path = $argv[3]
	set man_path = $argv[4]

else
	echo "Usage : perf_install [ bin_path lib_path inc_path man_path ]" > /dev/tty
	exit 1
endif

set instbls = ("$bin_path" "$inc_path" "$lib_path" "$man_path")

#
#	AGGGGGGGH!!!  DEC and HP systems map CD-ROM HSF file names into
#	caps. Sun, SGI, and IBM systems map names into all lower case.
#
set utars = ("BIN.TAR" "INCLUDE.TAR" "LIB.TAR" "MAN.TAR")
set ltars = ("bin.tar" "include.tar" "lib.tar" "man.tar")
#
# Upper case by default.
#
set tars = ($utars)

if ("$bin_path" != "/dev/null") then
	set bin_line = "	Executables	$bin_path"
else
	set bin_line = ""
endif

if ("$inc_path" != "/dev/null") then
	set inc_line = "	C includes	$inc_path"

	#
	# this is a hack to make sure there isn't already a ncarg directory
	# in the include dir. tar won't overwrite it with a symlink if 
	# there is so we move it away first
	#
	if (-d "$inc_path/ncarg") then
		echo "Moving $inc_path/ncarg to $inc_path/ncarg.old" > /dev/tty	
		$MV $inc_path/ncarg $inc_path/ncarg.old
	endif
else
	set inc_line = ""
endif

if ("$lib_path" != "/dev/null") then
	set lib_line = "	Libraries	$lib_path"
	if (-d "$lib_path/ncarg") then
		echo "Moving $lib_path/ncarg to $lib_path/ncarg.old" > /dev/tty	
		$MV $lib_path/ncarg $lib_path/ncarg.old
	endif
else
	set lib_line = ""
endif

if ("$man_path" != "/dev/null") then
	set man_line = "	Man pages	$man_path"
else
	set man_line = ""
endif


clear > /dev/tty
cat <<EOF > /dev/tty



	The following components will be installed:

	$bin_line
	$inc_line
	$lib_line
	$man_line

EOF
echo -n "	Is this correct [y/n](y) ? " > /dev/tty
set answer = $<
if ("$answer" != "y" && "$answer" != "") then
	exit 1
endif

switch ("$INSTALL_SOURCE") 

case	"tape":
	set line = (`grep $SYSTEM_TO_INSTALL $LOCALDIR/var/TOC`)
	if ($status != 0) then
		echo "Can't find <$SYSTEM_TO_INSTALL> files on tape device $INSTALL_TAPE_DEV" > /dev/tty
		echo "Installation from $INSTALL_SOURCE failed" > /dev/tty
		exit 1
	endif
	set fsf = $line[1]	# location of $SYSTEM_TO_INSTALL files on tape
	
	$LOCALDIR/read_tape $INSTALL_TAPE_DEV $fsf $instbls > /dev/tty
	if ($status != 0) then
		exit 1
	endif
	breaksw

case	"cdrom":

	#
	# Ugh! ISO 9660 restricts file names to eight upper case characters,
	# followed by a '.', followed by three more upper case characters.
	# Need to map $SYSTEM into something that conforms.
	#
	switch ("$SYSTEM")
	case	"HPUX_HP9000_7xx"
	case	"HPUX_HP9000_8xx"
	case	"OSF1_alpha"
	case	"ULTRIX_RISC"
	case	"UNICOS_YMP"
	case	"UNICOS_J90"
	case	"UNICOS_C90"
	case	"UNICOS_T90"
    set tars = ($utars)
	switch ("$SYSTEM_TO_INSTALL")

  	  case	"HPUX_HP9000_7xx"
		set cd_system = "HPUX_7XX"
		breaksw

	  case	"HPUX_HP9000_8xx"
		set cd_system = "HPUX_8XX"
		breaksw

	  case	"AIX_RS6000"
		set cd_system = "AIX_R6K"
		breaksw

	  case	"LINUX_PC"
		set cd_system = "LINUX"
		breaksw

	  case	"LINUX_IA64"
		set cd_system = "LINUX"
		breaksw

	  case	"DARWIN_MAC"
		set cd_system = "DARWIN"
		breaksw

	  case	"SunOS_sun4"
		set cd_system = "SUNOS_S4"
		breaksw

	  case	"Solaris_sun4"
		set cd_system = "SOLARIS"
		breaksw

	  case	"Solaris_PC"
		set cd_system = "SOLARPC"
		breaksw

	  case	"OSF1_alpha"
		set cd_system = "OSF1_ALF"
		breaksw

	  case	"ULTRIX_RISC"
		set cd_system = "ULTRIX"
		breaksw

	  case	"IRIX_n32_IP"
		set cd_system = "IRIXN32"
		breaksw

	  case	"IRIX_64_IP"
		set cd_system = "IRIX64"
		breaksw

	  case	"UNICOS_YMP"
		set cd_system = "UCOS_YMP"
		breaksw

	  case	"UNICOS_C90"
		set cd_system = "UCOS_C90"
		breaksw

	  case	"UNICOS_J90"
		set cd_system = "UCOS_J90"
		breaksw

	  case	"UNICOS_T90"
		set cd_system = "UCOS_T90"
		breaksw
    endsw

	case	"AIX_RS6000"
	case	"LINUX_PC"
	case	"LINUX_IA64"
	case	"DARWIN_MAC"
	case	"CYGWIN_PC"
	case	"SunOS_sun4"
	case	"Solaris_sun4"
	case	"Solaris_PC"
	case	"IRIX_IP"
    set tars = ($ltars)
	switch ("$SYSTEM_TO_INSTALL")

	  case	"HPUX_HP9000_7xx"
		set cd_system = "hpux_7xx"
		breaksw

	  case	"HPUX_HP9000_8xx"
		set cd_system = "hpux_8xx"
		breaksw

	  case	"AIX_RS6000"
		set cd_system = "aix_r6k"
		breaksw

	  case	"LINUX_PC"
		set cd_system = "linux"
		breaksw

	  case	"LINUX_IA64"
		set cd_system = "linux"
		breaksw

	  case	"DARWIN_MAC"
		set cd_system = "darwin"
		breaksw

	  case	"CYGWIN_PC"
		set cd_system = "cygwin"
		breaksw

	  case	"SunOS_sun4"
		set cd_system = "sunos_s4"
		breaksw

	  case	"Solaris_sun4"
		set cd_system = "solaris"
		breaksw

	  case	"Solaris_PC"
		set cd_system = "solarpc"
		breaksw

	  case	"OSF1_alpha"
		set cd_system = "osf1_alf"
		breaksw

	  case	"ULTRIX_RISC"
		set cd_system = "ultrix"
		breaksw

	  case	"IRIX_n32_IP"
		set cd_system = "irixn32"
		breaksw

	  case	"IRIX_64_IP"
		set cd_system = "irix64"
		breaksw

	  case	"UNICOS_YMP"
		set cd_system = "ucos_ymp"
		breaksw

	  case	"UNICOS_C90"
		set cd_system = "ucos_c90"
		breaksw

	  case	"UNICOS_J90"
		set cd_system = "ucos_j90"
		breaksw

	  case	"UNICOS_T90"
		set cd_system = "ucos_t90"
		breaksw
	endsw
    endsw

	if (! $?cd_system)  then
		echo "Unknown system ($SYSTEM_TO_INSTALL)" > /dev/tty
		exit 1
	endif

	# 
	# read the tar file off the CDROM
	#
	while ($#instbls)
		if ("$instbls[1]" != "/dev/null") then
			set tarfile = $INSTALL_CD_DEV/$cd_system/$tars[1]
			$LOCALDIR/read_tarfile $tarfile $instbls[1] > /dev/tty
			if ($status != 0) then
				exit 1
			endif
		endif

		shift instbls
		shift tars
	end

	breaksw

case	"tar-file":
	while ($#instbls)
		if ("$instbls[1]" != "/dev/null") then
			$LOCALDIR/read_tarfile $INSTALL_FILE_DEV/$tars[1] $instbls[1] > /dev/tty
			if ($status != 0) then
				exit 1
			endif
		endif

		shift instbls
		shift tars
	end
	breaksw

default:
	echo "$0 : Unknown install source <$INSTALL_SOURCE>" > /dev/tty
	exit 1
endsw

echo ""> /dev/tty
echo ""> /dev/tty
echo ""> /dev/tty
echo "Installation is complete" > /dev/tty

#
# Need to install site file to $lib_path/ncarg/$NCARG_SITE_FILE
#

if ("$lib_path" != "/dev/null" && $NCARG_SITE_ID != "") then
  if (-f $lib_path/ncarg/$NCARG_SITE_FILE) then
    /bin/rm $lib_path/ncarg/$NCARG_SITE_FILE
  endif
  echo $NCARG_SITE_ID >> $lib_path/ncarg/$NCARG_SITE_FILE
endif

$LOCALDIR/pause

exit 0

cleanup:
exit 1
