#!/bin/bash
# piscal launcher

usage="$(basename "$0") [-h] -d working_directory [-u notify_url] -- script to launch Piscal

where:
    -h  show this help text
    -d  working directory
    -f  input filename
    -t  suppress storage copy
    -u  url to notify on completion"

# http://stackoverflow.com/a/246128/99492
base_directory="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
working_directory=""
output_directory_base_name="output"
input_directory_name="input"
cleaned_input_directory_name="clninput"
touser_directory_name="fitresult/touser"
nottouser_directory_name="fitresult/nottouser"

# import the settings from piscal.cfg
# $piscal_executable and $storage_directory
. "$base_directory/piscal_launcher.cfg"

while getopts "hd:f:u:t" opt; do
    case "$opt" in
    h )
        echo "$usage"
        exit
        ;;
    d )
        working_directory=$OPTARG
        ;;
    t )
        suppress_storage_copy=true
        ;;
    u )
        notify_url=$OPTARG
        ;;
    \?) printf "illegal option: -%s\n" "$OPTARG" >&2
        echo "$usage" >&2
        exit 1
        ;;
    esac
done

if [ -z "$working_directory" ]; then
    echo "working directory required (-d)"
    exit 1
fi
if [ ! -d "$working_directory" ]; then
    echo "working directory $working_directory not found"
    exit 1
fi
output_directory_base="$working_directory/$output_directory_base_name"
output_directory_touser="$output_directory_base/$touser_directory_name"
output_directory_nottouser="$output_directory_base/$nottouser_directory_name"
cleaned_input_directory="$output_directory_base/$cleaned_input_directory_name"
run_directory="$working_directory/run"

# setup output directories
if [ ! -d "$output_directory_base" ]; then
    mkdir "$output_directory_base"
fi
if [ ! -d "$cleaned_input_directory" ]; then
    mkdir "$cleaned_input_directory"
fi

#find "$working_directory/$input_directory_name" -maxdepth 1 -type f\
#    -exec cp {} "$cleaned_input_directory" \;

if [ ! -d "$output_directory_base/fitresult" ]; then
    mkdir "$output_directory_base/fitresult"
fi
if [ ! -d "$output_directory_touser" ]; then
    mkdir "$output_directory_touser"
fi
if [ ! -d "$output_directory_nottouser" ]; then
    mkdir "$output_directory_nottouser"
fi
if [ ! -d "$run_directory" ]; then
    mkdir "$run_directory"
fi

# run piscal
pushd $run_directory >> /dev/null

if [ -z "$test_output_directory" ]; then
    eval $piscal_executable
else
    cp -r "$test_output_directory"/* "$output_directory_base"/
fi

popd >> /dev/null

# copy output to storage
if [ -z "$suppress_storage_copy" ]; then
    cp "$output_directory_touser"/* "$storage_directory"/output/ 2>/dev/null
    cp "$output_directory_nottouser"/* "$storage_directory"/output/ 2>/dev/null
    cp "$cleaned_input_directory"/* "$storage_directory"/input/ 2>/dev/null

    mv_script=$base_directory/subdir_year.sh
    pushd "$storage_directory"/output >> /dev/null
    "$mv_script"
    popd >> /dev/null
    pushd "$storage_directory"/input >> /dev/null
    "$mv_script"
    popd >> /dev/null
fi

# notify given url of completion
if [ -n "$notify_url" ]; then
    wget -qO- -t 1 "$notify_url" # &> /dev/null 2>&1
fi
