#!/bin/csh -f
#
# $Id: switch_server,v 1.5 2001/01/22 11:54:05 ulises Exp cm $
#
# Copyright (C) Comunicacion Integral, Las Palmas de Gran Canaria, 1997
#
# $Log: switch_server,v $
# Revision 1.5  2001/01/22 11:54:05  ulises
# Fixed local directory bug (leg insertion :-( )
#
# Revision 1.4  2000/12/13  14:16:11  ulises
# Adjustments to distinguish between different versions with the same licenser
#
# Revision 1.3  1999/10/01  11:56:59  david
# Redesign.
# The script shows always options, it never works automatically.
#
# Revision 1.2  1999/09/30  12:42:15  david
# First checked beta version
#
# Revision 1.1  1999/09/30  12:31:48  cm
# Initial revision
#
#
#
set VERSION="0.5"
set ELMDIR="/usr/lib/elm"

clear
if ( "$1" == "-h" ) then
    echo 
    echo "NAME"
    echo "        `basename $0`   Jaleo Switch license server Tool $VERSION"
    echo 
    echo "SYNOPSIS"
    echo "\t`basename $0` [-h]"
    echo 
    echo "DESCRIPTION"
    echo "\tThis script allow to the user to backup the active license server,"
    echo "\tin order to install another license server version. This script"
    echo "\talso, could be used for switching between the current license"
    echo "\tserver and another one, to be selected from a the list of existing."
    echo "\tTherefore the machine have to keep a license server under"
    echo "\t$ELMDIR/jaleo/ before running this script."
    echo 
    echo "\tThis utility must be executed from a jaleo account going into superuser."
    echo "\tPlease, login into a Jaleo account and issue a 'su root' command."
    echo
    echo "\t   login <jaleo-account>"
    echo "\t   su"
    echo
    echo "OPTIONS"
    echo "\t[-h]   Prints this help message and quits."
    echo
    exit 0
endif

echo
echo "Jaleo Switch license server Tool $VERSION"
echo "------------------------------------"
echo

if ( ! -d "$HOME/JALEO-ENV" ) then
    echo
    echo " Please execute this utility inside a Jaleo account."
    echo " Login as the Jaleo User and issue a 'su root' command."
    echo " Type '`basename $0` -h' for more detailed help."
    echo
    echo
    echo "Exiting\n"
    exit 1
endif
which jlicver > /dev/null
if ( $status != 0 ) then
    echo
    #echo "\nERROR: jlicver is not accessible."
    echo " Please execute this utility inside a Jaleo account."
    echo " Login as the Jaleo User and issue a 'su root' command."
    echo " Type '`basename $0` -h' for more detailed help."
    echo
    echo
    echo "Exiting\n"
    exit 1
endif
# * jlicver is accessible

if ( `whoami` != "root" ) then
    echo
    echo " Please execute this utility as Super-User."
    echo " Login as the Jaleo User and issue a 'su root' command."
    echo " Type '`basename $0` -h' for more detailed help."
    echo
    echo
    echo "Exiting\n"
    exit 1
endif

# * Maybe There is a /usr/lib/elm/jaleo/ directory or not, therefore
#   each group on sentences have to go with the correct 'if', or
#   without any to execute at any situation.

# Due to is impossible extract only the number of version the directory
# will keep all the patter 'Elan License Manager [release] $version'
	# Elan License Manager release 4.1.3e
	# Elan License Manager 5.0.1d
	# Elan License Manager 5.1.1

if ( -d $ELMDIR/jaleo ) then
    if ( ! -e $ELMDIR/jaleo/jaleolicd ) then
	echo
	echo "  Warning: $ELMDIR/jaleo/ is corrupted. Deleting"
	echo
	rm -rf $ELMDIR/jaleo
    endif
    # Get the formated version of jaleolicd on jaleo/ directory
    set JaleoJaleolicd=`jlicver $ELMDIR/jaleo/jaleolicd | grep Elan | sed "s/^.*Elan/Elan/" | sed "s/ /_/g" | sed "s/\./_/g"`
    set LicServerName=`jlicver $ELMDIR/jaleo/jaleolicd | grep Elan | sed "s/^.*Elan/Elan/"`
    if ( ${JaleoJaleolicd} == "Elan_License_Manager_5_1_1" ) then
        if ( ! -e $ELMDIR/jaleo/jserver ) then
	    echo 30 > $ELMDIR/jaleo/jserver
	endif
	set JaleoJaleolicd = ${JaleoJaleolicd}_`cat $ELMDIR/jaleo/jserver`
	set LicServerName = "${LicServerName} for Jaleo `cat $ELMDIR/jaleo/jserver`"
    endif
