#!/bin/csh
#	@(#)makeapp	1.5	06/19/97
#
#
#
#     $Id: makeapp,v 1.1 1998/03/16 20:02:54 avvede Exp $
#     $Log: makeapp,v $
#     Revision 1.1  1998/03/16 20:02:54  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:
#		makeapp
#
#	Description:
#		Procedure to produve VRAM applications.
#		See usage line below.
#
#	Change				Reason
#	No.	Date		Name	for Change
#
#	0	13-Dec-91	rww	Original.
#	0	13-Dec-93		Original for rel5.
#	1	18-feb-94	lmg	Made a mechanizm to update on the fly
#	2	29-mar-95	lmg	Added -W.
#	3	10-jun-97	lmg	Added amu for vengine as a service.
#	4	19-jun-97	lmg	Merged with rel5. Don't execute if ASEHOME is not set.
#
if ( ${?ASEHOME} == 0 ) then
	echo "$0 : Environment ASEHOME is not set"
	exit 1
endif
set VASVRC=vrc
#
# Check if it's IS
#
set is="yes"
if ( -r ${ASEHOME}/etc/services ) then
	set vvpether=`egrep '^vvpether' ${ASEHOME}/etc/services`
	if ( "$vvpether" != "" ) then
		set is="no"
	endif
endif
if ( $is == "yes" ) then
	$VASVRC -W $*
	exit $status
endif

#
#	set up command name
#
if ($?0 == 0) then
	set cmd=makeapp
else
	set cmd=$0
endif
if ($#argv < 1) then
usage:
	echo "usage: $cmd [-w] [-W] [-d] [-c] [-q] [-s] [-l #] app_name1[.cob] [app_name2[.cob]] ..."
	echo "             -w			- suppress VRAM compiler warnings"
	echo "             -W			- print warnings on old style VPS code"
	echo "             -d			- enable diagnostic statements"
	echo "             -c			- enable column warnings"
	echo "             -q			- enable quiet VRAM compilation"
	echo "             -s			- output VRAM compiler statistics"
	echo "             -l #		- enlarge line context (0-3) on syntax errors"
	exit 1
endif
#
# Check all arguments
#
unalias cd
unalias rm
set opt = ( `getopt -wWdcqsl: $* `)
set files =
set vrcopts=
while ( $#opt )
	set nextopt=$opt[1]
	switch ( "$nextopt" )
                case "-w":
                case "-W":
                case "-s":
                case "-d":
                case "-c":
                case "-q":
                case "-l":
                	set vrcopts = "$vrcopts $nextopt"
			if ( "$nextopt" == "-l" ) then
				shift opt
				set vrcopts = "$vrcopts $opt[1]"
                     	endif
			breaksw
                case "--":
                     	breaksw
	default:
                     	set files = "$files $nextopt"
		     	breaksw
	endsw
	shift opt
end						# End of WHILE loop to get makeapp options
if ( "$files" == "" ) then
	goto usage
endif
#
# Save all files and recompile
#
set exitval=0
set fullapp=
onintr goodbye
foreach appname ( `echo $files` )
#
# 	First of all we need to know
# 	the full path to check existence of all files
#
	set libpath=`dirname $appname`		# directory path, may be with '.'
	set curdir=$cwd
	cd $libpath
	set fullpath=$cwd
	cd $curdir

	set fullapp="$fullpath"/"$appname:t"	# make a full appname with abs path
	set fullapp="$fullapp:r"		# 		but without extention
#
#	Chech if VRAM file exists
#	Note that the compiler will handle paths
#
   	if ($appname:r != $appname) then
		set ext="."$appname:e
		if ( ($ext != ".cob") && ($ext != ".COB") ) then
			echo "${cmd}: invalid extention $ext in file name $appname"
			set exitval=1
			continue
		endif
		set appname=$appname:r
   	else
		if (-r ${appname}.cob) then
			set ext=.cob
		else
			if (! -r ${appname}.COB) then
				echo "${cmd}: cannot find VRAM source code for $appname"
				set exitval=1
				continue
			endif
			set ext=.COB
		endif
   	endif
#
#	Verify if application already exists and should be saved
# 	Note that if *.vex is moved and removed it will preserve file mmaping
#
	set appexist=0
   	if ( -f $fullapp.vex ) then
		set appexist=1
		"mv" ${fullapp}.vex ${fullapp}.vex.
   	endif
#
#	Make it!
#
	${VASVRC} $vrcopts ${appname}${ext}
	if ( $status == 1 ) then
		set exitval=1
		echo Compilation of file ${appname} failed
		if ( $appexist == 1 ) then
			echo "	Old version ${fullapp} reinstalled"
			"mv" ${fullapp}.vex. ${fullapp}.vex
		endif
	else
		if ( $appexist == 1 ) then
#
#			We need to inform all applications to remmap
#
			amu -c "p b request location close ${fullapp}" >& /dev/null
			amu -c "s vengine request location close ${fullapp}" >& /dev/null
#
#			Remove the old guy (Thanx to UNIX. It will be around untill all remmap)
#
			"rm" ${fullapp}.vex.
		endif
	endif
	echo " "
end
#
# Exit
#
exit $exitval
#
# We were interrupted
#
goodbye:
if ( "$fullapp" != "" && -f "$fullapp.vex." ) then
	"mv" "$fullapp.vex." "$fullapp.vex"
endif
exit 1
