#!/bin/bash
#
# OBJECTIVE: 
# In Autosubmit 2.x experiments three projects were cloned (model, templates and ocean_diagnostics).
# New Autosubmit 3.x experiments only allow cloning one project (auto-ecearth).
# Auto-ecearth is a single project containing model, templates and ocean_diagnostics among others.
#
# This script covers the need of having to run an Autosubmit 2.x experiment with Autosubmit 3.x.
# It assumes that no auto-ecearth available version can reproduce what the old experiment was doing,
# and it adapts a copy of that old experiment, reshuffling the configuration files and removing 
# unnecessary variables from the old templates to adapt them to the Autosubmit 3.x framework.
#
# DESCRIPTION:
# This script will modify and adapt the experiment configuration file of the TARGET_EXPID
# according to Autosubmit 3.x standard.
#
# This script will also clone model, templates and ocean_diagnostics
# from ORIGINAL_EXPID into TARGET_EXPID "proj" folder. 
#
# This script will also remove unnecessary template header/tailer variables.
#
# USE:
# 1st step
# --------
# Before executing this script you must use the copy option of Autosubmit 3.x
# e.g. autosubmit expid -y i000 -H ithaca -d "copied from original i000"
#
# 2nd step
# --------
# ./migrate_exp.sh ORIGINAL_EXPID TARGET_EXPID
#
# CONTACT:
# Written by Domingo Manubens-Gil
#
# Institut Català de Ciències del Clima / Climate Forecasting Unit (IC3/CFU)
# Created:  November 13, 2014
copy_from="$1"
target_expid="$2"
experiments_path="/home/dmanubens/debug/autosubmit"
target_file="$experiments_path/$target_expid/conf/expdef_$target_expid.conf"

set -ex

if [[ -d $experiments_path/$target_expid/proj ]]; then
	cd $experiments_path/$target_expid/proj
	echo "Existing clone for model, templates and ocean_diagnostics..."
else
	mkdir $experiments_path/$target_expid/proj
	cd $experiments_path/$target_expid/proj
	git clone $experiments_path/$copy_from/git/model model
	git clone $experiments_path/$copy_from/git/templates templates
	git clone $experiments_path/$copy_from/git/ocean_diagnostics ocean_diagnostics

	echo "Cloned successfully model, templates and ocean_diagnostics..."
fi


if grep -q TEMPLATE_NAME "$target_file" ; then
	template=$(grep TEMPLATE_NAME $target_file | awk '{print $3}')
	sed -i '/%HEADER%/d' templates/$template/*
	sed -i '/%HEADER%/d' templates/common/*
	sed -i '/%AS-HEADER-LOC%/d' templates/$template/*
	sed -i '/%AS-HEADER-LOC%/d' templates/common/*
	sed -i '/%AS-HEADER-REM%/d' templates/$template/*
	sed -i '/%AS-HEADER-REM%/d' templates/common/*
	sed -i '/%AS-TAILER-LOC%/d' templates/$template/*
	sed -i '/%AS-TAILER-LOC%/d' templates/common/*
	sed -i '/%AS-TAILER-REM%/d' templates/$template/*
	sed -i '/%AS-TAILER-REM%/d' templates/common/*
	
	echo "Unnecessary template header/tailer variables deleted..."
else
	echo "No variables deleted..."
fi

if grep -q HPCPROJ "$target_file" ; then
	sed -i '/^HPCPROJ/ a\# HPCPROJ moved to queues.conf (Autosubmit 3.x migration)' $target_file
	sed -i '/^HPCPROJ/ d' $target_file
	sed -i '/^HPCUSER/ a\# HPCUSER moved to queues.conf (Autosubmit 3.x migration)' $target_file
	sed -i '/^HPCUSER/ d' $target_file
	sed -i '/^RERUN/ a\# RERUN moved to rerun section (Autosubmit 3.x migration)' $target_file
	sed -i '/^RERUN/ d' $target_file
	sed -i '/^CHUNKLIST/ a\# CHUNKLIST moved to rerun section (Autosubmit 3.x migration)' $target_file
	sed -i '/^CHUNKLIST/ d' $target_file
	sed -i '/^WALLCLOCK/ a\# WALLCLOCK moved to jobs.conf (Autosubmit 3.x migration)' $target_file
	sed -i '/^WALLCLOCK/ d' $target_file
	sed -i '/^NUMPROC/ a\# NUMPROC moved to jobs.conf (Autosubmit 3.x migration)' $target_file
	sed -i '/^NUMPROC/ d' $target_file

	echo "Unnecessary configuration variables deleted..."
else
	echo "No configuration variables deleted..."
fi

if grep -q CALENDAR "$target_file" ; then
	echo "No configuration variables added..."
else

sed -i '/CHUNKINI =*/a \
# Calendar used. LIST: standard, noleap \
CALENDAR = standard \
' $target_file

sed -i '/TEMPLATE_NAME =*/a \
\
[project] \
# Select project type. STRING = git, svn, local, none \
# If PROJECT_TYPE is set to none, Autosubmit self-contained dummy templates will be used \
PROJECT_TYPE = local \
\
# If PROJECT_TYPE is not git, no need to change \
[git] \
# Repository URL  STRING = "'"https://github.com/torvalds/linux.git"'" \
PROJECT_ORIGIN = \
# Select branch or tag, STRING, default = "'"master"'", help = {"'"master"'" (default), "'"develop"'", "'"v3.1b"'", ...} \
PROJECT_BRANCH = \
# type = STRING, default = leave empty, help = if model branch is a TAG leave empty \
PROJECT_COMMIT = \
\
# If PROJECT_TYPE is not svn, no need to change \
[svn] \
# type = STRING, help = "'"https://svn.ec-earth.org/ecearth3"'" \
PROJECT_URL = \
# Select revision number. NUMERIC = 1778 \
PROJECT_REVISION = \
\
# If PROJECT_TYPE is not local, no need to change \
[local] \
# type = STRING, help = /foo/bar/ecearth  \
PROJECT_PATH = /cfu/autosubmit/'$copy_from'/git \
\
# If PROJECT_TYPE is none, no need to change \
[project_files] \
# Where is PROJECT CONFIGURATION file location relative to project root path \
FILE_PROJECT_CONF = ../conf/expdef_'$target_expid'.conf \
\
[rerun] \
# Is a rerun or not? [Default: Do set FALSE]. BOOLEAN = TRUE, FALSE \
RERUN = FALSE \
# If RERUN = TRUE then supply the list of chunks to rerun \
# LIST = "[ 19601101 [ fc0 [1 2 3 4] fc1 [1] ] 19651101 [ fc0 [16-30] ] ]" \
CHUNKLIST = \
' $target_file

echo "New needed configuration variables added succesfuly..."
fi

exit 0
