#!/bin/bash
##
 # configure
 #
 # [Y]et [A]nother Winamp [P]lug-[i]n [B]undle (YAPiB)
 # Copyright (C) 2020  Peter Belkner (pbelkner@snafu.de)
 #
 # YAPiB 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 2 of the License, or
 # (at your option) any later version.
 #
 # YAPiB 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/>.
 ##
#
# 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'`
if [ -z "${srcdir}" ]; then # [
  srcdir=`pwd`
elif [ "." == "${srcdir}" ]; then # ] [
  srcdir=`pwd`
fi # ]
app=wapib
prefix=`pwd`
msvc='/c/Program Files (x86)/Microsoft Visual Studio 12.0'
mssdk='/c/Program Files (x86)/Microsoft SDKs/Windows/v7.1A'
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:\
,prefix:\
,libdir:\
,bindir:\
,sodir:\
,includedir:\
,unpackdir:\
,msvc:\
,mssdk:\
,wasdk:\
,winamp:\
,tools:\
,nsis:\
"
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}
    ;;
  --prefix)
    optarg="${1}"; shift          # Consume the argument and shift.
    prefix=${optarg}
    ;;
  --sodir)
    optarg="${1}"; shift          # Consume the argument and shift.
    sodir=${optarg}
    ;;
  --includedir)
    optarg="${1}"; shift          # Consume the argument and shift.
    includedir=${optarg}
    ;;
  --libdir)
    optarg="${1}"; shift          # Consume the argument and shift.
    libdir=${optarg}
    ;;
  --bindir)
    optarg="${1}"; shift          # Consume the argument and shift.
    bindir=${optarg}
    ;;
  --unpackdir)
    optarg="${1}"; shift          # Consume the argument and shift.
    unpackdir=${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}
    ;;
  --nsis)
    optarg="${1}"; shift          # Consume the argument and shift.
    nsis=${optarg}
    ;;
  --)
    break                         # Break out from the loop.
    ;;
  *)
    # We should never go here.
    echo "Internal error: ${opt}!" ;
    exit 1
    ;;
  esac # ]
done # ]

if [ -z "${bindir}" ]; then bindir="${prefix}/bin"; fi
if [ -z "${libdir}" ]; then libdir="${prefix}/lib"; fi
if [ -z "${sodir}" ]; then sodir="${bindir}"; fi
if [ -z "${includedir}" ]; then includedir="${prefix}/include"; fi

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

echo "app=${app}" >> ${config}
echo "srcdir=${srcdir}" >> ${config}

echo "prefix=${prefix}" >> ${config}
options+="'--prefix=${prefix}' "
echo "bindir=${bindir}" >> ${config}
options+="'--bindir=${bindir}' "
echo "libdir=${libdir}" >> ${config}
options+="'--libdir=${libdir}' "
echo "sodir=${sodir}" >> ${config}
echo "includedir=${includedir}" >> ${config}
options+="'--includedir=${includedir}' "
if [ ! \( -z "${unpackdir}" \) ]; then # [
  echo "unpackdir=${unpackdir}" >> ${config}
  options+="'--unpackdir=${unpackdir}' "
fi # ]
echo "msvc=${msvc}" >> ${config}
options+="'--msvc=${msvc}' "
echo "mssdk=${mssdk}" >> ${config}
options+="'--mssdk=${mssdk}' "
echo "wasdk=${wasdk}" >> ${config}
options+="'--wasdk=${wasdk}' "
echo "winamp=${winamp}" >> ${config}
options+="'--winamp=${winamp}' "
echo "nsis=${nsis}" >> ${config}
options+="'--nsis=${nsis}' "
if [ ! \( -z "${tools}" \) ]; then # [
  echo "tools=${tools}" >> ${config}
  options+="'--tools=${tools}' "
fi # ]

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

for app in "libpib" "libyas"; do # [
  if [ -d "${srcdir}/${app}" ]; then # [
    mkdir -p ./${app}
    cd ./${app}
    eval "${srcdir}/${app}/configure --app=${app} ${options}"
    cd ..
  fi # ]
done # ]

#if [ -d "${srcdir}/libpib" ]; then # [
#  mkdir -p ./libpib
#  cd ./libpib
#  eval "${srcdir}/libpib/configure --app=libpib ${options}"
#  cd ..
#fi # ]
#
#if [ -d "${srcdir}/libyas" ]; then # [
#  mkdir -p ./libyas
#  cd ./libyas
#  eval "${srcdir}/libyas/configure --app=libyas ${options}"
#  cd ..
#fi # ]
