#!/bin/csh -f
#
# $Id: install_tutorial,v 1.1 1997/08/07 22:46:11 cm Exp $
#
# Copyright (C) Comunicacion Integral, Las Palmas de Gran Canaria, 1997
#
#
# Installation script for
#
# Jaleo Tutorial Data
#
# Brought To You Directly From The Canary Islands
#
#
set VERSION = "Jaleo 2.7 Installation Script, 970805"
set CLEAR   = "clear"

$CLEAR
echo '==============================================='
echo '==                                           =='
echo '== JALEO Digital Video Postproduction System =='
echo '==                                           =='
echo '== Tutorial Data Installation Script         =='
echo '==                                           =='
echo '==============================================='
echo
echo $VERSION
echo
echo 'This script will install Jaleo Tutorial Data' 
echo 'in the current Work directory. If you have not'
echo 'yet set up your Jaleo system, you may wish to'
echo 'run this script at a later point in time'
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
endif

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

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

set J_TAPE = "/dev/nrtape"    # the default tape drive name
set J_DEFTAPE = "/dev/nrtape" # the default tape drive 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_HOME=$HOME
if ("$J_HOME" == '/') then
    set J_HOME=""
endif

set J_BASE = "$J_HOME/WORK"  # default location for WORK directory
set J_DHOME = "$J_HOME/WORK" # default location for home directory, backup

set J_DIR = "Jaleo25Tutorial"	# default installation fileset

#
# Values
#

set J_SPACE = "400"	    # MB required for installation
set J_SLEEP = "4"	    # Sleep time to guarantee tape load 
set BELL = "\007"	    # Character that rings a bell

#
# 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 a handler to be called on interrupt
#
onintr handleInterupt

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

# No prerequisites to install tutorial

#####################################################
# 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
	echo $BELL
	echo 'The tape drive you specify MUST BE a non-rewind tape'
	echo 'device; otherwise the installation will fail.'
	echo
	getTapeChoice:
	    echo 'Enter: '
	    echo '[l] to install from a local tape drive (DEFAULT), ' 
	    echo '[r] to install from a remote tape drive and'
	    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 Make sure you use a non-rewind driver, for
	echo example /dev/nrtape or /dev/mt/tps0d4nr.
	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

#####################################################
# Tape drive exists and is responding. Check for nr
#####################################################
if (`echo $J_TAPE:r | grep -c nr` == 0) then
    echo 
    echo $BELL
    echo Your tape selection looks as if this drive
    echo was not a non-rewind tape. Are you sure this
    echo drive is a non-rewind device'?'
    echo
    echo 'Enter:'
    echo '[1] to select another tape drive (DEFAULT),' 
    echo '[2] to continue,' 
    echo '[q] to quit installation'      
    echo
    echo 'Please enter your choice [1/2/q]: \c'
    set input = $<
    switch ($input)
    case 2:
	breaksw
    case "q":
    case "Q":
	echo
	echo You have decided to terminate the installation.
	echo
	echo Adios.
	echo
	exit 0
    case "1":
    default:
	#$CLEAR
	set J_TAPE = $J_DEFTAPE
	goto getTapeDrive
    endsw   
endif

#####################################################
# Check if we are inside a Jaleo installation directory
#####################################################

echo
echo Searching for Jaleo installation directory
echo 

if (-e $HOME/JALEO-ENV && -r $HOME/JALEO-ENV/JALEO_WORK) then
    J_BASE = `cat $HOME/JALEO-ENV/JALEO_WORK`
    echo Installation directory found: $J_BASE
else
    echo No Installation directory found. Defaulting to $J_BASE
endif


#####################################################
# Display diskspace and get Install directory
#####################################################
getInstDir:
    echo 
    echo The Jaleo tutorial 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)}'
    
    echo
    echo 'You also have the following NETWORK resources (performance'
    echo 'with network based data may be poor):'
    echo
    echo 'Mounted as:                             Free Space (MB):'
    echo '========================================================'
    $DF | $NAWK '$1 != "Filesystem" && $2 == "nfs" {print sprintf("%40-s%d", $7, $5/1024)}'

    echo
    echo 'On which of the above filing systems do you want to'
    echo 'place the tutorial?'
    echo 'Note that the tutorial will only be directly accessible'
    echo 'to Jaleo if it is placed inside the WORK directory set up' 
    echo 'in JALEO-ENV/JALEO_WORK. Otherwise, you have to change'
    echo 'the settings in this setup file.'
    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 tutorial 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

#########################################################
# Make sure the user is happy with her/his choice 
#########################################################
echo
echo You have chosen \"$J_BASE\" - do you want to set
echo \"$J_BASE\" as directory for the Jaleotutorial'?'
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  

#########################################################
# 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") ) then
    echo
    echo Making installation base directory
    $MKDIR -p $J_BASE
    echo
endif

if (!(-e $J_BASE)) then
    echo
    echo $BELL
    echo '*** Installation Problem:'
    echo '    The installation directory could not be made'
    echo 
    goto getInstDir
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 directory (DEFAULT) and' 
    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
	goto getInstDir
    endsw
endif

#########################################################
# Load Tutorial
#########################################################


echo
echo After all these choices, finally we will load 
echo the tutorial data from the tape
echo
echo You can always press '<ctrl>-c' to interrupt
echo
echo $BELL
echo Please put your Jaleo distribution tape into the tape drive
echo Press return when the tape is ready':\c'
set input = $<

readTape:
    $CD $J_BASE
    echo 
    echo Changing to base directory': '`$PWD`
    
    $SLEEP $J_SLEEP
    
    echo Loading files': '$J_DIR from tape
    echo
    $MT -t $J_TAPE rewind
    $MT -t $J_TAPE rewind
    $MT -t $J_TAPE fsf 1 
    $TAR xvbf 20 $J_TAPE $J_DIR

if ( $status != 0 ) then
    echo
    echo $BELL
    echo '*** Installation Problem:'
    echo '    The tape could not be read successfully'
    echo 
    echo '    The drive you selected is: '$J_TAPE
    echo
    echo '    Please restart the installation'
    echo
    echo 'Enter:'
    echo '[1] to re-read the tape' 
    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

#####################################################
# Fix execution permissions
#####################################################
chmod ugo+rwx  $J_BASE/Jaleo25Tutorial

echo
if (-e $HOME/JALEO-ENV && -w $HOME/JALEO-ENV/JALEO_PRJ) then

    if (`cat $HOME/JALEO-ENV/JALEO_PRJ | grep -c Jaleo25Tutorial` != 0) then
	echo Project JaleoTutorial already in the project list.
    else 
	echo Adding the tutorial project to the Jaleo project list
	echo Jaleo25Tutorial >> $HOME/JALEO-ENV/JALEO_PRJ
	echo Current project list:
	cat $HOME/JALEO-ENV/JALEO_PRJ
    endif
    echo
    echo To use the tutorial, start the project manager
    echo and select the project Jaleo25Tutorial
else
    echo No Installation directory found or permission denied.
    echo To use the tutorial, you must add it
    echo manually to the Project manager, and you
    echo must make sure that the JALEO_WORK
    echo directory is set properly.
    echo 
endif

echo 
echo Tutorial data is installed in $J_BASE.
echo 

exit 0
#####################################################
# 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
echo 'where the installed files are stored'
echo
echo 'Adios.'
echo
exit 0
