#! /bin/bash -
# ======================================================================
#  A script for Geant4 packaging
#
#  Version 2.2 Dec 2008
#
# ======================================================================
export LANG=C

IFS='
        '

PATH=/bin:/usr/bin
export PATH

# ======================================================================
# testing the echo features
# ======================================================================
if `(echo "testing\c"; echo 1,2,3) | grep c > /dev/null` ; then
  if `(echo -n testing; echo 1,2,3) | sed s/-n/xn/ | grep xn > /dev/null`; then
    ac_n= ac_c='
' ac_t='        '
  else
    ac_n=-n ac_c= ac_t=
  fi
else
  ac_n= ac_c='\c' ac_t=
fi

# ======================================================================
# help message
# ======================================================================
show_help() {
cat <<EOF
Usage: g4pack [<options>]

Options:
     --help, -h                  print this message

     --with-g4system=G4SYSTEM    G4SYSTEM [\$G4SYSTEM]
     --with-g4-dir=DIR           Geant4 dir [\$G4INSTALL]
     --with-clhep-dir=DIR        CLHEP dir [\$CLHEP_BASE_DIR]

     --with-lib-dir=DIR          G4 static granular libdir [lib]
     --with-glib-dir=DIR         G4 global static libdir [glib]
     --with-slib-dir=DIR         G4 global shared libdir [slib]

     --with-install-dir=DIR      install path (MacOSX only)

   Enable/disable options: prefix with either --enable- or --disable-
       lib                       packing static granular lib [disable]
       glib                      packing global static lib [enable]
       slib                      packing global shared lib [enable]

EOF
}

# ======================================================================
# rebuild shared library (MacOSX only)
# ======================================================================
rebuild_sharedlib() {
mkdir tmp
mv lib*.dylib ./tmp

liblist=lib*.a

for lib in $liblist
do
  slibname=${lib%.*}.dylib

  #check linked libraries...
  extliblist=`otool -L tmp/$slibname | grep -v /usr/lib | grep -v $slibname | awk '{print $1}'`

  ar x $lib
  g++ -dynamiclib -single_module -undefined dynamic_lookup \
      -install_name $install_dir/lib/$slibname \
      $extliblist -o $slibname *.o
  rm *.o __.SYMDEF

  chmod 755 $slibname
done

rm -r tmp
}


# ======================================================================
# main
# ======================================================================
# default values
g4system=${G4SYSTEM:-XXX}
g4dir=${G4INSTALL:-/zzz}
clhepdir=${CLHEP_BASE_DIR:-/zzz}

install_dir=/zzz

libdir=lib
glibdir=glib
slibdir=slib

enable_lib=0
enable_glib=1
enable_slib=1

# parsing options
while test $# -gt 0
do
  case $1 in
    -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
    *) optarg= ;;
  esac

  case $1 in
    --help|-h) show_help;  exit 0 ;;
    # ---------------------------------------------------------------
    --with-g4system=*)        g4system=$optarg     ;;
    --with-g4-dir=*)          g4dir=$optarg        ;;
    --with-clhep-dir=*)       clhepdir=$optarg     ;;
    --with-lib-dir=*)         libdir=$optarg       ;;
    --with-glib-dir=*)        glibdir=$optarg      ;;
    --with-slib-dir=*)        slibdir=$optarg      ;;
    --with-install-dir=*)     install_dir=$optarg  ;;
    # ---------------------------------------------------------------
    --enable-lib )            enable_lib=1         ;;
    --disable-lib )           enable_lib=0         ;;
    --enable-glib )           enable_glib=1        ;;
    --disable-glib )          enable_glib=0        ;;
    --enable-slib )           enable_slib=1        ;;
    --disable-slib )          enable_slib=0        ;;
    # ---------------------------------------------------------------
    -*)
      echo "Unrecognized option: $1"
      exit -1
      ;;
    *)
      echo "Invalid argument: $1"
      exit -1
      ;;
  esac
  shift
done

# check something
shlib=so
if [ $g4system = Darwin-g++ ]; then
  shlib=dylib
fi

echo $ac_n "Checking for G4 dir ...$ac_c"
if [ ! -d $g4dir ]; then
  echo "no"
  echo "### $g4dir does not exist."
  exit -1
fi
echo $g4dir

echo $ac_n "Checking for CLHEP dir ...$ac_c"
if [ ! -d $clhepdir ]; then
  echo "no"
  echo "### $clhepdir does not exist."
  exit -1
fi
echo $clhepdir

echo $ac_n "Checking for G4SYSTEM ...$ac_c"
if [ ! -e $g4dir/config/sys/$g4system.gmk ]; then
  echo "no"
  echo "### $g4system is invalid."
  exit -1
fi
echo $g4system

if [ $enable_lib = 1 ]; then
  echo $ac_n "Checking for lib dir ...$ac_c"
  if [ ! -d $g4dir/$libdir ]; then
    echo "no"
    echo "### $libdir is invalid."
    exit -1
  fi
  echo $libdir
fi

if [ $enable_glib = 1 ]; then
  echo $ac_n "Checking for glib dir ...$ac_c"
  if [ ! -d $g4dir/$glibdir ]; then
    echo "no"
    echo "### $glibdir is invalid."
    exit -1
  fi
  echo $glibdir
fi

if [ $enable_slib = 1 ]; then
  echo $ac_n "Checking for slib dir ...$ac_c"
  if [ ! -d $g4dir/$slibdir ]; then
    echo "no"
    echo "### $slibdir is invalid."
    exit -1
  fi
  echo $slibdir
fi

# ----------------------------------------------------------------------
# ok go ahead

echo ""
echo "Packing ..."

g4packdir=g4

if [ -d $g4packdir ]; then
  echo "### $g4packdir/ already exist. Please move it."
  exit -1
fi

mkdir -p $g4packdir
mkdir -p $g4packdir/include
mkdir -p $g4packdir/lib

# CLHEP
cp -pR $clhepdir/include/CLHEP  $g4packdir/include/
cp -pR $clhepdir/lib/libCLHEP-[12]* $g4packdir/lib/

# Geant4
cp -pR $g4dir/include $g4packdir/include/
mv $g4packdir/include/include $g4packdir/include/Geant4
rm $g4packdir/include/Geant4/README

if [ $enable_glib = 1 ]; then
  cp -pR $g4dir/$glibdir/$g4system/lib* $g4packdir/lib
fi

if [ $enable_slib = 1 ]; then
  cp -pR $g4dir/$slibdir/$g4system/lib* $g4packdir/lib    

  if [ $g4system = Darwin-g++ ]; then
    if [ $enable_glib = 0 ]; then
      echo "### gobal static library is required for the operation."
      exit -1
    fi
    pushd $g4packdir/lib > /dev/null 2>&1
    rebuild_sharedlib
    popd > /dev/null 2>&1
  fi
fi

if [ $enable_lib = 1 ]; then
  cp -pR $g4dir/$libdir/$g4system/lib* $g4packdir/lib
fi

# CLHEP links
pushd $g4packdir/lib > /dev/null 2>&1
ln -s libCLHEP-*.a libCLHEP.a
ln -s libCLHEP-*.$shlib libCLHEP.$shlib
popd > /dev/null 2>&1

exit 0

