#!/bin/sh
# $Header: get_license_key.sh,v 1.16 2000/11/22 19:07:33 raa Stab $
#bcpyrght
#***************************************************************************
#* $VRTScprght: Copyright 1993 - 2002 VERITAS Software Corporation, All Rights Reserved $ *
#***************************************************************************
#ecpyrght
#

# This utility allows the user to Add, Delete and list license key information
#
# license_menu -- default interface for user's to access license key file
#
# Read_key -- no args. prompts Enter key, sets global variable L_KEY
# check_key -- 1 arg: license key; returns 1 if CRC fails, 0 if CRC okay.
#		sets variables
#		with license key information, description
#		displays what the key is for.
# add_key
# delete_key -- 1 arg: license key
# check_4_feature -- 2 args: license file and feature ID bit; returns YES or NO
# usage_ext -- provide usage message for customers
# what_reg -- queries permanent license key file for active product id BusinesServer
#		or DataCenter, unrestricted yes or no, demo yes or no

# return 0 = No error, false, no, not found
# return 1 = found, true
#

#
#  Define the Prompt function.
#

case "`echo 'x\c'`" in
	'x\c')
		Prompt()
		{
		echo -n "$*"
		}
		;;
	x)
		Prompt()
		{
		echo "$*\c"
		}
		;;
	*)
		Prompt()
		{
		echo -n "$*"
		}
		;;
esac

set_hardware ()
{
	if [ -f ${NB_DIR}/version ] ; then
        	HARDWARE=`head -1 ${NB_DIR}/version | cut -f2 -d" "`
	else
        	HARDWARE=NONE
	fi
	whats_needed="A NetBackup DataCenter license key is needed"
	case "$HARDWARE" in
        	ALPHA)
                	;;
        	HP*)
			whats_needed="A NetBackup BusinesServer or DataCenter license key is needed"
                	;;
		LINUX*)
			whats_needed="A NetBackup BusinesServer license key is needed"
			;;
        	NCR)
                	;;
        	PYRAMID)
                	;;
        	RS6000)
                	;;
        	SEQUENT*)
                	;;
        	SGI)
                	;;
        	SOLARIS)
			whats_needed="A NetBackup BusinesServer or DataCenter license key is needed"
                	;;
        	SUN4)
                	;;
        	*)
                	$ECHO "
	${NB_DIR}/version not found or corrupted.
	Unknown hardware type: \"$HARDWARE\".
	"
                	exit 2
                	;;
	esac
}

# -----------------------------------------------------------------------------
# Confirm an action with yes as the default
#
#	confirm_yes prompt_string
#	Returns 0 for yes and 1 for no.

confirm_yes ()
{

	Valid=1
	while [ $Valid = 1 ]
	do
		Prompt "\n$1 (y/n) [y] "
		read ans
		: ${ans:=y}
		case $ans in
			N*|n*)  return 1 ;;
			Y*|y*)  return 0 ;;
			*)      $ECHO "You have entered an invalid option."
		esac
	done
}

# -----------------------------------------------------------------------------
# has one argument, a license key
# return 1 if not valid
# return 0 if valid
# use a tmp file to verify the CRC is valid and then
# to discern the following pieces of info
# FEAT_DESCR = what features the key has
# DEMO= 0, 1, 2, 3, 4
# DEMO_TITLE= Evaluation or blank string
# PROD_ID = base product ID, 6, 7, 8
# PROD_TILE = base product title, NetBackup BusinesServer or NetBackup DataCenter
# IS_BASE_KEY = 1 if feature ID 20 is set
# IS_NEXT= 1 if feature ID 21 is set
#
# displays what the key is and what base product it is for.
#

