#!/bin/bash

# USAGE:
#   mklib <std|deb|opt|all>
#
# FUNCTION
#   Creates ".a" files in "$saclib/lib/" depending on the argument:
#   - 'std' causes a standard library to be built. The library file will have
#     the name "saclib.a" and the corresponding object files are in
#     "saclib/lib/obj".   
#   - 'deb' switches on the '-g' option of the compiler which includes 
#     debugging information in the object files. The library file will have
#     the name "saclibd.a" and the corresponding object files are in
#     "saclib/lib/objd".  
#   - 'opt' switches on the '-O' option which produces optimized code. The
#     library file will have the name "saclibo.a" and the corresponding object
#     files are in "saclib/lib/objo".
#   - 'all' builds all three types of libraries.

if [ $# -lt 1 ] 
then
  echo "USAGE:"
  echo "  mklib <deb|opt|all>"
  exit
fi

if [ $1 = "clean" ] 
then
  ### remove .o and .a and makefiles
  echo "Removing object files, libraries and makefiles ..."
  pushd >/dev/null $saclib/lib/objo
  'rm' -f *
  popd >/dev/null
  pushd >/dev/null $saclib/lib/objd
  'rm' -f *
  popd >/dev/null
  pushd >/dev/null $saclib/lib/
  'rm' -f *.a
  popd >/dev/null
  ### do sysdep cleanup
  echo "Removing system-dependent files ..."
  pushd >/dev/null $saclib/sysdep/linuxX86
  ./cleanup
  popd >/dev/null
  pushd >/dev/null $saclib/sysdep/linuxX86_64
  ./cleanup
  popd >/dev/null
  pushd >/dev/null $saclib/sysdep/solarisSparc
  ./cleanup
  popd >/dev/null
  pushd >/dev/null $saclib/sysdep/macosX86
  ./cleanup
  popd >/dev/null
  pushd >/dev/null $saclib/sysdep/macosX86_64
  ./cleanup
  popd >/dev/null
  exit
fi


if [ ! -n "${CC+1}" ] 
then
  CC=cc
fi

echo "Compiling with" $CC

if [ $1 = "std" ]
then
  echo "This option no longer exists!"
elif [ $1 = "deb" ]; then
  pushd >/dev/null  $saclib/lib/objd
  make  CC=$CC "SACFLAG=-g -DNO_SACLIB_MACROS" EXTENSION=d
  popd >/dev/null
elif [ $1 = "opt" ]; then
  pushd >/dev/null  $saclib/lib/objo
  make  CC=$CC "SACFLAG=" EXTENSION=o
  popd >/dev/null
elif [ $1 = "all" ]; then
  pushd >/dev/null  $saclib/lib/objd
  make  CC=$CC "SACFLAG=-g -DNO_SACLIB_MACROS" EXTENSION=d
  popd >/dev/null
  pushd >/dev/null  $saclib/lib/objo
  make  CC=$CC "SACFLAG=" EXTENSION=o
  popd >/dev/null
else
  echo "USAGE:"
  echo "  mklib <deb|opt|all>"
  exit
fi

echo "mklib done."
