# Manually written configuration script for yash
# (C) 2007-2010 magicant
#
# This program 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.
# 
# This program 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 <http://www.gnu.org/licenses/>.

makefile="Makefile"
makefilein="Makefile.in"
configlog="config.log"
configh="config.h"
versionh="version.h"
tempsrc=".temp.c"
tempo=".temp.o"
tempd=".temp.d"
tempout="./.temp.out"
temptxt="./.temp.txt"
dirs=". builtins doc lineedit tests"

version="2.20"
copyrightyear="2007-2010"

# object files to be linked as `yash'
objs='$(MAIN_OBJS)'
# object files compiled into builtins.a
builtin_objs=''

null=""

help="no"
debug="no"
enable_alias="yes"
enable_array="yes"
enable_dirstack="yes"
enable_help="yes"
enable_history="yes"
enable_lineedit="yes"
enable_printf="yes"
enable_socket="yes"
enable_test="yes"
enable_ulimit="yes"
ctags_args=""
etags_args=""
term_lib='tinfo curses ncurses ncursesw'
unset prefix eprefix bindir datarootdir docdir htmldir
unset checkresult
unset MAKEFLAGS
umask u=rwx
cd "$(dirname "$0")"

parseenable() {
    case "$1" in
	*=yes|*=no|*=true|*=false) ;;
	*=*)  echo "$0: $1: invalid option" >&2;  exit 2 ;;
    esac
    opt="${1#--*able-}"
    opt="${opt%%=*}"
    case "$opt" in
	alias)        enable_alias=$2 ;;
	array)        enable_array=$2 ;;
	dirstack)     enable_dirstack=$2 ;;
	help)         enable_help=$2 ;;
	history)      enable_history=$2 ;;
	lineedit)     enable_lineedit=$2 ;;
	printf)       enable_printf=$2 ;;
	socket)       enable_socket=$2 ;;
	test)         enable_test=$2 ;;
	ulimit)       enable_ulimit=$2 ;;
	*)            echo "$0: $1: invalid option" >&2;  exit 2 ;;
    esac
}

# parse options
for i
do
    case "$i" in
	-h|--help)
	    help="yes" ;;
	-d|--debug)
	    debug="yes" ;;
	--prefix=*)
	    prefix="${i#--prefix=}" ;;
	--exec-prefix=*)
	    eprefix="${i#--exec-prefix=}" ;;
	--bindir=*)
	    bindir="${i#--bindir=}" ;;
	--datarootdir=*)
	    datarootdir="${i#--datarootdir=}" ;;
	--docdir=*)
	    docdir="${i#--docdir=}" ;;
	--htmldir=*)
	    htmldir="${i#--htmldir=}" ;;
	--enable-*=no|--enable-*=false|--disable-*)
	    parseenable "$i" "no" ;;
	--enable-*)
	    parseenable "$i" "yes" ;;
	--with-term-lib=*)
	    term_lib="${i#--with-term-lib=}" ;;
	?*=*)
	    # parse variable assignment
	    if echo "$i" | grep -E "^[[:alpha:]][[:alnum:]]*=" >/dev/null
	    then
		export "$i"
	    else
		echo "$0: $i: unknown argument" >&2
		exit 1
	    fi
	    ;;
	*)
	    echo "$0: $i: unknown argument" >&2
	    exit 1
    esac
done

if [ x"${help}" = x"yes" ]
then
    exec cat <<END
Usage: ./configure [options...]

Available options:
  --debug                configure for debug build (GCC required)

Installation options:
  --prefix=PREFIX        install architecture-independent files in PREFIX
                         [/usr/local]
  --exec-prefix=EPREFIX  install architecture-dependent files in EPREFIX
                         [PREFIX]
  --bindir=DIR           install executable in DIR [EPREFIX/bin]
  --datarootdir=DIR      architecture-independent data root [PREFIX/share]
  --docdir=DIR           documentation root [DATAROOTDIR/doc/yash]
  --htmldir=DIR          HTML documentation [DOCDIR]

Optional features:
  --enable-FEATURE[=ARG]  enable or disable FEATURE [ARG=yes]
  --disable-FEATURE       disable FEATURE (same as --enable-FEATURE=no)
  --enable-alias          enable aliases
  --enable-array          enable the array builtin
  --enable-dirstack       enable the directory stack (pushd, popd, dirs)
  --enable-help           enable the help builtin
  --enable-history        enable history
  --enable-lineedit       enable command line editing
  --enable-printf         enable the echo/printf builtins
  --enable-socket         enable socket redirection by /dev/tcp, /dev/udp
  --enable-test           enable the test builtin
  --enable-ulimit         enable the ulimit builtin

Optional packages:
  --with-term-lib=LIBS    search space-separated LIBS for terminal handling

Influential environment variables:
  CC, CFLAGS, CADDS, LDFLAGS, LDADDS, LDLIBS, AR, ARFLAGS, INSTALL_PROGRAM,
  CTAGS, CTAGSARGS, ETAGS, ETAGSARGS, CSCOPE, CSCOPEARGS
END
    exit
fi

clean_up () {
    rm -f "${tempsrc}" "${tempo}" "${tempout}" "${temptxt}" "${tempd}"
}
trap 'clean_up' EXIT
trap 'clean_up; exit 1' HUP INT QUIT ABRT TERM PIPE USR1 USR2
trap '' ALRM

exec 5>"${configlog}"
exec 6>"${configh}"

