#! /bin/sh

# The following settings are determined by the configure script.

C_COMPILER="gcc"
C_PREPROC="gcc -E"
PKG_CONFIG="pkg-config"

FLAGS_OBJ="-march=armv7-a -mfloat-abi=hard -mfpu=neon -O2 -pipe -fstack-protector-strong -fno-plt -fexceptions         -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security         -fstack-clash-protection   -Wno-unused -Wno-write-strings -Wdisabled-optimization -fwrapv -fno-strict-aliasing -fno-trapping-math -fno-math-errno -fschedule-insns2 -foptimize-sibling-calls -fmodulo-sched -freschedule-modulo-scheduled-loops -fomit-frame-pointer -fPIC -fno-common "
FLAGS_DYN="-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -march=armv7-a -mfloat-abi=hard -mfpu=neon -O2 -pipe -fstack-protector-strong -fno-plt -fexceptions         -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security         -fstack-clash-protection   -Wno-unused -Wno-write-strings -Wdisabled-optimization -fwrapv -fno-strict-aliasing -fno-trapping-math -fno-math-errno -fschedule-insns2 -foptimize-sibling-calls -fmodulo-sched -freschedule-modulo-scheduled-loops -fomit-frame-pointer -fPIC -fno-common   -rdynamic -shared"
FLAGS_LIB="-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -march=armv7-a -mfloat-abi=hard -mfpu=neon -O2 -pipe -fstack-protector-strong -fno-plt -fexceptions         -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security         -fstack-clash-protection    -rdynamic -shared"
FLAGS_EXE="-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -march=armv7-a -mfloat-abi=hard -mfpu=neon -O2 -pipe -fstack-protector-strong -fno-plt -fexceptions         -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security         -fstack-clash-protection   -Wno-unused -Wno-write-strings -Wdisabled-optimization -fwrapv -fno-strict-aliasing -fno-trapping-math -fno-math-errno -fschedule-insns2 -foptimize-sibling-calls -fmodulo-sched -freschedule-modulo-scheduled-loops -fomit-frame-pointer -fPIC -fno-common   -rdynamic"

FLAGS_OPT=" -O1"
FLAGS_OPT_RTS=" -O3"

DEFS_OBJ=" -D___SINGLE_HOST "
DEFS_DYN=" -D___SINGLE_HOST -D___DYNAMIC"
DEFS_LIB=" -D___SINGLE_HOST "
DEFS_EXE=" -D___SINGLE_HOST "

BUILD_OBJ="\${C_COMPILER} \${FLAGS_OPT} \${FLAGS_OBJ} \${DEFS_OBJ} -I\"\${GAMBITDIR_INCLUDE}\" -c -o \${BUILD_OBJ_OUTPUT_FILENAME_PARAM} \${BUILD_OBJ_CC_OPTIONS_PARAM} \${BUILD_OBJ_INPUT_FILENAMES_PARAM}"
BUILD_DYN="\${C_COMPILER} \${FLAGS_OPT} \${FLAGS_DYN} \${DEFS_DYN} -I\"\${GAMBITDIR_INCLUDE}\" -o \${BUILD_DYN_OUTPUT_FILENAME_PARAM} \${BUILD_DYN_CC_OPTIONS_PARAM} \${BUILD_DYN_LD_OPTIONS_PRELUDE_PARAM} \${BUILD_DYN_INPUT_FILENAMES_PARAM} \${BUILD_DYN_LD_OPTIONS_PARAM}"
BUILD_LIB="echo BUILD_LIB not yet implemented"
BUILD_EXE="\${C_COMPILER} \${FLAGS_EXE} \${DEFS_EXE} -I\"\${GAMBITDIR_INCLUDE}\"  -o \${BUILD_EXE_OUTPUT_FILENAME_PARAM} \${BUILD_EXE_CC_OPTIONS_PARAM} \${BUILD_EXE_LD_OPTIONS_PRELUDE_PARAM} \${BUILD_EXE_INPUT_FILENAMES_PARAM} \"\${GAMBITDIR_LIB}/libgambit.a\" \${LIBS} \${BUILD_EXE_LD_OPTIONS_PARAM}"

