#!/bin/csh -f
#Tag 0xFFFFFF11
#
# $Id: jaleo_install,v 1.36 2001/02/27 12:04:46 ulises Exp cm $
#
# Copyright (C) Jaleo Europe, Las Palmas de Gran Canaria, 2000
#
# $Log: jaleo_install,v $
# Revision 1.36  2001/02/27 12:04:46  ulises
# Added remaining manual files and fixed Toolchest/DOCS entries
#
# Revision 1.35  2001/01/22  11:52:27  ulises
# Fixed change directory bug
#
# Revision 1.34  2001/01/17  11:13:12  ulises
# Manual files are stored in different gzip files and there is a menu to choose
# which ones will be installed.
#
# Revision 1.33  2000/12/18  17:32:06  ulises
# Default version number to 3.5
#
# Revision 1.32  2000/01/10  23:00:15  david
# In order to avoid to have to update the dates showed to the
# jaleo-user, they will be removed
#
# Revision 1.31  1999/07/20 16:12:43  david
# Do automatic PARALLEL setup
#
# Revision 1.30  1999/06/30 12:30:06  david
# Some echo output has been modified
#
# Revision 1.29  1999/06/10 13:29:56  david
# Fixed a mistake of automatic detection of license server
#
# Revision 1.28  1999/05/25 22:01:26  david
# show the resolution of the standards and other stuff
#
# Revision 1.27  1999/05/19 22:00:18  david
# Added the wacom setup and other identation stuff
#
# Revision 1.26  1999/05/05 17:20:32  david
# Indexing the echo output, standard stuff, etc
#
# Revision 1.25  1999/04/19 22:02:28  david
# Setting owner on '.jaleorc' file, and other stuff.
#
# Revision 1.24  1999/03/24 23:05:16  david
# Commented the root.sys chown of RTvideo application
#
# Revision 1.23  1999/03/18 23:00:18  david
# Added options to STANDARD question
#
# Revision 1.22  1999/03/11 23:04:10  david
# Added the standard configuation on .jaleorc file
#
# Revision 1.21  1999/03/02 23:08:52  david
# refine
#
# Revision 1.20  1999/02/18 23:05:02  martin
# Added extra rsh checkings and warnings
# Corrected some spelling
#
# Revision 1.19  1999/01/21 23:02:33  david
# refine
#
# Revision 1.18  1999/01/13  19:55:48  david
# refine seting of JALEO_ELMHOST
#
# Revision 1.17  1999/01/13  17:24:11  david
# Fixing the JALEO_ELMHOST mistake
#
# Revision 1.16  1998/11/27  19:50:20  david
# Added check of correct license server name
#
# Revision 1.15  1998/11/25  13:00:23  david
# Automatic actualizacion de JALEO_ELMHOST
#
# Revision 1.14  1998/11/13  23:01:07  david
# fixing a mistake
#
# Revision 1.13  1998/10/29  23:00:51  david
# Added one user's mess
#
# Revision 1.12  1998/10/29  13:02:45  cm
# *** empty log message ***
#
# Revision 1.11  1998/09/28 18:15:35  cm
# update from 2.7 version to 3.0
#
# Revision 1.10  1997/12/12 23:00:20  ceballos
# Upgrade 80->120Mg of memory needed
#
# Revision 1.9  1997/12/05 23:01:00  ceballos
# jaleo_install set RTvideo as superuser
#
# Revision 1.8  1997/09/26 08:11:38  anthon
# made sure no warning pops up without impact_catch
#
# Revision 1.7  1997/08/19 17:25:23  cm
# general_update
#
# Revision 1.6  1997/08/08  17:13:39  cm
# improved the set uid and set gid to skip files that are not there
#
# Revision 1.5  1997/08/08  16:25:51  cm
# forgot to change VN to VN_NODOT
#
# Revision 1.4  1997/08/08  07:59:38  cm
# changed version number
#
# Revision 1.3  1997/08/07  21:26:08  cm
# stripping install script
#
# Revision 1.2  1997/08/07  11:48:33  cm
# updated with 2.7J stuff
#
# Revision 1.1  1997/08/07  11:40:58  cm
# Initial revision
#
# Installation script for
#
# Jaleo
#
# The Digital Video Postproduction System 
#
# Brought To You Directly From The Canary Islands
#
#
if ($1 == "-h") then
    echo 
    echo "NAME"
    echo "	install.sh   Install a Jaleo's product."
    echo 
    echo "SYNOPSIS"
    echo " 	install.sh [-h]"
    echo 
    echo "DESCRIPTION"
    echo "	This script installs a Jaleo's product, building"
    echo "	its proper account. The sections of the installation"
    echo "	procedure are the below."
    echo 
    echo "	The configuration section collects the user options"
    echo "	and the installation carry out it."
    echo 
    echo "	  Introduction"
    echo 
    echo "	  Configuration settings"
    echo "	    Creating a user account"
    echo "		User name"
    echo "		Account home path"
    echo "		Account password"
    echo "	    License server"
    echo "	    Work resolution"
    echo 
    echo "	  Installation"
    echo "	    Extracting"
    echo "	    Running License Setup"
    echo "	      Creating license directory structure"
    echo "	      Creating jaleolicd boot files"
    echo "	      Starting daemon"
    echo "	    Running Jaleo Setup Script"
    echo "	      Setting up login files"
    echo "	      Setting up Manual files"
    echo "	      Creating default desktop configuration"
    echo "	      Copying Jaleo Icons"
    echo "	      Installing Color Database"
    echo "	      Setting up desktop integration"
    echo "	      Creating Jaleo Work Directory"
    echo "	      Updating /etc/magic"
    echo "	      Fixing Ownership"
    echo "	    Last steps"
    echo 
    echo "OPTIONS"
    echo "	[-h]	Show this documentation."
    echo 
    exit 0
endif

set VN=3.5
set VERSION = "Jaleo $VN Installation Script"
set CLEAR   = "clear"

