#!/bin/sh
#
# request script for Cisco Network Registrar (nwreg2)
#
# Copyright (C) 2002-2007 by Cisco Systems, Inc.  This program contains
# proprietary and confidential information.  All rights reserved except as
# may be permitted by prior written consent.

# To enable debugging of this script, uncomment the next line.
# set -vx

# Capture SIGINT and SIGTERM and exit without error
trap 'exit 3' 2 15

ROOTDIR="/"

DEBUG_FILE=/tmp/cnr_install_dtrace
VALIDATE_FILE=/tmp/cnr_skip_validate
       
# Check for existence of cookie file to enable additional debug logging
if [ -f $DEBUG_FILE ]; then
    echo ""
    echo "Detected presence of $DEBUG_FILE, additional log information will"
    echo "be displayed during install/upgrade."
    echo ""
    INSTALL_DEBUG=y
fi

# Check for existence of cookie file to skip disk space and OS validation
if [ -f $VALIDATE_FILE ]; then
    echo ""
    echo "Detected presence of $VALIDATE_FILE, no disk space requirements"
    echo "checks or operating system version compatibility verification will"
    echo "be performed during the install/upgrade."
    echo ""
    SKIP_VALIDATION=y
fi

if [ "$SKIP_VALIDATION" != "y" ]; then
    # Obtain the operating system version major and minor numbers
    OS=`uname -r`
    MAJOR=`/usr/bin/expr "$OS" : '\([0-9]*\)'`
    MINOR=`/usr/bin/expr "$OS" : '[0-9]*\.\([0-9]*\)'`

    # Fail the installation if the system is running less than Solaris 2.8
    if [ $MAJOR -lt 6 -a $MINOR -lt 8 ]; then
	echo "Network Registrar requires a version of Solaris greater than or equal to"
	echo "Solaris 2.8.  It appears the version of Solaris on this system does"
	echo "not meet this requirement."
	echo ""
	exit 3
    fi
fi


# Must clear out any values that are associated with the nwreg2 package
# registered with pkgadd.  This may throw off the default values to be used
# if Network Registrar is being installed in a different mode
# (local vs. regional)
DATADIR=""
JAVADIR=""
LOGDIR=""
TEMPDIR=""
CCM_PORT=""
WEBUI_PORT=""
WEBUI_SEC_PORT=""
CNR_CCM_TYPE="cnr"

# Prompt for the cnr mode - regional or local

valid_resp=0
one=1
while [ $valid_resp -eq 0 ]; do
    echo ""
    echo "Specify the mode for this Network Registrar installation:"
    echo ""
    echo "\t1.  Local mode (default)"
    echo "\t2.  Regional mode"
    echo ""
    echo "Select the Network Registrar mode [1,2]: \c"
    read RESPONSE
    SELECTION=${RESPONSE:=$one}

    # The following validation will only allow the integral
    # response of 1 or 2.
    case $SELECTION in
    1)
	CNR_CCM_MODE=local
	valid_resp=1
	;;

    2)
	CNR_CCM_MODE=regional
	valid_resp=1
	;;

    *)
	echo ""
	echo "Invalid selection. Select a choice from the list provided."
    esac
done

# Set some default variables, which may get overridden by the contents of
# the cnr.conf file, or the pkgparam values (only when upgrading from pre-6.1
# versions of CNR and running in local mode)
if [ "$CNR_CCM_MODE" = "regional" ]; then
    RCFILE=/etc/init.d/nwregregion
    ROOTDIR=/opt/nwreg2/regional
    BACKUPDIR=/opt/nwreg2/regional.sav
    CCM_PORT=1244
    SECURE_WEBUI_PORT=8453
    UNSECURE_WEBUI_PORT=8090
    product="Network Registrar Regional Server Agent"
fi

if [ "$CNR_CCM_MODE" = "local" ]; then
    RCFILE=/etc/init.d/nwreglocal
    ROOTDIR=/opt/nwreg2/local
    BACKUPDIR=/opt/nwreg2/local.sav
    CCM_PORT=1234
    SECURE_WEBUI_PORT=8443
    UNSECURE_WEBUI_PORT=8080
    product="Network Registrar Local Server Agent"
