#!/bin/sh --
# configure - configuration script for the MyMan text-terminal video game
# Copyright 2003, 2006-2007, Benjamin Sittler <bsittler@gmail.com>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use, copy,
# modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

# Bourne-compatible shell
SHELL=/bin/sh

# GNU sed (or possibly a compatible stream editor)
SED="${SED:-`type -p gsed >/dev/null 2>&1 && type -p gsed 2>/dev/null || echo sed`}"

# version of this script
configure_version=0.5

# character classes
charclass_ascii_lower="abcdefghijklmnopqrstuvwxyz"
charclass_ascii_upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
charclass_ascii_digit="0123456789"
charclass_ascii_id="${charclass_ascii_lower}${charclass_ascii_upper}${charclass_ascii_digit}_"

# name of this package, without version number
basename=myman

# file we search for to verify that a directory contains MyMan sources
testfile="src/myman.c"

# temporary filename used during generation of output files
conftmp="_conftmp.$$"
trap 'test -f "${conftmp}" && rm -f "${conftmp}"' 0 >/dev/null 2>&1

# make sure some standard directories are in our search path
if test :"${OSTYPE}" != :"msdosdjgpp"
then
    PATH="${PATH:-}:/usr/bin:/bin"
    export PATH
fi

features=

feature()
{
    _features_=" ${features} "
    if test :"${_features_%* $1 *}" = :"${_features_}"
    then
        if test :"${features}" != :""
        then
            features="${features} "
        fi
        features="${features}${1}"
    fi
    eval "${1}"'="${2}"'
}

export cputype

cputype="${cputype:-`
    uname -p 2>/dev/null ||
        uname -m 2>/dev/null
`}"

export vendor

vendor="${vendor:-unknown}"

export ostype

ostype="${ostype:-`
    uname -o 2>/dev/null ||
        uname -s 2>/dev/null
`}"

srcdir=
date=`unset LC_TIME; unset LC_ALL; unset LANG; unset LC_CTYPE; date || echo unknown`
feature date "${date}"

build=`
    (
        ${SHELL} -c '
            echo "${MACHTYPE:-${cputype:-unknown}-${vendor:-unknown}-${ostype:-unknown}}"
        ' 2>/dev/null ||
            echo "${MACHTYPE:-${cputype:-unknown}-${vendor:-unknown}-${ostype:-unknown}}"
    ) |
        ${SED} '
           s/\([^-/]\+\)\/\([^-]\+\)/\2-\1/g;
        ' |
            tr "${charclass_ascii_upper}"/ "${charclass_ascii_lower}"-
`
feature build "${build}"
host=

${START_ECHOLINE_LIBRARY}

# GNU sed (or possibly a compatible stream editor)
SED="${SED:-`type -p gsed >/dev/null 2>&1 && type -p gsed 2>/dev/null || echo sed`}"

# special characters
char_tab='	'
char_newline='
'
export char_tab
export char_newline

echoline()
{
    _echoline_all="${*}"
    _echoline_firstline="${_echoline_all%%
*}"
    _echoline_rest=""
    _echoline_has_rest=f
    if test :"${_echoline_firstline}" != :"${_echoline_all}"
    then
        _echoline_has_rest=t
        _echoline_rest="${_echoline_all#*
}"
    fi
    if test :"${_echoline_firstline}" = :"__EOF__"
    then
        echo "${_echoline_firstline}"
    else
        cat <<__EOF__
${_echoline_firstline}
__EOF__
    fi
    if test ${_echoline_has_rest} = t
    then
        echoline "${_echoline_rest}"
    fi
}

# like echoline, but does C string-style quoting (apostrophe is not quoted)
echolinex()
{
    echoline "$*" |
        ${SED} -ne '1 h; 2,$ H; $ {; g; s/[\\?""]/\\&/g; s/'"${char_tab}"'/\\t/g; s/'"\\${char_newline}"'/\\n/g; p; }'
}

${END_ECHOLINE_LIBRARY}

echo_n=
echo_c=

if test :"`echo -n \"_\"`" != :"_"
then
    echo_c='\c'