{
    echo "===== Configuration log: generated by configure ====="
    echo
    LC_TIME=C date
    echo
    printf '# Invocation command line was:\n%%'
    for i in "$0" "$@"
    do
	printf ' %s' "$(printf '%s\n' "$i" |
	    sed -e '/[^[:alnum:]./=-]/ {
	    s/'"'"'/'"'\\''"'/g
	    s/\(.*\)/'"'"'\1'"'"'/
	    }')"
    done
    echo
    echo
    printf '# uname -i = %s\n' "$(uname -i 2>/dev/null || echo \?)"
    printf '# uname -n = %s\n' "$(uname -n 2>/dev/null || echo \?)"
    printf '# uname -m = %s\n' "$(uname -m 2>/dev/null || echo \?)"
    printf '# uname -o = %s\n' "$(uname -o 2>/dev/null || echo \?)"
    printf '# uname -p = %s\n' "$(uname -p 2>/dev/null || echo \?)"
    printf '# uname -r = %s\n' "$(uname -r 2>/dev/null || echo \?)"
    printf '# uname -s = %s\n' "$(uname -s 2>/dev/null || echo \?)"
    printf '# uname -v = %s\n' "$(uname -v 2>/dev/null || echo \?)"
    echo
    printf '# PATH=%s\n' "$PATH"
    echo
} >&5

printf '/* %s: generated by configure */\n' "${configh##*/}" >&6

checking () {
    printf 'checking %s... ' "$1"
    printf '\nchecking %s...\n' "$1" >&5
}
checkby () {
    printf '%% %s\n' "$*" >&5
    "$@" >&5 2>&1
    laststatus=$?
    if [ ${laststatus} -eq 0 ]
    then
	checkresult="yes"
    else
	printf '# exit status: %d\n' "${laststatus}" >&5
	checkresult="no"
	return ${laststatus}
    fi
}
trymake () {
    checkby ${CC-${cc}} ${CFLAGS-${cflags}} ${CADDS-${null}} \
	${LDFLAGS-${ldflags}} ${LDADDS-${null}} -o "${tempout}" "${tempsrc}" \
	"$@" ${LDLIBS-${ldlibs}}
}
trycompile () {
    checkby ${CC-${cc}} ${CFLAGS-${cflags}} ${CADDS-${null}} -c \
	-o "${tempo}" "${tempsrc}" "$@"
}
trylink () {
    checkby ${CC-${cc}} ${LDFLAGS-${ldflags}} ${LDADDS-${null}} \
	-o "${tempout}" "${tempo}" "$@" ${LDLIBS-${ldlibs}}
}
tryexec () {
    checkby "${tempout}"
}
checked () {
    if [ $# -ge 1 ]
    then
	checkresult="$1"
    fi
    printf '%s\n' "${checkresult}"
    printf '# result: %s\n' "${checkresult}" >&5
}
defconfigh () {
    printf '# defining %s=%s in %s\n' "$1" "${2-1}" "${configh}" >&5
    printf '#define %s %s\n' "$1" "${2-1}" >&6
}
fail () {
    echo "configuration failed" >&2
    exit 1
}


cc="${CC-c99}"
cflags="${CFLAGS--O1 -g}"
ldflags="${LDFLAGS-${null}}"
ldlibs="${LDLIBS-${null}}"
ar="${AR-ar}"
arflags="${ARFLAGS--rc}"
ctags="${CTAGS-ctags}"
etags="${ETAGS-etags}"
cscope="${CSCOPE-cscope}"


# check OS type
checking 'operating system'
ostype=$(uname -s | tr "[:upper:]" "[:lower:]")
checked "${ostype}"
case "${ostype}" in
    darwin)
	defconfigh "_DARWIN_C_SOURCE"
	;;
    sunos)
	defconfigh "__EXTENSIONS__"
	;;
esac


# check POSIX conformance
checking 'POSIX conformance'
posixver=$(getconf _POSIX_VERSION 2>/dev/null)
if [ -n "${posixver}" ] && [ x"${posixver}" != x"undefined" ]
then
    checked "${posixver}"
else
    checked "no"
    posixver=""
fi
posix=${posixver}

checking 'SUS conformance'
xopenver=$(getconf _XOPEN_VERSION 2>/dev/null)
if [ -n "${xopenver}" ] && [ x"${xopenver}" != x"undefined" ]
then
    checked "${xopenver}"
else
    checked "no"
    xopenver=""
fi
xopen=${xopenver}


# check if gcc is available if the debug option is enabled
if [ x"${debug}" = x"yes" ]
then
    if
	checking 'whether GCC is available'
	checkby eval "${CC:-gcc} --version | grep -i gcc"
    then
	checked "yes"
	cc="${CC:-gcc}"
	cflags="-std=c99 -pedantic -MMD -Wall -Wextra -O1 -fno-inline -ggdb"
    else
	checked "no"
	echo "--debug option specified but GCC is not available" >&2
	fail
    fi
fi
if [ x"${debug}" = x"no" ]
then
    defconfigh "NDEBUG"
fi


# check if the compiler works
if
    checking 'whether the compiler works'
    cat >"${tempsrc}" <<END
#include "${configh}"
int main() { return 0; }
END
    trymake && tryexec
then
    checked "yes"
elif
    if [ x"${cc}" = x"c99" ] && [ x"${CC+set}" != x"set" ]
    then
	cc='gcc -std=c99'
	trymake && tryexec
    else
	false
    fi
then
    checked "yes (using gcc -std=c99)"
else
    checked "no"
    printf 'Compiler "%s" not found or not working.\n' "${cc}" >&2
    fail
fi

