#!/bin/sh
################################################################################
# This script takes in an orient, aperture, and pointing (ra, dec, v2, v3) and
# generates the s_region of the footprint on the sky.
################################################################################

# Displays the help info and exits with an error status 1
display_full_help() {
    echo "This script takes in an orient, aperture, and pointing (ra, dec, v2, v3)"
    echo "and generates the s_region of the aperture's footprint on the sky.\n"
    echo "The parameters can either be passed to this script or provided in a CSV file."
    echo "If no input file is provided then the parameters must be specified."
    echo "If no output file is provided then the footprint will be printed to stdout."
    echo "Providing an input file takes precedence over passing arguments."
    echo "The input file must have the header row 'ra,dec,v2,v3,orient,aperture' (case and order insensitive).\n"
    echo "INPUT FILE EXAMPLE: siafFootprint -i <input.csv> [-o <output.csv>]\n"
    echo "INPUT PARAMETERS EXAMPLE: siafFootprint --ra 13:29:52.6980 --dec +47:11:42.93 --v2 1.13872 --v3 -0.363763 --orient 238.7 --aperture MIRIFU_CHANNEL1A [-o <output.csv>]\n"
    echo "Exit status 0 on success, 1 on failure.\n"
    echo "Parameter control:"
    echo "  --ra \t\t Target's RA given in HH:MM:SS.ssss"
    echo "  --dec \t Target's DEC given in +/-DD:MM:SS.ssss"
    echo "  --v2 \t\t Pointing's V2 given as a position x"
    echo "  --v3 \t\t Pointing's V3 given as a position y"
    echo "  --orient \t Spacecraft's orient (roll) angle given in degrees"
    echo "  --aperture \t Name of the aperture for which to calculate the footprint\n"
    echo "File control:"
    echo "  -i, --input \t CSV input file name listing above parameters"
    echo "  -o, --output \t CSV output file name to print generated footprints\n"
    echo "Miscellaneous:"
    echo "  -h, --help \t Display this help text and exit\n"
    echo "Outputs:"
    echo "  siaf_version \t\t The SIAF version used to retrieve the aperture data"
    echo "  v2ref \t\t The aperture's V2 reference point"
    echo "  v3ref \t\t The aperture's V3 reference point"
    echo "  aperture_vertices \t The aperture's vertices in V2V3 coordinates"
    echo "  s_region \t\t The aperture's shape in sky coordinates"
    exit 1
}

# Displays a little reminder of how to display the full help and exits with an error status 1
display_help() {
    echo "Try 'siafFootprint --help' for more information"
    exit 1
}

# Usage
if [ $# -lt 1 ]; then
    display_full_help
fi

# Assign parameters
while [ "$#" -gt 0 ]; do
    case "$1" in
        -i) inputfile="$2"; shift 2;;
        -o) outputfile="$2"; shift 2;;
        --input) inputfile="$2"; shift 2;;
        --output) outputfile="$2"; shift 2;;

        --ra) ra="$2"; shift 2;;
        --dec) dec="$2"; shift 2;;
        --v2) v2="$2"; shift 2;;
        --v3) v3="$2"; shift 2;;
        --orient) orient="$2"; shift 2;;
        --aperture) aperture="$2"; shift 2;;
        --ra|--dec|--v2|--v3|--orient|--aperture) echo "$1 requires an argument" >&2; exit 1;;

        -h) display_full_help;;
        --help) display_full_help;;

        *) echo "Unknown option: $1" >&2; display_help;;
    esac
done

# Check parameters before continuing
if [ ! -z ${inputfile+x} ] && ([[ ! -f $inputfile ]] || [[ ${inputfile: -4} != .csv ]]); then
    echo "The input file must be a CSV file."
    display_help
    exit 1
elif [ ! -z ${outputfile+x} ] && [[ ${outputfile: -4} != .csv ]]; then
    echo "The output file must be a CSV file."
    display_help
    exit 1
elif [ -z ${inputfile+x} ] && ([ -z ${ra+x} ] || [ -z ${dec+x} ] || [ -z ${v2+x} ] || [ -z ${v3+x} ] || [ -z ${orient+x} ] || [ -z ${aperture+x} ]); then
    echo "You must provide either an input file or all the parameters (ra, dec, v2, v3, orient, aperture)"
    display_help
    exit 1
fi

# Set the arguments to pass to the Java class, input file takes precedence
if [ ! -z ${inputfile+x} ]; then
    args="$inputfile"
else
    args="$ra $dec $v2 $v3 $orient $aperture"
fi

# Output file can always be included
if [ ! -z ${outputfile+x} ]; then
    args="$args $outputfile"
fi

# Figure out where this script is located
scriptPath="$0"
link=`readlink "${scriptPath}"`
while [ "${link}" != "" ]
do
   case "${link}" in
      /*) scriptPath="${link}";;
      *)  scriptPath=`dirname "${scriptPath}"`/"${link}";;
   esac
   link=`readlink "${scriptPath}"`
done

scriptDir=`dirname "${scriptPath}"`

# Call setAptEnv to define lib and javaCommand. Note that it *requires* scriptDir to be defined
. "${scriptDir}/setAptEnv"

# Call the ApertureFootprintExporter class passing the arguments
"${javaCommand}" -Xms64m -Xmx1300m \
    -cp "$lib/APT.jar" \
    edu.stsci.util.ApertureFootprintExporter ${args}