fi

if [ -f $RCFILE ]; then
    # Obtain the installation location from the rc file
    ROOTDIR=`/bin/grep INSTALLPATH= $RCFILE | /bin/sed -e '/INSTALLPATH=/s///'`
    CONFFILE=$ROOTDIR/conf/cnr.conf
    if [ -f $CONFFILE ]; then
	CNR_EXISTS=y
	DATADIR=`/bin/grep cnr.datadir= $CONFFILE | /bin/sed -e '/cnr.datadir=/s///'`
	JAVADIR=`/bin/grep cnr.java-home= $CONFFILE | /bin/sed -e '/cnr.java-home=/s///'`
	LOGDIR=`/bin/grep cnr.logdir= $CONFFILE | /bin/sed -e '/cnr.logdir=/s///'`
	TEMPDIR=`/bin/grep cnr.tempdir= $CONFFILE | /bin/sed -e '/cnr.tempdir=/s///'`
	CCM_PORT=`/bin/grep cnr.ccm-port= $CONFFILE | /bin/sed -e '/cnr.ccm-port=/s///'`
	WEBUI_PORT=`/bin/grep cnr.http-port= $CONFFILE | /bin/sed -e '/cnr.http-port=/s///'`
	WEBUI_SEC_PORT=`/bin/grep cnr.https-port= $CONFFILE | /bin/sed -e '/cnr.https-port=/s///'`
    fi
elif [ "$CNR_CCM_MODE" = "local" ]; then
    # Obtain the installation location from the old rc file, if possible
    RCFILE=/etc/init.d/aicservagt
    if [ -f $RCFILE ]; then
	ROOTDIR=`/bin/grep INSTALLPATH= $RCFILE | /bin/sed -e '/INSTALLPATH=/s///'`
	CONFFILE=$ROOTDIR/conf/aic.conf
	if [ -f $CONFFILE ]; then
	    CNR_EXISTS=y
	    DATADIR=`/bin/grep DATADIR $CONFFILE | /bin/sed -e '/DATADIR /s///'`
	    JAVADIR=`/bin/grep CNR_JAVA_HOME $CONFFILE | /bin/sed -e '/CNR_JAVA_HOME /s///'`
	    LOGDIR=`/bin/grep LOGDIR $CONFFILE | /bin/sed -e '/LOGDIR /s///'`
	    TEMPDIR=`/bin/grep TEMPDIR $CONFFILE | /bin/sed -e '/TEMPDIR /s///'`

	    TMPVAL=`/bin/grep CNR_CCM_PORT $CONFFILE | /bin/sed -e '/CNR_CCM_PORT /s///'`
	    # Make sure to only set CCM_PORT to TMPVAL if TMPVAL was located
	    CCM_PORT=${TMPVAL:=$CCM_PORT}

	    WEBUI_PORT=`/bin/grep CNR_WEBUI_PORT $CONFFILE | /bin/sed -e '/CNR_WEBUI_PORT /s///'`
	    WEBUI_SEC_PORT=`/bin/grep CNR_SEC_WEBUI_PORT $CONFFILE | /bin/sed -e '/CNR_SEC_WEBUI_PORT /s///'`
	fi
    fi
fi

# Prompt the user for the installation location
valid_resp=0
while [ $valid_resp -eq 0 ]; do
    echo ""
    echo "Where do you want to install the $product executable files? [$ROOTDIR] \c"
    read PROMPTDIR
    TMPROOTDIR=${PROMPTDIR:=$ROOTDIR}
    if [ -f $TMPROOTDIR ]; then
	echo ""
	echo "$TMPROOTDIR appears to be a file, not a valid directory.  Choose a different location."
    else
	valid_resp=1
	if [ "$ROOTDIR" != "$TMPROOTDIR" ]; then
	    # User selected a different default root directory.
	    ROOTDIR=$TMPROOTDIR
	    default_root=0
	else
	    # User selected the default root directory.  Use the existing
	    # default DATA, LOG, and TEMP directories, if they exist.
	    default_root=1
	fi

    fi