#$CLEAR
#echo; echo; echo; echo; echo; echo; echo; echo; echo; echo; echo; echo; 
#echo; echo; echo; echo; echo; echo; echo; echo; echo; echo; echo; echo; 
echo 
echo '==============================================='
echo '==                                           =='
echo '== JALEO Digital Video Postproduction System =='
echo '==                                           =='
echo "== Version $VN\t\t                     =="
echo '==                                           =='
echo '== Installation Script                       =='
echo '==                                           =='
echo '==============================================='
echo
echo $VERSION
echo
echo 'This script will install and configure Jaleo' 
echo 'in a new user home directory'
echo
echo 'You can interrupt the installation at any time'
echo 'by pressing <ctrl>-c. Note that while the system'
echo 'is waiting for input on a prompt, you may have'
echo 'to press <enter> before the script reacts on'
echo 'the interruption'
echo
echo Almost all input prompts have default values.
echo If you are in doubt what to do, just press return
echo
echo 'Do you want to continue? [y/n]: \c'
set input = $<
if ("$input" == "N" || "$input" == "n") then
    echo
    echo 'You have decided to terminate the installation.'
    echo
    echo 'Adios.'
    echo
    exit 0
else
    # do nothing
endif

#
# Ok, the user decided to go on.
# It is getting serious
#

######################################################
# Variables, commands and handlers
######################################################

set J_TAPE = "/dev/tape"    # the default tape drive name
set J_DEFTAPE = "/dev/tape" # the default tape drive name

set J_ARCHIVE = "jaleo.tar" # the archive name, in case we install from CD-ROM
set J_ARCHIVEGZ = "jaleo.tgz" # the compressed archive name

set J_TAPESTYLE = "local"   # by default, use local tape

set J_RTAPEHOST = ""	    # NO default remote host
set J_RTAPEUSER = "guest"   # default remote user

set J_BASE = "/usr/people"  # default location for home directory
set J_DHOME = "/usr/people" # default location for home directory, backup

# version number without dot (Assumes VN looks like X.Y
set VN_NODOT=`echo $VN | sed s/\\.//` 
set J_USER = "jaleo"$VN_NODOT    # default user to be created for jaleo
set J_DUSER = $J_USER       # backup of default user to be created for jaleo

set J_DIR = "INSTALL LICENSE PLUGIN_EXAMPLES JALEO-ENV"	# default installation fileset

set J_LICDIR = "LICENSE"    # The directory with license software
set J_ENVDIR = "JALEO-ENV"  # The Jaleo content directory
set J_INSDIR = "INSTALL"    # The Jaleo Installation file directory

set J_LICSCRIPT = "setup_license"	# The setup script for license s.
set J_INSSCRIPT = "setup_jaleo"		# The setup script for the account

set J_HEADERDIR = "/usr/local/jaleoHeaders"   # The Header directory

set J_BIN			= "bin"
set J_REALTIME_APP		= "RTvideo"

set J_MANUALFILE = "manual.tar"	# File with compressed manual files

#
# Values
#

set J_SPACE = "100"	    # MB required for installation

set J_SLEEP = "4"	    # Sleep time to guarantee tape load

set J_SHELL = "/bin/tcsh"   # Default Shell
set J_GROUP = "20"	    # Default Group
 
set BELL = "\007"

set FROM_CDROM = "FALSE"    # by default, we assume we install from tape

#
# Commands used
#
set UNAME   = "uname -r"
set ID	    = "id"
set CUT	    = "cut"
set WHOAMI  = "whoami"
set DF	    = "df -k"
set NAWK    = "nawk"
set PWD	    = "pwd"
set MKDIR   = "mkdir"
set MT	    = "mt"
set SLEEP   = "sleep"
set CD	    = "cd"
set TAR	    = "tar"
set CHOWN   = "chown"
set PASSMGMT = "passmgmt"
set PASSWD  = "passwd"
#
#  Set a handler to be called on interrupt
#
onintr handleInterupt

#####################################################
# Check prerequisites
#####################################################

#
# Operating System must be at least 6.2
#
set OS_VER = `$UNAME`
if ( `$UNAME | grep -c '6.[2-9]'` == 0) then
    echo
    echo $BELL
    echo '*** Installation Problem:'
    echo '    You are running an operating system release'
    echo '    that is older than IRIX 6.2'
    echo '    (You have '$OS_VER')'
    echo
    echo '    Jaleo '$VN' needs at least IRIX 6.2 to run'
    echo
    echo 'You may proceed to install anyway, although'
    echo '1) you may run into trouble later in the installation'
    echo '2) it is very improbable that the software will run on this machine'
    echo
    echo One reason to proceed would be that you are trying to prepare
    echo a disk for another target machine,  or that you plan to
    echo upgrade your operating system soon
    echo
    echo 'Do you want to install anyway? [y/n]: \c'
    set input = $<
    if ("$input" == "Y" || "$input" == "y") then
	#do nothing
    else
	echo
	echo You have decided to terminate the installation.
	echo
	echo Adios.
	echo
	exit 0
    endif
endif

#echo
#echo You are running IRIX $OS_VER, and this is appropriate for Jaleo
#echo 

#set CUR_USER = `$ID | $CUT -c6-11`
#if ( $CUR_USER != "(root)" ) then

if ( `$WHOAMI` != "root" ) then
    #$CLEAR
    echo
    echo $BELL
    echo '*** Installation Problem:'
    echo '    You are not running this script logged in as root,'
    echo '    or your user id is not 0'
    echo 
    echo '    You are logged in as: '`$WHOAMI`
    echo
    echo '    Installation will terminate now'
    echo '    Please log out and log in as root'
    echo '    to install Jaleo'
    exit 0
endif

#echo
#echo You are running this script as root, and this is appropriate
#echo

echo 
echo 
echo 
echo 
echo "CONFIGURATION SETTINGS"
echo 

#####################################################
# Check if we have tar files available
#####################################################
if ( -e $J_ARCHIVE || -e $J_ARCHIVEGZ) then
    set J_ARCH=$J_ARCHIVE
    if ( -e $J_ARCHIVEGZ ) set J_ARCH=$J_ARCHIVEGZ
    echo "  Found Jaleo tar archive, installing from archive file $J_ARCH"
    set FROM_CDROM = "TRUE"
    set cdpath = `pwd`
    set J_TAPE = $cdpath"/"$J_ARCH
    echo "  $J_TAPE"
    echo
