#!/bin/sh
#
# Copyright by The HDF Group.
# Copyright by the Board of Trustees of the University of Illinois.
# All rights reserved.
#
# This file is part of HDF.  The full HDF copyright notice, including
# terms governing use, modification, and redistribution, is contained in
# the COPYING file, which can be found at the root of the source code
# distribution tree, or in https://support.hdfgroup.org/ftp/HDF/releases/.
# If you do not have access to either file, you may request a copy from
# help@hdfgroup.org.
#

# This script runs flex/lex and bison/yacc to generate parser code for
# the mfhdf code.
#
# NOTE CAREFULLY!
#
# There is NO dependency in either the autotools or CMake to regenerate
# the parser code. If you modify ncgen.l or ncgen.y, you will need to run
# this script manually on a system with a suitable lexer and parser generator.
#
# IMPORTANT OS X NOTE
#
# If you are using OS X, you will probably not have flex or bison
# installed. In addition, even if you do have bison installed, the bison
# version you have installed may also have a bug that makes it unable to
# process our input files.
#
# The easiest way to fix this is to install everything via Homebrew:
#
#   http://brew.sh/
#
# After you install the base packages, install flex/bison.
#
#   brew install flex
#   brew install bison
#
# END IMPORTANT OS X NOTE
#
# If you want to use a particular version of flex or bison, the paths
# to each tool can be overridden using the following environment
# variables:
#
#   HDF_FLEX
#   HDF_BISON
#
# This script takes one option:
#
# -v
#
# This emits some extra information, mainly tool versions.

echo
echo "***************************************"
echo "* HDF4 NetCDF parser generator script *"
echo "***************************************"
echo

# Default is not verbose output
verbose=false

optspec=":hpv-"
while getopts "$optspec" optchar; do
    case "${optchar}" in
    h)
        echo "usage: $0 [OPTIONS] /path/to/ncgen/directory"
        echo
        echo "      -h      Print this help message."
        echo
        echo "      -v      Show more verbose output."
        echo
        echo "  NOTE: Each tool can be set via an environment variable."
        echo "        These are documented inside this script."
        echo
        exit 0
        ;;
    v)
        echo "Setting verbosity: high"
        echo
        verbose=true
        ;;
    *)
        if [ "$OPTERR" != 1 ] || [ "${optspec:0:1}" = ":" ]; then
            echo "Non-option argument: '-${OPTARG}'" >&2
        fi
        ;;
    esac
done

# Get the path to the ncgen directory
shift $(($OPTIND - 1))
path_to_ncgen_src=$1
if test -z ${path_to_ncgen_src}; then
    echo "*** ERROR *** - Path to mfhdf/ncgen not set"
    echo "Please add the path to the mfhdf/ncgen directory as a parameter"
    echo "See $0 -h for more help."
    echo
    exit -1
fi

# If paths to autotools are not specified, use whatever the system
# has installed as the default. We use 'which <tool>' to
# show exactly what's being used.
if test -z ${HDF_BISON}; then
    HDF_BISON="$(command -v bison)"
fi
if test -z ${HDF_FLEX}; then
    HDF_FLEX="$(command -v flex)"
fi

# Make sure that these versions of the tools are in the path
BISON_DIR=`dirname ${HDF_BISON}`
FLEX_DIR=`dirname ${HDF_FLEX}`
PATH=${FLEX_DIR}:${BISON_DIR}:$PATH

# Run flex and bison
# automatically generates mfhdf/ncgen/ncgenyy.c and mfhdf/ncgen/ncgentab.c
# Note that, as of Xcode 6.1 (2015), the default bison version on OS X
# is old enough to have the circular dependency bug. You'll have
# to install a later version of bison. See the OS X note at the top
# of this script.
echo
echo "Generating ncgen parser code (requires yacc/bison):"
echo "Generate mfhdf/ncgen/ncgenyy.c from mfhdf/ncgen/ncgen.y"
# HDF_BISON is set via the environment or 'command -v bison', above
if test -z ${HDF_BISON}; then
    echo
    echo "*************************"
    echo " ERROR - bison not found"
    echo "*************************"
    echo "bison is required to generate parser code in mfhdf/ncgen"
    echo
    exit 127
fi
if [ "$verbose" = true ] ; then
    ${HDF_BISON} --version
fi
${HDF_BISON} -o ${path_to_ncgen_src}/ncgentab.c -d ${path_to_ncgen_src}/ncgen.y

echo
echo "Generating ncgen lexer code (requires lex/flex):"
echo "Generate mfhdf/ncgen/ncgentab.c from mfhdf/ncgen/ncgen.l"
# HDF_FLEX is set via the environment or 'command -v flex', above
if test -z ${HDF_FLEX}; then
    echo
    echo "************************"
    echo " ERROR - flex not found"
    echo "************************"
    echo "flex is required to generate lexer code in mfhdf/ncgen"
    echo
    exit 127
fi
if [ "$verbose" = true ] ; then
    ${HDF_FLEX} --version
fi
${HDF_FLEX} --nounistd -o ${path_to_ncgen_src}/ncgenyy.c ${path_to_ncgen_src}/ncgen.l

echo
exit 0