done

if [ $default_root -eq 0 ]; then
    # The user selected a different default ROOTDIR, so clobber any existing
    # values of DATADIR, LOGDIR, and TEMPDIR, to be calculated below based on
    # the new value of ROOTDIR
    DATADIR=""
    LOGDIR=""
    TEMPDIR=""
fi

SCRATCHDIR=`echo $ROOTDIR | sed -e 's/^\/opt\//\/var\//'`

# For each of the following directories, modify the default locations only if
# there is not a default value from a previous installation.
if [ "$DATADIR"x != ""x ]; then
    TMPDATADIR=$DATADIR
else
    TMPDATADIR=$SCRATCHDIR/data
fi

if [ "$LOGDIR"x != ""x ]; then
    TMPLOGDIR=$LOGDIR
else
    TMPLOGDIR=$SCRATCHDIR/logs
fi

if [ "$TEMPDIR"x = ""x ]; then
    TEMPDIR=$SCRATCHDIR/temp
fi

valid_resp=0
while [ $valid_resp -eq 0 ]; do
    echo ""
    echo "Where do you want to put the $product data files? [$TMPDATADIR] \c"
    read PROMPTDIR
    DATADIR=${PROMPTDIR:=$TMPDATADIR}
    if [ -f $DATADIR ]; then
	echo ""
	echo "$DATADIR appears to be a file, not a valid directory.  Choose a different location."
    else
	valid_resp=1
    fi
done

valid_resp=0
while [ $valid_resp -eq 0 ]; do
    echo ""
    echo "Where do you want to put the $product log files? [$TMPLOGDIR] \c"
    read PROMPTDIR
    LOGDIR=${PROMPTDIR:=$TMPLOGDIR}
    if [ -f $LOGDIR ]; then
	echo ""
	echo "$LOGDIR appears to be a file, not a valid directory.  Choose a different location."
    else
	valid_resp=1
    fi
done

export ROOTDIR
export LOGDIR
UTILDIR=$INST_DATADIR/nwreg2/install_util
TCLSH=$UTILDIR/tclsh8.4
LD_LIBRARY_PATH=$UTILDIR
export LD_LIBRARY_PATH
TCL_LIBRARY=$UTILDIR
export TCL_LIBRARY

$TCLSH $UTILDIR/checklicense.tcl $UTILDIR check_license_file $CNR_CCM_MODE $CNR_CCM_TYPE $ROOTDIR
status=$?
if [ $status -ne 0 ]; then
    echo ""
    echo "The installer did not locate a valid $product license file.  Administration of the cluster will not be possible without a valid license."
    valid_resp=0
    while [ $valid_resp -eq 0 ]; do
	echo ""
	echo "Please enter your $product license file, or press the Return key to continue the installation without entering a valid license at this point: \c"
	read license_key
	if [ "$license_key"x != ""x ]; then
	    $TCLSH $UTILDIR/checklicense.tcl $UTILDIR check_new_license_file $CNR_CCM_MODE $license_key $CNR_CCM_TYPE
	    status=$?
	    if [ $status -eq 0 ]; then
		# A valid license file was provided, terminate the loop
		CNR_LICENSE_FILE="$license_key"
		valid_resp=1
	    else
		# Invalid license file provided.  Print a warning message.
		echo "$license_key is not a valid $product license file for this version of the product."
	    fi
	else
	    # The user chose not to enter a license file.  Double check
	    # to make sure this was the intent.
	    echo ""
	    RESPONSE=`ckyorn -d n -p \
		"Administration of the cluster will not be possible without a valid license.  Are you sure you wish to continue? [n] "` \
		|| exit $?
	    case $RESPONSE in
	    [yY]*)
		valid_resp=1
		;;
	    *)
		;;
	    esac
	fi
    done
fi

echo ""
echo "If upgrading, Cisco Systems recommends that you archive the existing $product binaries and database to recover in the event that the current installation is unsuccessful."
RESPONSE=`ckyorn -d y -p \
	"Would you like to save an archive of your current $product database files? [y] "` \
	|| exit $?
