#!/sbin/sh
#
#ident	"@(#)license_manager	1.0	93/05/10 UvA"	/* gert */
#
# /etc/init.d/license_manager - Start/Stop the lmgrd daemon
#
PATH=/usr/bin:/bin
export PATH
umask 022


yes () {
set +x
while :
do
 echo "$@"
done
}

rotatelog()
{
  if [ ! -s "$1" ]; then return ; fi
    prev=
    for i in `echo \`yes "" | cat -n | head -5 | sort -nr\` ` 0
    do
        if [ "$prev" = "" ]; then prev=$i; continue; fi
        test -f $1.$i.Z && mv $1.$i.Z $1.$prev.Z
        prev=$i
    done
    mv $1 $1.0
    compress -f $1.0
    cp /dev/null $1
    chgrp 0 $1
    chmod 640 $1
}

case $1 in 
'start')
    # When the system crashed, save the log at startup.
    if [ -s /var/adm/lmgrd.log ]
    then
	rotatelog /var/adm/lmgrd.log
    fi
    if [ -f /opt/SUNWspro/SunTech_License/bin/lmgrd -a \
	 -f /usr/local/flexlm/licenses/license.dat ]; then
	    /opt/SUNWspro/SunTech_License/bin/lmgrd -c \
		    /usr/local/flexlm/licenses/license.dat \
		    >/var/adm/lmgrd.log 2>&1 &
    fi	
    ;;
'stop')
    pid=`ps -e | grep lmgrd | sed -e 's/^  *//' -e 's/ .*//'`
    if test "$pid"
    then
	# lmdown shutsdown all license managers
	#/opt/SUNWspro/SunTech_License/bin/lmdown \
	#	-c /usr/local/flexlm/licenses/license.dat -q
	kill "$pid"
	if [ -s /var/adm/lmgrd.log ]
	then
	    rotatelog /var/adm/lmgrd.log
	fi
    fi
    ;;
*)
    echo "usage: /etc/init.d/license_manager {start|stop}"
    ;;
esac