# check if the compiler accepts the -O2 option if we are not debugging
if [ x"${debug}" = x"no" ]
then
    if
	checking 'whether the compiler accepts -O2 flag'
	savecflags=${cflags}
	cflags="-O2 -g"
	cat >"${tempsrc}" <<END
#include "${configh}"
int main() { return 0; }
END
	trycompile
    then
	checked "yes"
    else
	checked "no"
	cflags=${savecflags}
    fi
    unset savecflags
fi

# check if make supports include statements
checking 'whether make supports include statements'
if
    cat >"${temptxt}" <<END
_TEST_:
	@echo >/dev/null
END
    checkby eval "${MAKE:-make} -f - _TEST_ <<END
.POSIX:
include ${temptxt}
END
"
then
    checked "yes"
    make_supports_include=true make_include='include'
else
    checked "no"
    make_supports_include=false make_include='# include'
fi


# check if defining _POSIX_C_SOURCE and _XOPEN_SOURCE as values larger than
# _POSIX_VERSION and _XOPEN_VERSION is accepted.
if [ -n "${posix}" ]
then
    checking 'what values _POSIX_C_SOURCE and _XOPEN_SOURCE should have'
    if
	cat >"${tempsrc}" <<END
#define _POSIX_C_SOURCE 200809L
#define _XOPEN_SOURCE 700
#include <unistd.h>
int main() { fork(); return 0; }
END
	trycompile
    then
	checked "_POSIX_C_SOURCE=200809L, _XOPEN_SOURCE=700"
	posix=200809 xopen=700
    elif
	cat >"${tempsrc}" <<END
#define _POSIX_C_SOURCE 200112L
#define _XOPEN_SOURCE 600
#include <unistd.h>
int main() { fork(); return 0; }
END
	trycompile
    then
	checked "_POSIX_C_SOURCE=200112L, _XOPEN_SOURCE=600"
	posix=200112 xopen=600
    else
	checked "failed"
    fi
fi
if [ -n "${posix}" ]
then
    defconfigh "_POSIX_C_SOURCE" "${posix}L"
fi
if [ -n "${xopen}" ]
then
    defconfigh "_XOPEN_SOURCE" "${xopen}"
fi

# check if _XOPEN_SOURCE_EXTENDED is accepted
if [ -n "${posix}" ]
then
    checking 'whether _XOPEN_SOURCE_EXTENDED should be defined'
    cat >"${tempsrc}" <<END
#include "${configh}"
#define _XOPEN_SOURCE_EXTENDED 1
#include <unistd.h>
int main() { fork(); return 0; }
END
    trycompile
    checked
    if [ x"${checkresult}" = x"yes" ]
    then
	defconfigh "_XOPEN_SOURCE_EXTENDED" "1"
    fi
fi


# check if we need -lm
checking 'if loader flag -lm can be omitted'
cat >"${tempsrc}" <<END
#include "${configh}"
#include <math.h>
int main(int argc, char **argv) {
    return fmod(argc, 1.1) == trunc(argc) || argv == 0;
}
END
trymake
checked
if [ x"${checkresult}" = x"no" ]
then
    ldlibs="${ldlibs} -lm"
fi

# enable/disable socket redirection
if [ x"$enable_socket" = x"yes" ]
then
    checking 'for socket library'
    saveldlibs="${ldlibs}"
    if
	cat >"${tempsrc}" <<END
#include "${configh}"
#include <netdb.h>
#include <sys/socket.h>
int main() {
    struct addrinfo ai = {
	.ai_flags = 0, .ai_family = AF_UNSPEC, .ai_protocol = 0,
	.ai_socktype = 1 ? SOCK_STREAM : SOCK_DGRAM,
	.ai_addrlen = 0, .ai_addr = (void*)0, .ai_canonname = (void*)0,
	.ai_next = (void*)0
    };
    gai_strerror(getaddrinfo("", "", &ai, (void*)0));
    connect(socket(ai.ai_family, ai.ai_socktype, ai.ai_protocol),
	ai.ai_addr, ai.ai_addrlen);
    freeaddrinfo(&ai);
}
END
	trymake
    then
	checked "yes"
    else
	for lib in '-lxnet' '-lsocket' '-lnsl' '-lsocket -lnsl'
	do
	    ldlibs="${saveldlibs} ${lib}"
	    if trymake
	    then
		checked "with ${lib}"
		break
	    fi
	done
	if [ x"${checkresult}" = x"no" ]
	then
	    checked
	fi
    fi
    case "${checkresult}" in
    yes)
	ldlibs="${saveldlibs}"
	defconfigh "YASH_ENABLE_SOCKET"
	;;
    with*)
	ldlibs="${saveldlibs} ${checkresult#with }"
	defconfigh "YASH_ENABLE_SOCKET"
	;;
    no)
	printf 'The socket library is unavailable!\n' >&2
	printf 'Add the "--disable-socket" option and try again.\n' >&2
	fail
	;;
    esac
fi

# check if terminfo is available
if [ x"${enable_lineedit}" = x"yes" ]
then
    if
	checking 'for curses.h'
	cat >"${tempsrc}" <<END
#include "${configh}"
#include <curses.h>
int main() { return ERR; }
END
	trycompile
	checked
	[ x"${checkresult}" = x"yes" ]
    then
	defconfigh "HAVE_CURSES_H"
    elif
	checking 'for ncurses.h'
	cat >"${tempsrc}" <<END