DEFS=""
LIBS="-lm "
GAMBITLIB="gambit"
GAMBITGSCLIB="gambitgsc"
GAMBITGSILIB="gambitgsi"
LIB_PREFIX="lib"
LIB_EXTENSION=".a"
OBJ_EXTENSION=".o"
EXE_EXTENSION=""
BAT_EXTENSION=""

ALL_BUILD_FEATURES="unix windows gcc clang visualc cc"
BUILD_FEATURE_C_COMP="gcc"
BUILD_FEATURE_OS="unix"

GAMBUILD_CC_OPTIONS_REGEXP="^ #|\\*/\"\\*/\"cc-options|#"
GAMBUILD_LD_OPTIONS_PRELUDE_REGEXP="^ #|\\*/\"\\*/\"ld-options-prelude|#"
GAMBUILD_LD_OPTIONS_REGEXP="^ #|\\*/\"\\*/\"ld-options|#"
GAMBUILD_PKG_CONFIG_REGEXP="^ #|\\*/\"\\*/\"pkg-config|#"
GAMBUILD_PKG_CONFIG_PATH_REGEXP="^ #|\\*/\"\\*/\"pkg-config-path|#"

GAMBUILD_SPACE=' '
GAMBUILD_NL='
'

scheme_string_decode()
{
  printf "%s" "$1" | sed -e "s,\\\\\\\",\\\",g" -e "s,\\\\\\\\,,g" -e "s,\\\\t,	,g" -e "s,\\\\v,,g" -e "s,\\\\f,,g" -e "s,,\\\\,g" -e "s,^\",,g" -e "s,\"$,,g"
}

shell_string_encode()
{
  printf "%s" "$1" | sed "s,','\\\\'',g;s,^,',;s,\$,'," ;
}

shell_quote()
{
  RESULT=""
  for arg in $1 ; do
    if test -z "$RESULT" ; then
      RESULT="$(shell_string_encode ${arg})"
    else
      RESULT="${RESULT} $(shell_string_encode ${arg})"
    fi
  done
  printf "%s" "${RESULT}"
}

sed_quote()
{
  # see: https://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed
  printf "%s" "$1" | sed 's/[&/\]/\\&/g'
}

eval_sexpr()
{
  sexpr="$(printf "%s" "$1" | sed -e "s,\\\\\\\\,,g" -e "s,\\\\\\\",,g" -e "s,',,g" -e "s,\\\\t,	,g" -e "s,\\\\v,,g" -e "s,\\\\f,,g")"

  # temporarily remove doublequotes in strings
  sexpr="$(printf "%s" "$sexpr" | sed -e "s/\\\\\\\\\\\\\\\\//g" -e "s/\\\\\\\\\"//g")"

  # create a table of the strings in sexpr
  str=
  prev=""
  while test "$sexpr" != "$prev"; do
    prev="$sexpr"
    str=_$str
    eval "STR$str=$(printf "%s" "$sexpr" | sed -e "s/[^\\\"]*\\\"\\([^\\\"]*\\)\\\".*/\\1/" -e "s/^/'/" -e "s/$/'/")"
    sexpr="$(printf "%s" "$sexpr" | sed -e "s/\\\"[^\\\"]*\\\"/$str/")"
  done

  sexpr="$(printf "%s" "$sexpr" | sed -e "s/)/ )/g")"

  GAMBUILD_SAVE_IFS2="$IFS"
  IFS="${GAMBUILD_SPACE}"

  for feature in ${ALL_BUILD_FEATURES}; do
    if test "$feature" = "${BUILD_FEATURE_OS}" -o \
            "$feature" = "${BUILD_FEATURE_C_COMP}"; then
      sexpr="$(printf "%s" "$sexpr" | sed -e "s/ $feature / #t /g")"
    else
      sexpr="$(printf "%s" "$sexpr" | sed -e "s/ $feature / #f /g")"
    fi
  done

  IFS="${GAMBUILD_SAVE_IFS2}"

  # apply reduction rules until a normal form is reached
  prev=""
  while test "$sexpr" != "$prev"; do
    prev="$sexpr"
    sexpr="$(printf "%s" "$sexpr" | sed \
      -e "s/(and #t /(and /g" \
      -e "s/(and #f #t /(and #f /g" \
      -e "s/(and #f #f /(and #f /g" \
      -e "s/(and #f )/#f/g" \
      -e "s/(and )/#t/g" \
      -e "s/(or #f /(or /g" \
      -e "s/(or #t #t /(or #t /g" \
      -e "s/(or #t #f /(or #t /g" \
      -e "s/(or #t )/#t/g" \
      -e "s/(or )/#f/g" \
      -e "s/(not #t )/#f/g" \
      -e "s/(not #f )/#t/g" \
      -e "s/^(when #t \([^() ]*\) )/\1/g" \
      -e "s/^(when #f \([^() ]*\) )//g")"
  done

  if test "$sexpr" != "" ; then
    if printf "%s" "$sexpr" | grep "^_*$" > /dev/null ; then
      # get string and put doublequotes back in string
      eval "printf \"%s\" \"\\\"\$STR$sexpr\\\"\"" | sed -e "s//'/g" -e "s//\\\\\"/g" -e "s//\\\\\\\\/g"
    else
      printf "%s" "unsupported expression"
    fi
  fi
}