case $RESPONSE in
    [yY]*)
        PERFORM_BACKUP=y
	echo ""
	echo "Provide a directory, which is outside the current installation area, to back up the current $product installation files."

	echo ""
	echo "Please specify an archive directory [$BACKUPDIR]: \c"
	read PROMPTDIR
	BACKUPDIR=${PROMPTDIR:=$BACKUPDIR}
	;;

    *)
	PERFORM_BACKUP=n
esac


if [ "$CNR_CCM_MODE" = "local" ]; then
    valid_resp=0
    one=1
    while [ $valid_resp -eq 0 ]; do
    	echo ""
	echo "Specify whether you would like to perform a complete installation of"
	echo "Network Registrar or whether you only want the client utilities."
	echo ""
	echo "\t1.  Both server and client (default)"
	echo "\t2.  Client only"
	echo ""
	echo "Select your installation type [1,2]: \c"
	read RESPONSE
	SELECTION=${RESPONSE:=$one}

	# The following validation will only allow the integral
	# response of 1 or 2.
	case $SELECTION in
	1)
	    START_SERVERS=y
	    valid_resp=1
	    ;;

	2)
	    START_SERVERS=n
	    valid_resp=1
	    ;;

	*)
	    echo ""
	    echo "Invalid selection.  Select the index from the list provided."
	esac
    done
fi

valid_resp=0
DEFAULT_PORT=$CCM_PORT
while [ $valid_resp -eq 0 ]; do
    echo ""
    echo "Network Registrar uses the CCM management SCP port for internal communications"
    echo "between servers."
    echo ""
    echo "Enter the CCM SCP port number [$DEFAULT_PORT]: \c"
    read PORTNUM
    CCM_PORT=${PORTNUM:=$DEFAULT_PORT}

    #  The following trick is used to make sure an integer was provided
    if expr "$CCM_PORT" + 1 > /dev/null 2>&1; then
	#  OK, now check for a valid range
	if [ $CCM_PORT -ge 0 -a $CCM_PORT -le 65535 ]; then
	    valid_resp=1
        fi
    fi

    if [ $valid_resp -ne 1 ]; then
	echo ""
	echo "Invalid port number specified.  Port must be between 0 and 65535."
    fi
done

# Prompt for the Java installation directory
echo ""
echo "$product $VERSION requires Java version 1.5.0_06 (or later) to run."

# Attempt to locate the previously specified Java installation location from
# the following locations (in order of precedence):
#    1. The value of the cnr.java-home variable stored in the cnr.conf file
#    2. From the JAVA_HOME environment variable

tmp_java=/usr/java

if [ "$JAVADIR"x != ""x ]; then
    # JAVADIR was obtained from a previous installation of Network Registrar
    tmp_java=$JAVADIR
elif [ "$JAVA_HOME"x != ""x ]; then
    tmp_java=$JAVA_HOME
fi

if [ "$tmp_java"x != ""x ]; then
    valid_resp=0
    while [ $valid_resp -eq 0 ]; do
	echo ""
	echo "Where is your Java software installed? [$tmp_java] \c"
	read PROMPTDIR
	JAVADIR=${PROMPTDIR:=$tmp_java}
	if [ -f $JAVADIR ]; then
	    echo ""
	    echo "$JAVADIR appears to be a file, not a valid directory.  Choose a different location."
	else
	    # If JAVADIR is a directory, then terminate the while loop
	    if [ -d $JAVADIR ]; then
		valid_resp=1
	    else
		echo ""
		echo "$JAVADIR does not appear to be a valid directory.  Choose a different location"
	    fi
	fi
    done
fi

