#! /bin/sh

# Script to generate a shared library.
# Called by main Makefile.
#
# Author: Fons Rademakers, 29/2/2000

if [ "$1" = "-v" ] ; then
   MAJOR=$2
   MINOR=$3
   REVIS=$4
   shift
   shift
   shift
   shift
fi

if [ "$1" = "-x" ] ; then
   EXPLICIT="yes"
   shift
fi

PLATFORM=$1
LD=$2
LDFLAGS=$3
SOFLAGS=$4
SONAME=$5
LIB=$6
OBJS=$7
EXTRA=$8

rm -f $LIB

if [ $PLATFORM = "macosx" ]; then
   soext="dylib"
else
   soext="so"
fi

EXPLLNKCORE=
if [ "x$EXPLICIT" = "xyes" ]; then
   if [ $LIB != "lib/libCint.$soext" ]; then
      if [ $LIB != "lib/libCore.$soext" ]; then
         EXPLLNKCORE="-Llib -lCore -lCint"
      else
         EXPLLNKCORE="-Llib -lCint"
      fi
   fi
fi

if [ $PLATFORM = "aix" ] || [ $PLATFORM = "aix5" ]; then
   if [ $PLATFORM = "aix" ]; then
      makeshared="/usr/ibmcxx/bin/makeC++SharedLib"
   fi
   if [ $PLATFORM = "aix5" ]; then
      makeshared="/usr/vacpp/bin/makeC++SharedLib"
   fi
   if [ $LD = "xlC" ]; then
      EXPLLNKCORE=
      if [ $LIB != "lib/libCint.a" ]; then
         if [ $LIB != "lib/libCore.a" ]; then
            EXPLLNKCORE="-Llib -lCore -lCint"
         else
            EXPLLNKCORE="-Llib -lCint"
         fi
      fi

      cmd="$makeshared -o $LIB -p 0 $OBJS $EXTRA $EXPLLNKCORE"
      echo $cmd
      $cmd
   fi
elif [ $PLATFORM = "alphaegcs" ] || [ $PLATFORM = "hpux" ] || \
     [ $PLATFORM = "solaris" ]   || [ $PLATFORM = "sgi" ]; then
   cmd="$LD $SOFLAGS $LDFLAGS -o $LIB $OBJS $EXTRA $EXPLLNKCORE"
   echo $cmd
   $cmd
elif [ $PLATFORM = "lynxos" ]; then
   cmd="ar rv $LIB $OBJS $EXTRA"
   echo $cmd
   $cmd
elif [ $PLATFORM = "fbsd" ]; then
    cmd="$LD $SOFLAGS$SONAME.$MAJOR.$MINOR $LDFLAGS -o $LIB.$MAJOR.$MINOR \
         `lorder $OBJS | tsort -q` $EXTRA $EXPLLNKCORE"
    echo $cmd
    $cmd
elif [ $PLATFORM = "macosx" ]; then
   macosx_minor=`sw_vers | sed -n 's/ProductVersion://p' | cut -d . -f 2`
   # Look for a fink installation
   FINKDIR=`which fink 2>&1 | sed -ne "s/\/bin\/fink//p"`
   if [ $macosx_minor -ge 3 ]; then
      unset LD_PREBIND
      export MACOSX_DEPLOYMENT_TARGET=10.$macosx_minor
   fi
   # We need two library files: a .dylib to link to and a .so to load
   BUNDLE=`echo $LIB | sed s/.dylib/.so/`
   # Add versioning information to shared library if available
   if [ "x$MAJOR" != "x" ]; then
       VERSION="-compatibility_version ${MAJOR} -current_version ${MAJOR}.${MINOR}.${REVIS}"
       SONAME=`echo $SONAME | sed "s/.*\./&${MAJOR}./"`
       LIB=`echo $LIB | sed "s/\/*.*\/.*\./&${MAJOR}.${MINOR}./"`
   fi
   cmd="$LD $SOFLAGS$SONAME -o $LIB $OBJS \
        `[ -d ${FINKDIR}/lib ] && echo -L${FINKDIR}/lib` \
        -ldl $EXTRA $EXPLLNKCORE $VERSION"
   echo $cmd
   $cmd
   if [ "x`echo $SOFLAGS | grep -- '-g'`" != "x" ]; then
      opt=-g
   else
      opt=-O
   fi
   if [ $macosx_minor -ge 3 ]; then
      cmd="$LD $opt -bundle -flat_namespace -undefined dynamic_lookup -o \
          $BUNDLE $OBJS `[ -d ${FINKDIR}/lib ] && echo -L${FINKDIR}/lib` \
          -ldl $EXTRA $EXPLLNKCORE"
      echo $cmd
      $cmd
   else
      cmd="$LD $opt -bundle -flat_namespace -undefined suppress -o $BUNDLE \
	   $OBJS `[ -d ${FINKDIR}/lib ] && echo -L${FINKDIR}/lib` \
           -ldl $EXTRA $EXPLLNKCORE"
      echo $cmd
      $cmd
   fi
elif [ $LD = "KCC" ]; then
   cmd="$LD $LDFLAGS -o $LIB $OBJS $EXTRA $EXPLLNKCORE"
   echo $cmd
   $cmd
elif [ $LD = "build/unix/wingcc_ld.sh" ]; then
   EXPLLNKCORE=
   if [ $SONAME != "libCint.dll" ]; then
      if [ $SONAME != "libCore.dll" ]; then
         EXPLLNKCORE="-Llib -lCore -lCint"
      else
         EXPLLNKCORE="-Llib -lCint"
      fi
   fi
   cmd="$LD $SOFLAGS$SONAME $LDFLAGS -o $LIB $OBJS $EXTRA $EXPLLNKCORE"
   echo $cmd
   $cmd
else
   if [ "x$MAJOR" = "x" ] ; then
      cmd="$LD $SOFLAGS$SONAME $LDFLAGS -o $LIB $OBJS $EXTRA $EXPLLNKCORE"
      echo $cmd
      $cmd
   else
      cmd="$LD $SOFLAGS$SONAME.$MAJOR.$MINOR $LDFLAGS \
           -o $LIB.$MAJOR.$MINOR $OBJS $EXTRA $EXPLLNKCORE"
      echo $cmd
      $cmd
   fi
fi

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

if [ "x$MAJOR" != "x" ]; then
    if [ -f $LIB.$MAJOR.$MINOR ]; then
        # Versioned library has format foo.so.3.05
	ln -fs $SONAME.$MAJOR.$MINOR $LIB.$MAJOR
	ln -fs $SONAME.$MAJOR        $LIB
    elif [ -f $LIB ]; then
	# Versioned library has format foo.3.05.so
	ln -fs `echo $SONAME | sed "s/.*\./&${MINOR}./"` `echo $LIB | sed "s/\.${MINOR}//"`
	ln -fs `echo $SONAME | sed "s/.*\./&${MINOR}./"` `echo $LIB | sed "s/\.${MAJOR}\.${MINOR}//"`
    fi
fi

if [ $PLATFORM = "hpux" ]; then
   chmod 555 $LIB
fi

echo "==> $LIB done"

exit 0