#include "${configh}"
#include <ncurses.h>
int main() { return ERR; }
END
	trycompile
	checked
	[ x"${checkresult}" = x"yes" ]
    then
	defconfigh "HAVE_NCURSES_H"
    elif
	checking 'for ncurses/ncurses.h'
	cat >"${tempsrc}" <<END
#include "${configh}"
#include <ncurses/ncurses.h>
int main() { return ERR; }
END
	trycompile
	checked
	[ x"${checkresult}" = x"yes" ]
    then
	defconfigh "HAVE_NCURSES_NCURSES_H"
    elif
	checking 'for ncursesw/ncurses.h'
	cat >"${tempsrc}" <<END
#include "${configh}"
#include <ncursesw/ncurses.h>
int main() { return ERR; }
END
	trycompile
	checked
	[ x"${checkresult}" = x"yes" ]
    then
	defconfigh "HAVE_NCURSESW_NCURSES_H"
    fi

    if
	checking 'for term.h'
	cat >"${tempsrc}" <<END
#include "${configh}"
#if HAVE_CURSES_H
#include <curses.h>
#elif HAVE_NCURSES_H
#include <ncurses.h>
#elif HAVE_NCURSES_NCURSES_H
#include <ncurses/ncurses.h>
#elif HAVE_NCURSESW_NCURSES_H
#include <ncursesw/ncurses.h>
#endif
#include <term.h>
int main() {
    int (*f1)(char *, int, int *) = setupterm; f1("", 0, 0);
    int (*f2)(char *) = tigetflag; f2("");
    int (*f3)(char *) = tigetnum; f3("");
    char *(*f4)(char *) = tigetstr; f4("");
    int (*f5)(const char *, int, int (*)(int)) = tputs; f5("", 0, 0);
/*  char *(*f6)(char *, long,long,long,long,long,long,long,long,long) = tparm;
    f6("", 0, 0, 0, 0, 0, 0, 0, 0, 0); */
    int (*f7)(TERMINAL *) = del_curterm; f7(cur_term);
}
END
	trycompile
	checked
	[ x"${checkresult}" = x"yes" ]
    then
	defconfigh "HAVE_TERM_H"
    elif
	checking 'for ncurses/term.h'
	cat >"${tempsrc}" <<END
#include "${configh}"
#if HAVE_CURSES_H
#include <curses.h>
#elif HAVE_NCURSES_H
#include <ncurses.h>
#elif HAVE_NCURSES_NCURSES_H
#include <ncurses/ncurses.h>
#elif HAVE_NCURSESW_NCURSES_H
#include <ncursesw/ncurses.h>
#endif
#include <ncurses/term.h>
int main() {
    int (*f1)(char *, int, int *) = setupterm; f1("", 0, 0);
    int (*f2)(char *) = tigetflag; f2("");
    int (*f3)(char *) = tigetnum; f3("");
    char *(*f4)(char *) = tigetstr; f4("");
    int (*f5)(const char *, int, int (*)(int)) = tputs; f5("", 0, 0);
/*  char *(*f6)(char *, long,long,long,long,long,long,long,long,long) = tparm;
    f6("", 0, 0, 0, 0, 0, 0, 0, 0, 0); */
    int (*f7)(TERMINAL *) = del_curterm; f7(cur_term);
}
END
	trycompile
	checked
	[ x"${checkresult}" = x"yes" ]
    then
	defconfigh "HAVE_NCURSES_TERM_H"
    elif
	checking 'for ncursesw/term.h'
	cat >"${tempsrc}" <<END
#include "${configh}"
#if HAVE_CURSES_H
#include <curses.h>
#elif HAVE_NCURSES_H
#include <ncurses.h>
#elif HAVE_NCURSES_NCURSES_H
#include <ncurses/ncurses.h>
#elif HAVE_NCURSESW_NCURSES_H
#include <ncursesw/ncurses.h>
#endif
#include <ncursesw/term.h>
int main() {
    int (*f1)(char *, int, int *) = setupterm; f1("", 0, 0);
    int (*f2)(char *) = tigetflag; f2("");
    int (*f3)(char *) = tigetnum; f3("");
    char *(*f4)(char *) = tigetstr; f4("");
    int (*f5)(const char *, int, int (*)(int)) = tputs; f5("", 0, 0);
/*  char *(*f6)(char *, long,long,long,long,long,long,long,long,long) = tparm;
    f6("", 0, 0, 0, 0, 0, 0, 0, 0, 0); */
    int (*f7)(TERMINAL *) = del_curterm; f7(cur_term);
}
END
	trycompile
	checked
	[ x"${checkresult}" = x"yes" ]
    then
	defconfigh "HAVE_NCURSESW_TERM_H"
    fi

    checking 'for terminfo library'
    saveldlibs="${ldlibs}"
    if
	cat >"${tempsrc}" <<END
