#!/usr/bin/env bash
# based on cipl-delivery in use by AAT-PPI

set -o errexit -o nounset

function usage {
  echo "Usage: deliver-cipl [parent_directory]

  This script moves a completed CIPL calibration into the appropriate location for
  Data Analysts to run Quality Assurance.

  Required parameters:
  parent_directory    The main directory for this calibration run, which holds the
                      rawdata, working and products directories
  "
}

#
# command line argument
#
# We only need the subdirectory name, as everything else is placed
# consistently by casa.
echo "RUNNING STANDARD CALIBRATION DELIVERY!"
SUBDIRECTORY=$1
echo "SUBDIRECTORY: " "$SUBDIRECTORY"


# Get the qa2 and parent directory paths from CAPO
WS_DIR=$(pycapo -q edu.nrao.archive.workspaces.ProcessingSettings.rootDirectory)
echo "WS_DIR: " "$WS_DIR"
QA2_DIR=$(pycapo -q edu.nrao.archive.workflow.config.CiplWorkflowSettings.ciplDelivery)
echo "QA2_DIR: " "$QA2_DIR"

# Get the weblog caching directory from CAPO
WEBLOG_CACHE=$(pycapo -q edu.nrao.archive.workflow.config.CiplWorkflowSettings.cacheWeblogDirectory)
echo "WEBLOG_CACHE: " "$WEBLOG_CACHE"


#
# Cache the weblog for viewing, and extract it in the /products/ directory for
# easy local access as well.
#
if [ -e  "${WS_DIR}"/"${SUBDIRECTORY}"/products/weblog.tgz ]
then

    #ensure the cache path exists
    mkdir -p "${WEBLOG_CACHE}"/"${SUBDIRECTORY}"
    #cache the weblog
    tar -C"${WEBLOG_CACHE}"/"${SUBDIRECTORY}" -xzf "${WS_DIR}"/"${SUBDIRECTORY}"/products/weblog.tgz
    if [ $? -ne 0 ]
        then
            echo "Warning: Unzipping of weblog.tgz into the cache failed"
    fi

    #and unzip in place before we move everything
    tar -C"${WS_DIR}"/"${SUBDIRECTORY}"/products -xzf "${WS_DIR}"/"${SUBDIRECTORY}"/products/weblog.tgz
    if [ $? -ne 0 ]
        then
            echo "Warning: Unzipping of weblog.tgz into /products/ failed"
    fi


else
    WEBLOG_DIR=$(ls -t "${WS_DIR}"/"${SUBDIRECTORY}"/products | grep pipeline-)

    if [ -n WEBLOG_DIR ]
    then
        #The weblog exists, copy that instead
        echo "Warning, no weblog.tgz, copying ${WEBLOG_DIR} instead"
        mkdir -p "${WEBLOG_CACHE}"/"${SUBDIRECTORY}"
        cp -r "${WEBLOG_DIR}" /"${SUBDIRECTORY}"
        if [ $? -ne 0 ]
        then
            echo "Warning Copying of ${WEBLOG_DIR} to ${WEBLOG_CACHE}/${SUBDIRECTORY} failed"
        fi
    else
        echo "ERROR: There is neither a weblog directory or tar file for ${SUBDIRECTORY}.  Exiting"
        exit -1
    fi
fi


# Move the parent copy of the data subdirectories (rawdata/products/working)
# over to qa2
if [ ! -e "${QA2_DIR}"/"${SUBDIRECTORY}" ]
then
    mkdir -p "${QA2_DIR}"/"${SUBDIRECTORY}"
else
    echo "Delivering to existing ${QA2_DIR}/${SUBDIRECTORY}:"
    echo $(ls -l "${QA2_DIR}"/"${SUBDIRECTORY}")
fi

declare -a CONTENTS
CONTENTS=$(ls -d "${WS_DIR}"/"${SUBDIRECTORY}"/*)

for individual_fn in ${CONTENTS[@]}; do
    if [[ -d ${individual_fn} ]]
    then
        /bin/mv -f "${individual_fn}" "${QA2_DIR}"/"${SUBDIRECTORY}"
    fi
done