#!/bin/bash

###############################################################################
#                   COPY_GRAPH a000 EXPERIMENT
###############################################################################
###################
# Autosubmit header
###################
locale_to_set=$(locale -a | grep ^C.)
if [ -z "$locale_to_set" ] ; then
    # locale installed...
    export LC_ALL=$locale_to_set
else
    # locale not installed...
    locale_to_set=$(locale -a | grep ^en_GB.utf8)
    if [ -z "$locale_to_set" ] ; then
        export LC_ALL=$locale_to_set
    else
        export LC_ALL=C
    fi 
fi

set -xuve
job_name_ptrn='/home/kinow/autosubmit/a000/tmp/LOG_a000/a000_COPY_GRAPH'
echo $(date +%s) > ${job_name_ptrn}_STAT

################### 
# AS CHECKPOINT FUNCTION
###################
# Creates a new checkpoint file upon call based on the current numbers of calls to the function

AS_CHECKPOINT_CALLS=0
function as_checkpoint {
    AS_CHECKPOINT_CALLS=$((AS_CHECKPOINT_CALLS+1))
    touch ${job_name_ptrn}_CHECKPOINT_${AS_CHECKPOINT_CALLS}
}
###################
# Autosubmit job
###################

#!/bin/bash

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

set -eux -o pipefail

REMOTE_WORKFLOW_RUN_DIRECTORY="/tmp/remote/mhm-project/kinow/a000"
MHM_DATA_DIRECTORY="${REMOTE_WORKFLOW_RUN_DIRECTORY}/data"
SCP_USER="kinow@localhost"
PROJECT_DIRECTORY="/home/kinow/autosubmit/a000/proj/git_project"

#######################################
# Copy files from the workflow directory to the project directory.
# Globals:
#   None
# Arguments:
#   Remote mhm data directory.
#   SCP user string (user@host).
#   Autosubmit project directory location.
# Outputs:
#   0 if the files are successfully copied via scp, >0 otherwise.
#######################################
copy_files() {
  local remote_mhm_data_directory=$1
  local scp_user=$2
  local project_directory=$3
  local target_location="${scp_user}:${remote_mhm_data_directory}"
  scp \
    "${target_location}/mhm_output.tar.gz" \
    "${target_location}/plot.gif" \
    "${project_directory}"
}

copy_files $MHM_DATA_DIRECTORY $SCP_USER $PROJECT_DIRECTORY

###################
# Autosubmit tailer
###################
set -xuve
echo $(date +%s) >> ${job_name_ptrn}_STAT
touch ${job_name_ptrn}_COMPLETED
exit 0