#include "${configh}"
#include <stdio.h>
#if HAVE_CURSES_H
#include <curses.h>
#elif HAVE_NCURSES_H
#include <ncurses.h>
#elif HAVE_NCURSES_NCURSES_H
#include <ncurses/ncurses.h>
#elif HAVE_NCURSESW_NCURSES_H
#include <ncursesw/ncurses.h>
#endif
#if HAVE_TERM_H
#include <term.h>
#elif HAVE_NCURSES_TERM_H
#include <ncurses/term.h>
#elif HAVE_NCURSESW_TERM_H
#include <ncursesw/term.h>
#endif
int main() {
int i1 = setupterm((char*)0, 1, (int*)0);
int i2 = tigetflag("");
int i3 = tigetnum("");
char *s1 = tigetstr("");
char *s2 = tparm(s1, (long) i1, (long) i2, (long) i3, 0L, 0L, 0L, 0L, 0L, 0L);
int i4 = tputs(s2, 1, putchar);
del_curterm(cur_term);
return i4;
}
END
	trycompile
    then
	if trylink
	then
	    checked "yes"
	else
	    for lib in ${term_lib}
	    do
		ldlibs="${saveldlibs} -l${lib}"
		if trylink
		then
		    checked "with -l${lib}"
		    break
		fi
	    done
	    if [ x"${checkresult}" = x"no" ]
	    then
		checked
	    fi
	fi
    fi
    case "${checkresult}" in
    yes)
	ldlibs="${saveldlibs}"
	defconfigh "YASH_ENABLE_LINEEDIT"
	;;
    with*)
	ldlibs="${saveldlibs} ${checkresult#with }"
	defconfigh "YASH_ENABLE_LINEEDIT"
	;;
    no)
	printf 'The terminfo (curses) library is unavailable!\n' >&2
	printf 'Add the "--disable-lineedit" option and try again.\n' >&2
	fail
	;;
    esac
fi


# check whether system has /proc/self/exe or similar utility file
if
    checking 'for /proc/self/exe'
    checkby /proc/self/exe -c true
    checked
    [ x"${checkresult}" = x"yes" ]
then
    defconfigh "HAVE_PROC_SELF_EXE"
elif
    checking 'for /proc/curproc/file'
    checkby /proc/curproc/file -c true
    checked
    [ x"${checkresult}" = x"yes" ]
then
    defconfigh "HAVE_PROC_CURPROC_FILE"
elif
    checking 'for /proc/$$/object/a.out'
    checkby eval '/proc/$$/object/a.out -c true'
    checked
    [ x"${checkresult}" = x"yes" ]
then
    defconfigh "HAVE_PROC_OBJECT_AOUT"
fi


# check for strnlen
checking 'for strnlen'
cat >"${tempsrc}" <<END
#include "${configh}"
#include <string.h>
#ifndef strnlen
size_t strnlen(const char*, size_t);
#endif
int main() { return strnlen("12345", 3) != 3; }
END
trymake && tryexec
checked
if [ x"${checkresult}" = x"yes" ]
then
    defconfigh "HAVE_STRNLEN"
fi

# check for wcsnlen
checking 'for wcsnlen'
cat >"${tempsrc}" <<END
#include "${configh}"
#include <wchar.h>
#ifndef wcsnlen
size_t wcsnlen(const wchar_t*, size_t);
#endif
int main() { return wcsnlen(L"12345", 3) != 3; }
END
trymake && tryexec
checked
if [ x"${checkresult}" = x"yes" ]
then
    defconfigh "HAVE_WCSNLEN"
fi

# check for wcstold
checking 'for wcstold'
cat >"${tempsrc}" <<END
#include "${configh}"
#include <wchar.h>
#ifndef wcstold
long double wcstold(const wchar_t *restrict, wchar_t **restrict);
#endif
int main() { return wcstold(L"10.0", (wchar_t **) 0) != 10.0; }
END
trymake && tryexec
checked
if [ x"${checkresult}" = x"yes" ]
then
    defconfigh "HAVE_WCSTOLD"
fi

# check for wcwidth
checking 'for wcwidth'
cat >"${tempsrc}" <<END
#include "${configh}"
#include <wchar.h>
#ifndef wcwidth
int wcwidth(wchar_t);
#endif
int main() { return wcwidth(L'\0'); }
END
trymake && tryexec
checked
if [ x"${checkresult}" = x"yes" ]
then
    defconfigh "HAVE_WCWIDTH"
fi

# check if sys/stat.h defines S_ISVTX
checking 'if <sys/stat.h> defines S_ISVTX'
cat >"${tempsrc}" <<END
#include "${configh}"
#include <sys/stat.h>
int main() { return S_ISVTX & 0; }
END
trymake
checked
if [ x"${checkresult}" = x"yes" ]
then
    defconfigh "HAVE_S_ISVTX"
fi

# check if the "st_mtim"/"st_mtimespec"/"st_mtimensec"/"__st_mtimensec" member
# of the "stat" structure is available
if
    checking 'for st_mtim'
    cat >"${tempsrc}" <<END
#include "${configh}"
#include <sys/stat.h>
int main() {
struct stat st;
st.st_mtim = (struct timespec) { .tv_sec = 0, .tv_nsec = 0 };
struct timespec ts = st.st_mtim;
return ts.tv_nsec != 0;
}
END
    trymake
    checked
    [ x"${checkresult}" = x"yes" ]
then
    defconfigh "HAVE_ST_MTIM"
elif
    checking 'for st_mtimespec'
    cat >"${tempsrc}" <<END
#include "${configh}"
#include <sys/stat.h>
int main() {
struct stat st;
st.st_mtimespec = (struct timespec) { .tv_sec = 0, .tv_nsec = 0 };
struct timespec ts = st.st_mtimespec;
return ts.tv_nsec != 0;
}
END
    trymake
    checked
    [ x"${checkresult}" = x"yes" ]
then
    defconfigh "HAVE_ST_MTIMESPEC"
elif
    checking 'for st_mtimensec'
    cat >"${tempsrc}" <<END
#include "${configh}"
#include <sys/stat.h>
int main() {
struct stat st;
st.st_mtimensec = 0;
return st.st_mtimensec != 0;
}
END
    trymake
    checked
    [ x"${checkresult}" = x"yes" ]
