#! /bin/sh
##
# Copyright by The HDF Group.
# 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://www.hdfgroup.org/licenses.
# If you do not have access to either file, you may request a copy from
# help@hdfgroup.org.
##

prg=$0
if [ ! -e "$prg" ]; then
  case $prg in
    (*/*) exit 1;;
    (*) prg=$(command -v -- "$prg") || exit;;
  esac
fi
dir=$(
  cd -P -- "$(dirname -- "$prg")/.." && pwd -P
) || exit
prg=$dir/bin/$(basename -- "$prg") || exit

#printf '%s\n' "$prg"
#printf 'dir is %s\n' "$dir"

pc_args=""

# Show the configuration summary of the library recorded in the
# libhdf4.settings file residing in the lib directory.
showconfigure()
{
  cat $dir/lib/libhdf4.settings
  status=$?
}

usage() {
  # "How-to use" message.
  echo "usage: $prg [OPTIONS] <pkg-config line>"
  echo "  OPTIONS:"
  echo "    -help           This help message."
  echo "    -show           Show the commands without executing them"
  echo "    -showconfig     Show the HDF4 library configuration summary"
  echo " "
  echo "  <pkg-config line> - the pkg-config compile line options for the compiler"
  echo "                    that was used to compile HDF4." 
  echo "                    Use pkg-config --help for more information"
  echo "                    on which options are available. $prg passes pkg-config options"
  echo "                    through as those options use double-underscores."
  echo " "
  echo "                    NOTE: pkg-config is required to be installed on your system and"
  echo "                    using --static requires a static version of the C runtime library"
  echo "                    to be have been installed on your system."
  echo " "
  exit $EXIT_FAILURE
}

export PKG_CONFIG_PATH=$dir/lib/pkgconfig

for arg in $@ ; do
  case "$arg" in
    -showconfig)
      showconfigure
      exit $status
      ;;
    -show)
      shift
      echo /usr/bin/gcc.exe "$@" `pkg-config $pc_args --define-variable=prefix=$dir --cflags --libs hdf`
      exit $status
      ;;
    -help)
      usage
      exit $status
      ;;
    --*)
      # gather pkg-config specific options
      pc_args="$pc_args $arg"
      ;;
    *)
      /usr/bin/gcc.exe "$@" `pkg-config $pc_args --define-variable=prefix=$dir --cflags --libs hdf`
      status=$?
      exit $status
      ;;
  esac
done
