#!/bin/sh
#	@(#)startapp	1.1	02/25/94
#
#
#     $Id: startapp,v 1.1 1998/03/16 20:03:01 avvede Exp $
#     $Log: startapp,v $
#     Revision 1.1  1998/03/16 20:03:01  avvede
#     Initial move to CVS
#
#
#
#
#	Copyright (c) 1986, 1987, 1988 by
#	Periphonics Corporation
#	4000 Veterans Memorial Highway
#	Bohemia
#	NY 11716
#
#	This software is furnished under a license and may be used and
#	copied only in accordance with the terms of such license and with
#	the inclusion of the above copyright notice. This software or any
#	other copies thereof, may not be provided or otherwise made available
#	to anyone other than the licensee. Title to and ownership of this
#	software remains with the Periphonics Corporation.
#
#	The information in this document is subject to change without notice
#	and should not be construed as a commitment by Periphonics.
#
#	Periphonics assumes no responsibility for the use or reliability of
#	its software on equipment that is not supplied by Periphonics.
#
#	File Name:
#		startapp
#
#	Description:
#		Procedure to produve VRAM applications.
#		See usage line below.
#
#	Change				Reason
#	No.	Date		Name	for Change
#
#	0	19-mar-92	lmg	Original.
#	1	24-mar-92	lmg	Changed default for generated stopapp.
#	2	26-mar-92	rww	Only strip out openwin library from
#					load path.
#	3	25-sep-92	lmg	Removed vps# check to permit to use
#					names.
#	4	28-dec-92	rww	Make stopapp executable.
#	5	13-dec-93	lmg	Changed for VENGINE.
#
#       startapp [-s] # # # app [options]
#
vengine=vengine
if [ "$1" = "-s" ]; then
	stopapp=yes
	shift
else
	stopapp=no
fi
if [ -z "$3" ]; then
        echo "$0: [-s] vps linelow linehigh application [opts]"
        exit 1
fi
vps=$1
curl=$2
if [ -z "`expr $curl + 1 2>/dev/null`" ]; then
        echo "$0: bogus starting line $curl"
        exit 1
fi
endl=$3
if [ -z "`expr $endl + 1 2>/dev/null`" ]; then
        echo "$0: bogus ending line $endl"
        exit 1
fi
if [ `expr $curl \> $endl` = 1 ]; then
        echo "$0: starting line $curl greater than ending line $endl"
        exit 1
fi
#
# Get application name and all options
#
app=$4
shift
shift
shift
shift
count=0
wcount=5
#
#       Check for pre-existing pid file.
#
if [ "$stopapp" = "yes" ]; then
	pidfile=stopapp$vps
	if [ -f $pidfile ]; then
        	echo -n "$0: process id file $pidfile already exits - continue? "
        	read yes
        	if [ "$yes" != "y" -a "$yes" != "yes" ]; then
                	exit 1
        	fi
        	/bin/rm -f $pidfile
	fi
#
#       Initialize pid file.
#
	cat > $pidfile <<_EOF_
#!/bin/sh
#
#       usage: $pidfile [SIGNAL]
#
if [ "\$1" != "" ]; then
        signal="\$1"
else
        signal=INT
fi
_EOF_
	chmod +x $pidfile
fi
#
#       Turn off openwin library for applications.
#
LD_LIBRARY_PATH=`echo ${LD_LIBRARY_PATH} | sed -e "s;/usr/openwin/lib:;;"`
export LD_LIBRARY_PATH
#
#       Generate processes.
#
while true
do
        ${vengine} $app -v $vps -p $curl "$@" &
	if [ "$stopapp" = "yes" ]; then
        	echo kill -\$signal $! >> $pidfile
	fi
        count=`expr $count + 1`
        if [ "expr $count % $wcount" = 0 ]; then 
                sleep $wcount
                slept=y
        else
                slept=n
        fi
        if [ $curl = $endl ]; then
                if [ $slept = n ]; then
                        sleep $wcount
                fi
                exit 0;
        fi
        curl=`expr $curl + 1 2>/dev/null`
done