else
#
# The endif for this else is quite far...
#
    set FROM_CDROM = "FALSE"
     
    #####################################################
    # Check if user has specified a tape drive
    #####################################################
    
    if ( $#argv == 1 && `echo $1 | grep -c /dev` != 0 ) then
	echo 
	echo A tape drive was specified on the command line':' $1
	echo
	set J_TAPE = $1
    else
	#####################################################
	# Get Tapedrive
	#####################################################
	getTapeDrive:
	    echo 
	    echo You must have either a local or a remote tape drive available
	    echo to proceed with the installation.
	    echo
	    getTapeChoice:
		echo 'Enter: '
		echo '[l] to install from a local tape drive (DEFAULT)' 
		echo '[r] to install from a remote tape drive'
		echo '[q] to quit'      
		echo
		echo 'Please enter your choice [l/r/q]: \c'
		set input = $<
		switch ($input)
		case "r":
		case "R":
		    set J_TAPESTYLE = "remote"
		    breaksw
		case "q":
		case "Q":
		    echo
		    echo You have decided to terminate the installation.
		    echo
		    echo Adios.
		    echo
		    exit 0
		case "l":
		case "L":
		default:
		    set J_TAPESTYLE = "local"
		    breaksw
		endsw
		
	getRHost:
	    if ( "$J_TAPESTYLE" == "remote" ) then
		echo
		echo For a remote tapedrive, you must enter the hostname
		echo of the host the tape drive is connected to:
		echo
		echo 'Hostname: \c'
		set J_RTAPEHOST = $<
		echo
		echo You have chosen \"$J_RTAPEHOST\"
		
		echo 'Enter: '
		echo '[1] if this is correct (DEFAULT)'
		echo '[2] to enter another hostname'
		echo '[3] to choose local tape'
		echo '[q] to abort installation'      
		echo
		echo 'Please enter your choice [1/2/3/q]: \c'
		set input = $<
		switch ($input)
		case "2":
		    goto getRHost
		case "3":
		    set J_TAPESTYLE = "local"
		    set J_RTAPEHOST = ""
		    breaksw
		case "q":
		case "Q":
		    echo
		    echo You have decided to terminate the installation.
		    echo
		    echo Adios.
		    echo
		    exit 0
		case "1":
		default:
		    breaksw
		endsw
	    endif
	    
	getRUser:    
	    if ( "$J_TAPESTYLE" == "remote" ) then	    
		echo
		echo For a remote tapedrive, you must enter a valid
		echo user name to access the tape drive. The default, 
		echo guest, will do in most cases.
		echo
		echo 'User ['$J_RTAPEUSER']: \c'
		set input = $<
		if ( "$input" != "" ) then
		    set J_RTAPEUSER = $input
		endif
		echo
		echo You have chosen \"$J_RTAPEUSER\"
		
		echo 'Enter: '
		echo '[1] if this is correct (DEFAULT)'
		echo '[2] to enter another user name'
		echo '[3] to choose local tape'
		echo '[q] to abort installation'
		echo
		echo 'Please enter your choice [1/2/3/q]: \c'
		set input = $<
		switch ($input)
		case "2":
		    goto getRUser
		case "3":
		    set J_TAPESTYLE = "local"
		    set J_RTAPEHOST = ""
		    set J_RTAPEUSER = "guest"
		    breaksw
		case "q":
		case "Q":
		    echo
		    echo You have decided to terminate the installation.
		    echo
		    echo Adios.
		    echo
		    exit 0
		case "1":
		default:
		    breaksw
		endsw
	    endif
	
	getTapePath:
	    echo
	    echo Enter the tape driver path you wish to use
	    echo The default, $J_TAPE, will do in most cases.
	    echo
	    echo 'Tapedrive ['$J_TAPE']: \c'
	    set input = $<
	    if ( "$input" != "" ) then
		set J_TAPE = $input
	    endif
	    echo
	    echo You have chosen \"$J_TAPE\"
	    
	    echo 'Enter: '
	    echo '[1] if this is correct (DEFAULT)'
	    echo '[2] to enter another driver name'
	    echo '[q] to abort installation'
	    echo
	    echo 'Please enter your choice [1/2/q]: \c'
	    set input = $<
	    switch ($input)
	    case "2":
		goto getTapePath
	    case "q":
	    case "Q":
		echo
		echo You have decided to terminate the installation.
		echo
		echo Adios.
		echo
		exit 0
	    case "1":
	    default:
		breaksw
	    endsw
	
	if ( "$J_TAPESTYLE" == "remote" ) then
	    set J_TAPE = $J_RTAPEUSER\@$J_RTAPEHOST\:$J_TAPE
	else
	    #nothing to do, J_TAPE is the drive
	endif
    endif #check for tape
    
    #
    # check tape for existence and accessability
    #
    testTapeDrive:
	echo
	echo Testing tape drive
	echo 
	
	#
	# Sometimes in life you have been dissapointed.
	# So you doubt unless proven otherwise
	# We assume the tape does not work
	@ tapeError = 1 
	mt -f $J_TAPE exist
	
	if ($status == 0) then
	    @ tapeError = 0
	    mt -f $J_TAPE stat
	    if ($status != 0) then
		@ tapeError = 1
	    endif
	endif
    
    if ( $tapeError == 1 ) then
	#$CLEAR
	echo
	echo $BELL
	echo '*** Installation Problem:'
	echo '    You have selected a tapedrive that at'
	echo '    least temporarily is not accessible'
	echo 
	echo '    The drive you selected is: '$J_TAPE
	echo
	echo 'Check your tape drive, network connection,'
	echo 'hostname, user account and permissions.'
	echo
	echo 'Enter:'
	echo '[1] to select another tape drive (DEFAULT)'
	echo '[2] to try drive again'
	echo '[q] to quit installation'
	echo
	echo 'Please enter your choice [1/2/q]: \c'
	set input = $<
	switch ($input)
	case 2:
	    #$CLEAR 
	    goto testTapeDrive
	case "q":
	case "Q":
	    echo
	    echo You have decided to terminate the installation.
	    echo
	    echo Adios.
	    echo
	    exit 0
    ### WARNING XXX temporary EXIT
	case "x":
	    breaksw
	case "1":
	default:
	    #$CLEAR
	    set J_TAPE = $J_DEFTAPE
	    goto getTapeDrive
	endsw
    else
	echo
	echo Your tapedrive is accessible
	echo
    endif

endif	# this is the endif from the tape check stuff, which is HUGE

#####################################################
# User Installation
#####################################################
#
# 1. Get user name
#
# 2. Get Home Directory
#
# 3. Check and Install user
#
echo 
echo "  CREATING A USER ACCOUNT"
getNewUser:
    echo 
    echo "    USER NAME"
    echo 
    #$CLEAR
    #echo
    #echo "    Create an initial Jaleo user"
    #echo $BELL
    #echo
    echo "      Jaleo needs a 'base user' account to be installed in."
    echo "      You must select an account name that does not exist yet."
    echo
    echo "      Enter a new user name to install Jaleo to."
    echo "      User names must not have more than 8 characters."
    echo
    echo '      User Name ['$J_USER']: \c' 
    
    set input = $<
    if ( "$input" != "" ) then
	set J_USER = $input
    endif
    
    # make sure user is happy with her or his choice
    echo
    echo "      You have chosen '$J_USER' - do you want to"
    echo "      create this user?"
    echo
    echo '      Enter: '
    echo '      [1] to proceed, creating a new user(DEFAULT)'
    echo '      [2] to type in another user name'
    echo '      [q] to abort installation'
    echo
    echo '      Please enter your choice [1/2/q]: \c'
    set input = $<
    switch ($input)
    case "2":
	goto getNewUser
    case "q":
    case "Q":
	echo
	echo "      You have decided to terminate the installation."
	echo
	echo "      Adios."
	echo
	exit 0
    case "1":
    default:
	breaksw
    endsw

#####################################################
# Display diskspace and get Install directory
#####################################################
getInstDir:
    echo 
    echo 
    echo "    ACCOUNT HOME PATH"
    echo 
    echo "      Jaleo requires $J_SPACE megabytes of disk space"
    echo
    echo 'You currently have the following resources mounted locally:'
    echo 'Mounted as:                             Free Space (MB):'
    echo '========================================================'
    $DF | $NAWK '$1 != "Filesystem" && $2 != "nfs" {print sprintf("%40-s%d", $7, $5/1024)}' | grep -v CDROM
    
    echo
    #echo '      On which of the above systems do you want to'
    #echo '      place the home directory for the new user?'
    #echo
    echo "      The user name you entered above will be appended to"
    echo "      the path you enter here to make the new users home directory"
    echo
    echo "      In case you do not have a separate disk for your"
    echo "      users home directories, you probably want to"
    echo "      stay with the default location, that is '$J_BASE'"
    echo
    echo '      Installation Path ['$J_BASE']: \c' 

    set input = $<
    if ( "$input" != "" ) then
	set J_BASE = $input
    endif

#########################################################
# Check for "dangerous" directories 
#########################################################
set DANGER = "OK"
if ( "$J_BASE" == "/" ) then
    set DANGER = "rootdir"
endif
if ( "$J_BASE" == "/tmp" ) then
    set DANGER = "tmpdir"
endif
if ( "$J_BASE" == "/usr/tmp" ) then
    set DANGER = "usrtmpdir"
endif
if ( "$J_BASE" == "/var/tmp" ) then
    set DANGER = "usrtmpdir"
endif
if ( "$J_BASE" == "/etc" ) then
    set DANGER = "etcdir"
endif

if ("$DANGER" != "OK") then
    #$CLEAR
    echo
    echo $BELL
    echo *** Warning: Possibly dangerous directory choice
    echo 
    echo You selected to place the home directory in $J_BASE
    echo This directory is not recommended, because
    switch ($DANGER)
	case "rootdir":
	    echo it is the root directory
	    breaksw
	case "tmpdir":
	    echo it is the systems temporary directory
	    echo that will be cleaned out at every startup
	    breaksw
	case "usrtmpdir":
	    echo it is a system temporary directory
	    breaksw
	case "etcdir":
	    echo it is the system configuration directory
	    breaksw
	default:
	    echo --- Internal Error: Please enter another directory
	    goto getInstDir
	    breaksw
    endsw
    echo
    echo 'Enter: '
    echo '[1] to select another directory (DEFAULT)'
    echo '[q] to abort installation'
    echo
    echo 'Please enter your choice [1/x/q]: \c'
    set input = $<
    switch ($input)
    case "q":
    case "Q":
	echo
	echo You have decided to terminate the installation.
	echo
	echo Adios.
	echo
	exit 0
### WARNING XXX temporary EXIT
    case "x":
	breaksw
    case "1":
    default:
	set J_BASE = $J_DHOME
	goto getInstDir
	breaksw
    endsw    
endif

# check for absolute path, and make sure user
# is happy with her or his choice
#
if ( `echo $J_BASE | cut -c1-1` == "/" ) then    
#
# Absolute directory
#
    echo
    echo "      You have chosen '$J_BASE' - do you want to set"
    echo "      '$J_BASE/$J_USER' as the home directory for the"
    echo "      Jaleo base user account?"
    echo
    echo '      Enter:'
    echo '      [1] to proceed (DEFAULT)'
    echo '      [2] to select another directory'
    echo '      [q] to abort installation'
    echo
    echo '      Please enter your choice [1/2/q]: \c'
    set input = $<
    switch ($input)
    case "2":
	goto getInstDir
    case "q":
    case "Q":
	echo
	echo You have decided to terminate the installation.
	echo
	echo Adios.
	echo
	exit 0
    case "1":
    default:
	breaksw
    endsw
else
#
# Relative directory
# 
    #$CLEAR   
    echo
    echo $BELL
    echo *** WARNING: Relative directory choices are not recommended 
    echo 
    echo You have chosen a directory name relative to your
    echo current directory which is\: `$PWD`
    echo
    echo Please enter an absolute directory path, i.e. a path
    echo that starts with a slash '("/")' 
    echo 'Enter: ' 
    echo '[1] to select another directory (DEFAULT)'
    echo '[q] to abort installation'
    echo
    echo 'Please enter your choice [1/2/q]: \c'
    set input = $<
    switch ($input)
    case "q":
    case "Q":
	echo
	echo You have decided to terminate the installation.
	echo
	echo Adios.
	echo
	exit 0
### WARNING XXX temporary EXIT
    case "x":
	breaksw
    case "1"
    default:
	set J_BASE = $J_DHOME
	goto getInstDir
	breaksw
    endsw    
endif

#########################################################
# user has decided for an install directory
#########################################################
#
# check for base dir existence, and make it if it does not exist
#
# No more check for the pathologic "/" case required, as this
# directory is not permitted
if ( !(-e "$J_BASE/$J_USER") ) then
    echo
    echo "      Making installation base directory"
    $MKDIR -p $J_BASE/$J_USER
    echo
else
    echo
    echo $BELL
    echo '*** Installation Problem:'
    echo '    A subdirectory with the name '$J_USER' already exists'
    echo '    under '$J_BASE' '
    echo
    echo 'You must select a location where no file or directory'
    echo 'of the name '$J_USER' exists'
    echo 'Enter: '
    echo '[1] to restart user/directory choices (DEFAULT)'
    echo '[q] to quit installation'
    echo
    echo 'Please enter your choice [1/q]: \c'
    set input = $<
    switch ($input)
    case "q":
    case "Q":
	echo
	echo You have decided to terminate the installation.
	echo
	echo Adios.
	echo
	exit 0
### WARNING XXX temporary EXIT
    case "x":
	breaksw
    case 1:
    default:
	set J_BASE = $J_DHOME
	set J_USER = $J_DUSER
	goto getNewUser
    endsw
endif

#################################################################
# Check if there is sufficient space on the selected file system
#################################################################
@ size = `$DF $J_BASE | $NAWK '$1 != "Filesystem" {print $5}'`
@ size = ($size / 1024)
if ( $size < $J_SPACE ) then
    #$CLEAR
    echo
    echo $BELL
    echo '*** Installation Problem:'
    echo '    You have selected an installation location'
    echo '    that does not have enough diskspace'
    echo 
    echo '    Available space: '$size
    echo '    Approximated required space: '$J_SPACE
    echo
    echo 'You must select a location with sufficient diskspace'
    echo 'Enter: '
    echo '[1] to select another user and directory (DEFAULT)' 
    echo '[q] to quit installation'      
    echo
    echo 'Please enter your choice [1/q/x]: \c'
    set input = $<
    switch ($input)
    case "q":
    case "Q":
	echo
	echo You have decided to terminate the installation.
	echo
	echo Adios.
	echo
	exit 0
### WARNING XXX temporary EXIT
    case "x":
	breaksw
    case "1":
    default:
	set J_BASE = $J_DHOME
	set J_USER = $J_DUSER
	goto getNewUser
    endsw
endif

#####################################################
# Install manual
#####################################################
#echo
#echo "    MANUAL INSTALLATION"
#echo
#echo "	  Manual documents are available in English and Spanish"
#echo "	  and their sizes are more than 260 Mb"
#echo "    Do you want to install:"
#echo "    [1] English version"
#echo "    [2] Spanish version"
#echo "    [3] All versions"
#echo "    [4] None (DEFAULT)"
#echo "    [q] Abort installation"
#echo
#echo "Please enter your choice [1/2/3/4/q]: \c"
#set input = $<
#switch ($input)
#    case "q":
#    case "Q":
#	echo
#	echo You have decided to terminate the installation.
#	echo
#	echo Adios.
#	echo
#	exit 0
#    case "1":
#	set MANUALOPT="ENG"
#	breaksw
#    case "2":
#	set MANUALOPT="ESP"
#	breaksw	
#    case "3":
#	set MANUALOPT="ALL"
#	breaksw
#    default:
#	set MANUALOPT="NONE"
#	breaksw
#endsw	
	
#####################################################
# Create account
#####################################################
echo 
echo "    ACCOUNT PASSWORD"
echo 
makeUser:
$PASSMGMT -a -c "Jaleo $VN Base Account" -s $J_SHELL -h $J_BASE/$J_USER -g $J_GROUP $J_USER
set userstatus = $status
if ( $userstatus == 0 ) then
    echo "Please enter a password for the account:"
    $PASSWD $J_USER
    echo
    echo "      Fixing account ownership to $J_USER"
    $CHOWN $J_USER.$J_GROUP $J_BASE/$J_USER
else
#
# User Creation Error
#
    echo
    echo $BELL
    echo '*** Installation Problem:'
    echo '    User Creation Error'
    echo
    switch ($status)
    case "1": 
	echo You do not have permissions to change password file
	echo As you are logged in as root, this sounds like a rather 
	echo uncommon system configuration problem. 
	echo You may wish to try to fix the problem in another shell
	echo window, for example by checking and fixing the permissions
	echo of the file /etc/passwd, but you should not attempt this if
	echo you are not quite familiar with UNIX
	echo 
	echo If you do not know how to fix that problem, you may wish to
	echo contact your dealer or distributor for help.
	breaksw
    case "2":
    case "3":
    case "4":
	echo --- Internal Error: Please try another user name
	breaksw
    case "5":
	echo You have an inconsistent password file - probably you
	echo are using a shadow file that is not up to date. See the
	echo manpage on shadow (4) and pwconv (1M) for a possible
	echo solution
	echo 
	echo If you do not know how to fix that problem, you may wish to
	echo contact your dealer or distributor for help.
	breaksw
    case "6":
    case "7":
	echo The SGI password management utility has returned an
	echo unspecified internal error. This is very probably a 
	echo serious system configuration error.
	echo
	echo You may try another user - if this does not help try
	echo the other installation method described in the installation
	echo guide
	breaksw
    case "8":
	echo Someone else has already opened the password file with
	echo write permissions. Make sure you are not editing the file in 
	echo an editor, and that nobody is currently editing over the net.
	echo Then retry.
	breaksw
    case "9"
	echo The user already exists.
	echo You must specify a user name that does not yet exist
	breaksw
    default:
	echo The password file management system has returned an 
	echo unknown error
    endsw  	 	
    echo
    echo 'Enter:'
    echo '[1] to enter other name (DEFAULT)'
    echo '[2] to retry the same name'
    echo '[q] to quit installation'
    echo
    echo 'Please enter your choice [1/2/q]: \c'
    set input = $<
    switch ($input)
    case "q":
    case "Q":
	echo
	echo You have decided to terminate the installation.
	echo
	echo Adios.
	echo
	exit 0
### WARNING XXX temporary EXIT
    case "x":
	breaksw
    case "2":
	goto makeUser
	breaksw
    case "1":
    default:
	set J_BASE = $J_DHOME
	set J_USER = $J_DUSER
	goto getNewUser
    endsw
endif

#####################################################
# Check for License Server Installation
#####################################################
    echo
    echo
    echo
    echo "  LICENSE SERVER"
    echo
    echo "    Jaleo requires a license server to run"
    echo 
    echo "    Do you want to install the license server on this"
    echo "    machine, or do you have planned to run the license"
    echo "    server on another machine?"
    echo "    You should not install the license server on two machines"
    echo "    at the same time."
    echo "    If you do not install the license server on this machine, "
    echo "    you must install it on another machine on your network"
    echo "    before you can use Jaleo."
    echo 
    echo '    Enter: '
    echo '    [1] to install license server (DEFAULT)' 
    echo '    [2] to not install license server'
    echo '    [q] to abort installation'      
    echo
    echo '    Please enter your choice [1/2/q]: \c'
    set input = $<
    switch ($input)
    case "2":
        set license_action=2

Get_license_server:
	echo
	echo "    Which machine will supply you the license?"
	echo 
	echo '    Please enter the hostname: \c'
	set server_host = $<
	#  echo -${server_host}-
	if (  ${server_host} == "" ) then
	    echo
	    echo
	    echo "    Please supply the machine name"
	    goto Get_license_server
	endif
	set host_name = `hostname`

	# Set server_host and host_name all uppercase
	set server_host = `echo ${server_host} | tr -s '[:lower:]' '[:upper:]'`
	set host_name = `echo ${host_name} | tr -s '[:lower:]' '[:upper:]'`

	if ( ${server_host} != ${host_name} ) then
	    echo 
	    echo "    We will try now to connect to the Jaleo License Server"
	    echo "    host to verify that it is serving licenses properly."
	    echo "    It might result in some errors that have nothing to do"
	    echo "    with the Jaleo installation process."
	    echo
	    ping -c1 ${server_host} > /dev/null
	    if ( $status == 0 ) then
		echo
		echo "    The host ${server_host} does exist."
		echo
		echo "    Attempting to connect host ${server_host}...\c"
		echo
		rsh root@${server_host} "exit" > /dev/null
		if ( $status == 0 ) then
		    echo "    success\!"
		    echo
		    echo "    Let us see if there is a Jaleo License Daemon running..."
		    rsh root@${server_host} "ps -efl | grep jaleolicd | grep -v grep" > /tmp/garbage
		    grep jaleolicd /tmp/garbage > /dev/null
		    if ( $status == 0 ) then
		  	echo "    The license daemon 'jaleolicd' has been found\!"
		    else
			echo "    The license daemon 'jaleolicd' has not been found at '${server_host}'\!".
			echo "    Try again"
			goto Get_license_server
		    endif
		    rm /tmp/garbage
		else
		    echo "    failed\!"
		    echo
		    echo "    ${BELL}Oops! Jaleo License Daemon not found on ${server_host}"
		    echo
		    echo "    Please, if Jaleo does not work, check the following line"
		    echo "    in the ~/.cshrc file in your Jaleo Account."
		    echo "        setenv JALEO_ELMHOST @${server_host}"
		    echo
		    sleep 10
		endif
	    else
		echo
		echo "    ${BELL}Cannot find the machine names ${server_host} across your network."
		echo
		echo "    Please make sure that ${server_host} is accesible and try again."
		echo
		goto Get_license_server
	    endif
	else
	    ps -efl | grep jaleolicd > /dev/null
	    if ( $status == 0 ) then
	  	echo "    The license daemon 'jaleolicd' has been found\!"
	    else
		echo "    The license daemon 'jaleolicd' has not been found at '${server_host}'\!.
		echo "    Try again"
		goto Get_license_server
	    endif
	endif
	breaksw
    case "q":
    case "Q":
	echo
	echo You have decided to terminate the installation.
	echo
	echo Adios.
	echo
	exit 0
    case "1":
    default:
        set license_action=1
	breaksw
    endsw

#####################################################
# Check for STANDARD option
#####################################################
    echo
    echo
    echo
    echo "  WORK RESOLUTION"
    echo 
    echo "  To change these values after the installation,"
    echo "  refer to your installation manual."
    echo
    echo '    Enter: '
    echo '    [1] PAL      (720*576*50) (DEFAULT)' 
    echo '    [2] NTSC DF  (720*486*60)'
    echo '    [3] NTSC NDF (720*486*60)'
    echo '    [4] FILM     (2048*1536*24)'
    echo '    [5] Custom'
    echo '    [q] to abort installation'      
    echo
    echo '    Please enter your choice [1/2/3/4/5/q]: \c'
    set input = $<
    switch ($input)
    case "1":
	# PAL
        set standard_action=1
	breaksw
    case "2":
	# NTSC
        set standard_action=2
	breaksw
    case "3":
	# NTSC
        set standard_action=2
	breaksw
    case "4":
	# FILM
        set standard_action=3
	breaksw
    case "5":
	# PAL
	echo
        set standard_action=4
	echo '    Please enter IMAGESIZEX: \c'
	set imagesizex = $<
	echo '    Please enter IMAGESIZEY: \c'
	set imagesizey = $<
	echo '    Please enter FRAMESSEG:  \c'
	set framesseg = $<
	breaksw
    case "q":
    case "Q":
	echo
	echo You have decided to terminate the installation.
	echo
	echo Adios.
	echo
	exit 0
    default:
	    # I think that this may be dangerous, due to a wrote mistake of the user
	    # set the PAL option as default with comunicate to the user.
	# PAL
        set standard_action=1
	breaksw
    endsw

#####################################################
# Load Tape Content
#####################################################

echo
echo
echo
echo
echo
echo "INSTALLATION"
echo
echo "  After all these choices, finally we will load Jaleo"
echo "  You can always press '<ctrl>-c' to interrupt"
echo
echo "  EXTRACTING"
echo
sleep 4

if ( $FROM_CDROM == "FALSE" ) then
    echo $BELL
    echo Please put your Jaleo distribution tape into the tape drive
    echo Press return when the tape is ready':\c'
    set input = $<
endif

readTape:
    $CD $J_BASE/$J_USER
    echo 
    echo Changing to base directory': '`$PWD`

if ( $FROM_CDROM == "FALSE" ) then    
    $SLEEP $J_SLEEP
    
    echo Loading files': '$J_DIR from tape
    echo
else
    echo Loading files': '$J_DIR from archive file    
endif    
    
if ( $FROM_CDROM == "TRUE" ) then
  if ( $J_TAPE:e == "tgz" || $J_TAPE:e == "taz" ) then
    gzip -dc $J_TAPE | $TAR xvf - $J_DIR
  else
    $TAR xvf $J_TAPE $J_DIR
  endif
else
  $TAR xvbf 20 $J_TAPE $J_DIR
endif


if ( $status != 0 ) then
    echo
    echo $BELL
    echo '*** Installation Problem:'
    echo '    The tape/archive could not be read successfully'
    echo 
    echo '    The drive or archive you selected is: '$J_TAPE
    echo
    echo '    Please restart the installation'
    echo
    echo 'Enter:'
    echo '[1] to re-read the tape or archive'
    echo '[q] to quit installation'
    echo
    echo 'Please enter your choice [1/q]: \c'
    set input = $<
    switch ($input)
    case "q":
    case "Q":
	echo
	echo You have decided to terminate the installation.
	echo
	echo Adios.
	echo
	exit 0
    case "1":
    default:
	goto readTape
    endsw
endif

#####################################################
# Decompress manual files
#####################################################
#set INST_PATH=$J_TAPE:h
#$CD $J_BASE/$J_USER/$J_ENVDIR/Manual/Release35
#switch ($MANUALOPT)
#    case "ENG":	
#	echo "Uncompressing manual (English version)"
#	$TAR xvf $INST_PATH/$J_MANUALFILE handbook.pdf.gz
#	gunzip -f handbook.pdf.gz	
#	$TAR xvf $INST_PATH/$J_MANUALFILE install.pdf.gz
#	gunzip -f install.pdf.gz
#	$TAR xvf $INST_PATH/$J_MANUALFILE whatsnew.pdf.gz
#	gunzip -f whatsnew.pdf.gz
#	breaksw
#    case "ESP":
#	echo "Uncompressing manual (Spanish version)"
#	$TAR xvf $INST_PATH/$J_MANUALFILE manual.pdf.gz
#	gunzip -f manual.pdf.gz	
#	$TAR xvf $INST_PATH/$J_MANUALFILE instalacion.pdf.gz
#	gunzip -f instalacion.pdf.gz
#	$TAR xvf $INST_PATH/$J_MANUALFILE quesnuevo.pdf.gz
#	gunzip -f quesnuevo.pdf.gz	
#	breaksw
#    case "ALL":    
#	echo "Uncompressing all manuals"
#	$TAR xvf $INST_PATH/$J_MANUALFILE
#	gunzip -f *.gz
#	breaksw
#endsw
$CD $J_BASE/$J_USER

#####################################################
# Fix execution permissions
#####################################################
chmod ugo+x  $J_LICDIR/$J_LICSCRIPT
chmod ugo+x  $J_INSDIR/$J_INSSCRIPT

#####################################################
# Do License Server Installation
#####################################################
    switch ($license_action)
    case 2:
	rm -r $J_LICDIR
	breaksw
    case 1:
    default:
	echo 
	# echo Running License Setup
	echo
	echo
	echo
	$J_LICDIR/$J_LICSCRIPT $J_USER
	breaksw
    endsw

########################################################
# Run config script
########################################################
echo
# echo Setting up installation
echo
echo
echo
$J_INSDIR/$J_INSSCRIPT $J_USER

########################################################
# Cleanup
########################################################
echo
echo "  LAST STEPS"
echo
echo Cleaning Up...
echo
if ( -e $J_LICDIR ) then
    rm -r $J_LICDIR
endif
rm -r $J_INSDIR

#####################################################
# Fix execution permissions
#####################################################
if ( !(-e $J_HEADERDIR) ) then
    echo
    echo Creating raw header directory...
    echo
    $MKDIR -m 7777 -p $J_HEADERDIR
    chmod -R ugo+rwx $J_HEADERDIR
else
    echo
    echo Raw header directory already exists...
    echo
endif

#foreach i ( $J_BASE/$J_USER/$J_ENVDIR/$J_BIN/$J_REALTIME_APP )
#  if ( -e $i ) then
#    chmod ug+s $i
#    chown root.sys $i
#    chmod ugo-w $i
#  endif
#end

#####################################################
# Set up the license server host on '.cshrc' 
#####################################################
    switch ($license_action)
    case 2:
      echo "setenv JALEO_ELMHOST @${server_host}" >> .cshrc
      breaksw
    case 1:
    default:
      echo "setenv JALEO_ELMHOST @`hostname`" >> .cshrc
      breaksw
    endsw

#####################################################
# Do STANDARD setup
#####################################################
    switch ($standard_action)
    case 1:
	# PAL do nothing, it is the default configuration
	breaksw
    case 2:
	# NTSC
	# Change STANDARD option on .jaleorc file
	mv ${J_BASE}/${J_USER}/JALEO-ENV/.jaleorc  ${J_BASE}/${J_USER}/JALEO-ENV/.jaleorc.org
	sed 's/^[ 	]*STANDARD[ 	][ 	]*PAL[ 	]*$/STANDARD		NTSC/g' < ${J_BASE}/${J_USER}/JALEO-ENV/.jaleorc.org \
	> ${J_BASE}/${J_USER}/JALEO-ENV/.jaleorc
	rm ${J_BASE}/${J_USER}/JALEO-ENV/.jaleorc.org
	chown ${J_USER}.user ${J_BASE}/${J_USER}/JALEO-ENV/.jaleorc
	# Change the start.vst image
	cp ${J_BASE}/${J_USER}/JALEO-ENV/etc/pixmap/startupimage/jaleoLogoNTSC.vst \
	   ${J_BASE}/${J_USER}/JALEO-ENV/etc/pixmap/startupimage/start.vst
	breaksw
    case 3:
	# FILM
	# Change STANDARD option on .jaleorc file
	mv ${J_BASE}/${J_USER}/JALEO-ENV/.jaleorc  ${J_BASE}/${J_USER}/JALEO-ENV/.jaleorc.org
	sed 's/^[ 	]*STANDARD[ 	][ 	]*PAL[ 	]*$/STANDARD		FILM/g' < ${J_BASE}/${J_USER}/JALEO-ENV/.jaleorc.org \
	> ${J_BASE}/${J_USER}/JALEO-ENV/.jaleorc
	rm ${J_BASE}/${J_USER}/JALEO-ENV/.jaleorc.org
	chown ${J_USER}.user ${J_BASE}/${J_USER}/JALEO-ENV/.jaleorc
	breaksw
    case 4:
	# Custom. Get some fields of the standard PAL and override the IMAGESIZEX and IMAGESIZEY fields.
	echo
	echo "IMAGESIZEX ${imagesizex}" >> ${J_BASE}/${J_USER}/JALEO-ENV/.jaleorc
	echo "IMAGESIZEY ${imagesizey}" >> ${J_BASE}/${J_USER}/JALEO-ENV/.jaleorc
	echo "FRAMESSEG  ${framesseg}"  >> ${J_BASE}/${J_USER}/JALEO-ENV/.jaleorc
	chown ${J_USER}.user ${J_BASE}/${J_USER}/JALEO-ENV/.jaleorc
	breaksw
    default:
	echo Error detected while debugging
	exit 0
	breaksw
    endsw


#####################################################
# Do PARALLEL setup
#####################################################
# The PARALLEL option is set to '1' as default in jaleorc file to all platforms.
# The strategy is: The default in .jaleorc file is '1' but if the hinv show that the machine
# has more CPUs then set parallel to 'hinv Number'. So in O2 machines the default
# has kept.

# Find the number of CPUs via the Hardware Inventory utility

set HINV_OUTPUT = "/usr/tmp/hinv_temporary_file"
set HINV_OUTPUT_2 = "/usr/tmp/hinv_temporary_file_2"

hinv -c processor > ${HINV_OUTPUT}
grep MHZ < ${HINV_OUTPUT} | sed 's/ /%/g' > ${HINV_OUTPUT_2}

set F1 = `cat ${HINV_OUTPUT_2} | cut -d% -f1`
set F2 = `cat ${HINV_OUTPUT_2} | cut -d% -f2`
set F3 = `cat ${HINV_OUTPUT_2} | cut -d% -f3`
set F4 = `cat ${HINV_OUTPUT_2} | cut -d% -f4`
set F5 = `cat ${HINV_OUTPUT_2} | cut -d% -f5`
set F6 = `cat ${HINV_OUTPUT_2} | cut -d% -f6`

if ( $F3 != "MHZ" )  then
    echo " Warning: The format of hinv does not is known/supported"
else
    if ( $F5 != "Processor" && $F5 != "Processors" ) then
	echo " Warning: The format of hinv does not is known/supported"
    else
	if ( $F6 != "" ) then
	    echo " Warning: The format of hinv does not is known/supported"
	else
	    echo " The number of processors is: $F1"
	endif
    endif
endif

rm ${HINV_OUTPUT} ${HINV_OUTPUT_2}

# Set the jaleorc PARALLEL flag to the number of CPUs

if ( $F1 != "1" ) then
    mv ${J_BASE}/${J_USER}/JALEO-ENV/.jaleorc  ${J_BASE}/${J_USER}/JALEO-ENV/.jaleorc.org
    sed 's/^[ 	]*PARALLEL[ 	][ 	]..*$/PARALLEL		Ricardito/g' < ${J_BASE}/${J_USER}/JALEO-ENV/.jaleorc.org \
    | sed "s/Ricardito/$F1/" > ${J_BASE}/${J_USER}/JALEO-ENV/.jaleorc
    rm ${J_BASE}/${J_USER}/JALEO-ENV/.jaleorc.org
    chown ${J_USER}.user ${J_BASE}/${J_USER}/JALEO-ENV/.jaleorc
endif

#####################################################
# Add/remove comments in jaleoDocsrc
#####################################################
$CD $J_BASE/$J_USER
#$NAWK -v OPTION=$MANUALOPT ' { stag="#"; etag="#"; \
#    if ((OPTION=="ESP")||(OPTION=="ALL")) { stag=""; } \
#    if ((OPTION=="ENG")||(OPTION=="ALL")) { etag=""; } \
#    if ((match($0,"handbook.pdf"))||(match($0,"install.pdf"))||(match($0,"whatsnew.pdf")))  \
#    { $0=sprintf("%s %s", etag, $0); } \
#    else if ((match($0,"manual.pdf"))||(match($0,"instalacion.pdf"))||(match($0,"quesnuevo.pdf"))) \
#    { $0=sprintf("%s %s", stag, $0); } \
#    print $0; \
#    }' \
#    jaleoDocsrc > jaleoDocsrc.new
#mv jaleoDocsrc.new jaleoDocsrc
#rm jaleoDocsrc.new



#####################################################
# Do WACOM setup
#####################################################
    cp ${J_BASE}/${J_USER}/JALEO-ENV/extern/wacom/wacom /usr/lib/X11/input/config/wacom
    chmod 600 /usr/lib/X11/input/config/wacom


#
# Make sure we exit...
#
echo $BELL $BELL
echo Installation has finished successfully
echo
echo Please, follow the necessary steps in the configuration
echo manual to ensure a proper functioning of the Software.
echo
echo After you have configured Jaleo, you can install the
echo Jaleo tutorial data as follows:
echo '- Log in into your Jaleo user account'
echo '- From the toolchest menu Jaleo select the Utils submenu'
echo '- In the Utils submenu, select Install Tutorial'
echo
echo
echo
echo "Installation Completed\!"
echo
echo
exit


#####################################################
# END OF INSTALLATION
#####################################################

handleInterupt:
#
# This is called after the user pressed control-c
#

echo
echo
echo 'You have interrupted the installation. You may have'
echo 'loaded files to disk partially or completely.'
echo 'We will leave the installed files where they are, '
echo 'in case this is what you wanted'
echo
echo 'You may want to clean out the directory'
echo $J_BASE/$J_USER
echo 'where the installed files are stored'
echo
echo 'Adios.'
echo
exit 0