check_key ()
{
	TMP_FILE=/tmp/testkey.$$

	${BP_LIC} -path ${TMP_FILE} $1 > /dev/null 2>&1
	chk_status=$?
	if [ $chk_status != 0 ] ; then
		# 159 means its a demo key and license key file is empty or doesn't exist
		# 223 means the CRC failed
		# 161 means the license key is a demo key with hard end date 
		# that has exipred
		CHK_KEY="is an invalid key ($chk_status)"
		if [ $chk_status = 223 ] ; then
			CHK_KEY="has an invalid check-sum ($chk_status)"
		fi
		if [ $chk_status = 161 ] ; then
			CHK_KEY="is an expired demo key ($chk_status)"
		fi
		rm -f ${TMP_FILE}
		return 1
	fi

	BPMIN_FILE=/tmp/bpout.$$
	${BP_LIC} -path ${TMP_FILE} -verbose > ${BPMIN_FILE} 2>&1

	# based on single digit product id
	PROD_ID=`cat ${BPMIN_FILE} | egrep "product ID"`
	if [ $? != 0 ]; then
		CHK_KEY="key not listed"
		rm -f ${BPMIN_FILE}
		return 1
	fi
	PROD_ID=`echo ${PROD_ID} | cut -d"=" -f2 | cut -c2`

	check_4_feature ${TMP_FILE} ${BASE_ID}
	IS_BASE_KEY=$?
	if [ $IS_BASE_KEY = 11 ] ; then
		IS_BASE_KEY=0
	fi

	check_4_feature ${TMP_FILE} ${NEXT_ID}
	IS_NEXT=$?
        if [ $IS_NEXT = 11 ] ; then 
                IS_NEXT=0 
        fi

	FEAT_DESCR=`cat ${BPMIN_FILE} | egrep "Feature ID" | egrep -v "Base NetBackup" | awk '{ feat=$5; for (i = 6; i < NF; i++) feat = sprintf("%s %s",feat,$i); oline=sprintf("            %s", feat); print oline }'`

	PROD_TITLE=`cat ${BPMIN_FILE} | egrep "product ID" | cut -d"=" -f2 | cut -c4-`

	DEMO=`cat ${BPMIN_FILE} | egrep "license type" | cut -d"=" -f2 | cut -c2`
	if [ "x${DEMO}" = "x0" ] || [ "x${DEMO}" = "x5" ] ; then
		DEMO_TITLE="Evaluation "
		EXPIRE=`cat ${BPMIN_FILE} | egrep "Expiration" | egrep -v "Firm" | cut -d"=" -f2`
                IS_DEAD=`echo $EXPIRE | cut -d" " -f1`
                if [ "${IS_DEAD}x" = "Notx" ] ; then
                        DOD1=`echo $EXPIRE | cut -d" " -f4-5`
                        DOD2=`echo $EXPIRE | cut -d" " -f7`
                        DOD_LINE=" with ${DOD1}, ${DOD2} expiration date"
                else
                        # IS_DEAD will be expired
                        DOD=`echo $EXPIRE | cut -d" " -f2-`
                        DOD_LINE=" which expired ${DOD}"
                fi

	else
		DEMO_TITLE=""
                IS_DEAD="Never"
                DOD_LINE=""
	fi

	if [ ${IS_NEXT} = 1 ] ; then
		if [ ${IS_BASE_KEY} = 1 ] ; then
			# bit 20 and 21 set
			FID=" Base product with all the features enabled"
		else
			# just bit 21 set
			FID=" features enabled: ALL"
		fi
	else
		if [ ${IS_BASE_KEY} = 1 ] ; then
			if [ "X${FEAT_DESCR}" = "X" ] ; then
				FID=" Base product"
			else
				FID=" Base product with the following features enabled:  
${FEAT_DESCR}"
			fi	
		else
			FID=" features enabled:  
${FEAT_DESCR}"
		fi
	fi
	CHK_KEY="${DEMO_TITLE}${PROD_TITLE}${FID}"

	rm -f ${TMP_FILE} ${BPMIN_FILE}
	return 0
}

# -----------------------------------------------------------------------------
# checks for permanant license key file
# return 0 if license file is found and readable
# return 1 if not found or not readable
#

check_Lfile ()
{
	LFILE_ERR=""
	ret=0
	if [ "${HOST}" != "${whereami}" ] ; then
		${BP_LIC} -M ${HOST} -verbose > /dev/null 2>&1
		chk_status=$?
		if [ $chk_status = 23 ] ; then
			# error reading host's license key file
			LFILE_ERR="${HOST}'s license key file could not be read."
			ret=1
		fi
                if [ $chk_status = 46 ] ; then 
                        LFILE_ERR="${whereami} is not allowed access to ${HOST}."
                        ret=1 
                fi 
	else
		# check local license key file
		if [ ! -f ${PERM_FILE} ] ; then
			LFILE_ERR="The license key file was not found."
			ret=3
		else
			# test for empty license key file
			${BP_LIC} -path ${PERM_FILE} > /dev/null 2>&1
			if [ $? = 12 ] ; then
				LFILE_ERR="The license key file could not be opened."
				ret=1
			else
				grep "VMD5_DIGEST" ${PERM_FILE} >/dev/null 2>&1
				if [ $? != 0 ] ; then
					LFILE_ERR="The license key file is missing a checksum."
					ret=1
				fi
			fi
		fi
	fi
#if [ "${LFILE_ERR}" != "" ] ; then
#	echo "
#${LFILE_ERR}
#"
#fi
	return ${ret}
}

