#! /bin/sh -
# ======================================================================
#  A script for Geant4 autobuild
#
#  [usage]
#    g4autobuild [<options>]
#
#    [options]
#      --help, -h       print this message
#      --prefix=PREFIX  base directory of Geant4 builds [$(cwd)/]
#      --config=CONFIG  configure file [PREFIX/g4auto.cfg]
# ======================================================================
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]

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

}


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

# 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                    ;;
    # ---------------------------------------------------------------
    -*)
      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

# ======================================================================
# 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}'` )
vis_set=`grep '^v' $config | awk '{print $2}'`
ui_set=`grep '^u' $config | awk '{print $2}'`

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

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

idx=0
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

  pushd $g4dir >& /dev/null

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

# ========================================================================
# System
# ========================================================================
export G4SYSTEM=$g4sys
export G4INSTALL=$g4base/$g4
export CLHEP_BASE_DIR=$clhepbase/${clhep_set[$idx]}

# ========================================================================
# Visualization
# ========================================================================
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_set[$idx]}

# ========================================================================
# 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

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

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

    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
    make global >& make-global.log
    popd >& /dev/null

    touch $status_dir/$g4.glib
  fi

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

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

    touch $status_dir/$g4.slib
  fi

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

  popd >& /dev/null

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

exit 0

