#! /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="400101"
CONFIG_MAPNIK_VERSION_STRING="4.1.1"
CONFIG_GIT_REVISION="N/A"
CONFIG_GIT_DESCRIBE="${CONFIG_MAPNIK_VERSION_STRING}"

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

CONFIG_CXX="c++"
CONFIG_CXXFLAGS="-std=c++20 -DU_USING_ICU_NAMESPACE=0 -march=x86-64 -mtune=generic -O2 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=3 -fstack-clash-protection -fcf-protection -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -Wp,-D_GLIBCXX_ASSERTIONS -g -ffile-prefix-map=/build/mapnik/src=/usr/src/debug/mapnik -flto=auto -ffat-lto-objects -fvisibility=hidden -fvisibility-inlines-hidden -pthread -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=90600 -DHAVE_PNG -DHAVE_WEBP -DHAVE_TIFF -DLINUX -DMAPNIK_THREADSAFE -DBOOST_SPIRIT_NO_PREDEFINED_TERMINALS=1 -DBOOST_PHOENIX_NO_PREDEFINED_TERMINALS=1 -DBOOST_SPIRIT_USE_PHOENIX_V3=1 -DNDEBUG -DHAVE_CAIRO -DGRID_RENDERER -DHAVE_LIBXML2"
CONFIG_LDFLAGS="-L/build/mapnik/src/mapnik-v4.1.1/freetype -L/build/mapnik/src/mapnik-v4.1.1/xml2 -L/usr/lib -Wl,-O1 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,-z,pack-relative-relocs -flto=auto -pthread"
CONFIG_DEP_INCLUDES="-I/usr/include -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/harfbuzz -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/sysprof-6 -I/usr/include/libxml2 -I/usr/include/cairo -I/usr/include/pixman-1"
CONFIG_DEP_LIBS="-lboost_regex -lcairo -lpng -lproj -lsqlite3 -ltiff -lwebp -lxml2 -licui18n -lharfbuzz -ljpeg -licuuc -lfreetype -lz -ldl"
CONFIG_QUERIED_GDAL_DATA="/usr/share/gdal"
CONFIG_QUERIED_PROJ_LIB="/usr/share/proj"
CONFIG_QUERIED_ICU_DATA="/usr/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