# -----------------------------------------------------------------------------
# global variables set

what_reg ()
{
	ACTIVE_NEXT="NO"
	ACTIVE_BASE="none"
	ACTIVE_PROD=0
	ACTIVE_DEMO="NO"
	ACTIVE_LT=""
	ACTIVE_21=""

	check_Lfile
	Lfile_status=$?
	if [ $Lfile_status != 0 ] ; then
		# error with reading license file,
		if [ $Lfile_status != 3 ] ; then
			echo $LFILE_ERR
		fi
		return 11
	fi

	BPWHATOUT=/tmp/bpwhat.$$
	${BP_LIC} -nb_features > ${BPWHATOUT} 2>&1
	if [ $? = 11 ] ; then
		# no base product active, so nothing else is
		WHAT_REG="NetBackup is not active."
		rm -f ${BPWHATOUT}
		return 0
	fi

	is_it=`cat ${BPWHATOUT} | egrep "6 ${BASE_ID}" | cut -d" " -f1,2`
	if [ "${is_it}x" = "6 ${BASE_ID}x" ] ; then
		ACTIVE_BASE="DataCenter"
		ACTIVE_PROD=6
	fi
	is_it=`cat ${BPWHATOUT} | egrep "7 ${BASE_ID}" | cut -d" " -f1,2`
	if [ "${is_it}x" = "7 ${BASE_ID}x" ] ; then
		ACTIVE_BASE="BusinesServer"
		ACTIVE_PROD=7
	fi

	is_it=`cat ${BPWHATOUT} | egrep "${ACTIVE_PROD} ${NEXT_ID}" | cut -d" " -f1,2`
	if [ "${is_it}x" = "${ACTIVE_PROD} ${NEXT_ID}x" ] ; then
		ACTIVE_NEXT="YES"
		ACTIVE_21=" with all the features enabled"
	fi

	is_it=`cat ${BPWHATOUT} | egrep "${ACTIVE_PROD} ${BASE_ID} 0" | cut -d" " -f1,2,3`
	if [ "${is_it}x" = "${ACTIVE_PROD} ${BASE_ID} 0x" ] ; then
		ACTIVE_DEMO="YES"
		ACTIVE_LT=" Evaluation"
	fi

	WHAT_REG="NetBackup${ACTIVE_LT} ${ACTIVE_BASE}${ACTIVE_21} is the active NetBackup."
	echo ${WHAT_REG}

	rm -f ${BPWHATOUT}
	return 0
}

# -----------------------------------------------------------------------------
# just reads a string and sets L_KEY

Read_key ()
{
	L_KEY="FAKE-KEY"
	echo ""
	Prompt "Enter license key: "
	read L_KEY
}

# -----------------------------------------------------------------------------
# 2 arguments: TMP_FILE or PERM_FILE and feature ID bit
#
# returns 11 for a bad license key file
# return 0, feature not found as active
# return 1, feature found as active

check_4_feature ()
{
	FILE=$1
	BIT_2FIND=$2

	ret=0
	BPOUT=/tmp/bpfeat.$$

	if [ "${FILE}" = "${PERM_FILE}" ] ; then
		check_Lfile
		if [ $? != 0 ] ; then
			# error with reading license file,
			echo $LFILE_ERR
			return 11
		fi
		${BP_LIC} -nb_features -verbose > ${BPOUT} 2>&1
		ret=$?
	else
                # if key we are checking doesn't have the base fid bit set,
                # bpminlicense -nb_features returns status 11
                # but if FILE is not the perm_file, then it should
                # only have one key in it.
		${BP_LIC} -path ${FILE} -verbose > ${BPOUT} 2>&1
		ret=$?

	fi

	if [ $ret != 11 ] ; then
		cat ${BPOUT} | egrep "Feature ID" | grep " ${BIT_2FIND}" > /dev/null 2>&1
		if [ $? -eq 0 ] ; then
			ret=1
		fi
	fi
	rm -f ${BPOUT}
	return ${ret}
}

