#!/bin/ksh
#/* Part of the Maestro sequencer software package.
# * Copyright (C) 2011-2015  Operations division of the Canadian Meteorological Centre
# *                          Environment Canada
# *
# * Maestro is free software; you can redistribute it and/or
# * modify it under the terms of the GNU Lesser General Public
# * License as published by the Free Software Foundation,
# * version 2.1 of the License.
# *
# * Maestro is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# * Lesser General Public License for more details.
# *
# * You should have received a copy of the GNU Lesser General Public
# * License along with this library; if not, write to the
# * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# * Boston, MA 02111-1307, USA.
# */


# Create an experiment from a reference implementation.

# Argument and error checks
export EXPPATH=${1}
if [[ -z "${MAESTRO_CURRENT_REF}" ]] ; then
  echo "MAESTRO_CURRENT_REF is not defined: load a Maestro module before continuing"
  exit 1
fi
if [[ -z "${EXPPATH}" ]] ; then
  echo "Usage: $0 EXP_PATH"
  exit 1
fi
if [[ $(ls -al ${EXPPATH} | wc -l) -gt 3 ]] ; then
  echo "Clean the target directory before building your experiment with $0"
  exit 1
fi

# Import reference experiment
import_maestro_exp -s ${MAESTRO_CURRENT_REF}/ref -d ${EXPPATH} -n -y

# Inject experiment documentation
docfile=${EXPPATH}/.ref_info
echo MAESTRO_CURRENT_REF=${MAESTRO_CURRENT_REF} >${docfile}
echo MAESTRO_MODULE_NAME=${MAESTRO_MODULE_NAME} >>${docfile}
echo MAESTRO_MODULE_PATH=${MAESTRO_MODULE_PATH} >>${docfile}

# Inject s.ssmuse.dot command required to acquire reference
ssmfile=${EXPPATH}/.ref_load
echo ". s.ssmuse.dot $(dirname ${MAESTRO_CURRENT_REF})" >${ssmfile}

# Run reference post-import hook
post_hook=${MAESTRO_CURRENT_REF}/bin/makeexp_hook
if [[ -x ${post_hook} ]] ; then
  ${post_hook}
fi
