#!/bin/csh -f
#
#
# Setup script for
#
# Jaleo
#
# The Digital Video Postproduction System 
#
# Brought To You Directly From The Canary Islands
#
# Part III: Licensing
set VERSION = "Jaleo 3.5 Licensing Script"
echo
echo ==============================================
echo $VERSION
echo ==============================================
echo


###########################################################
# Variables and other settings
###########################################################
set J_DEBUG = "FALSE"

set J_LICENSE = "LICENSE"

set J_LICDIR = "/usr/lib/elm/jaleo"
set J_LICDAEMON = "jaleolicd"
set J_LICVER = "jlicver"
set J_DAEMONDIR = "daemon"
set J_BINDIR = "bin"

set J_SELMD = "Selmd"
set J_STARTUP = "jaleolic"
set J_CHKCONFIG = "/etc/config"
set J_INIT = "/etc/init.d"
set J_BOOT = "/etc/rc2.d"
set J_BOOTHEAD = "S98"
set J_SHUTDOWN = "/etc/rc0.d"
set J_SHUTHEAD = "K98"

set J_ETC_FILESET = "/etc/config/jaleolic /etc/init.d/jaleolic /etc/rc2.d/S98jaleolic /etc/rc0.d/K98jaleolic"

set EXP = "XXXJaleoXXX"
set J_ADDLIC_REP = "JALEO-ENV/extern/license"
set J_ADDLIC_TARGET = "JALEO-ENV/extern/license/addLicense"
set J_ADDLIC_PURETARGET = "bin/addLicense"
set J_ADDLIC_TEMPLATE = "addLicense.template"

set BELL = "\007"

#
# 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 CP	    = "cp"
set HOSTNAME = "hostname"
set MAKE    = "make"
set CHMOD   = "chmod"
set CHOWN   = "chown"
set RM = "rm"
set RGB = "rgb"
set LN = "ln"
set MV = "mv"

#
#  Set a handler to be called on interrupt
#
onintr handleInterupt