then
    defconfigh "HAVE_ST_MTIMENSEC"
elif
    checking 'for __st_mtimensec'
    cat >"${tempsrc}" <<END
#include "${configh}"
#include <sys/stat.h>
int main() {
struct stat st;
st.__st_mtimensec = 0;
return st.__st_mtimensec != 0;
}
END
    trymake
    checked
    [ x"${checkresult}" = x"yes" ]
then
    defconfigh "HAVE___ST_MTIMENSEC"
fi

# check if WCONTINUED and WIFCONTINUED are available
checking 'for WCONTINUED and WIFCONTINUED'
cat >"${tempsrc}" <<END
#include "${configh}"
#include <errno.h>
#include <sys/wait.h>
static int s;
int main() {
if (WIFCONTINUED(s)) { }
return waitpid(-1, &s, WNOHANG|WCONTINUED) < 0 && errno == EINVAL;
}
END
trymake && tryexec
checked
if [ x"${checkresult}" = x"yes" ]
then
    defconfigh "HAVE_WCONTINUED"
fi

# check for faccessat/eaccess
if
    checking 'for faccessat'
    cat >"${tempsrc}" <<END
#include "${configh}"
#include <fcntl.h>
#include <unistd.h>
#ifndef faccessat
extern int faccessat(int, const char *, int, int);
#endif
int main() { faccessat(AT_FDCWD, ".", F_OK | R_OK | W_OK | X_OK, AT_EACCESS); }
END
    trymake
    checked
    [ x"${checkresult}" = x"yes" ]
then
    defconfigh "HAVE_FACCESSAT"
elif
    checking 'for eaccess'
    cat >"${tempsrc}" <<END
#include "${configh}"
#include <unistd.h>
#ifndef eaccess
extern int eaccess(const char *, int);
#endif
int main() { eaccess(".", F_OK | R_OK | W_OK | X_OK); }
END
    trymake
    checked
    [ x"${checkresult}" = x"yes" ]
then
    defconfigh "HAVE_EACCESS"
fi

# check for strsignal
checking 'for strsingal'
cat >"${tempsrc}" <<END
#include "${configh}"
#include <signal.h>
#include <string.h>
#ifndef strsignal
char *strsignal(int);
#endif
int main() { (void) strsignal(SIGKILL); }
END
trymake
checked
if [ x"${checkresult}" = x"yes" ]
then
    defconfigh "HAVE_STRSIGNAL"
fi

# check for paths.h
checking 'for paths.h'
cat >"${tempsrc}" <<END
#include "${configh}"
#include <paths.h>
#include <stdio.h>
int main() { printf("%s\n", _PATH_BSHELL); }
END
trycompile
checked
if [ x"${checkresult}" = x"yes" ]
then
    defconfigh "HAVE_PATHS_H"
fi

# check if getcwd accepts (NULL,0) as argument
checking "if getcwd(NULL,0) returns malloc'ed string"
cat >"${tempsrc}" <<END
#include "${configh}"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char *xgetcwd(void) {
    size_t pwdlen = 80;
    char *pwd = malloc(pwdlen);
    if (!pwd) return NULL;
    while (getcwd(pwd, pwdlen) == NULL) {
	if (errno == ERANGE) {
	    pwdlen *= 2;
	    pwd = realloc(pwd, pwdlen);
	    if (!pwd) return NULL;
	} else {
	    free(pwd);
	    return NULL;
	}
    }
    return pwd;
}
int main(void) {
    char *wd1 = getcwd(NULL, 0);
    if (!wd1) return 1;
    char *wd2 = xgetcwd();
    if (!wd2 || strcmp(wd1, wd2) != 0) return 1;
    char *wd11 = realloc(wd1, strlen(wd1) + 10);
    if (!wd11 || strcmp(wd11, wd2) != 0) return 1;
    free(wd11); free(wd2); return 0;
}
END
trymake && tryexec
checked
if [ x"${checkresult}" = x"yes" ]
then
    defconfigh "GETCWD_AUTO_MALLOC"
fi

# check if ioctl supports TIOCGWINSZ
if [ x"${enable_lineedit}" = x"yes" ]; then
    checking 'if ioctl supports TIOCGWINSZ'
    cat >"${tempsrc}" <<END
#include "${configh}"
#include <sys/ioctl.h>
int main() {
    struct winsize ws;
    ioctl(0, TIOCGWINSZ, &ws);
    (void) ws.ws_row, (void) ws.ws_col;
}
END
    trymake
    checked
    if [ x"${checkresult}" = x"yes" ]
    then
	defconfigh "HAVE_TIOCGWINSZ"
    fi
fi