# -----------------------------------------------------------------------------
# uses global variable L_KEY
# return 1 if L_KEY has invalid CRC or fails to register
# return 0 if L_KEY successfully registered
#

add_key()
{
	HOST=$whereami
	check_key $L_KEY
	if [ $? != 0 ] ; then
		echo "
$0 (ADD):  \"${L_KEY}\" ${CHK_KEY}.
"
		return 1
	fi
	# check for registered keys
	${BP_LIC} -find_keys $L_KEY > /dev/null 2>&1
	if [ $? = 0 ] ; then
		# its found so don't add it again
		echo "
$0 (ADD):  ${L_KEY} is already registered.
"
		return 0
	fi

	what_reg
	# if what reg fails with status 11, its a check_Lfile error
	${BP_LIC} -add_keys $L_KEY > /dev/null 2>&1
	is_it=$?
	if [ $is_it != 0 ] ; then
		echo "
$0 (ADD):  ${L_KEY} failed ( $is_it).
"
		return 1
	fi

	echo "
${L_KEY}:  
	${CHK_KEY} 
	has been registered.
"
	return 0
}

# -----------------------------------------------------------------------------
# uses global variable L_KEY
# returns 11 for a bad license key file
# return 1 if L_KEY has invalid CRC or fails to delete
# return 0 if L_KEY successfully deleted
#

delete_key ()
{
	HOST=$whereami

	# if license file is not valid, don't bother trying to do anything

	check_Lfile
	if [ $? != 0 ] ; then
		echo $LFILE_ERR
		return 1
	fi

	if [ "${L_KEY}" = "" ] ; then
		echo "
$0 (DELETE):  \"${L_KEY}\" is an invalid key.
"
		return 1
	fi
	${BP_LIC} -find_keys $L_KEY >/dev/null 2>&1
	is_it=$?
	if [ $is_it != 0 ] ; then
		# key not found, can't delete it.
		# returns 0 if its found
		echo "
$0 (DELETE):  ${L_KEY} key not found ($is_it).
"
		return 1
	fi

	${BP_LIC} -delete_keys $L_KEY >/dev/null 2>&1
	is_it=$?
	if [ $is_it = 159 ] ; then
		echo "
$0 (DELETE):  Cannot delete an evaluation key ($is_it).
"
		return 1
	fi

	if [ $is_it != 0 ] ; then
		echo "
$0 (DELETE):  failed ($is_it).
"
		return 1
	fi
	echo "
${L_KEY} has been deleted.
"
	return 0
}

# -----------------------------------------------------------------------------
# Global variable HOST is used
# output -- a list of the active features
#

List_NB_features ()
{
	T_file=/tmp/list.$$
	echo "
Active NetBackup Features
========================="

	check_Lfile
	if [ $? != 0 ] ; then
		echo $LFILE_ERR
		return 11
	fi

	R_LIST=""
	if [ ${HOST} != $whereami ] ; then
		# then go remote
		R_LIST="-M ${HOST} "
	fi

	${BP_LIC} ${R_LIST}-nb_features -verbose 2>/dev/null | awk '
	{ if ( $1 ~ /[0-9]/ ) {
		print " "
		oline=sprintf("License Key:	%s",$1)
		print oline
	} else if ( ( $1 == "product" ) && ( $2 == "ID" ) ) {
		pname = $5
		for (i=6; i<= NF; i++ ) pname = sprintf("%s %s",pname,$i)
		pid = sprintf("%s",pname)
		oline=sprintf("Product:	%s",pid)
		print oline
	}else if ( ( $1 == "time" ) && ( $2 == "added" ) ) {
		oline=sprintf("Date Added:	%s %s, %s at %s", $6, $7, $9, $8)
		print oline
	}else if ( $1 == "hostname" ) {
		oline=sprintf("Host:		%s",$3)
		print oline
	}else if (( $1 == "license" ) && ( $2 == "type" ) && ( $5 == "Permanent" )) {
		print "Expires:	No Expiration Date"
	}else if ( $1 == "count" ) {
		oline=sprintf("Client count:	%s",$3)
		print oline
	}else if ( ( $1 == "Feature" ) && ( $2 == "ID" ) ) {
		feat = $5
		for (i = 6; i <= NF; i++) feat = sprintf("%s %s",feat,$i)
		oline=sprintf("Feature:	%s",feat)
		print oline
	}else if ( $1 == "Expiration" ) {
		oline=sprintf("Expires:	%s %s, %s at %s",$6,$7,$9,$8)
		print oline
	}
	} '> ${T_file}

	output=`head ${T_file}`
	if [ "${output}x" = "x" ] ; then
		echo "
No active features to list."
		rm -f ${T_file}
		return 0
	fi
	cat ${T_file}
	echo "
========================="

	rm -f ${T_file}
	return 0
}

