#!/bin/bash

# Wrapper for selection between installed versions of cylc.
# Author: Matt Shin (Met Office).

#_____________________________
# INSTALLATION: 
#
#  1. Install this script as "cylc" in $PATH for normal users, AND
#  create a symlink gcylc -> cylc to allow continued use of "gcylc" for
#  "cylc gui" under this framework.
#
#  2. Install cylc versions as described in the INSTALL file, e.g.:
#     /home/admin/cylc/cylc-5.4.4/
#     /home/admin/cylc/cylc-5.4.5/
#     /home/admin/cylc/cylc -> cylc.5.4.5 # symlink DEFAULT version
#
# Environment variables:
#  $CYLC_HOME_ROOT: top installation directory, e.g. /home/admin/cylc/
#  $CYLC_HOME:   a specific version, e.g. /home/admin/cylc/cylc-5.4.5/
#  $CYLC_VERSION: a specific version string, e.g. 5.4.5
#
#  3. Set $CYLC_HOME_ROOT for your site - see "!!! EDIT ME !!! below.

#_____________________________
# ACTION IN ORDER OF PRIORITY:
# 
# 1) If $CYLC_HOME is defined and exists it will be selected. If it does
#   not exist the command will fail.
#
# 2) If $CYLC_VERSION is defined and exists it will be selected. If it
#   does not exist the default under $CYLC_HOME_ROOT will be selected.
# 
# 3) If $CYLC_HOME and $CYLC_VERSION are both undefined, the default
#   under $CYLC_HOME_ROOT will be selected.
 
#_____________________________
# NOTES:
# * Users will typically accept the default or set $CYLC_VERSION, but
#   they may set $CYLC_HOME to pick up a private cylc installation.
# * Developers may set $CYLC_HOME to pick up their own repository clone.
# * Running suites export $CYLC_VERSION to task environments, so that
#    task messaging commands etc. pick up the compatible cylc version.
# * Note that $CYLC_ROOT_HOME can be overridden in the environment too.

CYLC_HOME_ROOT=${CYLC_HOME_ROOT:-/home/admin/cylc} # !!! EDIT ME !!!

if [[ -z ${CYLC_HOME:-} ]]; then
    if [[ -n ${CYLC_VERSION:-} && -d $CYLC_HOME_ROOT/cylc-$CYLC_VERSION ]]; then
        CYLC_HOME=$CYLC_HOME_ROOT/cylc-$CYLC_VERSION
    else
        CYLC_HOME=$CYLC_HOME_ROOT/cylc
    fi
fi
exec $CYLC_HOME/bin/$(basename $0) "$@"
