#!/bin/bash
# Usage:
# ./build_gcm [path]
# where 'path' is an optional argument to this script specifying
# the full path to where the 'fcm' command is to be found

if [[ -f '.lock' && $force_compile == false ]]
then
    echo "WARNING: you are probably already compiling the model somewhere else."
    echo "Wait until the first compilation is finished before launching this one."
    echo "If you are sure that you are not compiling elsewhere,"
    echo "run makelmdz_fcm with option -force_compile"
    exit 1
fi

echo "compiling..." > '.lock'

job=1
dirname="" #path to where the fcm command will be found
if (( $# >= 1 )) ; then
  dirname=$1
  # check that "dirname" exists and is a directory
  if [[ ! -d $dirname ]] ; then
    echo "$0 error : $dirname is not a directory"
    exit
  fi
  # add a trailing "/" to $dirname
  dirname=${dirname}"/"
  shift;
fi

# run "fcm build" command
${dirname}fcm build $*

err=$?
# Check error message from fcm build
if [ $err != 0 ] ; then
  # ERROR IN COMPILING
  echo ERROR IN COMPILING LMDZ : $err
  exit 1
fi

# cleanup
\rm -f '.lock' 

