#!/bin/sh
# dll abstractions for various platforms..
# (c) 1993 Bristol Technology Inc.
# All rights reserved.
#
# dllink abstracts the various platform linker quirks from the user.
# All platform specific uglyness is handled here.
#
#        $0     $1          $2          $3      $4
# usage: dllink dllname dllname.def file1.o file2.o ...

WUEXP=-bI:$HHHOME/HH.exp

if [ $# -gt 0 ] && [ $1 = "-s" ]  # indicates building Wind/U from source
then
	WUEXP=-bI:../Install/WindU.exp
	shift
fi

if [ $# -lt 3 ]
then
	echo "usage: dllink dllname dllname.def file1.o file2.o ..."
	exit 1
fi

OUTFILE=$1
DLLBASENAME=`basename $1`
DLLDIRNAME=`dirname $1`
shift
DEFFILE=$1
shift

#######################
# Identify Architecture
#######################
# figure it out ourselves
# Try to use "uname" - it's POSIX-compliant
ARCH="ERROR"
( exec uname ) > /dev/null 2>&1
if [ $? -eq 0 ]; then
  case "`uname`" in
     OSF1) ARCH=decaosf ;;
    HP-UX) ARCH=hp700;;
     IRIX) ARCH=sgi ;;
      AIX) ARCH=rs6000 ;;
  UNIX_SV) ARCH=ncr ;;
    SunOS) case "`uname -r`" in
             4.1.*) ARCH=sun4 ;;
               5.1) ARCH=sol2_x86 ;;
               5.*) ARCH=sol2 ;;
                 *) ARCH="ERROR" ;;
           esac ;;
        *) ARCH="ERROR" ;;
  esac
fi
if [ "$ARCH" = "ERROR" ]; then
  if [ -f /usr/kvm/stand/kadb ] ; then
    if [ `/bin/arch` = "sun4" ] ; then
      ARCH=sun4
    fi
  elif [ -f /hp-ux ] ; then
    ARCH=hp700
  elif [ -f /usr/lib/boot/unix ] ; then
    ARCH=rs6000
  elif [ -f /ufsboot ] ; then
        if [ "`uname -m`" = "i86pc" ]
        then
          ARCH=sol2_x86
        else
          ARCH=sol2
        fi
  else
    ARCH="ERROR"
  fi
fi


case "$ARCH" in
hp700)
	# get list of EXPORTS

	EXPORTLINE=`grep -n -i "^exports" $DEFFILE|cut -d: -f1`
	TOTALLINES=`wc -l $DEFFILE | cut -c6-400 |cut -d'     ' -f1`
	TAIL=`expr $TOTALLINES - $EXPORTLINE`
	
	EXPORTS="+e WEP +e LibMain +e WINDU_Menus +e WINDU_Dialogs +e WINDU_Icons +e WINDU_Cursors +e WINDU_Bitmaps +e WINDU_Accelerators +e WINDU_Stringtable +e WINDU_RCDATA "

	for i in `tail -$TAIL $DEFFILE | cut -d'@' -f1`
	do
		EXPORTS="$EXPORTS +e $i"
	done

	# do the link
	ld -E -b -o $OUTFILE.dll $EXPORTS $*
	rm -f $DLLDIRNAME/lib$DLLBASENAME.sl
# ln -s $DLLDIRNAME/$DLLBASENAME.dll $DLLDIRNAME/lib$DLLBASENAME.sl
	exit $?
	;;

hp700R5)
	# get list of EXPORTS

	EXPORTLINE=`grep -n -i "^exports" $DEFFILE|cut -d: -f1`
	TOTALLINES=`wc -l $DEFFILE|cut -d' ' -f1`
	TAIL=`expr $TOTALLINES - $EXPORTLINE`

	EXPORTS="+e WEP +e LibMain +e WINDU_Menus +e WINDU_Dialogs +e WINDU_Icons +e WINDU_Cursors +e WINDU_Bitmaps +e WINDU_Accelerators +e WINDU_Stringtable +e WINDU_RCDATA "

	for i in `tail -$TAIL $DEFFILE |cut -d'@' -f1`
	do
		EXPORTS="$EXPORTS +e $i"
	done

	# do the link
	ld +s -E -b -o $OUTFILE.dll $EXPORTS $*
	rm -f $DLLDIRNAME/lib$DLLBASENAME.sl
# ln -s `pwd`/$DLLDIRNAME/$DLLBASENAME.dll $DLLDIRNAME/lib$DLLBASENAME.sl
	exit $?
	;;