if [ "$JAVADIR"x = ""x ]; then
    # At this point we have looked in all of the places where we might find
    # the Java installation area and were unsuccessful in locating the Java
    # installation.  Prompt the user without providing a default value.
    valid_resp=0
    while [ $valid_resp -eq 0 ]; do
	echo ""
	echo "Where is your Java software installed? \c"
	read JAVADIR
	if [ -f $JAVADIR ]; then
	    echo ""
	    echo "$JAVADIR appears to be a file, not a valid directory.  Choose a different location."
	else
	    # If JAVADIR is a directory, then terminate the while loop
	    if [ -d $JAVADIR ]; then
		valid_resp=1
	    else
		echo ""
		echo "$JAVADIR does not appear to be a valid directory.  Choose a different location"
	    fi
	fi
    done
fi

# Select whether to use secure (HTTPS) and/or non-secure (HTTP) connection for
# the Web based UI.
valid_resp=0
one=1
while [ $valid_resp -eq 0 ]; do
    echo ""
    echo "Specify whether you would like to configure security for the browser"
    echo "connection to the Network Registrar web server using a pre-"
    echo "configured JSSE installation."
    echo ""
    echo "\t1.  Non-secure/HTTP (default)"
    echo "\t2.  Secure/HTTPS (requires JSSE)"
    echo "\t3.  Both HTTP and HTTPS"
    echo ""
    echo "Select your installation type [1-3]: \c"
    read RESPONSE
    SELECTION=${RESPONSE:=$one}

    # The following validation will only allow the integral
    # response of 1, 2, or 3.
    case $SELECTION in
    1)
	USE_HTTP=y
	USE_HTTPS=n
	valid_resp=1
	;;

    2)
	USE_HTTP=n
	USE_HTTPS=y
	valid_resp=1
	;;

    3)
	USE_HTTP=y
	USE_HTTPS=y
	valid_resp=1
	;;

    *)
	echo ""
	echo "Invalid selection.  Select the index from the list provided."
    esac
done


# Initialize the WebUI port numbers using the default values if they are not
# already set.
if [ "$WEBUI_SEC_PORT"x = ""x ]; then
    WEBUI_SEC_PORT=$SECURE_WEBUI_PORT
fi

if [ "$WEBUI_PORT"x = ""x ]; then
    WEBUI_PORT=$UNSECURE_WEBUI_PORT
fi


if [ "$USE_HTTP" = "y" ]; then
    # Prompt for the non-secure web port
    valid_resp=0
    DEFAULT_PORT=$WEBUI_PORT
    while [ $valid_resp -eq 0 ]; do
	echo ""
	echo "Network Registrar uses the Web UI port to provide the Web user "
	echo "interface service to clients.  The product default port number is $UNSECURE_WEBUI_PORT."
	echo ""
	echo "Enter the Web UI port number [$DEFAULT_PORT]: \c"
	read PORTNUM
	WEBUI_PORT=${PORTNUM:=$DEFAULT_PORT}

	#  The following trick is used to make sure an integer was provided
	if expr "$WEBUI_PORT" + 1 > /dev/null 2>&1; then
	    #  OK, now check for a valid range
	    if [ $WEBUI_PORT -ge 0 -a $WEBUI_PORT -le 65535 ]; then
		valid_resp=1
	    fi
	fi

	if [ $valid_resp -ne 1 ]; then
	    echo ""
	    echo "Invalid port number specified.  Port must be between 0 and 65535."
	fi

	if [ $WEBUI_PORT -eq $CCM_PORT ]; then
	    echo ""
	    echo "Specified port conflicts with the CCM management SCP port."
	    valid_resp=0
	fi
    done
fi

