##
 # configure-homegrown
 #
 # Yet Another (WA)SAPI (YASAPI) NT Output Plugin for Winamp (out_yasapi-nt)
 # Copyright (C) 2018  Peter Belkner (pbelkner@snafu.de)
 #
 # out_yasapi-nt is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
 #
 # out_yasapi-nt is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 ##
#!/bin/bash
#
# Small getopts tutorial:
# http://wiki.bash-hackers.org/howto/getopts_tutorial
# Command Line Options: How To Parse In Bash Using "getopt":
# http://www.bahmanm.com/blogs/command-line-options-how-to-parse-in-bash-using-getopt
# Using getopts in bash shell script to get long and short command line options:
# https://stackoverflow.com/questions/402377/using-getopts-in-bash-shell-script-to-get-long-and-short-command-line-options

srcdir=`dirname "$0"|sed -e 's,/$,,g'`
app=out_yasapi-nt
wasdk='/c/Program Files (x86)/Winamp SDK'
winamp='/c/Program Files (x86)/Winamp'
nsis='/c/Program Files (x86)/NSIS'

# read the options
# * options  consist of a single letter and are listed without any separation.
#   a colon indicates an argument.
opts="a:"
# * long options  consist of more than one letter and are listed separated by
#   comma. a colon indicates an argument.
longopts="app:,msvc:,mssdk:,wasdk:,winamp:,tools:,reldir:,nsis:,gtk:"
TEMP=`getopt -o ${opts} -l ${longopts} -- "$@"`
eval set -- "${TEMP}"

# "getopt" just re-orders the options and their (optional) argument in a
# canonical way. We need to parse them ourselfs.
while true ; do # [
  opt="${1}"; shift               # Consume an option and shift.
  case "${opt}" in # [
  -a|--app)
    optarg="${1}"; shift          # Consume the argument and shift.
    app=${optarg}
    ;;
  --msvc)
    optarg="${1}"; shift          # Consume the argument and shift.
    msvc=${optarg}
    ;;
  --mssdk)
    optarg="${1}"; shift          # Consume the argument and shift.
    mssdk=${optarg}
    ;;
  --wasdk)
    optarg="${1}"; shift          # Consume the argument and shift.
    wasdk=${optarg}
    ;;
  --winamp)
    optarg="${1}"; shift          # Consume the argument and shift.
    winamp=${optarg}
    ;;
  --tools)
    optarg="${1}"; shift          # Consume the argument and shift.
    tools=${optarg}
    ;;
  --reldir)
    optarg="${1}"; shift          # Consume the argument and shift.
    reldir=${optarg}
    ;;
  --nsis)
    optarg="${1}"; shift          # Consume the argument and shift.
    nsis=${optarg}
    ;;
  --gtk)
    optarg="${1}"; shift          # Consume the argument and shift.
    gtk=${optarg}
    ;;
  --)
    break                         # Break out from the loop.
    ;;
  *)
    # We should never go here.
    echo "Internal error: ${opt}!" ;
    exit 1
    ;;
  esac # ]
done # ]

config="config.tmp.mak"
rm -f ${config}

echo "app=${app}" >> ${config}
echo "srcdir=${srcdir}" >> ${config}
echo "msvc=${msvc}" >> ${config}
echo "mssdk=${mssdk}" >> ${config}
echo "wasdk=${wasdk}" >> ${config}
echo "winamp=${winamp}" >> ${config}
echo "tools=${tools}" >> ${config}
echo "reldir=${reldir}" >> ${config}
echo "nsis=${nsis}" >> ${config}
echo "gtk=${gtk}" >> ${config}

if [ -e config.mak ]; then # [
  count=`diff ${config} config.mak | wc -l`
  if [ 0 -lt ${count} ]; then # [
    mv ${config} config.mak
  else # ] [
    rm -f ${config}
  fi # ]
else # ] [
  mv ${config} config.mak
fi # ]

if [ ! \( '.' -ef "${srcdir}" \) ]; then # [
  cp -p ${srcdir}/Makefile ./
fi # ]