# -----------------------------------------------------------------------------
# Global variable HOST is used
# output -- a list of the registered license keys
#

List_L_keys ()
{
	# had problems with awk clobbering T_file so have a tmp_T_file
	T_file=/tmp/list.$$
	tmp_T_file=/tmp/list.$$.tmp
	echo "
Registered License Keys
======================="
	check_Lfile
	if [ $? != 0 ] ; then
		echo $LFILE_ERR
		return 11
	fi


	R_LIST=""
	if [ ${HOST} != $whereami ] ; then
		# then go remote
		R_LIST="-M ${HOST} "
	fi


	# i'm counting on the awk variables having default
	# value of null or blank.
	#
        ${BP_LIC} ${R_LIST}-verbose > ${T_file} 2>&1
	cat ${T_file} | awk '
	{ if ( $1 ~ /[0-9]/ ) {
		print " "
		oline=sprintf("License Key:	%s",$1)
		print oline
       } else if ( ( $1 == "product" ) && ( $2 == "ID" ) ) {
		pname = $5
		for (i=6; i<= NF; i++ ) pname = sprintf("%s %s",pname,$i)
		pid = sprintf("%s",pname)
		oline=sprintf("Product:	%s",pid)
		print oline
	}else if ( ( $1 == "time" ) && ( $2 == "added" ) ) {
		oline=sprintf("Date Added:	%s %s, %s at %s", $6, $7, $9, $8)
		print oline
	}else if ( $1 == "count" ) {
		oline=sprintf("Client count:	%s",$3)
		print oline
	}else if (( $1 == "license") && ( $2 == "type" ) && ( $5 == "Permanent" )) {
		print "Expires:	No Expiration Date"
	}else if ( $1 == "hostname" ) {
		oline=sprintf("Host:		%s",$3)
		print oline
	}else if ( ( $1 == "Feature" ) && ( $2 == "ID" ) ) {
		feat = $5
		active="Active"
		if ( $NF == "-" ) {
			active="Not Active" 
		}
		for (i = 6; i < NF; i++) feat = sprintf("%s %s",feat,$i)
		oline=sprintf("Feature:	%s (%s)",feat,active)
		print oline
	}else if (( $1 == "Expiration" ) && ( $3 == "Expired" )) {
		oline=sprintf("Expired:	%s %s, %s at %s",$5,$6,$8,$7)
		print oline
	}else if (( $1 == "Expiration" ) && ( $3 == "Not" )) {
                oline=sprintf("Expires:	%s %s, %s at %s",$6,$7,$9,$8)
                print oline
	}
	} '> ${tmp_T_file}
	output=`head ${tmp_T_file}`
	# output has blanks when bpminlicense reports no keys to list.
	if [ "${output}x" = "    x" -o "${output}x" = "x" ] ; then
		echo "No license keys have been registered."
		rm -f ${T_file} ${tmp_T_file}
		return 0
	fi

	cat ${tmp_T_file}
	echo "
======================="

	rm -f ${T_file} ${tmp_T_file}
	return 0
}

# -----------------------------------------------------------------------------
# prompts users to enter a host, default is the local machine
# sets global variable $HOST
# returns 3 if host entered is not reachable
# returns 0 if host is reachable
#

