#! /bin/bash -
# ======================================================================
#  A script for Geant4 autobuild
#
#  Version 2.3.1 Dec 2008
#
#  [usage]
#    g4autobuild [<options>]
#
#    [options]
#      --help, -h       print this message
#      --prefix=PREFIX  base directory of Geant4 builds [$(cwd)/]
#      --config=CONFIG  configure file [PREFIX/g4auto.cfg]
#      --jobs=N         allow N jobs at once
#      --with-fpic      default -fPIC option
# ======================================================================
export LANG=C

IFS='
        '

PATH=/bin:/usr/bin
export PATH

# ======================================================================
# help message
# ======================================================================
show_help() {
cat <<EOF
 [usage]
   g4autobuild [<options>]

   [options]
     --help, -h       print this message
     --prefix=PREFIX  root directory of autobuild [\$(PWD)/]
     --config=CONFIG  configure file [PREFIX/g4auto.cfg]
     --jobs=N         allow N jobs at once
     --with-fpic      default -fPIC option

EOF
}

# ======================================================================
# external environment
# ======================================================================
add_ext_env() {
aext=$1
aval=$2

# for bash
cat >> g4rc.sh <<EOF
export $aext=$aval
EOF

# for csh
cat >> g4rc.csh <<EOF
setenv $aext $aval
EOF

}

# ======================================================================
# vis. environment
# ======================================================================
add_vis_envs() {
avis=$1

# for bash
cat >> g4rc.sh <<EOF
export G4VIS_BUILD_${avis}_DRIVER=1
export G4VIS_USE_${avis}=1

EOF

# for csh
cat >> g4rc.csh <<EOF
setenv G4VIS_BUILD_${avis}_DRIVER 1
setenv G4VIS_USE_${avis} 1

EOF

}

# ======================================================================
# ui environment
# ======================================================================
add_ui_envs() {
aui=$1

# for bash
cat >> g4rc.sh <<EOF
export G4UI_USE_${aui}=1
EOF

# for csh
cat >> g4rc.csh <<EOF
setenv G4UI_USE_${aui} 1
EOF

}

# ======================================================================
# options
# ======================================================================
add_options() {
aopt=$1

# for bash
cat >> g4rc.sh <<EOF
export G4LIB_BUILD_${aopt}=1
export G4LIB_USE_${aopt}=1
EOF

# for csh
cat >> g4rc.csh <<EOF
setenv G4LIB_BUILD_${aopt} 1
setenv G4LIB_USE_${aopt} 1
EOF

}

# ======================================================================
# main
# ======================================================================
# default values
prefix=`pwd`
config=$prefix/g4auto.cfg
status_dir=$prefix/status
njobs=1
qfpic=0

# 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 ;;
    # ---------------------------------------------------------------
    --prefix=*)                prefix=$optarg                    ;;
    --config=*)                config=$optarg                    ;;
    --jobs=*)                  njobs=$optarg                     ;;
    --with-fpic )              qfpic=1                           ;;
    # ---------------------------------------------------------------
    -*)
      echo "Unrecognized option: $1"
      exit -1
      ;;
    *)
      echo "Invalid argument: $1"
      exit -1
      ;;
  esac
  shift
done

# check something
if [ ! -d $prefix ]; then
  echo "*** abort :$prefix does not exist."
  exit -1
fi

if [ ! -e $config ]; then
  echo "*** abort :$config does not exist."
  exit -1
fi

if [ ! -d $status_dir ]; then
  echo "$status_dir does not exist. It is created."
  mkdir $status_dir
fi

if [ ! `echo "$njobs" | egrep "^[0-9]+$"` ];  then
  echo "*** abort : #jobs must be a number."
  exit -1
fi

# ======================================================================
# parsing a config file...
# ======================================================================
g4sys=`grep '^G4SYS'  $config | awk '{print $2}'`
g4base=`grep '^G4BASE' $config | awk '{print $2}'`
clhepbase=`grep '^CLHEPBASE' $config | awk '{print $2}'`
geant4_set=`grep '^-' $config | awk '{print $2}'`
clhep_set=`grep '^-' $config | awk '{print $3}'`
xercescroot=`grep '^XERCESCROOT' $config | awk '{print $2}'`
vis_set=`grep '^v' $config | awk '{print $2}'`
ui_set=`grep '^u' $config | awk '{print $2}'`
opt_set=`grep '^o' $config | awk '{print $2}'`