# check if wide-oriented I/O is working
checking 'if wide-oriented I/O is fully working'
cat >"${tempsrc}" <<END
#include "${configh}"
#include <stdio.h>
#include <wchar.h>
#define LEN 12345
int main(void)
{
    FILE *f;
    fpos_t pos;
    f = fopen("${tempsrc}", "w+");
    if (f == NULL)                  return 1;
    if (fwprintf(f, L"123\n") != 4) return 2;
    if (fseek(f, 0, SEEK_SET) != 0) return 3;
    if (fgetwc(f) != L'1')          return 4;
    if (fgetwc(f) != L'2')          return 5;
    if (fgetpos(f, &pos) != 0)      return 6;
    if (fseek(f, 0, SEEK_SET) != 0) return 7;
    if (fgetwc(f) != L'1')          return 8;
    if (fsetpos(f, &pos) != 0)      return 9;
    if (fgetwc(f) != L'3')          return 10;
    if (fseek(f, 0, SEEK_SET) != 0) return 11;
    for (size_t i = 0; i < LEN; i++)
	if (fputwc(L'0', f) != L'0') return 12;
    if (fgetpos(f, &pos) != 0)      return 13;
    if (fputwc(L'1', f) != L'1')    return 14;
    if (fseek(f, 0, SEEK_SET) != 0) return 15;
    if (fgetwc(f) != L'0')          return 16;
    if (fsetpos(f, &pos) != 0)      return 17;
    if (fgetwc(f) != L'1')          return 18;
    if (fseek(f, 0, SEEK_SET) != 0) return 19;
    for (size_t i = 0; i < LEN; i++)
	if (fgetwc(f) != L'0')       return 20;
    if (fgetwc(f) != L'1')          return 21;
    if (fclose(f) != 0)             return 22;
    return 0;
}
END
trymake && tryexec
checked
if [ x"${checkresult}" = x"no" ]
then
    defconfigh "WIO_BROKEN"
fi

echo >&5

# enable/disable aliases
if [ x"$enable_alias" = x"yes" ]
then
    defconfigh "YASH_ENABLE_ALIAS"
    objs="$objs "'$(ALIAS_OBJS)'
fi

# enable/disable the array builtin
if [ x"$enable_array" = x"yes" ]
then
    defconfigh "YASH_ENABLE_ARRAY"
fi

# enable/disable the directory stack
if [ x"$enable_dirstack" = x"yes" ]
then
    defconfigh "YASH_ENABLE_DIRSTACK"
fi

# enable/disable the help builtin
if [ x"$enable_help" = x"yes" ]
then
    defconfigh "YASH_ENABLE_HELP"
fi

# enable/disable history
if [ x"$enable_history" = x"yes" ]
then
    defconfigh "YASH_ENABLE_HISTORY"
    objs="$objs "'$(HISTORY_OBJS)'
else
    if [ x"$enable_lineedit" = x"yes" ]; then
	printf 'History is required for lineedit but disabled.\n' >&2
	printf 'Add the "--disable-lineedit" option and try again.\n' >&2
	fail
    fi
fi

# enable/disable the echo/printf builtins
if [ x"$enable_printf" = x"yes" ]
then
    defconfigh "YASH_ENABLE_PRINTF"
    builtin_objs="$builtin_objs "'$(PRINTF_OBJS)'
fi

# enable/disable the test builtin
if [ x"$enable_test" = x"yes" ]
then
    defconfigh "YASH_ENABLE_TEST"
    builtin_objs="$builtin_objs "'$(TEST_OBJS)'
fi

# check for getrlimit and setrlimit
if [ x"$enable_ulimit" = x"yes" ]
then
    checking "for getrlimit and setrlimit"
    cat >"${tempsrc}" <<END
#include "${configh}"
#include <stdint.h> /* required before <sys/resource.h> on freebsd */
#include <sys/resource.h>
int main() {
struct rlimit l = { .rlim_cur = RLIM_INFINITY, .rlim_max = RLIM_INFINITY };
getrlimit(RLIMIT_FSIZE, &l);
setrlimit(RLIMIT_FSIZE, &l);
}
END
    trymake
    checked
    if [ x"${checkresult}" = x"yes" ]
    then
	defconfigh "YASH_ENABLE_ULIMIT"
	builtin_objs="$builtin_objs "'$(ULIMIT_OBJS)'
    else
	printf 'The getrlimit and setrlimit functions are unavailable.\n' >&2
	printf 'Add the "--disable-ulimit" option and try again.\n' >&2
	fail
    fi
fi

# check for RLIM_SAVED_CUR & RLIM_SAVED_MAX
if [ x"$enable_ulimit" = x"yes" ]
then
    for i in CUR MAX
    do
	checking "for RLIM_SAVED_${i}"
	cat >"${tempsrc}" <<END
#include "${configh}"
#include <stdint.h> /* required before <sys/resource.h> on freebsd */
#include <sys/resource.h>
int main() {
struct rlimit l = { .rlim_cur = RLIM_SAVED_${i}, .rlim_max = RLIM_SAVED_${i} };
return l.rlim_cur == l.rlim_max;
}
END
	trymake
	checked
	if [ x"${checkresult}" = x"yes" ]
	then
	    defconfigh "HAVE_RLIM_SAVED_${i}"
	fi
    done
fi

# check for RLIMIT_***
if [ x"$enable_ulimit" = x"yes" ]
then
    for i in LOCKS MEMLOCK MSGQUEUE NICE NPROC RSS RTPRIO SIGPENDING
    do
	checking "for RLIMIT_${i}"
	cat >"${tempsrc}" <<END
#include "${configh}"
#include <stdint.h> /* required before <sys/resource.h> on freebsd */
#include <sys/resource.h>
int main() { return RLIMIT_${i} == RLIMIT_AS; getrlimit(RLIMIT_${i}, 0); }
END
	trymake && tryexec
	checked
	if [ x"${checkresult}" = x"yes" ]
	then
	    defconfigh "HAVE_RLIMIT_${i}"
	fi
    done
fi


# check if ctags/etags accepts the --recurse option
if [ x"${CTAGSARGS+set}" != x"set" ]
then
    checking "if ctags accepts --recurse option"
    checkby ${ctags} --recurse -f /dev/null /dev/null
    checked
    if [ x"${checkresult}" = x"yes" ]
    then
	CTAGSARGS='--recurse'
    else
	CTAGSARGS='*.[ch]'
    fi
