#! /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\)$//' `'.dylib'

ldcmd="/usr/bin/clang++ -dynamiclib -dylib -Wl,-flat_namespace -Wl,-undefined -Wl,suppress    $libdirs -L/opt/local/lib -Wl,-headerpad_max_install_names -arch x86_64  -o $outfile $args -lreadline -lncurses -lssl -lcrypto -ldl -lm  -install_name /opt/local/lib/$outfile"

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

# for HP-UX
chmod 555 "$outfile"
