#!/bin/bash
# -*- shell-script -*-
PKG_VERSION=`(lua -e "V=require('src/Version'); print(V.tag())")`
echo Making Version: $PKG_VERSION

parallel_make_install()
{
  if [ "$BUILD_TYPE" != "EXPERIMENTAL" ]; then
    $SUDO $MAKE -j install     $MAKE_EXTRA
  else
    echo "$SUDO $MAKE pre-install $MAKE_EXTRA"
    $SUDO $MAKE -j pre-install $MAKE_EXTRA
    $SUDO rm -f $BASE_DIR/lmod/EXPERIMENTAL
    $SUDO ln -s $PKG_VERSION $BASE_DIR/lmod/EXPERIMENTAL
  fi
}

save_old_version()
{
  if [ "$SITE_CONTROLLED_PREFIX" != "yes" ]; then
    $SUDO rm -rf $BASE_DIR/$pkgName/orig
    if [ -d $BASE_DIR/$pkgName/$PKG_VERSION ]; then
	$SUDO cp -r $BASE_DIR/$pkgName/$PKG_VERSION $BASE_DIR/$pkgName/orig
	$SUDO rm -f $PKG/../$pkgName
	$SUDO ln -s orig $PKG/../$pkgName
    fi
  fi
}

make_symlink()
{
  if [ "$SITE_CONTROLLED_PREFIX" = "yes" ]; then
      $SUDO rm -f $BASE_DIR/../lmod
      $SUDO ln -s $PKG_VERSION $BASE_DIR/../lmod
      bb=${BASE_DIR/$PKG_VERSION/lmod}
      $SUDO find $BASE_DIR/modulefiles $BASE_DIR/init -type f -exec perl -p -i -e "s|$BASE_DIR|$bb|g" {} \;
  fi
}

runMe()
{
  local cmdA=("$@")

  local j
  local jj
  local i
  local ignoreError
  local j=0
  for i in "${cmdA[@]}" ; do
    ignoreError=
    if [ "x${i:0:1}" = x- ]; then
      i=${i:1}
      ignoreError=1
    fi

    j=$((j+1))
    jj=$(printf "%02d" $j)
    echo
    echo "%%---------------------------------%%"
    echo "   " $jj: $i
    echo "%%---------------------------------%%"
    echo

    eval $i
    if [ -z "$ignoreError" -a $? != 0 ]; then
      break
    fi
  done
}

BUILD_TYPE="standard"
if [ "$1" = "-e" ]; then
  BUILD_TYPE="EXPERIMENTAL"
  echo BUILD_TYPE: $BUILD_TYPE
fi

myhost=$(hostname -f)
myhost=${myhost%.tacc.utexas.edu}
myhost=${myhost%.local}
first=${myhost%%.*}
SYSHOST=${myhost#*.}
SUDO=""

if [ -x $HOME/.up/bin/arch.py ]; then
    MY_ARCH=$($HOME/.up/bin/arch.py)
    MY_ARCH="$MY_ARCH/"
fi

MCLAY=~mclay

cat > /tmp/TACC.build.$$ <<EOF
$MCLAY/l/moduleData/${MY_ARCH}cacheDir:$MCLAY/l/moduleData/${MY_ARCH}system.txt
EOF


if [ -n "$LUA_PATH" ]; then
   MAKE_EXTRA="LUA_PATH=$LUA_PATH LUA_CPATH=$LUA_CPATH"
fi


case $SYSHOST in
  mijo | vmijo )
    SITE_CONTROLLED_PREFIX=yes
    SUDO="sudo"
    base="/opt/apps/lmod/$PKG_VERSION"
    ADMIN_DIR=/opt/moduleData
    UPDATE_FN=$ADMIN_DIR/system.txt
    CACHEDIRS=$ADMIN_DIR/cacheDir
    EXTRA="--with-spiderCacheDir=$CACHEDIRS  --with-updateSystemFn=$UPDATE_FN --with-siteControlPrefix=yes --with-module-root-path=/opt/apps/modulefiles --with-silentShellDebugging=yes --with-supportKsh=yes"

    if [ "$SYSHOST" == "mijo" ]; then
      EXTRA="$EXTRA --with-exportedModuleCmd=no"
    fi
    ;; 

  rios | jedrik | vato | devo | xalt-lmod | bt)
    SUDO="sudo"
    base="/opt/apps"
    ADMIN_DIR=/opt/moduleData
    UPDATE_FN=$ADMIN_DIR/system.txt
    CACHEDIRS=$ADMIN_DIR/cacheDir
    EXTRA="--with-spiderCacheDir=$CACHEDIRS  --with-updateSystemFn=$UPDATE_FN  --with-silentShellDebugging=yes --with-supportKsh=no"
    ;;

  ls6 | maverick2 | stampede2 | frontera | longhorn )
    base="$HOME/l/pkg"
    UPDATE_FN=/tmp/losf_last_update
    CACHEDIRS=/tmp/moduleData/cacheDir
    if [ $SYSHOST == "longhorn" ]; then
      UPDATE_FN=/home/moduleData/spider_cache_timestamp.txt
      CACHEDIRS=/home/moduleData/cacheDir
    fi
    EXTRA="--with-spiderCacheDir=$CACHEDIRS  --with-updateSystemFn=$UPDATE_FN --with-siteName=TACC --with-silentShellDebugging=yes"
    SITE_PKG="contrib/TACC/*.lua"
    ;;

  gligar.os)
    ADMIN_DIR=/apps/gent/lmodcache/
    UPDATE_FN=$ADMIN_DIR/timestamp
    CACHEDIRS=$ADMIN_DIR
    EXTRA="--with-cachedLoads=yes --with-autoSwap=no --with-disableNameAutoSwap=yes --with-caseIndependentSorting=yes --with-pinVersions=yes  --with-silentShellDebugging=yes"
    base="$HOME/l/pkg";;

  *)
    ADMIN_DIR=$HOME/w/moduleData
    UPDATE_FN=$ADMIN_DIR/system.txt
    CACHEDIRS=$ADMIN_DIR/cacheDir
    EXTRA="--with-spiderCacheDir=$CACHEDIRS  --with-updateSystemFn=$UPDATE_FN  --with-silentShellDebugging=yes  --with-supportKsh=no"
    base="$HOME/l/pkg"
    ;;
esac
BASE_DIR=$base

pkgName=lmod
PKG=$BASE_DIR/$pkgName/$PKG_VERSION

EXTRA_CMD="echo "
if [ -n "$SITE_PKG" ]; then
   EXTRA_CMD="$SUDO cp $SITE_PKG $PKG/libexec"
fi

rm -f config.status config.log

MAKE=make
command -v remake 2>&1 > /dev/null
if [ "$?" = 0 ]; then
  MAKE=remake
fi

cmdA=("-$MAKE distclean"
      "./configure --prefix=$BASE_DIR --with-settarg=yes $EXTRA"
      "save_old_version"
      "parallel_make_install"
      "$EXTRA_CMD"
      "make_symlink"
      "$SUDO  rm -rf $BASE_DIR/$pkgName/orig"
      "$MAKE clobber"
      )


runMe "${cmdA[@]}"

rm -rf /tmp/TACC.build.$$
