#! /bin/sh -
#
# Simple "configure" script for Qi.
#
# Copyright (c) 2016-2018, 2020-2022, 2024 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

usage() {
    printf '%s' \
"Usage: configure [options]

Defaults for the options are specified in brackets.

Options:
  --prefix=DIR            install files in DIR [${prefix}]
  --exec-prefix=DIR       base DIR for arch-dependent files [${exec_prefix}]
  --bindir=DIR            user executables [${bindir}]
  --sbindir=DIR           system admin executables [${sbindir}]
  --libexecdir=DIR        program executables [${libexecdir}]
  --sysconfdir=DIR        read-only single-machine data [${sysconfdir}]
  --localstatedir=DIR     modifiable single-machine data [${localstatedir}]
  --datarootdir=DIR       read-only arch-independent data root [${datarootdir}]
  --infodir=DIR           info documentation [${infodir}]
  --mandir=DIR            man documentation [${mandir}]
  --docdir=DIR            documentation root [${docdir}]
  --arch=NAME             architecture name to build packages [${arch}]
  --packagedir=DIR        directory for package installations [${packagedir}]
  --targetdir=DIR         target directory for symbolic links [${targetdir}]
  --outdir=DIR            output directory for binary packages [${outdir}]
  --qi-cflags=CFLAGS      default Qi C flags for compiler [${qi_cflags}]
  --qi-cxxflags=CXXFLAGS  default Qi C++ flags for compiler [${qi_cxxflags}]
  --qi-ldflags=LDFLAGS    default Qi flags for linker [${qi_ldflags}]
  --qi-cppflags=CPPFLAGS  default Qi C/C++ preprocessor flags [${qi_cppflags}]
"
}

# Defaults
prefix=/usr/local
exec_prefix='$(prefix)'
bindir='$(exec_prefix)/bin'
sbindir='$(exec_prefix)/sbin'
libexecdir='$(exec_prefix)/libexec'
sysconfdir='$(prefix)/etc'
localstatedir='$(prefix)/var'
datarootdir='$(prefix)/share'
infodir='$(datarootdir)/info'
mandir='$(datarootdir)/man'
docdir='$(datarootdir)/doc'
arch="$(uname -m)"
packagedir='$(prefix)/pkgs'
targetdir='$(prefix)'
outdir='$(localstatedir)/cache/qi/packages'
qi_cflags='-O2'
qi_cxxflags='-O2'
qi_ldflags=''
qi_cppflags=''

return_variables()
{
    printf '%s\n' \
     "prefix = $prefix"                \
     "exec_prefix = $exec_prefix"      \
     "bindir = $bindir"                \
     "sbindir = $sbindir"              \
     "libexecdir = $libexecdir"        \
     "sysconfdir = $sysconfdir"        \
     "localstatedir = $localstatedir"  \
     "datarootdir = $datarootdir"      \
     "infodir = $infodir"              \
     "mandir = $mandir"                \
     "docdir = $docdir"                \
     "arch = $arch"                    \
     "packagedir = $packagedir"        \
     "targetdir = $targetdir"          \
     "outdir = $outdir"                \
     "qi_cflags = $qi_cflags"          \
     "qi_cxxflags = $qi_cxxflags"      \
     "qi_ldflags = $qi_ldflags"        \
     "qi_cppflags = $qi_cppflags"      \
     ""
}

# Handle options
while test $# -gt 0
do
    case $1 in
    --prefix)
        prefix="$2"
        shift
        ;;
    --prefix=*)
        prefix="${1#*=}"
        ;;
    --exec-prefix)
        exec_prefix="$2"
        shift
        ;;
    --exec-prefix=*)
        exec_prefix="${1#*=}"
        ;;
    --bindir)
        bindir="$2"
        shift
        ;;
    --bindir=*)
        bindir="${1#*=}"
        ;;
    --sbindir)
        sbindir="$2"
        shift
        ;;
    --sbindir=*)
        sbindir="${1#*=}"
        ;;
    --libexecdir)
        libexecdir="$2"
        shift
        ;;
    --libexecdir=*)
        libexecdir="${1#*=}"
        ;;
    --sysconfdir)
        sysconfdir="$2"
        shift
        ;;
    --sysconfdir=*)
        sysconfdir="${1#*=}"
        ;;
    --localstatedir)
        localstatedir="$2"
        shift
        ;;
    --localstatedir=*)
        localstatedir="${1#*=}"
        ;;
    --datarootdir)
        datarootdir="$2"
        shift
        ;;
    --datarootdir=*)
        datarootdir="${1#*=}"
        ;;
    --infodir)
        infodir="$2"
        shift
        ;;
    --infodir=*)
        infodir="${1#*=}"
        ;;
    --mandir)
        mandir="$2"
        shift
        ;;
    --mandir=*)
        mandir="${1#*=}"
        ;;
    --docdir)
        docdir="$2"
        shift
        ;;
    --docdir=*)
        docdir="${1#*=}"
        ;;
    --arch)
        arch="$2"
        shift
        ;;
    --arch=*)
        arch="${1#*=}"
        ;;
    --packagedir)
        packagedir="$2"
        shift
        ;;
    --packagedir=*)
        packagedir="${1#*=}"
        ;;
    --targetdir)
        targetdir="$2"
        shift
        ;;
    --targetdir=*)
        targetdir="${1#*=}"
        ;;
    --outdir)
        outdir="$2"
        shift
        ;;
    --outdir=*)
        outdir="${1#*=}"
        ;;
    --qi-cflags)
        qi_cflags="$2"
        shift
        ;;
    --qi-cflags=*)
        qi_cflags="${1#*=}"
        ;;
    --qi-cxxflags)
        qi_cxxflags="$2"
        shift
        ;;
    --qi-cxxflags=*)
        qi_cxxflags="${1#*=}"
        ;;
    --qi-ldflags)
        qi_ldflags="$2"
        shift
        ;;
    --qi-ldflags=*)
        qi_ldflags="${1#*=}"
        ;;
    --qi-cppflags)
        qi_cppflags="$2"
        shift
        ;;
    --qi-cppflags=*)
        qi_cppflags="${1#*=}"
        ;;
    --help | --hel | --he | --h | '--?' | -help | -hel | -he | -h | '-?' )
        usage
        exit
        ;;
    --)
        shift
        break;	# End of options.
        ;;
    -*)
        echo "configure: WARNING: unrecognized option '${1}'" 1>&2
        ;;
    *)
        break
        ;;
    esac
    shift
done

echo "Creating config.mak ..."

return_variables;		# Show configured variables.
: > config.mak;			# Clean up config.mak, first.

if return_variables > config.mak
then
    touch src/qi.in && echo "OK, now you can run \`make'"
fi

