#!/bin/csh
#	@(#)putcall	1.1	7/22/93
#
#
#
#     $Id: putcall,v 1.1 1998/03/16 20:02:58 avvede Exp $
#     $Log: putcall,v $
#     Revision 1.1  1998/03/16 20:02:58  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:
#		putcall
#
#	Description:
#		Procedure to produce user's CALL functions.
#		See usage line below.
#
#	Change				Reason
#	No.	Date		Name	for Change
#
#	0	22-jul-93	lmg	Original.
#
if ( ${?ASEHOME} == 0 ) then
	if ( ${?VASHOME} == 0 ) then
		setenv ASEHOME /usr/ase
	else
		setenv ASEHOME $VASHOME
	endif
endif
#
set lib=$ASEHOME/lib/libcall.a
set host=
set hostlib=				# host library to extract from
set hostpath=
set shortlist=
set olist=
set oldobjs=
#
unalias cd
unalias rm
#
#	set up command name
#
if ($?0 == 0) then
	set cmd=putcall
else
	set cmd=$0
endif
#
#	check argument count and get all parameters and list of files
#
if ($#argv < 3) then
usage:
	echo "usage: $cmd [-C [path]libname.a] -h host[:[path][libname.a]] {C-filename[.c]}"
	echo "             -C [path]libname.a	- library name, default $ASEHOME/lib/libcall.a"
	echo "             -h host[:[path][libname.a]]	- host library"
	goto error
endif
#
set files=
set opt = ( `getopt -C:h: $* `)
while ( $#opt )
	set nextopt=$opt[1]
	switch ( "$nextopt" )
		case "-C":				# Library name
			shift opt
			set lib=$opt[1]
			if ($lib:e != "a" || `echo $lib:t | grep ^lib` == "") then
				echo "${cmd}: invalid user's CALL library name" $lib
				goto usage
			endif
			breaksw
		case "-h":				# Host Library
			shift opt
			set hostdef=`echo $opt[1] | awk -F: '{ print $1" "$2 }'`
			set host=$hostdef[1]		# Host machine
			if ( $#hostdef > 1 ) then
				shift hostdef
				if ($hostdef:e == "a") then	# Library itself
					set hostlib=$hostdef:t
					if ( `echo $hostlib | grep ^lib` == "") then
						echo "${cmd}: invalid user's host LINK library name" $hostlib
						goto usage
					endif
					set hostpath=`dirname $hostdef`

				else				# Library path
					set hostpath=$hostdef
				endif
			endif
			breaksw
                case "--":
                     	breaksw
		default:
                     	set files = "$files $nextopt"
		     	breaksw
	endsw
	shift opt
end					# End of WHILE loop to get makeapp options
#
if ( "$files" == "" || "$host" == "" ) then
	goto usage
endif
#
#	Set target and source libs and paths
#
set shlib="$lib:r".so
set libpath=`dirname $lib`		# directory path, may be with '.'
if ( ! -d $libpath ) then
	echo "${cmd}: path $libpath does not exist"
	goto usage
endif
set curdir=$cwd
cd $libpath
set fullpath=$cwd
cd $curdir
set fullib="$fullpath"/"$lib:t"		# make a full library name with abs path
set fullshlib="$fullib:r".so	# make a full shared library name with abs path
#
# 	Defaults for host library
#
if ( ${hostlib} == "" ) then
	set hostlib=$lib:t
endif
if ( ${hostpath} == "" ) then
	set hostpath=$fullpath
endif
#
#	Create a new lib or extract objects from the old one
#
if ( ! -r $fullib ) then		# make lib if it does not exist
	echo "ar r $fullib"
	ar r $fullib
else
	echo "ar x $fullib"
	ar x $fullib			# extract old objects
	if ( $status == 1 ) then
		goto error
	endif
	set oldobjs=`ar t $fullib`
	if ( "$oldobjs" != "" ) then
		if ( $oldobjs[1] == "__.SYMDEF" ) then
			shift oldobjs	# get rid of __.SYMDEF
		endif
	endif
endif
if (! -r $fullshlib) then
	ld -o $fullshlib
endif
#
#	figure out application names and extensions
#
onintr goodbye
set filelist=
set clist=
foreach appname ( `echo $files` )
	if ($appname:r != $appname) then
		set ext="."$appname:e
		if ( $ext != ".c" ) then
			echo "${cmd}: invalid extention $ext in file name $appname"
			continue
		endif
		set appname=$appname:r
	endif

	set filelist="$filelist ${appname}"	# List with stripped extentions
	set shortname=`echo ${appname}`
	set shortlist=($shortlist ${shortname})			# List with stripped extentions
	set olist=($olist ${shortname}.o)
end
#
if ( "$filelist" == "" ) then
	goto error
endif
#
# 	Get all objects. Hostlib is always defined
#
set myhost=`hostname`
echo rsh $host "'cd /tmp; ar x ${hostpath}/${hostlib} ${olist}; rcp ${olist} ${myhost}:$cwd; 'rm' -f ${olist}'"
rsh $host "cd /tmp; ar x ${hostpath}/${hostlib} ${olist}; rcp ${olist} ${myhost}:$cwd; 'rm' -f ${olist} "
if ( $status == 1 || $#olist != `ls ${olist} | wc -l` ) then
	goto error
endif
#
# 	Cut long names and make list of *.o
#
foreach shortname ( `echo $shortlist` )		# truncate long names
#
#	Remove compiled objects from the list of old objs
#	We cannot use foreach loop, because empty elements screw everything
#
	@ k = 1
	@ found = 0
	set nwords=${#oldobjs}
	while ( $k <= $nwords )
		set oldname=$oldobjs[$k]
		if ( $oldname == "${shortname}.o" ) then
			set oldobjs[$k]=
			@ found = $found + 1
		endif
		if ( $found == "2" ) then
			break
		endif
		@ k = $k + 1
	end
end
#
# Put all objects into lib
#
if ( -e ${fullshlib} ) then
	"cp" ${fullshlib} ${fullshlib}.		# Library for current use by applications
else
	touch ${fullshlib} ${fullshlib}.
endif
chmod 777 ${fullshlib}
liblocker  ${fullib} "ld -o ${fullshlib} -assert pure-text  ${oldobjs} ${olist}"
ar rv ${fullib} ${olist}			# Save everything
ranlib ${lib}
chmod 777 ${fullshlib}.
liblocker  -f ${fullshlib}. "rm -f ${fullshlib}."
#
# Farewell
#
goodbye:
	"rm" -f ${oldobjs} ${olist}
	exit 0
error:
	"rm" -f ${oldobjs} ${olist}
	exit 1
