#!/bin/bash

# Process any leading options.  If an option argument is not recognized, set
# the needHelp variable.
#
needHelp=0
collectCoords=0
collectFill=0
collectMinMax=0
collectMissing=0

token=${1:-foo}

while [ ${token:0:1} == "-" ] ; do
    case $1 in 
        --collect-all ) {
                           collectCoords=1
                           collectFill=1
                           collectMinMax=1
                           collectMissing=1
                        } ;;
        --collect-coords ) collectCoords=1 ;;
        --collect-fill ) collectFill=1 ;;
        --collect-minmax ) collectMinMax=1 ;;
        --collect-missing ) collectMissing=1 ;;
        * ) needHelp=1 ;;
    esac

    shift

    token=${1:-foo}
done

# There should only be two arguments remaining.  If that isn't true, set
# needHelp.
#
if (( $# != 2 )) ; then
    needHelp=1
fi

# If help is needed, write a usage message and exit.
#
if [ $needHelp == 1 ] ; then
    echo
    echo 'Usage: NcMLtoHTML     [--collect-all] [--collect-coords] [--collect-fill]'
    echo '                      [--collect-minmax] [--collect-missing]'
    echo '                      <input NcML file> <output HTML file>'
    echo
    echo '    --collect-all      Equivalent to specifying all of the collect options.'
    echo '    --collect-coords   Remove names from variable "coordinates" attributes that'
    echo '                       specify coordinates that have names corresponding to'
    echo '                       shape dimensions.'
    echo '    --collect-fill     Write a FillValues table in the HTML file that contains a'
    echo '                       list of the most-used fill values for each type.'
    echo '                       Variables with _FillValue attributes that match these'
    echo '                       values will have "default" entries in the FillValue'
    echo '                       column.'
    echo '    --collect-minmax   Change valid_min and valid_max attributes that match the'
    echo '                       full-range limits for the variable types to MAX_<TYPE>'
    echo '                       and MIN_<TYPE>, respectively.  (For example, if the'
    echo '                       variable type is "unsigned short", and the valid_max'
    echo '                       attribute is 65535, display the attribute value as'
    echo '                       "MAX_USHORT".)'
    echo '    --collect-missing  If the global attributes for the NcML file contain'
    echo '                       attributes that specify the meanings and corresponding'
    echo '                       values for missing_value attributes for different types,'
    echo '                       write a MissingValues table in the HTML file that'
    echo '                       contains a list of the missing value meanings and values'
    echo '                       for each type.  Variables with missing_value attributes'
    echo '                       that match these values have "default" entries in the'
    echo '                       MissingValue column.  The global attributes specifying'
    echo '                       the missing value meanings and values will be removed.'

    exit 1;
fi

# Get the base path for this script file to use in locating the awk scripts.
#
dirbase=`dirname $0`

# Create a string to use an an awk script that makes sure that the input NcML
# file is "pretty".
#
# If a line has anything other than whitespace preceding or following an
# element delimiter, break it into multiple lines so that each element (or
# partial element) appears on a line by itself.
#
read awkprog << 'AWKPROG'
$0 ~ /[^[:space:]][[:space:]]*</ || $0 ~ />[[:space:]]*[^[:space:]]/ \
{ \
    sTmp = gensub(/([^[:space:]][[:space:]]*)</, "\\\\1\\n<", "g"); \
    sTmp = gensub(/>([[:space:]]*[^[:space:]])/, ">\\n\\\\1", "g", sTmp); \
 \
    print sTmp; \
 \
    next; \
} \
\
{ \
    print $0; \
}
AWKPROG

# Run the awk script.
#
gawk "$awkprog" $1 | gawk --assign nCollectCoordinatesValues=$collectCoords --assign nCollectFillValues=$collectFill \
                          --assign nCollectMinMaxValues=$collectMinMax --assign nCollectMissingValues=$collectMissing \
                          -f $dirbase/NcMLtoOutput.awk -f $dirbase/OutputHTML.awk > $2
