#!/usr/bin/ksh
# @(#)instset.sh	1.0	11/15/00
#

LMICONFIG_PATH=/var/opt/lanman/config
LMICONFLOCK=/tmp/.pcnl.findinstance

MAXINST=50


oflag="add"

while [ /bin/true ]
do
	case $1 in
		-a)
			oflag="add"
			shift
			index=$1
			path=$2
			lhost=$3
			;;
		-d)
			oflag="delete"
			shift
			index=$1
			shift
			;;
		*)
			break
			;;
	esac
done


#if [ ! -f $LMICONFLOCK ] 
#then
#	exit 1
#fi

cp /dev/null ${LMICONFIG_PATH}.1

limit=`expr $MAXINST + 1`

if [ "$index" = "" ]
then
	exit 1
fi

if [ $oflag = "add" ]
then
	status=`while read inum path lhost 
	do
	    if [ $inum -le 0 -o $inum -ge $limit ]
	    then
		continue
	    fi 

	    if [ $index -eq $inum ]
	    then
		echo 2
		index=$limit
	    elif [ $index -le $inum ] 
	    then
		echo $1 $2 $3 >> ${LMICONFIG_PATH}.1 
		index=$limit
		echo 1
	    fi
	    echo $inum $path $lhost >> ${LMICONFIG_PATH}.1
	done < $LMICONFIG_PATH`

	if [ ${status:-0} -eq 2 ]
	then
		exit 1
	elif [ ${status:-0} -ne 1 ]
	then 
		echo $index $2 $3 >> ${LMICONFIG_PATH}.1
	fi
	if [ -f ${LMICONFIG_PATH}.1 ]
	then
	    mv ${LMICONFIG_PATH}.1 $LMICONFIG_PATH
	fi
elif [ $oflag = "delete" ]
then
	while read inum path lhost 
	do
	    if [ $inum -le 0 -o $inum -ge $limit ]
	    then
		continue
	    fi 

	    if [ $index -eq $inum ]
	    then
		continue
	    else
	        echo $inum $path $lhost >> ${LMICONFIG_PATH}.1
	    fi
	done < $LMICONFIG_PATH
	if [ -f ${LMICONFIG_PATH}.1 ]
	then
	    mv ${LMICONFIG_PATH}.1 $LMICONFIG_PATH
	fi
fi

#rm $LMICONFLOCK

exit 0