get_meta_info()
{
  for info in $(grep "$2" "$1" | sed -e "s,$2,,g"); do
    val_info="$(eval_sexpr "$info")"
    if test "$val_info" != ""; then
      if test "$val_info" = "unsupported expression"; then
        printf "%s\n" "*** WARNING -- unsupported meta-info is being ignored: $info" >> /dev/tty
      else
        printf "%s\n" "$val_info"
      fi
    fi
  done
}

get_meta_info_list()
{
  GAMBUILD_META_INFO_LIST=""
  for info in $(get_meta_info "$1" "$2"); do
    GAMBUILD_META_INFO_LIST="${GAMBUILD_META_INFO_LIST}$(scheme_string_decode "${info}") "
  done
}

process_pkg_config()
{
  for arg in $1; do
    if test -z "${PKG_CONFIG}"; then
      printf "%s\n" "*** WARNING -- the pkg-config program is unavailable but is needed to get C compiler options using 'pkg-config --cflags $arg' and 'pkg-config --libs $arg'" >> /dev/tty
      exit 1
    else
      GAMBUILD_SAVE_IFS2="$IFS"
      IFS="${GAMBUILD_SPACE}"
      set -e
      GAMBUILD_CFLAGS="$(${PKG_CONFIG} --cflags $arg)"
      GAMBUILD_LIBS="$(${PKG_CONFIG} --libs $arg)"
      set +e
      IFS="${GAMBUILD_SAVE_IFS2}"
      accumulate_cc_options "${GAMBUILD_CFLAGS}" "${GAMBUILD_NL}"
      accumulate_ld_options "${GAMBUILD_LIBS}" "${GAMBUILD_NL}"
    fi
  done
}

accumulate_cc_options()
{
  GAMBUILD_SAVE_IFS2="$IFS"
  IFS="$2"
  for flag in $1 ; do
    GAMBUILD_CC_OPTIONS="${GAMBUILD_CC_OPTIONS}$flag "
  done
  IFS="${GAMBUILD_SAVE_IFS2}"
}

accumulate_ld_options_prelude()
{
  GAMBUILD_SAVE_IFS2="$IFS"
  IFS="$2"
  for flag in $1 ; do
    GAMBUILD_LD_OPTIONS_PRELUDE="${GAMBUILD_LD_OPTIONS_PRELUDE}$flag "
  done
  IFS="${GAMBUILD_SAVE_IFS2}"
}

accumulate_ld_options()
{
  GAMBUILD_SAVE_IFS2="$IFS"
  IFS="$2"
  for flag in $1 ; do
    GAMBUILD_LD_OPTIONS="${GAMBUILD_LD_OPTIONS}$flag "
  done
  IFS="${GAMBUILD_SAVE_IFS2}"
}

process_pkg_config_path()
{
  for path in $1; do
    if test -z "${GAMBUILD_PKG_CONFIG_PATH}"; then
      GAMBUILD_PKG_CONFIG_PATH="$path"
    else
      # prepend path so that last path added has priority
      GAMBUILD_PKG_CONFIG_PATH="$path:${GAMBUILD_PKG_CONFIG_PATH}"
    fi
  done
}