###########################################################
# Check Arguments
###########################################################
if ( $#argv == 0 ) then
    set J_LICINSTMODE = "lserver"
    set J_USER = "root"
    set J_GROUP = "sys"
else
    set J_LICINSTMODE = "jaleo"
    set J_USER = $1
    set J_GROUP = "user"
endif

###########################################################
# Check directory
###########################################################
if ( ! -d $J_LICENSE ) then
    echo 
    echo $BELL
    echo '*** Error: Can not find directory '$J_LICENSE
    echo
    echo You propably try to run this script from the wrong directory
    echo Run the install script from the future home directory or the
    echo install directory for your license tools in case this is a
    echo pure license server installation without Jaleo. 
    echo Do NOT run this script inside the $J_LICENSE directory
    echo
    exit 0
endif

set BASE = `pwd`


###########################################################
# Previous installed server detection 
###########################################################

set InstallServer = "FALSE"
echo
echo "  Checking installed servers..."
if ( ( -d $J_LICDIR ) && ( -e $J_LICDIR/$J_LICDAEMON ) ) then
    set J_ACTIVELIC = `$J_LICENSE/$J_BINDIR/$J_LICVER $J_LICDIR/$J_LICDAEMON | grep Elan | sed "s/^.*Elan/Elan/" | sed "s/ /_/g" | sed "s/\./_/g"`
    set J_CURLIC =    `$J_LICENSE/$J_BINDIR/$J_LICVER $J_LICENSE/$J_DAEMONDIR/$J_LICDAEMON | grep Elan | sed "s/^.*Elan/Elan/" | sed "s/ /_/g" | sed "s/\./_/g"` 
    echo "$J_ACTIVELIC license server found in your system"
    if ( $J_ACTIVELIC == $J_CURLIC ) then	
	echo "Do you want to reinstall it?[y/n]"
	set input = $<
	if ( "$input" == "y" || "$input" == "Y" ) then
	    set InstallServer = "TRUE"
	endif
    else
	if ( $J_DEBUG == "TRUE" ) then
	    echo $MV $J_LICDIR $J_LICDIR/../jaleo_INACTIVE_$J_ACTIVELIC
	else
	    echo "Making old server inactive"
	    $MV $J_LICDIR $J_LICDIR/../jaleo_INACTIVE_$J_ACTIVELIC 
	endif
	set InstallServer = "TRUE"
    endif
else
    set InstallServer = "TRUE"    
endif  

if ( $InstallServer == "FALSE" ) goto SkipInstall


###########################################################
# Create a license directory
###########################################################

echo
echo "  Creating license directory structure..."
#XXXecho '\t'======================================
if ( $J_DEBUG == "TRUE" ) then
    if ( ! -e $J_LICDIR ) then
	echo $MKDIR -p $J_LICDIR
    endif
    echo $CP $J_LICENSE/$J_DAEMONDIR/$J_LICDAEMON $J_LICDIR
    echo $CHOWN $J_USER.$J_GROUP $J_LICDIR
    echo $CHMOD 775 $J_LICDIR            
else
    if ( ! -e $J_LICDIR ) then
	$MKDIR -p $J_LICDIR
    endif
    $CP $J_LICENSE/$J_DAEMONDIR/$J_LICDAEMON $J_LICDIR
    $CHOWN $J_USER.$J_GROUP $J_LICDIR
    $CHMOD 775 $J_LICDIR
endif

###########################################################
# Create automatic startup files
###########################################################
echo
echo "  Creating jaleolicd boot files..."
#XXXecho '\t'=================================
if ( $J_DEBUG == "TRUE" ) then
    echo $CP $J_LICENSE/$J_SELMD $J_INIT/$J_STARTUP
    echo $LN -s $J_INIT/$J_STARTUP $J_BOOT/$J_BOOTHEAD$J_STARTUP
    echo $LN -s $J_INIT/$J_STARTUP $J_SHUTDOWN/$J_SHUTHEAD$J_STARTUP
    echo 'echo on > '$J_CHKCONFIG/$J_STARTUP
    echo $CHOWN root.sys $J_ETC_FILESET
    echo $CHMOD 744 $J_ETC_FILESET
else
    $CP $J_LICENSE/$J_SELMD $J_INIT/$J_STARTUP
    $CHMOD ugo+x $J_INIT/$J_STARTUP
    $LN -s $J_INIT/$J_STARTUP $J_BOOT/$J_BOOTHEAD$J_STARTUP
    $LN -s $J_INIT/$J_STARTUP $J_SHUTDOWN/$J_SHUTHEAD$J_STARTUP
    echo on > $J_CHKCONFIG/$J_STARTUP
    $CHOWN root.sys $J_ETC_FILESET
    $CHMOD 744 $J_ETC_FILESET
endif

###########################################################
# Startup daemon
###########################################################
echo
echo "  Starting daemon..."
#XXXecho '\t'===================
if ( $J_DEBUG == "TRUE" ) then
    echo $J_INIT/$J_STARTUP start
else
    $J_INIT/$J_STARTUP start
endif

#XXXecho 
#XXXecho $BASE
#XXXecho
###########################################################
# Creating customized addLicense tool
###########################################################
$CD $BASE/$J_LICENSE
if ( $J_DEBUG == "TRUE" ) then
    if ( $J_LICINSTMODE == "lserver" ) then
	echo $J_ADDLIC_PURETARGET  
	$NAWK -v ext=$EXP -v rep=$BASE/$J_BINDIR '{sub(ext, rep); print $0}' $J_ADDLIC_TEMPLATE
    else
	echo $BASE/$J_ADDLIC_TARGET
	$NAWK -v ext=$EXP -v rep=$BASE/$J_ADDLIC_REP '{sub(ext, rep); print $0}' $J_ADDLIC_TEMPLATE
    endif
else
    if ( $J_LICINSTMODE == "lserver" ) then    
	$NAWK -v ext=$EXP -v rep=$BASE/$J_BINDIR '{sub(ext, rep); print $0}' $J_ADDLIC_TEMPLATE > $J_ADDLIC_PURETARGET
	$CHMOD ugo+x $J_ADDLIC_PURETARGET
    else
	$NAWK -v ext=$EXP -v rep=$BASE/$J_ADDLIC_REP '{sub(ext, rep); print $0}' $J_ADDLIC_TEMPLATE > $BASE/$J_ADDLIC_TARGET
	$CHMOD ugo+x $BASE/$J_ADDLIC_TARGET
    endif
endif

###########################################################
# Relocating license server files
###########################################################
# XXX      Is supported/used this option yet?
if ( $J_LICINSTMODE == "lserver" ) then
    echo
    echo '\t'Relocating admin tools...
    echo '\t'=========================
    if ( $J_DEBUG == "TRUE" ) then
	echo $MV $BASE/$J_LICENSE/$J_BINDIR $BASE
	echo $MV $BASE/$J_LICENSE/$J_DAEMONDIR $BASE
    else
	$MV $BASE/$J_LICENSE/$J_BINDIR $BASE
	$MV $BASE/$J_LICENSE/$J_DAEMONDIR $BASE
    endif

###########################################################
# User Warning
########################################################### 
    echo 
    echo $BELL
    echo Recommendation': '
    echo ===============
    echo 
    echo If this is a pure server installation \(i.e. without
    echo Jaleo on the same machine\) you may wish to add the
    echo license administration tools directory to the search
    echo path of the root user or any other user account you
    echo will be using for license administration.
    echo The administration commands are in $BASE/$J_BINDIR
    echo
    echo In normal configurations, you can add the files to your
    echo search path by adding a line like
    echo 'set path = ($path '$BASE/$J_BINDIR')'
    echo to your .cshrc file in the account home directory
endif

###########################################################
# User Warning
########################################################### 
echo 
echo   $BELL
echo "  === Reminder ==="
#XXXecho =========
echo 
echo "    Before you can run Jaleo you must add a license. Run"
echo "    the script addLicense to add licenses to your system."
echo

if ( $J_LICINSTMODE != "lserver" ) then
    echo "    To run the script, after finishing the"
    echo "    installation, type:"
    echo "  \t/usr/people/$J_USER/JALEO-ENV/extern/license/addLicense"
    echo "    or log out and log in as $J_USER, "
    echo "    open a shell window and type "
    echo "  \taddLicense"
else
    echo "    To run the script, type "
    echo "  \tbin/addLicense"
    echo "    later from the directory $BASE"
endif


SkipInstall:

echo
echo "  Press <enter> to continue:\c"
set input = $<

#
# Make sure we exit properly
#
exit 0

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

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