#!/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-2021 by The BRLTTY Developers.
#
# BRLTTY comes with ABSOLUTELY NO WARRANTY.
#
# This is free software, placed under the terms of the
# GNU Lesser General Public License, as published by the Free Software
# Foundation; either version 2.1 of the License, or (at your option) any
# later version. Please see the file LICENSE-LGPL for details.
#
# Web Page: http://brltty.app/
#
# This software is maintained by Dave Mielke <dave@mielke.cc>.
###############################################################################

set -e

. "`dirname "${0}"`/brltty-prologue.sh"
defaultSourceRoot="${programDirectory}"
addProgramOption s string.directory sourceRoot "the top-level directory of the source tree" "${defaultSourceRoot}"
parseProgramArguments "${@}"

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

inputName="config"
outputName="forbuild"

headerExtension="h"
logExtension="log"
makeExtension="mk"
sedExtension="sed"
statusExtension="status"

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

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

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

objectExtension='$(O_FOR_BUILD)'

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

   for variable in CC CFLAGS CXX CXXFLAGS LDFLAGS LDLIBS PKG_CONFIG
   do
      getVariable "${variable}_FOR_BUILD" value
      setVariable "${variable}" "${value}"
      export "${variable}"
   done

   "${sourceRoot}/configure" \
      --disable-api \
      --disable-gpm \
      --disable-iconv \
      --disable-icu \
      --disable-x \
      \
      --without-libbraille \
      --with-braille-driver=-all \
      \
      --disable-speech-support \
      --with-speech-driver=-all \
      \
      -with-screen-driver=-all \
      \
      --without-pgmpath-package \
      --without-service-package \
      --without-params-package \
      --without-dynld-package \
      --without-charset-package \
      --without-hostcmd-package \
      --without-mntpt-package \
      --without-mntfs-package \
      --without-kbd-package \
      --without-beep-package \
      --without-pcm-package \
      --without-midi-package \
      --without-fm-package \
      --without-serial-package \
      --without-usb-package \
      --without-bluetooth-package \
      --without-ports-package \
      \
      --quiet
fi

sedScript="${outputName}.${sedExtension}"
sed -n -e '
s/^ *\([A-Za-z][A-Za-z0-9_]*\) *=.*$/\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}"

echo "\$a\\
B = ${objectExtension}
" >>"${sedScript}"

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

for extension in "${headerExtension}" "${logExtension}" "${statusExtension}"
do
   inputFile="${inputName}.${extension}"

   [ ! -f "${inputFile}" ] || {
      cp "${inputFile}" "${initialDirectory}/${outputName}.${extension}"
   }
done

exit 0