extract_cc_ld_options()
{
  GAMBUILD_CC_OPTIONS=""
  GAMBUILD_LD_OPTIONS_PRELUDE=""
  GAMBUILD_LD_OPTIONS=""
  GAMBUILD_PKG_CONFIG_PATH="${PKG_CONFIG_PATH}"

  GAMBUILD_SAVE_IFS="$IFS"
  IFS="${GAMBUILD_NL}"

  if test ! -z "$1"; then
    get_meta_info_list "$1" "${GAMBUILD_PKG_CONFIG_PATH_REGEXP}"
    process_pkg_config_path "${GAMBUILD_META_INFO_LIST}"
  fi

  process_pkg_config_path "$3"

  export PKG_CONFIG_PATH="${GAMBUILD_PKG_CONFIG_PATH}"

  if test ! -z "$1"; then
    get_meta_info_list "$1" "${GAMBUILD_PKG_CONFIG_REGEXP}"
    process_pkg_config "${GAMBUILD_META_INFO_LIST}"
  fi

  process_pkg_config "$2"

  for file in $1; do

    get_meta_info_list "$file" "${GAMBUILD_CC_OPTIONS_REGEXP}"
    GAMBUILD_CC_OPTIONS="${GAMBUILD_CC_OPTIONS}${GAMBUILD_META_INFO_LIST}"

    get_meta_info_list "$file" "${GAMBUILD_LD_OPTIONS_PRELUDE_REGEXP}"
    GAMBUILD_LD_OPTIONS_PRELUDE="${GAMBUILD_LD_OPTIONS_PRELUDE}${GAMBUILD_META_INFO_LIST}"

    get_meta_info_list "$file" "${GAMBUILD_LD_OPTIONS_REGEXP}"
    GAMBUILD_LD_OPTIONS="${GAMBUILD_LD_OPTIONS}${GAMBUILD_META_INFO_LIST}"

  done

  accumulate_cc_options "$4" "${GAMBUILD_NL}"
  accumulate_ld_options_prelude "$5" "${GAMBUILD_NL}"
  accumulate_ld_options "$6" "${GAMBUILD_NL}"

  IFS="${GAMBUILD_SAVE_IFS}"
}

substitute_common()
{
  printf "%s" "$1" | sed \
    -e "s\${C_COMPILER}$(sed_quote "${C_COMPILER}")g" \
    -e "s\${C_PREPROC}$(sed_quote "${C_PREPROC}")g" \
    -e "s\${PKG_CONFIG}$(sed_quote "${PKG_CONFIG}")g" \
    -e "s\${GAMBITDIR_INCLUDE}$(sed_quote "${GAMBITDIR_INCLUDE}")g" \
    -e "s\${GAMBITDIR_LIB}$(sed_quote "${GAMBITDIR_LIB}")g" \
    -e "s\${DEFS_DYN}$(sed_quote "${DEFS_DYN}")g" \
    -e "s\${DEFS_EXE}$(sed_quote "${DEFS_EXE}")g" \
    -e "s\${DEFS_OBJ}$(sed_quote "${DEFS_OBJ}")g" \
    -e "s\${FLAGS_DYN}$(sed_quote "${FLAGS_DYN}")g" \
    -e "s\${FLAGS_EXE}$(sed_quote "${FLAGS_EXE}")g" \
    -e "s\${FLAGS_OBJ}$(sed_quote "${FLAGS_OBJ}")g" \
    -e "s\${FLAGS_OPT}$(sed_quote "${FLAGS_OPT}")g" \
    -e "s\${LIBS}$(sed_quote "${LIBS}")g"
}

substitute_obj()
{
  printf "%s" "$(substitute_common "$1")" | sed \
    -e "s\${BUILD_OBJ_CC_OPTIONS_PARAM}$(sed_quote "${BUILD_OBJ_CC_OPTIONS_PARAM}")g" \
    -e "s\${BUILD_OBJ_INPUT_FILENAMES_PARAM}$(sed_quote "$(shell_quote "${BUILD_OBJ_INPUT_FILENAMES_PARAM}")")g" \
    -e "s\${BUILD_OBJ_OUTPUT_FILENAME_PARAM}$(sed_quote "$(shell_quote "${BUILD_OBJ_OUTPUT_FILENAME_PARAM}")")g"
}

