#!/bin/csh
#	@(#)putlink	1.1	7/22/93
#
#
#
#     $Id: putlink,v 1.1 1998/03/16 20:02:58 avvede Exp $
#     $Log: putlink,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:
#		putlink
#
#	Description:
#		Procedure to put linked applications into a library.
#		See usage line below.
#
#	Change				Reason
#	No.	Date		Name	for Change
#
#	0	21-jul-93	lmg	Original.
#
set lib=				# target library name
set libpath=.				# path, if library is not defined
set host=
set hostlib=				# host library to extract from
set hostpath=
#
set filelist=
set shortlist=
set olist=
set oldobjs=
#
unalias cd
unalias rm
#
#	set up command name
#
if ($?0 == 0) then
	set cmd=putlink
else
	set cmd=$0
endif
#
#	Check argument count and get all parameters and list of files
#	-L defines LINK library
#
if ($#argv < 3) then
usage:
	echo "usage: $cmd [-L [path][libname.a]] -h host[:[path][libname.a]] {program_name[.cob]}"
	echo "             -L [path][libname.a]		- resulting library"
	echo "             -h host[:[path][libname.a]]	- host library"
	goto error
endif
#
set files=
set opt = ( `getopt -h:L: $* `)
while ( $#opt )
	set nextopt=$opt[1]
	switch ( "$nextopt" )
		case "-L":				# Library
			shift opt
			set outlib=$opt[1]
			if ($outlib:e == "a") then	# Library itself
				set lib=$outlib
				if ( `echo $lib:t | grep ^lib` == "") then
					echo "${cmd}: invalid user's LINK library name" $lib
					goto usage
				endif
				set libpath=`dirname $lib`

			else				# Library path
				set libpath=$outlib
			endif
			if ( ! -d $libpath ) then
				echo "${cmd}: path $libpath does not exist"
				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
#
# 	Convert local library to absolute path
#
set curdir=$cwd
cd $libpath
set fullpath=$cwd
cd $curdir
#
# 	Defaults for host library
#
if ( ${hostlib} == "" ) then
	set hostlib=$lib
endif
if ( ${hostpath} == "" ) then
	set hostpath=$fullpath
endif
#
#	If library was specified
#	Create a new one or extract objects from the old one
#
if ( $lib != "" ) then
	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
	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
endif
#
#	Figure out application names and extensions
#
onintr goodbye
foreach appname ( `echo $files` )
	if ($appname:r != $appname) then
		set ext="."$appname:e
		if ( ($ext != ".cob") && ($ext != ".COB") ) 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 ${shortname}tab.o)
end
#
if ( "$filelist" == "" ) then					# Nothing useful is found
	goto error
endif
#
# 	Get all objects
#
set myhost=`hostname`
if ( ${hostlib} != "" ) then

	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

else

	foreach appname ( `echo $filelist` )
		set shortname=`echo ${appname}`
		set objs="${shortname}.o ${shortname}tab.o"
		echo rsh $host "'cd /tmp; ar x ${hostpath}/lib${appname}.a ${objs} ; rcp ${objs} ${myhost}:$cwd; 'rm' -f ${objs}'"
		rsh $host "unalias cd rm; cd /tmp; ar x ${hostpath}/lib${appname}.a ${objs} ; rcp ${objs} ${myhost}:$cwd; 'rm' -f ${objs}"
		if ( $status == 1 || `ls ${objs} | wc -l` != 2 ) then
			goto error
		endif
	end

endif
#
# 	Make library [libraries]
#
if ( $lib != "" ) then					# One library for all
	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") || ($oldname == "${shortname}tab.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}."
else
#
#	Individual lib for each module
#
	foreach appname ( `echo $filelist` )
		set shortname=`echo ${appname}`
		set lib=${libpath}/lib${appname}.a
		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

		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 ${shortname}.o ${shortname}tab.o"
		ar rv ${fullib} ${shortname}.o ${shortname}tab.o
		ranlib ${fullib}
		chmod 777 ${fullshlib}.
		liblocker  -f ${fullshlib}. "rm -f ${fullshlib}."
	end
endif
#
#	Get out of town
#
goodbye:
	if ( $?save == 0 ) then
		"rm" -f  ${olist} ${oldobjs}
	endif
	"rm" -f __.SYMDEF
	exit 0
error:
	if ( $?must == 0 ) then
		"rm" -f ${olist} ${oldobjs}
	endif
	"rm" -f __.SYMDEF
	exit 1