if [ $g4sys == Darwin-g++ ]; then
  qfpic=1
fi

if [ ! -d $g4base ]; then
  echo "*** abort :$g4base does not exist."
  exit -1
fi

if [ ! -d $clhepbase ]; then
  echo "*** abort :$clhepbase does not exist."
  exit -1
fi

if [ $xercescroot ]; then
  if [ ! -d $xercescroot ]; then
    echo "*** abort :$xercescroot does not exist."
    exit -1
  fi
fi

# ======================================================================
# building Geant4

idx=1
for g4 in $geant4_set
do
  echo "@@@ building Geant4 ($g4) ..."
  
  g4dir=$g4base/$g4

  if [ ! -d $g4dir ]; then
    echo "*** (error) target directory does not exist."
    exit -1
  fi

  set $clhep_set
  clhep="clhep=\$$idx"
  eval $clhep

  pushd $g4dir > /dev/null 2>&1

  cat > g4rc.sh << EOF
# ========================================================================
#    Geant4 development environment
# ========================================================================
export LANG=C

# ========================================================================
# System
# ========================================================================
export G4SYSTEM=$g4sys
export G4INSTALL=$g4base/$g4
export CLHEP_BASE_DIR=$clhepbase/$clhep
EOF

  cat > g4rc.csh << EOF
# ========================================================================
#    Geant4 development environment
# ========================================================================
setenv LANG C

# ========================================================================
# System
# ========================================================================
setenv G4SYSTEM $g4sys
setenv G4INSTALL $g4base/$g4
setenv CLHEP_BASE_DIR $clhepbase/$clhep
EOF

if [ $xercescroot ]; then
  envval=XERCESCROOT
  add_ext_env XERCESCROOT $xercescroot
fi

  cat >> g4rc.sh <<EOF

# ========================================================================
# Visualization
# ========================================================================
EOF

  cat >> g4rc.csh << EOF

# ========================================================================
# Visualization
# ========================================================================
EOF

  # vis. envs
  for vis in $vis_set
  do
    add_vis_envs $vis
  done

  cat >> g4rc.sh <<EOF
# ========================================================================
# UI
# ========================================================================
EOF

  cat >> g4rc.csh <<EOF
# ========================================================================
# UI
# ========================================================================
EOF

  # ui envs
  for ui in $ui_set
  do
    add_ui_envs $ui
  done

  cat >> g4rc.sh <<EOF

# ========================================================================
# Options
# ========================================================================
EOF

  cat >> g4rc.csh <<EOF

# ========================================================================
# Options
# ========================================================================
EOF

  # options
  for aopt in $opt_set
  do
    add_options $aopt
  done

  # ======================================================================
  # ok, Lets' build...
  . g4rc.sh

  # ======================================================================
  # normal lib
  if [ ! -e $status_dir/$g4.lib ]; then
    echo "@@@ building normal lib ..."
    pushd source > /dev/null 2>&1
    make --jobs=$njobs > make.log 2>&1
    make includes > /dev/null 2>&1
    popd > /dev/null 2>&1

    touch $status_dir/$g4.lib
  fi

  # ======================================================================
  # global lib
  if [ ! -e $status_dir/$g4.glib ]; then
    echo "@@@ buinding global lib ..."
    export G4LIB=$G4INSTALL/glib

    pushd source > /dev/null 2>&1
    make global > make-global.log 2>&1
    popd > /dev/null 2>&1

    touch $status_dir/$g4.glib
  fi

  # ======================================================================
  # shared lib  
  if [ ! -e $status_dir/$g4.slib ]; then
    echo "@@@ buinding shared lib ..."
    export G4LIB=$G4INSTALL/slib
    if [ $qfpic == 1 ]; then
      export G4TMP=$G4INSTALL/tmp
    else
      export G4TMP=$G4INSTALL/tmp-slib
    fi
    export G4LIB_BUILD_SHARED=1

    pushd source > /dev/null 2>&1
    make --jobs=$njobs > make-slib.log 2>&1  
    make global >> make-slib.log 2>&1
    popd > /dev/null 2>&1

    touch $status_dir/$g4.slib
  fi

  # ======================================================================
  # done
  unset G4LIB
  unset G4TMP
  unset G4LIB_BUILD_SHARED

  popd > /dev/null 2>&1

  echo $((idx++)) > /dev/null 2>&1
done

exit 0