substitute_dyn()
{
  printf "%s" "$(substitute_common "$1")" | sed \
    -e "s\${BUILD_DYN_CC_OPTIONS_PARAM}$(sed_quote "${BUILD_DYN_CC_OPTIONS_PARAM}")g" \
    -e "s\${BUILD_DYN_LD_OPTIONS_PRELUDE_PARAM}$(sed_quote "${BUILD_DYN_LD_OPTIONS_PRELUDE_PARAM}")g" \
    -e "s\${BUILD_DYN_LD_OPTIONS_PARAM}$(sed_quote "${BUILD_DYN_LD_OPTIONS_PARAM}")g" \
    -e "s\${BUILD_DYN_INPUT_FILENAMES_PARAM}$(sed_quote "$(shell_quote "${BUILD_DYN_INPUT_FILENAMES_PARAM}")")g" \
    -e "s\${BUILD_DYN_OUTPUT_FILENAME_PARAM}$(sed_quote "$(shell_quote "${BUILD_DYN_OUTPUT_FILENAME_PARAM}")")g"
}

substitute_exe()
{
  printf "%s" "$(substitute_common "$1")" | sed \
    -e "s\${BUILD_EXE_CC_OPTIONS_PARAM}$(sed_quote "${BUILD_EXE_CC_OPTIONS_PARAM}")g" \
    -e "s\${BUILD_EXE_LD_OPTIONS_PRELUDE_PARAM}$(sed_quote "${BUILD_EXE_LD_OPTIONS_PRELUDE_PARAM}")g" \
    -e "s\${BUILD_EXE_LD_OPTIONS_PARAM}$(sed_quote "${BUILD_EXE_LD_OPTIONS_PARAM}")g" \
    -e "s\${BUILD_EXE_INPUT_FILENAMES_PARAM}$(sed_quote "$(shell_quote "${BUILD_EXE_INPUT_FILENAMES_PARAM}")")g" \
    -e "s\${BUILD_EXE_OUTPUT_FILENAME_PARAM}$(sed_quote "$(shell_quote "${BUILD_EXE_OUTPUT_FILENAME_PARAM}")")g"
}

