#!/bin/sh
#
# Copyright 05/16/01 Sun Microsystems, Inc. All Rights Reserved.
#
# pragma ident  "@(#)es-add-machine	1.6 01/05/16 Sun Microsystems"
#

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

set_basedir
set_db_env

PROGNAME=$0

if [ $# -ne 1 ] ; then
        echo "   "
        echolog "Please specify the input file to be used"
        echo "   "
        echolog 'Usage : $2 file' "$PROGNAME"
        echo " "
        exit 1
fi

FILENAME=$1

if [ ! -f "$FILENAME" ] ; then
        echo "   "
        echolog "Please specify the input file to be used"
        echo "   "
	echolog 'File : $2 not found' "$FILENAME"
	echo ""
	exit 2
fi

#
# Procedure that performs the database addition
#
# Input : machinename machinetype [ machineweight ] 
#
# e.g. - Machine1 Desktop
# e.g  - Machine2 ServerGroup 100
#	
addMachineType() {

	machinename=`echo $1 | awk '{print $1}'`
	machinetype=`echo $1 | awk '{print $2}'`
	machineweight=`echo $1 | awk '{print $3}'`

	if [ -n "$machinename" -a -n "$machinetype" ]; then

		sqlstring="call sp_add_licensable_host_type('$machinename','$machinetype','$machineweight');"
		#echo "sqlstring = $sqlstring"
		result=`echo $sqlstring | $ORACLE_HOME/bin/sqlplus sunmc/sunmc` 
		#echo "result = $result"

		success=`echo $result | grep 'Call completed'`
		if [ "$success" ] ; then
			echolog '$2 $3 $4 - SUCCESSFULLY added' "$machinename" "$machinetype" "$machineweight"
		else
			echolog '$2 $3 $4 - FAILED added' "$machinename" "$machinetype" "$machineweight"
		fi
	fi

}

NUMLINES=`/usr/bin/wc $FILENAME | awk '{print $1}'`
counter=$NUMLINES;
command=`sort -u -r $FILENAME | head -$counter | tail -1 | sed -e 's/	/ /g' -e 's/  */ /g' -e '/^ $/d'` 
while [ $counter -gt 0 ]
do

	if [ -n "$command" ] ;
	then
		addMachineType "$command"
	fi

	counter=`expr $counter - 1`
	#
	# retrieve the next line
	#
	command=`sort -u -r $FILENAME | head -$counter | tail -1 | sed -e 's/	/ /g' -e 's/  */ /g' -e '/^ $/d'` 
done

exit 0
