set -xuve
date

#
# General settings
#
arch=ARCH
model=MODEL
modver=MODVER
hpcdst=HPCDST
hpctmp=HPCTMP
case $arch in
 ecmwf)
  tar="/opt/freeware/bin/tar"
  find="/opt/freeware/bin/find"
  chmod="/opt/freeware/bin/chmod"
 ;;
 *)
  tar="tar"
  find="find"
  chmod="chmod"
 ;;
esac

#
# Prepare deploy_dir structure
#
source_dir=$hpctmp/$modver
deploy_dir=$hpcdst/$model/$modver
mkdir -p $deploy_dir
cd $deploy_dir
dirs_list="sources inidata scripts setup docs patches bin"
mkdir -p $dirs_list

# Inflate sources
if [[ ! -e $hpctmp/inflate ]]; then
 sfiles=$($find $source_dir/* -maxdepth 0 -type f)
 for sfile in $sfiles; do
  file=$(basename $sfile)
  zfrt=$(echo $file | rev | cut -d '.' -f1 | rev)
  case $zfrt in
   tar) tarf="$tar -xvf"; unTar=true ;;
   gz|tgz) tarf="$tar -zxvf"; unTar=true ;;
   *) echo "$zfrt not a valid format"; unTar=false ;;
  esac
  if [[ $unTar == true ]]; then
   for diR in $dirs_list; do
    srch=$diR
    case $diR in
     sources) srch=src ;;
     inidata) srch=data ;;
    esac
    if [[ $(echo $file | grep -c $srch) -gt 0 ]]; then
     $tarf $source_dir/$file -C $deploy_dir/$diR
     break
    fi
   done
  else
   mv $source_dir/$file $deploy_dir/docs
  fi
  rm -f $source_dir/$file
 done
 touch $hpctmp/inflate
fi
 
# Inflate patch
pfile=$(ls -1rt $hpctmp/patch_*.tar.gz | tail -n 1)
$tar -zxvf $pfile -C $deploy_dir/sources

#
# Perform compilation w.r.t model and version
#
comdone=1
if [[ ! -e $hpctmp/compilation ]]; then
 case $model in
  ecearth)
   case $modver in
    v2*)
     nemo_i=4
     nemo_j=4
     build_dir=$deploy_dir/sources/build; cd $build_dir
     ./compilation.cmd -i $nemo_i -j $nemo_j; if [[ $? -ne 0 ]]; then comdone=0; fi
     cp -pf $deploy_dir/sources/oasis3/prism_2-5/prism/*/bin/oasis3.MPI1.x $deploy_dir/bin
     cp -pf $deploy_dir/sources/nemo/nemo_build/opa_exe.ORCA1_OASIS3.$nemo_i.$nemo_j $deploy_dir/bin
     cp -pf $deploy_dir/sources/ifs/bin/ifsMASTER $deploy_dir/bin
    ;;
    v3*)
     build_dir=$deploy_dir/sources/sources/build-config; cd $build_dir
     ./compilation.cmd; if [[ $? -ne 0 ]]; then comdone=0; fi
     cp -pf $deploy_dir/sources/sources/oasis*/*/bin/oasis3.MPI1.x $deploy_dir/bin
     for nemo_config in $(ls -1 $deploy_dir/sources/sources/nemo*/CONFIG | grep ORCA); do
      mkdir -p $deploy_dir/bin/$nemo_config
      cp -pf $deploy_dir/sources/sources/nemo*/CONFIG/$nemo_config/BLD/bin/*.exe $deploy_dir/bin/$nemo_config
     done
     cp -pf $deploy_dir/sources/sources/ifs*/bin/ifsmaster-* $deploy_dir/bin
    ;;
   esac
  ;;
  nemo)
   case $modver in
    ecearth-v2*)
     nemo_i=6
     nemo_j=6
     build_dir=$deploy_dir/sources/build; cd $build_dir
     ./compilation.cmd -i $nemo_i -j $nemo_j; if [[ $? -ne 0 ]]; then comdone=0; fi
     cp -pf $deploy_dir/sources/nemo/nemo_build/opa_exe.ORCA1_OASIS3.$nemo_i.$nemo_j $deploy_dir/bin
    ;;
    v3.2)
     nemo_i=6
     nemo_j=6
     build_dir=$deploy_dir/sources/modipsl/config/ORCA2_LIM; cd $build_dir
     ./compilation.cmd -i $nemo_i -j $nemo_j; if [[ $? -ne 0 ]]; then comdone=0; fi
     cp -pf $deploy_dir/sources/modipsl/bin/* $deploy_dir/bin
    ;;
    v3.3)
     nemo_i=6
     nemo_j=6
     build_dir=$deploy_dir/sources/NEMOGCM/CONFIG; cd $build_dir
     cd $deploy_dir/sources/NEMOGCM/CONFIG
     ./compilation.cmd -i $nemo_i -j $nemo_j; if [[ $? -ne 0 ]]; then comdone=0; fi
     for nemo_config in $(ls -1 $deploy_dir/NEMOGCM/CONFIG | grep ORCA1); do
      mkdir -p $deploy_dir/bin/$nemo_config
      cp -pf $deploy_dir/sources/NEMOGCM/CONFIG/$nemo_config/BLD/bin/*.exe $deploy_dir/bin/$nemo_config
     done
    ;;
    ece-v3*)
     build_dir=$deploy_dir/sources/sources/build-config; cd $build_dir
     ./compilation.cmd; if [[ $? -ne 0 ]]; then comdone=0; fi
     for nemo_config in $(ls -1 $deploy_dir/sources/sources/nemo-*/CONFIG | grep ORCA); do
      mkdir -p $deploy_dir/bin/$nemo_config
      cp -pf $deploy_dir/sources/sources/nemo*/CONFIG/$nemo_config/BLD/bin/*.exe $deploy_dir/bin/$nemo_config
     done
    ;;
    *)
     echo "$modver is not available"
    ;;
   esac
  ;;
  *)
   echo "$model is not available"
  ;;
 esac
set +e
cp -pf $build_dir/run* $deploy_dir/scripts
cp -rpf $build_dir/setup $deploy_dir
set -e
fi

#
# Post deployment steps
#
if [[ $comdone == 1 ]]; then
 $chmod -R 775 $deploy_dir
 touch $hpctmp/compilation
fi

date