case "$1" in

  "obj")

    if test ! -z "${BUILD_OBJ_CC_PARAM}"; then
      C_COMPILER="${BUILD_OBJ_CC_PARAM}"
      FLAGS_OBJ=""
    fi

    extract_cc_ld_options "${BUILD_OBJ_META_INFO_FILE_PARAM}" "${BUILD_OBJ_PKG_CONFIG_PARAM}" "${BUILD_OBJ_PKG_CONFIG_PATH_PARAM}" "${BUILD_OBJ_CC_OPTIONS_PARAM}" "${BUILD_OBJ_LD_OPTIONS_PRELUDE_PARAM}" "${BUILD_OBJ_LD_OPTIONS_PARAM}"

    BUILD_OBJ_CC_OPTIONS_PARAM="${GAMBUILD_CC_OPTIONS}"

    BUILD_OBJ_CMD="$(substitute_obj "$BUILD_OBJ")"

    if test ! -z "${GAMBUILD_VERBOSE}"; then
      printf "%s\n" "${BUILD_OBJ_CMD}"
    fi

    eval "${BUILD_OBJ_CMD}"
    ;;

  "dyn")

    if test ! -z "${BUILD_DYN_CC_PARAM}"; then
      C_COMPILER="${BUILD_DYN_CC_PARAM}"
      FLAGS_DYN=""
    fi

    extract_cc_ld_options "${BUILD_DYN_META_INFO_FILE_PARAM}" "${BUILD_DYN_PKG_CONFIG_PARAM}" "${BUILD_DYN_PKG_CONFIG_PATH_PARAM}" "${BUILD_DYN_CC_OPTIONS_PARAM}" "${BUILD_DYN_LD_OPTIONS_PRELUDE_PARAM}" "${BUILD_DYN_LD_OPTIONS_PARAM}"

    BUILD_DYN_CC_OPTIONS_PARAM="${GAMBUILD_CC_OPTIONS}"
    BUILD_DYN_LD_OPTIONS_PRELUDE_PARAM="${GAMBUILD_LD_OPTIONS_PRELUDE}"
    BUILD_DYN_LD_OPTIONS_PARAM="${GAMBUILD_LD_OPTIONS}"

    BUILD_DYN_CMD="$(substitute_dyn "$BUILD_DYN")"

    if test ! -z "${GAMBUILD_VERBOSE}"; then
      printf "%s\n" "${BUILD_DYN_CMD}"
    fi

    eval "${BUILD_DYN_CMD}"
    ;;

  "lib")

    if test ! -z "${BUILD_LIB_CC_PARAM}"; then
      C_COMPILER="${BUILD_LIB_CC_PARAM}"
      FLAGS_LIB=""
    fi

    BUILD_LIB_CMD="$BUILD_LIB"

    if test ! -z "${GAMBUILD_VERBOSE}"; then
      printf "%s\n" "${BUILD_LIB_CMD}"
    fi

    eval "${BUILD_LIB_CMD}"
    ;;

  "exe")

    if test ! -z "${BUILD_EXE_CC_PARAM}"; then
      C_COMPILER="${BUILD_EXE_CC_PARAM}"
      FLAGS_EXE=""
    fi

    extract_cc_ld_options "${BUILD_EXE_META_INFO_FILE_PARAM}" "${BUILD_EXE_PKG_CONFIG_PARAM}" "${BUILD_EXE_PKG_CONFIG_PATH_PARAM}" "${BUILD_EXE_CC_OPTIONS_PARAM}" "${BUILD_EXE_LD_OPTIONS_PRELUDE_PARAM}" "${BUILD_EXE_LD_OPTIONS_PARAM}"

    BUILD_EXE_CC_OPTIONS_PARAM="${GAMBUILD_CC_OPTIONS}"
    BUILD_EXE_LD_OPTIONS_PRELUDE_PARAM="${GAMBUILD_LD_OPTIONS_PRELUDE}"
    BUILD_EXE_LD_OPTIONS_PARAM="${GAMBUILD_LD_OPTIONS}"

    BUILD_EXE_CMD="$(substitute_exe "$BUILD_EXE")"

    if test ! -z "${GAMBUILD_VERBOSE}"; then
      printf "%s\n" "${BUILD_EXE_CMD}"
    fi

    eval "${BUILD_EXE_CMD}"
    ;;

  "C_COMPILER")
    printf "%s\n" "${C_COMPILER}"
    ;;

  "C_PREPROC")
    printf "%s\n" "${C_PREPROC}"
    ;;

  "PKG_CONFIG")
    printf "%s\n" "${PKG_CONFIG}"
    ;;

  "FLAGS_OBJ")
    printf "%s\n" "${FLAGS_OBJ}"
    ;;

  "FLAGS_DYN")
    printf "%s\n" "${FLAGS_DYN}"
    ;;

  "FLAGS_LIB")
    printf "%s\n" "${FLAGS_LIB}"
    ;;

  "FLAGS_EXE")
    printf "%s\n" "${FLAGS_EXE}"
    ;;

  "FLAGS_OPT")
    printf "%s\n" "${FLAGS_OPT}"
    ;;

  "FLAGS_OPT_RTS")
    printf "%s\n" "${FLAGS_OPT_RTS}"
    ;;

  "DEFS_OBJ")
    printf "%s\n" "${DEFS_OBJ}"
    ;;

  "DEFS_DYN")
    printf "%s\n" "${DEFS_DYN}"
    ;;

  "DEFS_LIB")
    printf "%s\n" "${DEFS_LIB}"
    ;;

  "DEFS_EXE")
    printf "%s\n" "${DEFS_EXE}"
    ;;

  "BUILD_OBJ")
    printf "%s\n" "${BUILD_OBJ}"
    ;;

  "BUILD_DYN")
    printf "%s\n" "${BUILD_DYN}"
    ;;

  "BUILD_LIB")
    printf "%s\n" "${BUILD_LIB}"
    ;;

  "BUILD_EXE")
    printf "%s\n" "${BUILD_EXE}"
    ;;

  "DEFS")
    printf "%s\n" "${DEFS}"
    ;;

  "LIBS")
    printf "%s\n" "${LIBS}"
    ;;

  "GAMBITLIB")
    printf "%s\n" "${GAMBITLIB}"
    ;;

  "GAMBITGSCLIB")
    printf "%s\n" "${GAMBITGSCLIB}"
    ;;

  "GAMBITGSILIB")
    printf "%s\n" "${GAMBITGSILIB}"
    ;;

  "LIB_PREFIX")
    printf "%s\n" "${LIB_PREFIX}"
    ;;

  "LIB_EXTENSION")
    printf "%s\n" "${LIB_EXTENSION}"
    ;;

  "OBJ_EXTENSION")
    printf "%s\n" "${OBJ_EXTENSION}"
    ;;

  "EXE_EXTENSION")
    printf "%s\n" "${EXE_EXTENSION}"
    ;;

  "BAT_EXTENSION")
    printf "%s\n" "${BAT_EXTENSION}"
    ;;

  "ALL_BUILD_FEATURES")
    printf "%s\n" "${ALL_BUILD_FEATURES}"
    ;;

  "BUILD_FEATURE_OS")
    printf "%s\n" "${BUILD_FEATURE_OS}"
    ;;

  "BUILD_FEATURE_C_COMP")
    printf "%s\n" "${BUILD_FEATURE_C_COMP}"
    ;;

  "")
    # Dump configure script settings

    ESC_SH="sed -e s/\\\\/\\\\\\\\\\\\\\\\/g -e s/\\\"/\\\\\\\"/g -e s/\\\$/\\\\\\\$/g -e s/^/\"/ -e s/$/\"/"

    echo C_COMPILER=`echo "${C_COMPILER}" | ${ESC_SH}`
    echo C_PREPROC=`echo "${C_PREPROC}" | ${ESC_SH}`
    echo PKG_CONFIG=`echo "${PKG_CONFIG}" | ${ESC_SH}`

    echo FLAGS_OBJ=`echo "${FLAGS_OBJ}" | ${ESC_SH}`
    echo FLAGS_DYN=`echo "${FLAGS_DYN}" | ${ESC_SH}`
    echo FLAGS_LIB=`echo "${FLAGS_LIB}" | ${ESC_SH}`
    echo FLAGS_EXE=`echo "${FLAGS_EXE}" | ${ESC_SH}`

    echo FLAGS_OPT=`echo "${FLAGS_OPT}" | ${ESC_SH}`
    echo FLAGS_OPT_RTS=`echo "${FLAGS_OPT_RTS}" | ${ESC_SH}`

    echo DEFS_OBJ=`echo "${DEFS_OBJ}" | ${ESC_SH}`
    echo DEFS_DYN=`echo "${DEFS_DYN}" | ${ESC_SH}`
    echo DEFS_LIB=`echo "${DEFS_LIB}" | ${ESC_SH}`
    echo DEFS_EXE=`echo "${DEFS_EXE}" | ${ESC_SH}`

    echo BUILD_OBJ=`echo "${BUILD_OBJ}" | ${ESC_SH}`
    echo BUILD_DYN=`echo "${BUILD_DYN}" | ${ESC_SH}`
    echo BUILD_LIB=`echo "${BUILD_LIB}" | ${ESC_SH}`
    echo BUILD_EXE=`echo "${BUILD_EXE}" | ${ESC_SH}`

    echo DEFS=`echo "${DEFS}" | ${ESC_SH}`
    echo LIBS=`echo "${LIBS}" | ${ESC_SH}`
    echo GAMBITLIB=`echo "${GAMBITLIB}" | ${ESC_SH}`
    echo GAMBITGSCLIB=`echo "${GAMBITGSCLIB}" | ${ESC_SH}`
    echo GAMBITGSILIB=`echo "${GAMBITGSILIB}" | ${ESC_SH}`
    echo LIB_PREFIX=`echo "${LIB_PREFIX}" | ${ESC_SH}`
    echo LIB_EXTENSION=`echo "${LIB_EXTENSION}" | ${ESC_SH}`
    echo OBJ_EXTENSION=`echo "${OBJ_EXTENSION}" | ${ESC_SH}`
    echo EXE_EXTENSION=`echo "${EXE_EXTENSION}" | ${ESC_SH}`
    echo BAT_EXTENSION=`echo "${BAT_EXTENSION}" | ${ESC_SH}`
    echo ALL_BUILD_FEATURES=`echo "${ALL_BUILD_FEATURES}" | ${ESC_SH}`
    echo BUILD_FEATURE_OS=`echo "${BUILD_FEATURE_OS}" | ${ESC_SH}`
    echo BUILD_FEATURE_C_COMP=`echo "${BUILD_FEATURE_C_COMP}" | ${ESC_SH}`
    ;;

  *)
    echo "gambuild-C unknown operation \"$1\""
    exit 1
    ;;

esac