endif

# Check if there is a INACTIVE directory which keep the same version of /usr/lib/elm/jaleo/
# Check by the name of the directory
# Build a list of the INACTIVE directories and its versions and check againts /usr/lib/elm/jaleo/

iniciando:
# Check that all jaleo_INACTIVE_* keep the jaleolicd file
set ListsJaleolicdDirs=`ls $ELMDIR |& grep jaleo_INACTIVE_ | sed "s/://"`
foreach directory ( ${ListsJaleolicdDirs} )
    if ( ! -e $ELMDIR/$directory/jaleolicd ) then
	echo
	echo "  Warning: $ELMDIR/$directory is corrupted. Deleting"
	echo
	rm -rf $ELMDIR/$directory
	goto iniciando
    endif
    # Rename old server names to 3.0
    if ( $directory == "jaleo_INACTIVE_Elan_License_Manager_5_1_1" ) then
	if ( ! -e $ELMDIR/$directory/jserver ) then
	    echo 30 > $ELMDIR/$directory/jserver
	endif
	mv $ELMDIR/$directory $ELMDIR/${directory}_`cat $ELMDIR/$directory/jserver`
	goto iniciando
    endif
end
if ( -d $ELMDIR/jaleo ) then
    foreach directory ( ${ListsJaleolicdDirs} )
	if ( $ELMDIR/jaleo_INACTIVE_${JaleoJaleolicd} == $ELMDIR/$directory ) then
	    echo
	    echo "  Warning: jaleo_INACTIVE_${JaleoJaleolicd} is duplicated on $ELMDIR/jaleo/. Moving, (on coincidence, overwriting)"
	    echo
	    mv $ELMDIR/$directory $ELMDIR/jaleo_DUPLICATED_${JaleoJaleolicd}
	    goto iniciando
	endif
    end
    echo
    echo "  The current active license server version in $ELMDIR/jaleo/ is: \n\n\t\t ${LicServerName} version"
    echo
endif

if ( ! -d $ELMDIR/jaleo ) then
    echo
    echo "  There is not current active license server, the $ELMDIR/jaleo/ directory does not exits."
    echo
endif

# If the file /usr/tmp/ exists then delete it
if ( -e /usr/tmp/ListJaleolicdDirs ) then
    rm -rf /usr/tmp/ListJaleolicdDirs
endif
echo ${ListsJaleolicdDirs} > /usr/tmp/ListJaleolicdDirs
set processed=`sed "s/ /_/g" < /usr/tmp/ListJaleolicdDirs`
if ( $processed == "" ) then
    if ( ! -d $ELMDIR/jaleo ) then
	echo
	echo " The machine have to hold a license server under $ELMDIR/jaleo/"
	echo " before running this script. Install a Jaleo product."
	echo " Type '`basename $0` -h' for more detailed help."
	echo
	echo
	echo "Exiting\n"
	exit 1
    endif
endif


# If the list of INACTIVE directories is not empty => offer the list to switch or make a hole on jaleo/ directory
echo
echo "  Options:"
echo
# Note: Limited by the number of elements on the list
set x = ( 1 2 3 4 5 6 7 8 9 )
set Options=""
foreach directory ( ${ListsJaleolicdDirs} )
	set LicDirName=`jlicver $ELMDIR/$directory/jaleolicd | grep Elan | sed "s/^.*Elan/Elan/"`
	if ( -e $ELMDIR/$directory/jserver )  set LicDirName="${LicDirName} for Jaleo `cat $ELMDIR/$directory/jserver`"    	
	echo "\t[${x[1]}] Make active: $LicDirName"
	set Options=${Options}${x[1]}"/"
	shift x
end
if ( -d $ELMDIR/jaleo ) then    
    echo "\t[b] Make inactive: ${LicServerName} (prepare for a new license server installation) "
endif
echo "\t[q] Quit\n"

repetir:
echo
if ( -d $ELMDIR/jaleo ) then
    echo "  Please enter your choice [${Options}b/q]: \c"
else
    echo "  Please enter your choice [${Options}q]: \c"