Ask_for_host ()
{
	echo ""
	Prompt "Enter the name of the host (default is ${whereami}): "
	read HOST
	if [ "${HOST}x" = "x" ] ; then
		HOST=$whereami
	else
		if [ "${HOST}" != "${whereami}" ] ; then
			hello_host=`ping $HOST 2>/dev/null | grep "is alive"`
			if [ "${hello_host}x" = "x" ] ; then
				echo "
$0:  Could not ping host $HOST."
				HOST=""
				return 3
			fi
		fi
	fi
	return 0
}

# -----------------------------------------------------------------------------
# present user with menu

license_menu ()
{
	not_done=1
	while [ $not_done = 1 ]
	do
		echo "

	License Key Utility
	-------------------

	A) Add a License Key
	D) Delete a License Key
	F) List Active License Keys
	L) List Registered License Keys
	H) Help
	q) Quit License Key Utility

"
		Prompt "Enter a letter: "
		read pick_opt

		case "$pick_opt" in
			a|A)
				Read_key
				add_key
				;;
			d|D)
				Read_key
				delete_key
				;;
			f|F)
				Ask_for_host
				if [ $? = 0 ] ; then
					List_NB_features
				fi
				;;
			l|L)
				Ask_for_host
				if [ $? = 0 ] ; then
					List_L_keys
				fi
				;;
			h|H)
				usage_ext
				;;
			q|Q)
				not_done=0
				;;
			*)
				echo "
\"${pick_opt}\" is an invalid option."
				;;
		esac
	done
exit 0
}

# -----------------------------------------------------------------------------

usage_ext ()
{
	echo "

The command, $0, provides information about NetBackup license keys.

A) Add a License Key
	Allows the user to add a new license key.

D) Delete a License Key
	Allows the user to delete a license key.
	Evaluation NetBackup BusinesServer license keys can not be deleted.

F) List Active License Keys
	List the license keys for products and features that are active.  
	For example, an Evaluation BusinesServer license key that has
	expired, will not be shown.

L) List Registered License Keys
	List all keys (active and inactive) that have been registered.

H) Help
	Displays this information.

q) Quit License Key Utility
	Exit this script, $0.

"
}

# -----------------------------------------------------------------------------

error_exit ()
{
	echo "
$0:  $1
"
	if [ "${2}" = "usage" ] ; then
		exit 22
	fi
	exit 11
}

# -----------------------------------------------------------------------------

###############
#   MAIN
##############

ECHO=echo
NB_DIR=/usr/openv/netbackup
BP_LIC=/usr/openv/netbackup/bin/admincmd/bpminlicense
PERM_FILE=/usr/openv/var/license.txt
BASE_ID=20
NEXT_ID=21

#
#  To make sure bpminlicense strings are in English.
#

LC_ALL=C
export LC_ALL

#
#  Make sure this is being run from root.
#

ISROOT=`/usr/bin/id | egrep "^uid=0\("`
if [ "x${ISROOT}" = "x" ] ; then
	error_exit "must be run while logged in as root."
fi

# get local host name
whereami=`uname -n`

# look for bpminlicense
if [ ! -f ${BP_LIC} ] ; then
	error_exit "${BP_LIC} not found."
fi

# set the HARDWARE variable based on the version file
set_hardware

