#!/bin/sh

if [ $# -lt 3 ] ; then
  cat <<eof

$0 dir name obj1 obj2 ...
  dir:  directory where the library is to be created (ex: /home/i/tecgraf/lib)
  name: library name (ex: iup)
  objn: object files

For the above examples, the library is created in /home/i/tecgraf/lib/libiup.so

eof

  if [ $# -eq 1 ] ; then
    if [ $1 = "help" ] ; then
      cat <<eof

Currently supported systems are:
AIX, BSD/OS, HP-UX, IRIX, IRIX64, Linux, NeXT, OSF1, SCO_SV, SunOS and UNIX_SV

AIX
===
  Create export and import (optional) files to create the dynamic library, named <name>.exp and <name>.imp. In its simplest form, the file start with a line containing the two characters !#, followed with a list of the symbols, one in each line.

Linux
=====
   Use the option -export-dynamic with ld to create an executable file which loads a dynamic library at runtime (with dlfcn.h functions) if the library uses some symbol from the executable.

HP-UX
=====
  Compile the object files with option +z (c89 compiler).
  Use the option -E with ld to create an executable file which loads a dynamic library at runtime (with dl.h functions).

SCO_SV
======
  If geraso generates an error, try '/usr/ccs/bin/elf/ld' in place of 'ld -b elf'
  Use the option -b elf with cc to generate the executable
  Use the option -Bexport with ld to create an executable file which loads a dynamic library at runtime (with dlfcn.h functions) if the library uses some symbol from the executable.

Authors: rborges@tecgraf.puc-rio.br, costa@tecgraf.puc-rio.br
eof

    exit 1
    fi
  fi

  echo "Use the option <help> for more information"

  exit 1
fi

LIBDIR=$1
NAME=$2
LIBNAME=lib$NAME.so
LIBPATH=$LIBDIR/$LIBNAME

UNAME=`uname`

shift
shift

rm -fr $LIBPATH

case $UNAME in

  AIX)
    if [ `uname -v` = 3 ] ; then
      noentry='-e _nostart'
    else
      noentry=-bnoentry
    fi 
    if [ -s $NAME.imp ]; then
      import=-bI:$NAME.imp
    else
      import=
    fi
    if [ -s $NAME.exp ]; then
      export=-bE:$NAME.exp
    else
      export=
      echo ERROR: file $NAME.exp not found
    fi
    cc -o $LIBPATH -bM:SRE $export $import $noentry $*
    if [ -s $LIBPATH ]; then
      (cd $LIBDIR; \
      rm -f $NAME.o; \
      ln -s $LIBNAME $NAME.o)
    fi ;;

  BSD/OS)
    ld -o $LIBPATH -r $* ;;

  HP-UX)
    ld -o $LIBPATH -b $* ;;

  IRIX*) 
    ld -o $LIBPATH -elf -shared -rdata_shared -soname $LIBNAME $* ;;

  Linux)
    gcc -o $LIBPATH -shared $* ;;
#    ld -o $LIBPATH -m elf_i386 -shared $* ;;

  NeXT)
    ld -o $LIBPATH -r $* ;;

  OSF1)
    ld -o $LIBPATH -shared -soname $LIBNAME -expect_unresolved '*' $* ;;

  SCO_SV)
    ld -o $LIBPATH -b elf -G $* ;;
#    /usr/ccs/bin/elf/ld -o $LIBPATH -G $* ;;

  SunOS)
    if [ `uname -r|cut -f1 -d.` = 4 ] ; then
      ld -o $LIBPATH $*
      if [ -s $LIBPATH ]; then
        (cd $LIBDIR; \
        rm -f $LIBNAME.1.1; \
        ln -s $LIBNAME $LIBNAME.1.1)
      fi
    else
      ld -o $LIBPATH -G $*
    fi ;;

  UNIX_SV)
    ld -o $LIBPATH -G $* ;;

  *)
    echo "don't know how to make dynamic libraries for $UNAME"
esac

if [ ! -s $LIBPATH ]; then exit 1; fi