endif
# First validation of the option selected
set input = $<
switch ($input)
	case "9":
	case "8":
	case "7":
	case "6":
	case "5":
	case "4":
	case "3":
	case "2":
	case "1":
	if ( $input >= ${x[1]} ) then
	    echo
	    echo
	    echo "    The selected option is invalid. Please select one again."
	    goto repetir
	endif
endsw
# Second validation of the option selected and execution of the operation
switch ($input)
    case "9":
	    shift ListsJaleolicdDirs
    case "8":
	    shift ListsJaleolicdDirs
    case "7":
	    shift ListsJaleolicdDirs
    case "6":
	    shift ListsJaleolicdDirs
    case "5":
	    shift ListsJaleolicdDirs
    case "4":
	    shift ListsJaleolicdDirs
    case "3":
	    shift ListsJaleolicdDirs
    case "2":
	    shift ListsJaleolicdDirs
    case "1":
	if ( -d $ELMDIR/jaleo ) then
	    # Switch. Note that the duplication has been deleted at building ${ListsJaleolicdDirs}
	    /etc/init.d/jaleolic stop >& /dev/null

	    mv $ELMDIR/jaleo                     $ELMDIR/jaleo_INACTIVE_${JaleoJaleolicd}
	    mv $ELMDIR/${ListsJaleolicdDirs[1]}  $ELMDIR/jaleo
	    /etc/init.d/jaleolic start # >& /dev/null
	    # The switch will be ever successfully

	    # Get the formated version of jaleolicd on jaleo/ directory
	    set JaleoJaleolicd=`jlicver $ELMDIR/jaleo/jaleolicd | grep Elan | sed "s/^.*Elan/Elan/"`
	    if ( -e $ELMDIR/jaleo/jserver ) set JaleoJaleolicd="${JaleoJaleolicd} for Jaleo `cat $ELMDIR/jaleo/jserver`"
	    echo
	    echo
	    echo "  Switched successfully. Now the current active license server version in $ELMDIR/jaleo/ is: \n\n\t\t ${JaleoJaleolicd} version"
	    # If the list of INACTIVE directories is not empty => offer the list to switch or make a hole on jaleo/ directory
	    echo
	    echo
	    echo "Exiting\n"
	    exit 0
	else
	    # Activating. Note that the duplication has been deleted at building ${ListsJaleolicdDirs}
	    /etc/init.d/jaleolic stop >& /dev/null

	    mv $ELMDIR/${ListsJaleolicdDirs[1]}  $ELMDIR/jaleo
	    /etc/init.d/jaleolic start # >& /dev/null
	    # The activation will be ever successfully

	    # Get the formated version of jaleolicd on jaleo/ directory
	    set JaleoJaleolicd=`jlicver $ELMDIR/jaleo/jaleolicd | grep Elan | sed "s/^.*Elan/Elan/"`
	    if ( -e $ELMDIR/jaleo/jserver ) set JaleoJaleolicd="${JaleoJaleolicd} for Jaleo `cat $ELMDIR/jaleo/jserver`"
	    echo
	    echo
	    echo "  Activation successfully. Now the current active license server version in $ELMDIR/jaleo/ is: \n\n\t\t ${JaleoJaleolicd} version"
	    # If the list of INACTIVE directories is not empty => offer the list to switch or make a hole on jaleo/ directory
	    echo
	    echo
	    echo "Exiting\n"
	    exit 0
	endif
	breaksw
    case "b":
    case "B":
	if ( -d $ELMDIR/jaleo ) then
	    echo
	    echo
	    echo "    Making inactive the current license server ..."
	    echo
	    /etc/init.d/jaleolic stop >& /dev/null
	    mv $ELMDIR/jaleo $ELMDIR/jaleo_INACTIVE_${JaleoJaleolicd}
	    echo "    Now you can install another product and its licenses."
	    echo
	    echo
	    echo "Exiting\n"
	    exit 0
	    breaksw
	else
	    echo
	    echo
	    echo "    The selected option is invalid. Please select again."
	    goto repetir
	endif
    case "q":
    case "Q":
	    echo
	    echo
	    echo "Exiting\n"
	    exit 0
	    breaksw
    default:
	    echo
	    echo
	    echo "    The selected option is invalid. Please select again."
	    goto repetir
	    breaksw
endsw
echo
echo
echo This message should not ever show up
echo
echo