# if no arguments, default to present commandline menu
if [ $# -eq 0 ] ; then
	license_menu
else 
	# parse for options
	#  { -M <host> } -L [ keys | features ]
	#	keys --	Lists the license keys
	#	features -- Lists the active NetBackup features
	#	If -M is specified, <host> is queried for the information
	#  -I
	#	Initial license key entry:
	#	prompts for a NetBackup base product license key
	#	It must be a NetBackup BusinesServer or DataCenter key.
	#  -A <key>
	#	Add a key:
	#	registers a NetBackup license key
	#  -D <key>
	#	Delete a key:
	#	Deletes a NetBackup license key
	#	You cannot delete a demo license key
	#  -V <feature_id_bit>
	#	Verifies whether the product/feature specified
	#	is licensed on this host
	#
	# exits 11 if in error
	# exits 22 if error in usage
	# 0, 1, 2 are other possible values and mean something

	if [ $# -eq 1 ] ; then
		if [ "${1}" != "-I" ] ; then
			error_exit "Incorrect number of options/arguments were specified." "usage"
		fi
	elif [ $# -eq 2 ] ; then
		if [ "${1}" = "-M" ] ; then
			error_exit "-M <host> requires -L [ keys | features ]" "usage"
		fi
	elif [ $# -eq 4 ] ; then 
		if [ "${1}" != "-M" ] ; then
			error_exit "Too many options were specified." "usage"
		elif [ "${3}" != "-L" ] ; then
			error_exit "-M <host> requires -L [ keys | features ]" "usage"
		fi
	else
		error_exit "Incorrect number of options/arguments were specified." "usage"
	fi

	HOST=${whereami}

	case $1 in
	        -I)     if [ $# = 2 ] ; then
                        	D_KEY=$2
                	else
                        	D_KEY=""
                	fi
 
                	if [ "X${D_KEY}" != "X" ] ; then
                        	check_key $D_KEY
                        	if [ $? != 0 ] ; then
                                	error_exit "Default key, $D_KEY, ${CHK_KEY}" "not"
                        	fi
                        	D_DESCRIPT="(Default is ${DEMO_TITLE}${PROD_TITLE})"
                	else
                        	D_DESCRIPT=""
                	fi

			# Check for an existing base key
			# this displays wants active
			what_reg
			# if what_reg fails with a status 11, its a check_Lfile error.

			# we may want to provide checking for reinstalls to
			# disallow
			#	DataCenter to BusinesServer
			#	Permanent to Demo
			# or provide the current active key as the default (done by install_bp).

			not_nb=0 
			while [ $not_nb = 0 ] ; do
				echo "
${whats_needed} 
for installation to continue.
${D_DESCRIPT}"
				Read_key

				# no key was entered
	                        if [ "x${L_KEY}" = "x" ] ; then
	                        	if [ "X${D_KEY}" = "X" ] ; then
                                		echo "
You must enter a license key for the installation to continue.
"
                                		continue
					else
						L_KEY=${D_KEY}
					fi
                        	fi

				check_key $L_KEY
				if [ $? != 0 ] ; then
					# error with key entered (or the default)
					echo "${L_KEY}: ${CHK_KEY}"
					continue
				fi
				# if default or entered key is a demo key, confirm with user
				if [ "X${DEMO_TITLE}" != "X" ] ; then
					echo "${DEMO_TITLE}${PROD_TITLE}${DOD_LINE} will be registered."
					if confirm_yes "Is this OK?"
					then
						# move on with key registration
						echo ""
					else
						# ask for another key
						continue
					fi
				fi
				
				# check key sets global variables about a key
				if [ $IS_BASE_KEY = 1 ] ; then
					add_key
					if [ $? != 0 ] ; then
						continue
					fi
					not_nb=1
					if [ $PROD_ID = 6 ] ; then
						exit 2 # for DataCenter
					else
						exit 1 # for BusinesServer
					fi
				else
					echo "${L_KEY}: 
	${CHK_KEY}
This is not a NetBackup Base product license key."
				fi
			done
			;;
		-D)
			L_KEY=$2
			delete_key
			if [ $? -eq 1 ] ; then
				exit 11
			fi
			;;
		-A)
			L_KEY=$2
			add_key
			if [ $? -eq 1 ] ; then
				exit 11
			fi
			;;
		-M | -L)
			if [ "${1}" = "-M" ] ; then
				HOST=$2
				hello_host=`ping $HOST 2>/dev/null | grep "is alive"`
				if [ "${hello_host}x" = "x" ] ; then
					error_exit "Could not ping host $HOST."
				fi
			fi

			LIST_TYPE=$2
			if [ "$LIST_TYPE" = "keys" ] ; then
				List_L_keys
			elif [ "$LIST_TYPE" = "features" ] ; then
				List_NB_features
			else
				error_exit "$LIST_TYPE is not a valid argument for the -L option." "usage"
			fi
			;;
		-V)
			PROD_ID_BIT=$2

			for bit in ${NEXT_ID} ${PROD_ID_BIT} ; do
				check_4_feature ${PERM_FILE} ${bit}
				stat=$?
				if [ ${stat} -eq 11 ] ; then
					# license file in a bad state
					exit 11
				elif [ ${stat} -eq 1 ] ; then
					# 0 means feature is verified as registered
					exit 0
				fi
			done

			# 1 means feature is not registered.
			exit 1
			;;
		*)
			error_exit "$1 is an invalid option." "usage"
			;;
	esac
fi