sun4)
	ld -d -o $OUTFILE.dll $* 
	rm -f $DLLDIRNAME/lib$DLLBASENAME.so.1.3
# ln -s $DLLDIRNAME/$DLLBASENAME.dll $DLLDIRNAME/lib$DLLBASENAME.so.1.3
	exit $?
	;;

v4_x86)
	ld -Bsymbolic -G -o $OUTFILE.dll $* 2>/tmp/ld.err
	# linker will bitch if the DLL has undefines. It's actuall ok
	# here, so we throw away the output unless ld failed.
	if [ $? -ne 0 ]
	then
		cat /tmp/ld.err >&2
		exit 1
	fi
	rm /tmp/ld.err
	;;

sol2_x86)
	ld -G -dy -B symbolic -o $OUTFILE.dll $* 2>/tmp/ld.err
	# linker will bitch if the DLL has undefines. It's actuall ok
	# here, so we throw away the output unless ld failed.
	if [ $? -ne 0 ]
	then
		cat /tmp/ld.err >&2
		exit 1
	fi
	rm -f $DLLDIRNAME/lib$DLLBASENAME.so
# ln -s $DLLDIRNAME/$DLLBASENAME.dll $DLLDIRNAME/lib$DLLBASENAME.so
	rm /tmp/ld.err
	;;

sol2)
	ld -G -dy -B symbolic -o $OUTFILE.dll $* 2>/tmp/ld.err
	# linker will bitch if the DLL has undefines. It's actuall ok
	# here, so we throw away the output unless ld failed.
	if [ $? -ne 0 ]
	then
		cat /tmp/ld.err >&2
		exit 1
	fi
	rm -f $DLLDIRNAME/lib$DLLBASENAME.so
# ln -s $DLLDIRNAME/$DLLBASENAME.dll $DLLDIRNAME/lib$DLLBASENAME.so
	rm /tmp/ld.err
	;;
rs6000)
	# get list of EXPORTS

	EXPORTLINE=`grep -n -i "^exports" $DEFFILE|cut -d: -f1`
	TOTALLINES=`wc -l $DEFFILE|cut -c1-7`
	TAIL=`expr $TOTALLINES - $EXPORTLINE`

	rm -f /tmp/dllink.exp
	echo "WEP">>/tmp/dllink.exp
	echo "LibMain">>/tmp/dllink.exp
	echo "WINDU_Menus">>/tmp/dllink.exp
	echo "WINDU_Dialogs">>/tmp/dllink.exp
	echo "WINDU_Icons">>/tmp/dllink.exp
	echo "WINDU_Cursors">>/tmp/dllink.exp
	echo "WINDU_Bitmaps">>/tmp/dllink.exp
	echo "WINDU_Accelerators">>/tmp/dllink.exp
	echo "WINDU_Stringtable">>/tmp/dllink.exp
	echo "WINDU_RCDATA ">>/tmp/dllink.exp

	for i in `tail -$TAIL $DEFFILE |cut -d'@' -f1`
	do
		echo "$i">>/tmp/dllink.exp
	done


	# do the link
	rm -f $OUTFILE.dll
	rm -f $DLLDIRNAME/$DLLBASENAME.dll
	rm -f $DLLDIRNAME/lib$DLLBASENAME.a
	cc -o $OUTFILE.dll -bM:SRE $WUEXP -bE:/tmp/dllink.exp -e _nostart $* 
# ln -s $DLLDIRNAME/$DLLBASENAME.dll $DLLDIRNAME/lib$DLLBASENAME.a
	if [ $? -eq 0 ]
	then
		rm /tmp/dllink.exp
	else
		exit 1
	fi
	;;

decaosf)
        ld -shared -expect_unresolved "*" -all -o $OUTFILE.dll -soname lib$DLLBASENAME.so $*
        rm -f $DLLDIRNAME/lib$DLLBASENAME.so
        ln -s $DLLBASENAME.dll $DLLDIRNAME/lib$DLLBASENAME.so
        ;;
dec3000)
        ld -shared -expect_unresolved "*" -all -o $OUTFILE.dll -soname lib$DLLBASENAME.so $*
        rm -f $DLLDIRNAME/lib$DLLBASENAME.so
        ln -s $DLLBASENAME.dll $DLLDIRNAME/lib$DLLBASENAME.so
        ;;
*)	
	echo "$0: unsupported architecture"
	exit 1;
	;;
esac
exit 0
