#!/usr/bin/sh
#
# -*- mode: bash; -*-
#
# This file is part of the Back End Server component of the
# Hyrax Data Server.
#
# Copyright (c) 2015 OPeNDAP, Inc.
# Author: Nathan David Potter <ndp@opendap.org>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
# You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.

show_usage () { 
    echo ""
    echo "$0               OPeNDAP"
    echo ""
    echo "NAME"
    echo ""
    echo "    ${0} -- Get a DAP object from a local BES."
    echo ""
    echo "SYNOPSIS"
    echo ""
    echo "    ${0} [-v] [-c bes_conf_file] -d dap_request_type -i resource_id "; 
    echo ""
    echo "DESCRIPTION"
    echo ""
    echo " Retrieves a DAP object using the besstandalone application."
    echo " Because retrieving DAP objects from the BES exercises all of"
    echo " machinery of the BES (such as caches) '$0' can easily be used "
    echo " to pre-populate caches from a cron job or some other "
    echo " programatic activity."
    echo ""
    echo "OPTIONS"
    echo "    -h, -?     Shows this page."
    echo ""
    echo "    -v  file   Verbose mode."
    echo ""
    echo "    -i  file   The data file name relative to the "
    echo "               BES.Catalog.catalog.RootDirectory property in the"
    echo "               BES configuration file."
    echo ""
    echo "    -d  dds|das|dods|dmr|dap "
    echo "               The type of the DAP object to request"
    echo ""
    echo "    -c  file   Specifies the BES configuration file"
    echo ""
    echo "OPeNDAP              December 9, 2015"
}

OPTIND=1         # Reset in case getopts has been used previously in the shell.

verbose=0
debug=0

while getopts "h?vd:i:c:D" opt; do
    case "$opt" in
    h|\?)
        show_usage
        exit 0
        ;;
    d)  REQUESTED_DAP_OBJ=$OPTARG
        ;;
    i)  RESOURCE_ID=$OPTARG
        ;;
    v)  verbose=1
        echo "";
        echo "${0} verbose enabled";
        ;;
    c)  BES_CONF=$OPTARG
        ;;
    D)  debug=1
        echo "${0} debug enabled";
        ;;
    esac
done

shift $((OPTIND-1))

[ "$1" = "--" ] && shift

# [ -z $REQUESTED_DAP_OBJ ] && { echo "${0} You must supply a requested DAP response."; exit 1 }

# [ -z $RESOURCE_ID ] && { echo "${0} You must supply a resource (file)."; exit 1 }

if [ -z $BES_CONF ]
then 
    echo "${0} BES_CONF is not set trying env variable 'prefix'...";  
    if [ ! -z $prefix ] # True if var is set and str length != 0
    then
        test_conf=$prefix/etc/bes/bes.conf
        echo "${0} Env var 'prefix' is set. Checking $test_conf"; 
        if [ -r $test_conf ]
        then
            BES_CONF="$test_conf"
            if [ $verbose = 1 ]
            then
                echo "${0} BES_CONF: " $BES_CONF
            fi
        fi
    fi
fi   
    
if [ -z $BES_CONF ]
then     
    export BES_CONF=/etc/bes/bes.conf
    echo "${0} Last Attempt:  Trying $BES_CONF"; 
    if [ -r $BES_CONF ]
    then
       if [ $verbose = 1 ]
        then
            echo "${0} BES_CONF: " $BES_CONF
        fi
    else
        echo ""
        echo "${0} !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        echo ""
        echo "${0} Unable to locate a BES configuration file."
        echo "${0} You may identify your BES configurstion in one of three ways: "
        echo ""
        echo "${0}   1. Using the command line parameter '-c'"
        echo ""
        echo "${0}   2. Setting the environment variable $prefix to the install"
        echo "${0}      location of the BES."
        echo ""
        echo "${0}   3. Placing the bes.conf file in the well known location"
        echo "${0}          /etc/bes/bes.conf"
        echo ""
        echo "${0} The command line parameter is the recommended usage."        
        echo ""
        echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
         show_usage
        echo ""
       exit;
    fi
fi

if [ $verbose = 1 ]
then
    echo "${0} BES_CONF: ${BES_CONF}";
    echo "${0} REQUESTED_DAP_OBJ: $REQUESTED_DAP_OBJ";
    echo "${0} RESOURCE_ID: $RESOURCE_ID";
fi

cmdDoc=`cat <<EOF 
<?xml version="1.0" encoding="UTF-8"?>
<bes:request xmlns:bes="http://xml.opendap.org/ns/bes/1.0#" reqID="[http-8080-1:27:bes_request]">
  <bes:setContext name="xdap_accept">3.2</bes:setContext>
  <bes:setContext name="dap_explicit_containers">no</bes:setContext>
  <bes:setContext name="errors">xml</bes:setContext>
  <bes:setContext name="max_response_size">0</bes:setContext>
  <bes:setContainer name="catalogContainer" space="catalog">$RESOURCE_ID</bes:setContainer>
  <bes:define name="d1" space="default">
    <bes:container name="catalogContainer"/>
  </bes:define>
  <bes:get type="$REQUESTED_DAP_OBJ" definition="d1" />
</bes:request>
EOF
`

COMMAND_FILE=$(mktemp -t getDAP_XXXX)
if [ $verbose = 1 ]; then echo "${0} COMMAND_FILE: " $COMMAND_FILE; fi

echo $cmdDoc > $COMMAND_FILE

besstandalone_cmd="besstandalone -c ${BES_CONF} -i ${COMMAND_FILE}";
if [ $debug = 1 ]
then
    besstandalone_cmd="besstandalone -d cerr,metadata_store,dmrpp_store -c ${BES_CONF} -i ${COMMAND_FILE}";
fi
if [ $verbose = 1 ]; then echo "${0} BES_COMMAND: ${besstandalone_cmd}"; fi

$besstandalone_cmd;

if [  $debug != 1 ]
then
    rm $COMMAND_FILE
fi
