specfile=""
target=""
libfile=""
qflag=0
while [ "$1" != "" ] ; do
	case $1 in
	-s)	specfile=$2; shift;;
	-t)	target=$2; shift;;
	-h)	libfile=$2; shift;;
	-q)	qflag=1;;
	*)	echo "Illegal argument: " $1 >&2; exit 1;;
	esac
	shift ;
done
[ "$specfile" = "" ] && {
	echo "Missing spec file" >&2; exit 1;
}
[ "$target" = "" ] && {
	echo "Missing target file" >&2; exit 1;
}
[ "$libfile" = "" ] && {
	echo "Missing library file" >&2; exit 1;
}


sed -e '1,/#branch/d
	/#objects/,$d' $specfile | ${ETCDIR}/mkjumptab >jumptable.s

if [ -n "`grep '#objects noload' $specfile`" ]; then
	for i in `sed -e '1,/#objects noload/d;/#branch/,$d' $specfile | tr '\\\n' ' '`
	do
		nm $i | grep 'A ___SHLIB__' | sort -u \
		| awk '{print ".globl\t"$3"\n"$3":"; }' >>jumptable.s
	done
fi

gas jumptable.s -o jumptable.o
ld -r jumptable.o -o lib.o
sed -e '1,/#branch/d' $specfile \
| sed -e '1,/#objects/d; /#init/,/#externals/d' >spec.objects
tr '\\\n' ' ' <spec.objects | xargs ld -r lib.o -o lib.o

ld -Rt `awk '/#address *\.text *0x/ { print substr($3,3); }' $specfile` \
	-Rd `awk '/#address *\.data *0x/ { print substr($3,3); }' $specfile` \
-m lib.map lib.o -o lib.out `sed -e '1,/#objects noload/d;/#branch/,$d' $specfile | tr '\\\n' ' '`

if [ $? -ne 0 ] ; then exit 1; fi

${ETCDIR}/convcoff lib.out $target

if [ $? -ne 0 ] ; then exit 1; fi

rm -fr tmp
mkdir tmp

TADDR=`awk '/#address *\.text/ { print "0"substr($3,3)"H"; } ;' <$specfile`
echo "$target" >target
xargs <spec.objects nm -o | grep ' A '| tr -d ':' \
	| awk '{ print $4" "$1; }' | sort >data4

#
# Write out all the files for jump-table access
#
awk '/#address *\.text/ { TADDR="0x"substr($3,3); }
     /#/		{ junk = 1; }
     /#branch/, /#objects/ {
	if (length($1) > 0 && !junk)
	print "_"$1"\tEQU\t"TADDR"+"($2-1)*5;
	junk = 0; }' <$specfile | sort | join - data4 | sort +3 \
| awk '
	BEGIN { rn = ""; rfn = ""; getline target <"target"; }
	{
		if ($4 != rfn) {
			if (fn != "")
				close(fn);
			rfn = $4; nfn = rfn;
			slash = index(nfn, "/");
			if (slash != 0)
				nfn = substr(nfn, slash+1);
			slash = index(nfn, "/");
			if (slash != 0)
				nfn = substr(nfn, slash+1);
			slash = index(nfn, "/");
			if (slash != 0)
				nfn = substr(nfn, slash+1);
			fn = "tmp/" substr(nfn, 1, index(nfn, ".")) "s";
			print  "\t.file\"\t" substr(nfn, 1, index(nfn, ".")-1)".s\"" >>fn;
			printf "\t.data\n\t.long\t___SHLIB__%s\n", target >>fn;
		}
		print "\t.globl\t" $1 >>fn;
		print "\t.data\nJ"$1":\t.long\t"$3 >>fn;
		print "\t.text\n"$1":\tjmp\t*J"$1 >>fn;
	} '


sed -e '1,/#init/d
        /#hide/,$d
	/^ *$/d
	/#/d' <$specfile | sort -u | awk '{print "_" $2 " _" $1; }' >data3

echo "	TITLE	libinit.asm
        .386
EXTRN	init_${target}:NEAR
DGROUP	GROUP	XI
XI	SEGMENT DWORD USE32 PUBLIC 'DATA'
PUBLIC	___SHLIB__$target
___SHLIB__$target	DD	init_${target}
XI	ENDS
	END
" >libinit.asm
(cd tmp ;
 find . -name '*.s' | awk '
	{ fn = substr($1,3);
	  dot=index(fn,".");
	  system("gas "substr(fn,1,dot-1)".s -o "substr(fn,1,dot-1)".o");
	}'
 find . -name '*.o' | xargs ar r ../$libfile;
)
rm -f libglob.s
nm lib.out | sort +2 >lib.nm
join -j1 3 -j2 2 lib.nm data3 \
	| awk '{ print $1"=\t0x"substr($2,6);
		 print "\t.globl\t"$1; } ' >> libglob.s
sed -e '1,/#export/d' <spec | awk '{print "_"$1;}' | join -j2 3 - lib.nm \
	| awk '{print ".globl\t"$1"\n"$1"=\t0x"substr($2,6);}' >> libglob.s
echo "	.text
	.globl	init_${target}
init_${target}:
" >>libglob.s
if [ -n "`grep '#objects noload' $specfile`" ]; then
	for i in `sed -e '1,/#objects noload/d;/#branch/,$d' $specfile | tr '\\\n' ' '`
	do
		nm $i | grep 'U ___SHLIB__' | sort -u \
		| awk '{ print "\tcall\tinit"substr($2,10); }' >>libglob.s
	done
fi


awk '{ print "\tmovl\t$"$1","$2; }' data3 >>libglob.s
echo "\tret" >>libglob.s

masm libinit.asm
gas libglob.s -o libglob.o
ar r $libfile libglob.o libinit.o
ranlib $libfile
rm target libinit.asm libglob.s libinit.o libglob.o lib.o lib.out lib.nm
rm data4 data3 jumptable.s spec.objects *.map *.nm libglb.asm libvec.s
rm -fr tmp