if [ "$USE_HTTPS" = "y" ]; then

    # Prompt for keystore file location
    valid_resp=0
    while [ $valid_resp -eq 0 ]; do
	echo ""
	echo "Provide the fully qualified path to the keystore file that"
	echo "contains the certificate(s) to be used for the secure"
	echo "connection to the Network Registrar web server.  Do not remove"
	echo "this file or Network Registrar HTTPS connection requests will"
	echo "fail."
	echo ""
	echo ""
	echo "Where is your keystore file located? \c"
	read PROMPTDIR
	KEYSTORE_FILE=$PROMPTDIR
	if [ -d $KEYSTORE_FILE ]; then
	    echo ""
	    echo "$KEYSTORE_FILE appears to be a directory, not a valid file.  Choose a different location."
	else
	    # If KEYSTORE_FILE is a file, then terminate the while loop
	    if [ -f $KEYSTORE_FILE ]; then
		valid_resp=1
	    else
		echo ""
		echo "$KEYSTORE_FILE does not appear to be a valid file.  Choose a different location"
	    fi
	fi
    done

    # Prompt for keystore file password
    echo ""
    echo "Network Registrar requires the password that was provided when"
    echo "creating the JSSE keystore file to provide the secure Web user"
    echo "interface service to clients.  The default password is $KEYSTORE_PASSWORD."
    echo ""
    echo "What is your keystore password? \c"
    read PROMPTDIR
    KEYSTORE_PASSWORD=${PROMPTDIR:=$KEYSTORE_PASSWORD}
fi

if [ "$USE_HTTPS" = "y" ]; then
    # Prompt for the secure web port
    valid_resp=0
    DEFAULT_PORT=$WEBUI_SEC_PORT
    while [ $valid_resp -eq 0 ]; do
	echo ""
	echo "Network Registrar uses the secure Web UI port to provide the Web user "
	echo "interface service to clients.  The product default secure port number is $SECURE_WEBUI_PORT."
	echo ""
	echo "Enter the secure Web UI port number [$DEFAULT_PORT]: \c"
	read PORTNUM
	WEBUI_SEC_PORT=${PORTNUM:=$DEFAULT_PORT}

	#  The following trick is used to make sure an integer was provided
	if expr "$WEBUI_SEC_PORT" + 1 > /dev/null 2>&1; then
	    #  OK, now check for a valid range
	    if [ $WEBUI_SEC_PORT -ge 0 -a $WEBUI_SEC_PORT -le 65535 ]; then
		valid_resp=1
	    fi
	fi

	if [ $valid_resp -ne 1 ]; then
	    echo ""
	    echo "Invalid port number specified.  Port must be between 0 and 65535."
	fi

	if [ $WEBUI_SEC_PORT -eq $CCM_PORT ]; then
	    echo ""
	    echo "Specified port conflicts with the CCM management SCP port."
	    valid_resp=0
	elif [ $WEBUI_SEC_PORT -eq $WEBUI_PORT ]; then
	    echo ""
	    echo "Specified port conflicts with the HTTP WebUI port."
	    valid_resp=0
	fi
    done
fi

# Set the BASEDIR variable to either the existing value of BASEDIR, or to
# ROOTDIR if BASEDIR was not already set.
pkginfo -q nwreg2
if [ $? -eq 0 ]; then
    BASEDIR=`pkgparam nwreg2 BASEDIR`
else
    BASEDIR=$ROOTDIR
fi

# make the environment variables available to the installation service and
# to any other packaging scripts
cat >$1 <<!
BACKUPDIR='$BACKUPDIR'
BASEDIR='$BASEDIR'
ROOTDIR='$ROOTDIR'
CCM_PORT='$CCM_PORT'
CNR_EXISTS='$CNR_EXISTS'
CNR_LICENSE_FILE='$CNR_LICENSE_FILE'
DATADIR='$DATADIR'
INSTALL_DEBUG='$INSTALL_DEBUG'
JAVADIR='$JAVADIR'
KEYSTORE_FILE='$KEYSTORE_FILE'
KEYSTORE_PASSWORD='$KEYSTORE_PASSWORD'
LOGDIR='$LOGDIR'
PERFORM_BACKUP='$PERFORM_BACKUP'
WEBUI_SEC_PORT='$WEBUI_SEC_PORT'
SKIP_VALIDATION='$SKIP_VALIDATION'
START_SERVERS='$START_SERVERS'
TEMPDIR='$TEMPDIR'
USE_HTTP='$USE_HTTP'
USE_HTTPS='$USE_HTTPS'
WEBUI_PORT='$WEBUI_PORT'
CNR_CCM_MODE='$CNR_CCM_MODE'
CNR_CCM_TYPE='$CNR_CCM_TYPE'
!
exit 0
