#! /usr/bin/env bash

set -eu

RUNTIME_PREFIX=$(cd -- "${BASH_SOURCE%"${BASH_SOURCE##*/}"}.." && pwd)

## CONFIG variables substituted from build script

CONFIG_MAPNIK_NAME="mapnik"
CONFIG_MAPNIK_VERSION="400004"
CONFIG_MAPNIK_VERSION_STRING="4.0.4"
CONFIG_GIT_REVISION="N/A"
CONFIG_GIT_DESCRIBE="${CONFIG_MAPNIK_VERSION_STRING}"

CONFIG_PREFIX="/opt/local"
CONFIG_LIBDIR_SCHEMA="lib"
CONFIG_LIB_DIR_NAME="mapnik"
CONFIG_MAPNIK_LIB_BASE="/opt/local/lib"
CONFIG_MAPNIK_LIB_DIR="/opt/local/lib/mapnik"
CONFIG_MAPNIK_INPUT_PLUGINS="/opt/local/lib/mapnik/input"
CONFIG_MAPNIK_FONTS="/opt/local/lib/mapnik/fonts"

CONFIG_CXX="/usr/bin/clang++"
CONFIG_CXXFLAGS="-std=c++17 -DU_USING_ICU_NAMESPACE=0 -stdlib=libc++ -I/opt/local/libexec/boost/1.76/include -I/opt/local/include -DACCEPT_USE_OF_DEPRECATED_PROJ_API_H=1 -isysroot/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk -pipe -I/opt/local/libexec/boost/1.76/include -Os -stdlib=libc++ -isysroot/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk -arch x86_64 -fvisibility=hidden -fvisibility-inlines-hidden -ftemplate-depth-300 -O3"
CONFIG_DEFINES="-DMAPNIK_MEMORY_MAPPED_FILE -DMAPNIK_HAS_DLCFN -DBIGINT -DBOOST_REGEX_HAS_ICU -DHAVE_JPEG -DMAPNIK_USE_PROJ -DMAPNIK_PROJ_VERSION=70201 -DHAVE_PNG -DHAVE_WEBP -DHAVE_TIFF -DDARWIN -DMAPNIK_THREADSAFE -DBOOST_SPIRIT_NO_PREDEFINED_TERMINALS=1 -DBOOST_PHOENIX_NO_PREDEFINED_TERMINALS=1 -DBOOST_SPIRIT_USE_PHOENIX_V3=1 -DNDEBUG -DGRID_RENDERER"
CONFIG_LDFLAGS="-L/opt/local/libexec/boost/1.76/lib -L/opt/local/lib -L/opt/local/lib/proj7/lib -L/opt/local/lib/postgresql17 -stdlib=libc++ -Wl,-headerpad_max_install_names -Wl,-syslibroot,/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk -arch x86_64"
CONFIG_DEP_INCLUDES="-I/opt/local/include -I/opt/local/include/freetype2 -I/opt/local/include/libpng16 -I/opt/local/lib/proj7/include -I/opt/local/libexec/boost/1.76/include -I/opt/local/include/postgresql17"
CONFIG_DEP_LIBS="-lboost_regex-mt -lpng -lproj -lsqlite3 -ltiff -lwebp -licui18n -lharfbuzz -ljpeg -licuuc -lfreetype -lz"
CONFIG_QUERIED_GDAL_DATA=""
CONFIG_QUERIED_PROJ_LIB="/opt/local/lib/proj7/share/proj"
CONFIG_QUERIED_ICU_DATA="/opt/local/share/icu/76.1"

## D.R.Y. variables

DRY_INCLUDES="-I${RUNTIME_PREFIX}/include -I${RUNTIME_PREFIX}/include/mapnik/agg -I${RUNTIME_PREFIX}/include/mapnik/deps"
DRY_CFLAGS="${DRY_INCLUDES} ${CONFIG_DEP_INCLUDES} ${CONFIG_DEFINES} ${CONFIG_CXXFLAGS}"
DRY_LIBS="-L${RUNTIME_PREFIX}/${CONFIG_LIBDIR_SCHEMA} -l${CONFIG_MAPNIK_NAME}"

## program below

usage()
{
    cat <<EOF
Usage: mapnik-config [OPTION]

Known values for OPTION are:

  -h --help         display this help and exit
  -v --version      version information (MAPNIK_VERSION_STRING)
  --version-number  version number (MAPNIK_VERSION) (new in 2.2.0)
  --git-revision    git hash from "git rev-list --max-count=1 HEAD"
  --git-describe    git decribe output (new in 2.2.0)
  --fonts           default fonts directory
  --input-plugins   default input plugins directory
  --defines         pre-processor defines for Mapnik build (new in 2.2.0)
  --prefix          Mapnik prefix [default $CONFIG_PREFIX]
  --lib-name        Mapnik library name
  --libs            library linking information
  --dep-libs        library linking information for Mapnik dependencies
  --ldflags         library paths (-L) information
  --includes        include paths (-I) for Mapnik headers (new in 2.2.0)
  --dep-includes    include paths (-I) for Mapnik dependencies (new in 2.2.0)
  --cxxflags        c++ compiler flags and pre-processor defines (new in 2.2.0)
  --cflags          all include paths, compiler flags, and pre-processor defines (for back-compatibility)
  --cxx             c++ compiler used to build mapnik (new in 2.2.0)
  --all-flags       all compile and link flags (new in 2.2.0)
  --gdal-data       path to GDAL_DATA directory, if detected at build time (new in 3.0.16)
  --proj-lib        path to PROJ_LIB directory, if detected at build time (new in 3.0.16)
  --icu-data        path to ICU_DATA directory, if detected at build time (new in 3.0.16)
EOF

    exit $1
}

if test $# -eq 0; then
    usage 1
fi

while test $# -gt 0; do
    case "$1" in

    -h| --help)
      usage 0
      ;;

    -v| --version)
      echo ${CONFIG_MAPNIK_VERSION_STRING}
      ;;

    --version-number)
      echo ${CONFIG_MAPNIK_VERSION}
      ;;

    --git-revision)
      echo ${CONFIG_GIT_REVISION}
      ;;

    --git-describe)
      echo ${CONFIG_GIT_DESCRIBE}
      ;;

    --fonts)
      printf '%s\n' "${CONFIG_MAPNIK_FONTS/#"$CONFIG_PREFIX"/$RUNTIME_PREFIX}"
      ;;

    --input-plugins)
      printf '%s\n' "${CONFIG_MAPNIK_INPUT_PLUGINS/#"$CONFIG_PREFIX"/$RUNTIME_PREFIX}"
      ;;

    --defines)
      printf '%s\n' "${CONFIG_DEFINES}"
      ;;

    --prefix)
      printf '%s\n' "${RUNTIME_PREFIX}"
      ;;

    --lib-name)
      printf '%s\n' "${CONFIG_MAPNIK_NAME}"
      ;;

    --libs)
      printf '%s\n' "${DRY_LIBS}"
      ;;

    --dep-libs)
      printf '%s\n' "${CONFIG_DEP_LIBS}"
      ;;

    --ldflags)
      printf '%s\n' "${CONFIG_LDFLAGS}"
      ;;

    --includes)
      printf '%s\n' "${DRY_INCLUDES}"
      ;;

    --dep-includes)
      printf '%s\n' "${CONFIG_DEP_INCLUDES}"
      ;;

    --cxxflags)
      printf '%s\n' "${CONFIG_CXXFLAGS}"
      ;;

    --cflags)
      printf '%s\n' "${DRY_CFLAGS}"
      ;;

    --cxx)
      printf '%s\n' "${CONFIG_CXX}"
      ;;

    --all-flags)
      printf '%s\n' "${DRY_CFLAGS} ${DRY_LIBS} ${CONFIG_LDFLAGS} ${CONFIG_DEP_LIBS}"
      ;;

    --gdal-data)
      printf "%s${CONFIG_QUERIED_GDAL_DATA:+\\n}" "${CONFIG_QUERIED_GDAL_DATA}"
      ;;

    --proj-lib)
      printf "%s${CONFIG_QUERIED_PROJ_LIB:+\\n}" "${CONFIG_QUERIED_PROJ_LIB}"
      ;;

    --icu-data)
      printf "%s${CONFIG_QUERIED_ICU_DATA:+\\n}" "${CONFIG_QUERIED_ICU_DATA}"
      ;;

    *)
      # push to stderr any invalid options
      echo "unknown option $1" >&2
      ;;
    esac
    shift
done

exit 0
