#!/bin/sh
###############################################################################
# BRLTTY - A background process providing access to the console screen (when in
#          text mode) for a blind person using a refreshable braille display.
#
# Copyright (C) 1995-2012 by The BRLTTY Developers.
#
# BRLTTY comes with ABSOLUTELY NO WARRANTY.
#
# This is free software, placed under the terms of the
# GNU General Public License, as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any
# later version. Please see the file LICENSE-GPL for details.
#
# Web Page: http://mielke.cc/brltty/
#
# This software is maintained by Dave Mielke <dave@mielke.cc>.
###############################################################################

. "`dirname "${0}"`/prologue.sh"
set -e

inputName="config"
outputName="forbuild"

headerExtension="h"
makeExtension="mk"
sedExtension="sed"

makeInput="${inputName}.${makeExtension}"
makeOutput="${initialDirectory}/${outputName}.${makeExtension}"

sourceRoot=""

while getopts ":s:" option
do
   case "${option}"
   in
      s) sourceRoot="${OPTARG}";;
      :) syntaxError "missing value: -${OPTARG}";;
     \?) syntaxError "unknown option: -${OPTARG}";;
      *) syntaxError "unimplemented option: -${option}";;
   esac
done
shift `expr "${OPTIND}" - 1`

[ "${#}" -eq 0 ] || syntaxError "too many parameters."

[ -n "${sourceRoot}" ] || sourceRoot=`dirname "${0}"`
[ -d "${sourceRoot}" ] || semanticError "source root not found: ${sourceRoot}"
sourceRoot=`cd "${sourceRoot}" && pwd` || exit "${?}"

getMakeProperty() {
   grep "^ *${1} *=" "${makeInput}" | sed -e 's/^[^=]*= *//' -e 's/ *$//'
}

buildSystem=`getMakeProperty BUILD_SYSTEM`
hostSystem=`getMakeProperty HOST_SYSTEM`

if [ "${hostSystem}" != "${buildSystem}" ]
then
   needTemporaryDirectory

   for variable in CC CFLAGS CXX CXXFLAGS LDFLAGS LDLIBS
   do
      unset "${variable}"
      variableForBuild="${variable}_FOR_BUILD"
      eval test '"${'"${variableForBuild}"'+set}"' != "set" || eval "${variable}"'="${'"${variableForBuild}"'}"'
   done

   "${sourceRoot}/configure" \
      --disable-api --disable-x \
      --without-libbraille --disable-speech-support \
      --with-braille-driver=-all --with-speech-driver=-all -with-screen-driver=-all \
      --without-serial-package --without-usb-package --without-bluetooth-package \
      --disable-beeper-support --disable-pcm-support --disable-midi-support --disable-fm-support \
      --quiet
fi

sedScript="${outputName}.${sedExtension}"
sed -n -e '
s/^ *\([A-Z][A-Z_]*\) *=.*$/\1/
t found
d
:found
h
s/^\(.*\)$/s%^\\( *\\)\\(\1\\)\\( *=\\)%\\1\\2_FOR_BUILD\\3%/p
g
s/^\(.*\)$/s%\\(\\$(\\)\\(\1\\)\\([):]\\)%\\1\\2_FOR_BUILD\\3%g/p
g
s/^\(.\)$/s%\\$\\(\1\\)%$(\\1_FOR_BUILD)%g/p
' <"${makeInput}" >"${sedScript}"

sed <"${makeInput}" >"${makeOutput}" -f "${sedScript}"
rm "${sedScript}"

for extension in "${headerExtension}"
do
   cp "${inputName}.${extension}" "${initialDirectory}/${outputName}.${extension}"
done

exit 0
