#! /bin/sh -f

args=''
outfile=''
ofiles=''
libdirs=''
nolibs='n'

die () {
  echo "$1" >&2
  exit 1
}

for arg in ` echo "$LIBRARY_PATH" | tr : ' ' `
do
  libdirs="$libdirs '-L$arg'"
done

while [ $# -gt 0 ]
do
  case "$1" in
  -o)
    shift
    test $# -gt 0 || die "missing arg for -o"
    outfile="$1"
    ;;
  *.o | *.o0)
    ofiles="$ofiles '$1'"
    args="$args '$1'"
    ;;
  -L*)
    libdirs="$libdirs '$1'"
    ;;
  -nolibs)
    nolibs='y'
    ;;
  -l*)
    test "$nolibs" = n && args="$args '$1'"
    ;;
  *)
    args="$args '$1'"
    ;;
  esac
  shift
done

test "X$ofiles"  != X || die "no input files"
test "X$outfile" != X || die "no output file"

outfile=` echo "$outfile" | sed -e 's/\.\(so\|o\)$//' `'.so'

ldcmd="c++ -shared -fPIC -fPIC  $libdirs -L/usr/pkg/lib -Wl,-R/usr/pkg/lib  -o $outfile \
 $args" # -lcurses -ltermcap -lssl -lcrypto -lelf -lm 

echo "$ldcmd"
eval "$ldcmd" || exit

# for HP-UX
chmod 555 "$outfile"
