#!/bin/sh
# Copyright 04/17/01 Sun Microsystems, Inc. All Rights Reserved.
#
# pragma ident  "@(#)es-lic	1.19  01/04/17 Sun Microsystems"
#

#
#  This program installs a license
#

validLicenseKey() {
    KEY="$1"

    valid=`$CHECKKEY -k $KEY`

    if [ "$valid" -gt 0 ] ; then
        echo "$KEY" | /usr/bin/cat >> $VARDIR/.license 
	return 1;
    else 
        /usr/bin/gettext "\n  Please try again: Invalid key\n\n"
	return 0;
    fi

}

#
# when es-lic is run this function will filter out all incorrect
# license keys
#
filterLicenseFile() {

    /usr/bin/gettext "\n  Checking for invalid keys.  Please wait...\n"

    LICFILE="$VARDIR/.license"
    TMPLICFILE="/tmp/.license.$$"
    CHECKKEY="$BASEDIR/sbin/checkkey"
    
    if [ -f "$TMPLICFILE" ] ; then
       rm -f $TMPLICFILE 
    fi

    if [ -f "$LICFILE" ] ; then
        for key in `cat $LICFILE`
        do 
            valid=`$CHECKKEY -k $key`
            if [ $valid -gt 0 ] ; then
                echo "$key" >> $TMPLICFILE 
	    else 
                /usr/bin/gettext "\n  Removing invalid key : $key"
            fi

        done

        rm -f $LICFILE

    fi

    /usr/bin/gettext "\n  "

    if [ -f "$TMPLICFILE" ] ; then
        mv $TMPLICFILE $LICFILE
    fi
}

########################################################
# This procedure warns the user to restart topology
# if topology is running at the time of a key addition 
# into the /var/opt/SUNWsymon/cfg/.license file
########################################################
print_topology_running() {
    psout=`/usr/bin/ps -aef | /usr/bin/grep "esd - init topology" | /usr/bin/fgrep -v grep`
    if [ $? -eq 0 ] ; then
        echolog ""
        echolog "Topology server is currently running."
        echolog "It must be restarted for the license file update to take effect."
        echolog "Please stop and restart topology server using:"
        echolog '$2 -p\\\; $3 -p' "$BASEDIR/sbin/es-stop" "$BASEDIR/sbin/es-start"
        echolog ""
    fi
}

######################################################
#
#  Main entry point
#
######################################################

prog_base=`/usr/bin/basename $0`
common_cmd=`echo $0 | /usr/bin/sed s/$prog_base/es-common.sh/`
. $common_cmd

set_basedir
PROGNAME=$0; export PROGNAME

check_root
check_osver
cd "$BASEDIR"
PWD="$BASEDIR"; export PWD
set_paths

echo ""
/usr/bin/gettext "  -------------------------------------\n"
/usr/bin/gettext "  Sun Management Center License Program\n"
/usr/bin/gettext "  -------------------------------------\n"

KEY_TYPE=$1
QUESTION_TEXT=$2
COUNTER=0
CHECKKEY="$BASEDIR/sbin/checkkey"

filterLicenseFile

if [ -f $VARDIR/.license ] ; then
    oldkey=`/usr/bin/head -1 $VARDIR/.license`
    [ "$oldkey" = "AqfghxZI3642" ] && /usr/bin/rm $VARDIR/.license
fi

if [ $# -le 1 ];
then 
        /usr/bin/gettext "  \n"
        /usr/bin/gettext "  Press enter when finished entering license keys\n"
        /usr/bin/gettext "  \n"
        COUNTER=0
        KEYSADDED=0
	KEEPLOOPING=1

	while [ $KEEPLOOPING -eq 1 ]
	do
	    get_input_from_user "Please enter license key: " 
	    case $answer in
		"")	KEEPLOOPING=0
			break;;
		*)	validLicenseKey $answer
			if [ "$?" -eq 1 ] ; then
			    KEYSADDED=`expr $KEYSADDED + 1`;
			    KEEPLOOPING=0;
			else 
			    KEEPLOOPING=1;
			fi
            esac
	done

        if [ $KEYSADDED -ne 0 ];
        then
                print_topology_running
        fi


elif [ $KEY_TYPE = "de" ];
then
        /usr/bin/gettext "  \n"
	get_input_from_user "$QUESTION_TEXT"

	check_development_env

	if [ $? -eq 1 ] ; then

	    echo "$answer" | /usr/bin/cat >> $VARDIR/.license

	    print_topology_running

	else
	    number=`$BASEDIR/sbin/checkkey -k $answer`
	    if [ $number -eq -1 ] ; then
	        echolog "Invalid License, Exiting..."
	        exit 1
	    fi
	    [ ! -d "$VARDIR" ] && /usr/bin/mkdir -p "$VARDIR"
	    echo $answer >> "$VARDIR/.license"


	fi

elif [ $KEY_TYPE = "advanced_services" ];
then
	COUNTER=0
	KEYSADDED=0
        KEEPLOOPING=1

        while [ $KEEPLOOPING -eq 1 ]
        do

	    get_input_from_user "$QUESTION_TEXT"
	    case $answer in
		"")	KEEPLOOPING=0;
			break;;
		*)	validLicenseKey $answer
                        if [ "$?" -eq 1 ] ; then
                            KEYSADDED=`expr $KEYSADDED + 1`;
                            KEEPLOOPING=0;
                        else
                            KEEPLOOPING=1;
                        fi

	    esac	 		
	done

	if [ $KEYSADDED -ne 0 ];
	then
		print_topology_running
	fi

elif [ $KEY_TYPE = "advanced_monitoring" ];
then
	COUNTER=0
	KEYSADDED=0
        KEEPLOOPING=1

        while [ $KEEPLOOPING -eq 1 ]
        do

	    get_input_from_user "$QUESTION_TEXT"
	    case $answer in
		"")	KEEPLOOPING=0;
			break;;
		*)	validLicenseKey $answer
                        if [ "$?" -eq 1 ] ; then
                            KEYSADDED=`expr $KEYSADDED + 1`;
                            KEEPLOOPING=0;
                        else
                            KEEPLOOPING=1;
                        fi
	    esac	 		
	done

	if [ $KEYSADDED -ne 0 ];
	then
		print_topology_running
	fi


else
	/usr/bin/gettext "  \n"
	/usr/bin/gettext "  Invalid parameters passed to es-lic script\n"
	/usr/bin/gettext "  \n"
	exit 1
fi


#
#    I could not get this to work reliably due to timing issues :-(....
#
#    ask_user "Would you like to restart topology now?"
#    echolog ""
#    if [ $answer -eq 1 ] ; then
#	pid=`echo $psout | /usr/bin/awk '{print $2}'`
#	kill $pid
#	if [ $? -ne 0 ] ; then
#	    echolog ""
#	    echolog "Unable to kill topology server"
#	    echolog ""
#	    exit 1
#	fi
#	/usr/bin/ps -p $pid > /dev/null 2>&1
#	if [ $? -eq 0 ] ; then
#	    sleep 5
#	    kill $pid
#	fi
#	ESENV=""
#	$BASEDIR/sbin/es-start -p
#	if [ $? -ne 0 ] ; then
#	    echolog ""
#	    echolog "Unable to start topology server"
#	    echolog ""
#	    exit 1
#	fi
#	echolog "Topology server restarted"
#    fi

exit 0