else
    echo_n='-n'
fi

varsafe()
{

    # this is a non-information-preserving conversion used to smash
    # --enable and --with parameter names into shell variable names

    echoline "$*" |
        ${SED} -ne 's/[^'"${charclass_ascii_id}"']/_/g; 1 h; 2,$ H; $ {; g; s/\
/_/g; p; }'
}

qw()
{
    echoline "$*" |
        ${SED} -ne 's/\([ '\!'"#$&'\''()*;<>?@^`{|}~\\]\|\[\|\]\)/\\\1/g; 1 h; 2,$ H; $ {; g; s/\
/"${char_newline}"/g; s/'"${char_tab}"'/"${char_tab}"/g; p; }'
}

mqw()
{

    echoline "$*" |
        ${SED} -ne '
            1 h;
            2,$ H;
            $ {;
                g;
                s/\$/$$/g;
                s/ /$(char_space)/g;
                s/'"${char_tab}"'/$(char_tab)/g;
                s/#/$(char_number_sign)/g;
                s/@/$(char_commercial_at)/g;
                s/'\\"${char_newline}"'/$(char_newline)/g;
                s/\\/$(char_reverse_solidus)/g;
                p;
            }
        '
}

qws()
{
    _qws_qw="$1"
    shift
    _qws_list=
    while test $# -gt 0
    do
      eval _qws_arg$#='"$1"'
      if test :"${_qws_list}" != :""
      then
          _qws_list="${_qws_list} "
      fi
      _qws_list="${_qws_list}"'`'$_qws_qw' "${_qws_arg'$#'}"`'
      shift
    done
    eval echoline "${_qws_list}"
}

q()
{
    qws qw "$@"
}

mq()
{
    qws mqw "$@"
}

progname=`basename "$0"`

cmdline=`q "$0" "$@"`
non_option_arguments=
dump=
dump_tr=
dump_command=echoline

# parse arguments
while test $# -gt 0
do
  case :"$1" in
      :"-help"|:"-help="*|:"-enable"[-_]*"="*|:"-enable"[-_]*|:"-disable"[-_]*|:"-with"[-_]*"="*|:"-with"[-_]*|:"-without"[-_]*|:"-srcdir="*|:"-build="*|:"-host="*|:"-srcdir"|:"-build"|:"-host"|:"-prefix"|:"-exec"[-_]"prefix"|:"-bindir"|:"-objdir"|:"-sbindir"|:"-appdir"|:"-libexecdir"|:"-datarootdir"|:"-datadir"|:"-sysconfdir"|:"-sharedstatedir"|:"-localstatedir"|:"-docdir"|:"-includedir"|:"-oldincludedir"|:"-infodir"|:"-libdir"|:"-lispdir"|:"-localedir"|:"-mandir"|:"-program"[-_]"prefix"|:"-program"[-_]"suffix"|:"-program"[-_]"transform"[-_]"name"|:"-version"|:"-x"[-_]"includes"|:"-x"[-_]"libraries"|:"-prefix="*|:"-exec"[-_]"prefix="*|:"-bindir="*|:"-objdir="*|:"-sbindir="*|:"-appdir="*|:"-libexecdir="*|:"-datarootdir="*|:"-datadir="*|:"-sysconfdir="*|:"-sharedstatedir="*|:"-localstatedir="*|:"-docdir="*|:"-includedir="*|:"-oldincludedir="*|:"-infodir="*|:"-libdir="*|:"-lispdir="*|:"-localedir="*|:"-mandir="*|:"-program"[-_]"prefix="*|:"-program"[-_]"suffix="*|:"-program"[-_]"transform"[-_]"name="*|:"-version="*|:"-x"[-_]"includes="*|:"-x"[-_]"libraries="*|:"-dump"|:"-dump="*|:"-dump"[-_]"ucase"|:"-dump"[-_]"ucase="*)
          _param="${1}"
          shift
          set : "-${_param}" "$@"
          shift
          ;;
      :"--prefix="*|:"--exec"[-_]"prefix="*|:"--bindir="*|:"--objdir="*|:"--appdir="*|:"--sbindir="*|:"--libexecdir="*|:"--datarootdir="*|:"--datadir="*|:"--sysconfdir="*|:"--sharedstatedir="*|:"--localstatedir="*|:"--docdir="*|:"--includedir="*|:"--oldincludedir="*|:"--infodir="*|:"--libdir="*|:"--lispdir="*|:"--localedir="*|:"--mandir="*|:"--program"[-_]"prefix="*|:"--program"[-_]"suffix="*|:"--program"[-_]"transform"[-_]"name="*)
          opt="${1%%=*}"
          opt="${opt#--}"
          feature `varsafe "${opt}"` "${1#*=}"
          shift
          ;;
      :"--disable"[-_]*"="*|:"--with"[-_]*"="*|:"--without"[-_]*"="*|:"--version="*|:"--dump"[-_]"ucase="*|:"--dump"[-_]"escaped="*)
          opt="${1%%=*}"
          echolinex "${progname}": option \`"${opt}"\' doesn\'t allow an argument >&2
          echolinex Try \`"${progname}" --help\' for more information. >&2
          exit 2
          ;;
      :"--version"|:"-V")
          echolinex "${basename}" configure "${configure_version}"
          exit 0
          ;;
      :"--dump"[-_]"ucase")
          dump_tr=' | tr "${charclass_ascii_lower}" "${charclass_ascii_upper}"'
          shift
          ;;
      :"--dump"[-_]"escaped")
          dump_command=echolinex
          shift
          ;;
      :"--dump="*)
          dump="${1#*=}"
          shift
          ;;
      :"--help"|:"--help="*|:"-h"|:"-h"*)
          # undocumented: --dump=VAR, --dump-ucase, --dump-escaped
          echolinex Usage: "${progname}" \[OPTION\]... \[BUILD\]
          echolinex Configure the \`"${basename}"\' package.
          echoline
          echoline Options:
          echoline "--help             "show this message and exit
          echolinex "--version          "show \`"${progname}"\' version and exit
          echoline "--build=BUILD      "type of system to build the program on \[guessed\]
          echoline "--host=HOST        "type of system to build the program for \[BUILD\]
          echolinex "--srcdir=DIR       "look for \`"${basename}"\' package in DIR \(default: . or ..\)
          echoline "--objdir=DIR       "create intermediate object files in DIR \(default: obj\)
          echoline
          echoline Installation Directories:
          echoline "--prefix=PREFIX    "common prefix for installation directories \[/usr/local\]
          echoline --exec-prefix=EPREFIX
          echoline "                   "alternate prefix for directories of executables \[PREFIX\]
          echoline "--bindir=BINDIR    "for main executables \[EPREFIX/bin\]
          echoline --libexecdir=LIBEXECDIR
          echoline "                   "for individual variant executables \[EPREFIX/libexec\]
          echoline "--datadir=DATADIR  "for architecture-independent data files \[PREFIX/share\]
          echoline "--docdir=DOCDIR    "for plain-text documentation \[PREFIX/share/doc\]
          echoline "--appdir=APPDIR    "for myman.app \[/Applications\] \(only used when building for Carbon\)
          echoline
          echoline Installation Options:
          echoline --program-prefix=PREFIX
          echoline "                   "prepend PREFIX to installed program names
          echoline --program-suffix=SUFFIX
          echoline "                   "append SUFFIX to installed program names
          echoline --program-transform-name=SCRIPT
          echoline "                   "run sed commands given by SCRIPT on installed program names
          echoline
          echoline Optional Features and Packages:
          echoline --enable-FEATURE\[=yes\] or
          echoline --enable-FEATURE\[=PARAMETER\]
          echoline "                   "enable optional feature FEATURE with optional PARAMETER
          echoline "--disable-FEATURE  "equivalent to --enable-FEATURE=no
          echoline "--with-PACKAGE     "use PACKAGE
          echoline "--without-PACKAGE  "don\'t use PACKAGE
          echoline
          echolinex Supported Features for \`"${basename}"\':
          echoline --enable-variant=VARIANT
          echoline "                   "use the maze variant VARIANT as the default
          echoline --enable-variants\[=yes\]
          echoline "                   "include all variants except those explicitly disabled
          echoline --enable-variant-VARIANT\[=yes\]
          echoline "                   "include the maze variant VARIANT
          echoline --enable-size=SIZE use the tile size SIZE as the default
          echoline --enable-sizes\[=yes\]
          echoline "                   "include all sizes except those explicitly disabled
          echoline --enable-size-SIZE\[=yes\]
          echoline "                   "include the tile size SIZE
          echoline "--with-libiconv    "use iconv\(3\) for character set conversions
          echoline "--with-xterm       "use xterm\(1\) to display under the X\(7\) Window System
          echoline "                   "\(installs xmyman, xmyman3, xquackman, xbigman, xhugeman, and xbitman\*\)
          echoline "--with-rxvt        "use rxvt\(1\) or urxvt\(1\) to display under the X\(7\) Window System
          echoline "                   "\(installs xmyman2\)
          echoline "--with-kterm       "use kterm\(1\) to display under the X\(7\) Window System
          echoline "                   "\(installs xmyman4\)
          echoline "--with-ctheme      "use ctheme to install a custom console color palette
          echoline "                   "\(installs myman.ct\)
          echoline "--with-zip         "generate .zip-format binary distribution
          echoline "--with-dmg         "generate .dmg-format binary distribution
          echoline "--with-mac-icon    "include program icon for Mac OS
          echoline "--with-win-icon    "include program icon and version information for Win32
          echoline Output drivers \(select at most one\):
          echoline "--with-libcaca     "use libcaca
          echoline "--with-libcaca0    "use libcaca with \<caca0.h\>
          echoline "--with-aalib       "use AA-Lib
          echoline "--with-raw         "use *nix TTY/Win32 Console API \(Unicode\)
          echoline "--with-raw-cp437   "use *nix TTY/Win32 Console API \(CP-437\)
          echoline "--with-allegro     "use Allegro
          echoline "--with-twin        "use Twin
          echoline "--with-ggi         "use LibGGI/LibGII
          echoline "--with-ggiwmh      "use LibGGI/LibGII with WindowManagerHints
          echoline "--with-sdl         "use SDL
          echoline "--with-sdl-mixer   "use SDL Mixer for audio \(only works with SDL or PDCurses SDL\)
          echoline "--with-slang-utf8  "use UTF-8 S-Lang curses emulation \(a.k.a. slcurses\)
          echoline "--with-slang       "use S-Lang curses emulation \(a.k.a. slcurses\)
          echoline "--with-xcursesw    "use PDCurses for X \(a.k.a. XCurses\) \(wide character version\)
          echoline "--with-xcurses     "use PDCurses for X \(a.k.a. XCurses\)
          echoline "--with-pdcursesw   "use PDCurses \(wide character version\)
          echoline "--with-pdcurses    "use PDCurses
          echoline "--with-pdcursessdl "use PDCurses \(SDL version\)
          echoline "--with-ncursesw    "use ncurses \(wide character version\)
          echoline "                   "\(--with-libiconv is recommended too if not using glibc\)
          echoline "--with-ncurses     "use ncurses
          echoline "--with-mac         "use Carbon \(Quickdraw Text\)
          echoline "--with-mac-unicode "use Carbon \(ATSUI\)
          echoline
          echoline Report bugs to \<bsittler@gmail.com\>.
          exit 0
          ;;
      :"--x"[-_]"includes="*|:"--x"[-_]"libraries="*)
          opt="${1%%=*}"
          opt="${opt#--}"
          feature `varsafe "${opt}"` "${1#*=}"
          shift
          ;;
      :"--enable"[-_]*"="*)
          opt="${1%%=*}"
          opt="${opt#--enable-}"
          feature enable_`varsafe "${opt}"` "${1#*=}"
          shift
          ;;
      :"--enable"[-_]*)
          opt="${1#--enable-}"
          feature enable_`varsafe "${opt}"` yes
          shift
          ;;
      :"--disable"[-_]*)
          opt="${1#--disable-}"
          feature enable_`varsafe "${opt}"` no
          shift
          ;;
      :"--with"[-_]*"="*)
          opt="${1%%=*}"
          opt="${opt#--with-}"
          feature with_`varsafe "${opt}"` "${1#*=}"
          shift
          ;;
      :"--with"[-_]*)
          opt="${1#--with-}"
          feature with_`varsafe "${opt}"` yes
          shift
          ;;
      :"--without"[-_]*)
          opt="${1#--without-}"
          feature with_`varsafe "${opt}"` no
          shift
          ;;
      :"--srcdir="*)
          feature srcdir "${1#--srcdir=}" ;
          shift
          ;;
      :"--build="*)
          feature build "${1#--build=}" ;
          shift
          ;;
      :"--host="*)
          feature host "${1#--host=}" ;
          shift
          ;;
      :"--srcdir"|:"--build"|:"--host"|:"--prefix"|:"--exec"[-_]"prefix"|:"--bindir"|:"--objdir"|:"--sbindir"|:"--appdir"|:"--libexecdir"|:"--datarootdir"|:"--datadir"|:"--sysconfdir"|:"--sharedstatedir"|:"--localstatedir"|:"--docdir"|:"--includedir"|:"--oldincludedir"|:"--infodir"|:"--libdir"|:"--lispdir"|:"--localedir"|:"--mandir"|:"--dump"|:"--x"[-_]"includes"|:"--x"[-_]"libraries"|:"--program"[-_]"prefix"|:"--program"[-_]"suffix"|:"--program"[-_]"transform"[-_]"name")
          _param="${1}"
          _value="${2}"
          if test $# -lt 2 -o :"${_value}" != :"-" -a :"${_value#-}" != :"${_value}"
          then
              echolinex "${progname}": option \`"$1"\' requires an argument >&2
              echolinex Try \`"${progname}" --help\' for more information. >&2
              exit 2
          fi
          shift 2 || exit $?
          set : "${_param}=${_value}" "$@"
          shift
          ;;
      :"--"|:"-")
          if test :"$1" = :"--"
          then
              shift
          fi
          non_option_arguments="${non_option_arguments} $*"
          while test $# -gt 0
          do
            case :"$1" in
                :*"="*)
                    feature `varsafe "${1%%=*}"` "${1#*=}"
                    shift
                    ;;
                *)
                    feature build "$1"
                    shift
                    ;;
            esac
          done
          ;;
      :"-"*)
          echolinex "${progname}": unrecognized option \`"$1"\' >&2
          echolinex Try \`"${progname}" --help\' for more information. >&2
          exit 2
          ;;
      :*"="*)
          feature `varsafe "${1%%=*}"` "${1#*=}"
          shift
          ;;
      *)
          non_option_arguments="${non_option_arguments} $1"
          feature build "$1"
          shift
          ;;
  esac
done

non_option_arguments="${non_option_arguments# }"

if test :"$srcdir" = :""
then
    for dir in . .. "`dirname "$0"`"
    do
      if test -f "${dir}/${testfile}"
      then
          feature srcdir "${dir}"
          break
      fi
    done
    if test :"$srcdir" = :""
        then
        echolinex "${progname}": can\'t find "${basename}" sources in this or parent directory\; use --srcdir=... >&2
        echolinex Try \`"${progname}" --help\' for more information. >&2
        exit 2
    fi
fi

if test ! -f "${srcdir}/${testfile}"
then
    echolinex "${progname}": can\'t find "${basename}" sources in directory \`"${srcdir}"\' >&2
    echolinex Try \`"${progname}" --help\' for more information. >&2
    exit 2
fi

feature host "${host:-${build}}"

if test :"${dump}" != :""
then
    eval "${dump_command}"' "${'"${dump}"'}"'"${dump_tr}"
    exit 0
fi

# create config.status
echo ${echo_n} "creating config.status... "${echo_c}
(
    echoline \#\!`q "${SHELL}"` --
    echoline \# This file was generated automatically using \`configure\'.
    echoline \# You may later run this file \(or a copy\) in a working Bourne
    echoline \# shell to restore this configuration\; see INSTALL for more
    echoline \# information.
    echoline
    echoline \# special characters
    echoline char_tab=\'"${char_tab}"\'
    echoline char_newline=\'
    echoline \'
    echoline export char_tab
    echoline export char_newline
    echoline
    echoline exec `q "$SHELL"` ${cmdline} \"\$@\"
    echoline exit \$?
) > "${conftmp}" || exit $?
if test -f config.status && ( diff "${conftmp}" config.status ) </dev/null >/dev/null 2>&1
then
    rm -f "${conftmp}" || exit $?
    echoline "unchanged."
else
    if test -e config.status -o -L config.status
    then
        rm -f config.status || exit $?
    fi
    mv "${conftmp}" config.status || exit $?
    echoline "done."
fi

# okay if this fails
chmod 755 config.status >/dev/null 2>&1 || :

# this function prints variable settings in GNU make syntax
makevariables()
{
    echoline "MAKEFILE = GNUmakefile"
    echoline "_SHELL := "`mq "${SHELL}"`
    for feature in \
        ${features}
    do
      echoline 'ifeq ($(subst default,undefined,$(origin '"${feature}"')),undefined)'
      eval 'echoline "${feature} := "`mq "${'"${feature}"'}"`'
      echoline 'endif'
    done
    echoline
}

mqlib()
{
    ${SED} -n -e '/START_STRING_QUOTING_LIBRARY/,/END_STRING_QUOTING_LIBRARY/ p' < \
        "${srcdir}/Makefile"
}

# create makefile.cp
echo ${echo_n} "creating makefile.cp... "${echo_c} >&2
cat > "${conftmp}" < "${srcdir}/Makefile" || exit $?
if test -f makefile.cp && ( diff "${conftmp}" makefile.cp ) </dev/null >/dev/null 2>&1
then
    rm -f "${conftmp}" || exit $?
    echoline "unchanged." >&2
else
    if test -e makefile.cp -o -L makefile.cp
    then
        rm -f makefile.cp || exit $?
    fi
    mv "${conftmp}" makefile.cp || exit $?
    echoline "done." >&2
fi

# create GNUmakefile
echo ${echo_n} "creating GNUmakefile... "${echo_c} >&2
(
    echoline \# This file was generated automatically using \`configure\'.
    echoline \# Do not edit this file by hand.
    echoline
    echo ${echo_n} \# Date: ${echo_c}
    echolinex "${date}"
    echoline
    mqlib
    echoline
    makevariables
    echoline
    echoline \#\# copy of main Makefile
    echoline
    echoline include makefile.cp
    echoline
    echoline \#\# additional rules
    echoline
    echoline GNUmakefile makefile.cp:: \$\(call mw,\$\(srcdir\)\)/configure \$\(call mw,\$\(srcdir\)\)/Makefile config.status \; \$\(SHELL\) config.status \&\& test -f \$\(call q,\$@\) \&\& touch \$\(call q,\$@\)
    echoline
    echoline distclean:: \; \$\(REMOVE\) GNUmakefile config.status makefile.cp
    echoline
    echoline clean:: \; \$\(REMOVE\) _conftmp.\*
) > "${conftmp}" || exit $?
if test -e GNUmakefile -o -L GNUmakefile
then
    rm -f GNUmakefile || exit $?
fi
mv "${conftmp}" GNUmakefile || exit $?
echoline "done." >&2

exit 0