fi
if [ x"${ETAGSARGS+set}" != x"set" ]
then
    checking "if etags accepts --recurse option"
    checkby ${etags} --recurse -o /dev/null /dev/null >/dev/null 2>&1
    checked
    if [ x"${checkresult}" = x"yes" ]
    then
	ETAGSARGS='--recurse'
    else
	ETAGSARGS='*.[ch]'
    fi
fi

# set cscope invocation parameter
: ${CSCOPEARGS=-bR}


if [ -n "${builtin_objs}" ]
then
    objs="${objs} "'$(BUILTINS_ARCHIVE)'
fi
if [ x"${enable_lineedit}" = x"yes" ]
then
    objs="${objs} "'$(LINEEDIT_ARCHIVE)'
fi

cflags="${cflags} -D HAVE_CONFIG_H"

# create dependency files if there are none
makedep() {
    for i in ${1}*.c
    do
	if [ -f "${i}" ]
	then
	    >>"${i%.c}.d"
	fi
    done
    for i in ${1}*/
    do
	if [ -d "${i}" ]
	then
	    makedep "${i}"
	fi
    done
}
makedep ./

MAKE_INCLUDE="${MAKE_INCLUDE-${make_include}}"
CC="${CC-${cc}}"
CFLAGS="${CFLAGS-${cflags}} ${CADDS-${null}}"
LDFLAGS="${LDFLAGS-${ldflags}} ${LDADDS-${null}}"
LDLIBS="${LDLIBS-${ldlibs}}"
AR="${AR-${ar}}"
ARFLAGS="${ARFLAGS-${arflags}}"
CTAGS="${CTAGS-${ctags}}"
ETAGS="${ETAGS-${etags}}"
CSCOPE="${CSCOPE-${cscope}}"
DIRS="${dirs}"
OBJS="${objs}"
BUILTIN_OBJS="${builtin_objs}"
VERSION="${version}"
INSTALL_PROGRAM="${INSTALL_PROGRAM-./install-sh}"
PREFIX="${prefix-/usr/local}"
EPREFIX="${eprefix-${PREFIX}}"
if [ x"${PREFIX}" = x"/" ]; then PREFIX=""; fi
if [ x"${EPREFIX}" = x"/" ]; then EPREFIX=""; fi
BINDIR="${bindir-${EPREFIX}/bin}"
DATAROOTDIR="${datarootdir-${PREFIX}/share}"
if [ x"${DATAROOTDIR}" = x"/" ]; then DATAROOTDIR=""; fi
DOCDIR="${docdir-${DATAROOTDIR}/doc/yash}"
HTMLDIR="${htmldir-${DOCDIR}}"

# create Makefile($1) from Makefile.in($2) in directory $3
makemakefile () {
    printf "creating %s... " "$1"
    sed -e "1i\\
# ${1##*/}: generated by configure" -f "${temptxt}" "$2" >"$1"
    if ! $make_supports_include && ls "$3"/*.d >/dev/null 2>&1
    then
	printf "(including dependencies) "
	echo >>"$1"
	cat "$3"/*.d >>"$1"
    fi
    echo "done"
    echo "Created $1" >&5
}
{
    echo
    echo "===== Output variables ====="
    tee "${temptxt}" <<END
s!@MAKE_INCLUDE@!${MAKE_INCLUDE}!g
s!@CC@!${CC}!g
s!@CFLAGS@!${CFLAGS}!g
s!@LDFLAGS@!${LDFLAGS}!g
s!@LDLIBS@!${LDLIBS}!g
s!@AR@!${AR}!g
s!@ARFLAGS@!${ARFLAGS}!g
s!@CTAGS@!${CTAGS}!g
s!@CTAGSARGS@!${CTAGSARGS}!g
s!@ETAGS@!${ETAGS}!g
s!@ETAGSARGS@!${ETAGSARGS}!g
s!@CSCOPE@!${CSCOPE}!g
s!@CSCOPEARGS@!${CSCOPEARGS}!g
s!@DIRS@!${DIRS}!g
s!@OBJS@!${OBJS}!g
s!@BUILTIN_OBJS@!${BUILTIN_OBJS}!g
s!@VERSION@!${VERSION}!g
s!@INSTALL_PROGRAM@!${INSTALL_PROGRAM}!g
s!@PREFIX@!${PREFIX}!g
s!@EPREFIX@!${EPREFIX}!g
s!@BINDIR@!${BINDIR}!g
s!@DATAROOTDIR@!${DATAROOTDIR}!g
s!@DOCDIR@!${DOCDIR}!g
s!@HTMLDIR@!${HTMLDIR}!g
END
    echo
} >&5
for d in ${dirs}
do
    makemakefile "${d}/${makefile}" "${d}/${makefilein}" "${d}"
done


# create version.h
printf "creating %s... " "${versionh}"
cat >"${versionh}" <<END
/* ${versionh##*/}: generated by configure */
#ifndef YASH_VERSION_H
#define YASH_VERSION_H
#define PACKAGE_VERSION "${VERSION}"
#define PACKAGE_COPYRIGHT "Copyright (C) ${copyrightyear} magicant"
#endif
END
echo "done"
echo "Created ${versionh}" >&5


echo "configuration complete"


# print warning if POSIX conformance is missing
if [ -z "${posixver}" ] || [ "${posixver}" -lt 200112 ]
then
    echo "WARNING: yash is designed for systems that conform to POSIX.1-2001"
    echo "         but your system does not"
fi


# vim: set ft=sh ts=8 sts=4 sw=4 noet tw=80:
