#!/bin/sh
#
# Copyright (C) 2005-2025
#       pancake <pancake@nopcode.org>
#
# acr 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.
#
# acr 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 acr; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#===========================================================================
#
# ACR: Auto Conf Replacement
#
# @license:  GPLv2
# @author:   pancake <pancake@nopcode.org>
# @homepage: http://github.com/radare/acr
# @version:  @VERSION@
#
# acr-sh:
#   acr configure script generator.
#
#== How to find things in this large file ==
#
# Just search for the required string:
#
# ##INIT##     : All variable initilializations
# ##KEYWORDS## : switch/case with all keywords supported in configure.acr
# ##FI##       : Large enumeration of fi's :)
# ##FUNCS##    : helpkit functions
# ##LANGS##    : LANG_XXX keywords
# ##ENV_LANG## : env_XXX for languages
#
#=============================================================================
#

ACR_VERSION=@VERSION@
ACR_BIN_DIR=$(CDPATH= cd -- "$(dirname "$0")" && pwd)

if [ "${ACR_VERSION}" = "@VERSION@" ] && [ -f "${ACR_BIN_DIR}/../configure.acr" ]; then
	ACR_VERSION=$(awk '/^VERSION[ \t]+/ {print $2; exit}' "${ACR_BIN_DIR}/../configure.acr")
fi

CONFIGURE="$2"
: {CONFIGURE:=./configure}
: {STRIP:=0}

if [ -z "$1" ]; then
	if [ -f "configure.acr" ]; then
		ACRFILE=configure.acr
	else
		echo "No 'configure.acr' found in current directory." >&2
		echo "Give me the file name as argument." >&2
		exit 1
	fi
else
	ACRFILE="$1"
fi

##AWK##
AWK=awk
AWK_STRING="`awk 2>/dev/null`"
if [ -z "${AWK_STRING}" ]; then
	Q=`${AWK} 2>&1`
	if [ 1 = "$(echo ${Q} | wc -l | awk '{print $1}')" ]; then
		AWK_STRING=awk
	else
		AWK_STRING="`awk -W version 2>&1`"
	fi
fi
[ `expr "$AWK_STRING" : "mawk"` -gt 0 ] && AWK="awk -W interactive"

##
## boolean() {
## case "$1" in
##  ""|0) echo false ; ;;
##  *) echo true ; ;;
## }
##

## {{{ Variable initialization
##INIT##
SET_VAR=""
TMP_VAR=""
STR2=""
STRING=""
H_FILES=""
SH_FILES=""
MODE=""
SCRIPT="" # The user code script buffer

# Utilities
S="$" # hacky stuff
C="\`"

# Language initialization
LANG=0           # No langs yet defined
LANG_V=""        LANG_V_REQUIRED=""      LANG_V_ENV=""
LANG_C=""        LANG_C_REQUIRED=""      LANG_C_ENV=""    LANG_CPP_ENV=""
LANC_CXX=""      LANG_CXX_REQUIRED=""    LANG_CXX_ENV=""
LANG_VALA=""     LANG_VALA_REQUIRED=""   LANG_VALA_ENV=""
LANG_TCL=""      LANG_TCL_REQUIRED=""    LANG_TCL_ENV=""
LANG_PERL=""     LANG_PERL_REQUIRED=""   LANG_PERL_ENV=""
LANG_JAVA=""     LANG_JAVA_REQUIRED=""   LANG_JAVA_ENV=""
LANG_PYTHON=""   LANG_PYTHON_REQUIRED="" LANG_PYTHON_ENV=""
LANG_RUBY=""     LANG_RUBY_REQUIRED=""   LANG_RUBY_ENV=""
LANG_LUA=""      LANG_LUA_REQUIRED=""    LANG_LUA_ENV=""
# Defaults
PKGNAME="example"
VERSION="0.1.0"
_CONTACT=0
CONTACT_MAIL=""
CONTACT_NAME=""
DESCRIPTION=""
PCGEN=""
_AUTHOR=0
AUTHOR_MAIL=""
AUTHOR_NAME=""
# Report
REPORT_REQUIRED=""   REPORT_PKGCFG=""    REPORT_OPTIONAL=""
REPORT_FLAGS=""      REPORT_LANGS=""     REPORT_TOOLS=""
# Environment
ENVWORDS="MANDIR DESCRIPTION INFODIR LIBDIR INCLUDEDIR LOCALSTATEDIR ETCDIR SYSCONFDIR DATADIR DOCDIR"
ENVWORDS="${ENVWORDS} LIBEXECDIR SBINDIR BINDIR EPREFIX PREFIX SPREFIX"
ENVWORDS="${ENVWORDS} TARGET HOST BUILD INSTALL INSTALL_LIB INSTALL_MAN INSTALL_PROGRAM"
ENVWORDS="${ENVWORDS} INSTALL_PROGRAM_STRIP INSTALL_DIR INSTALL_SCRIPT INSTALL_DATA"
ENVWORDS="${ENVWORDS} HOST_OS HOST_CPU BUILD_OS BUILD_CPU TARGET_OS TARGET_CPU"
ENVWORDS="${ENVWORDS} VERSION VERSION_MAJOR VERSION_MINOR VERSION_PATCH VERSION_NUMBER"
ENVWORDS="${ENVWORDS} PKGCFG_LIBDIR PKGCFG_INCDIR"
ENVHELP=""
# Checks
CHKUSR_NAME=""
CHKUSR_TARGET=""
# ALL INTERNAL FLAGS #
HAVE_LIBS=0
REPORT=""
_REQUIRED=0
_USE_PKGCONFIG=0
_USE_LIBTOOL=0
_USE_LIBTOOL_REQUIRED=0
_USE_PTHREAD=0
_PC_CFLAGS=""
_PC_LDFLAGS=""
#-- ? --#
USE_BACKUP=0
# XXX #_REPORT_DEFINED=0
_REQUIRE_ACR_VERSION=0
NKEYS=0
_EXEC=0
_EXEC_VAR=""
_NOT=0
_USE_DL_USED=0
_CHKVER=0
_SIZEOF=0
_DIE_VAR=""
_DIE_BOOL=0
_IF=0
_IF_VAR=""
_IF_VAR2=""
_IF_OP=""
_IF_BOOL=0
_IF_SETVAR=""
_IF_VALUE=""
_IF_SETVALUE=""
_IF_VALUE_EOF=0
_IFAND=0
_IFEQ=0
_IFEQVAL=0
#_REPORT=0
_CHK_PM=0
_CHK_RU=0
_CHK_PM_NAME=""
#_CHKLIB=0
_CHKLIB_NAME=""
_CHKFUNC=0
_CHKFUNC_NAME=""
_CHKFUNC_LIB=""
_CHKDECL=0
_CHKDECL_NAME=""
_CHKDECL_INC=""
_CHKPRG_VAR=""
_CHKPRG_NAME=""
CHECK_VAR=""
CHECK_EOF=""
CHECK_STR=""
ARGUMENT=""    # ARGUMENTS #
ARG_VALUE=""
ARG_NAME=""
ARG_FLAG=""
ARG_DESC=""
FLAGHELP=""
FLAGSCRIPT=""
_EQUAL_3=0
EQUAL=""
EQUALSCR=""
_PKGNAME=""
PACKAGE_BUGREPORT=""
S_FILES="" # subst files
S_MKDIR="" # subst directories to be created
SD_FILES=""
INIVARS=""
SUBDIRS=0
SUBCONF=0
SUBCONF_DIRS=""
## }}}
## {{{ print_header
print_header() {
cat <<_EOF_
#!/bin/sh
# This script was automatically generated by ACR v${ACR_VERSION}
# @author: pancake <nopcode.org>
# @url:    http://www.nopcode.org
# @repo:   git clone https://github.com/radare/acr

[ -z "${S}{AWK}" ] && AWK=awk
do_remove() {
if [ "${S}{ACR_RMFILES}" ]; then
  printf "cleaning temporally files... "
  rm -f ${S}{ACR_RMFILES}
  echo "done"
fi
}
control_c() {
  printf "\\n\\n^C control-c : script execution interrupted.\\n"
  do_remove
  exit 1
}
trap control_c 2
_EOF_
}
## }}}
## {{{ Help Functions
##FUNCS##
add_envword() {
	for A in ${ENVWORDS}; do
	if [ "${A}" = "$1" ]; then
		if [ "${EQUAL}" = 1 ]; then # IFOP='='
			echo "`word` < Warning: duplicated export variable '$1'." >&2
		fi
		return;
	fi
	done

	[ "${DEBUG}" = 1 ] && echo "     |   env $1" >&2

	ENVWORDS="${ENVWORDS} $1"
}

add_lang_envwords() {
	for A in `eval echo ${S}LANG_$1_ENV`; do
		add_envword ${A}
	done
}

word() {
	printf "%04d" $WORDS_N
}

append() {
	SCRIPT="`cat <<EOF
${SCRIPT}
$@
EOF
`"
}

reset_vars() {
	MODE=""
	_IF_OP=""
	_IF_VAR=""
	_IF_VAR2=""
	_IF_SETVAR=""
	_IF_SETVAL=""
	_IF_VALUE=""
}

var_filter() {
	echo $* | sed -e 's,:,,g' | tr '[a-z]' '[A-Z]' | tr '.\-+/' '____'
}

get_current_compiler() {
	case "${CURRENT_LANG}" in
	"c")   COMPILER="CC" ; ;;
	"c++") COMPILER="CXX" ; ;;
	*) echo "No current compiler selected." >&2 ; exit 1 ; ;;
	esac
}

concat() {
	OLD="$1"
	NEW="$2"

	[ "${NEW}" = "\;" ] && NEW=";"

	if [ -z "${OLD}" ]; then
		printf -- "${NEW}"
	else
		printf -- "${OLD} ${NEW}"
	fi
}

semver() {
	N=`echo "$1" | awk -F. '{print $'$2'}'`
	echo $(( $N ))
}

check_ifop() {
	IFOP=$1
	case "${IFOP}" in
	"="|"+="|"?=")
		;;
	"<-")
		echo "`word` | The '<-' keyword is deprecated. Use $VAR instead" >&2
		;;
	*)
		echo "`word` | Invalid operator '${IFOP}' in assignment." >&2
		exit 1
		;;
	esac
}
## }}}
## {{{ Check program path
check_prg() {
	_CHKPRG_VAR=$1
	_CHKPRG_NAME=$2
	_CHKPRG_REQUIRED=$3

	add_envword HAVE_${_CHKPRG_VAR}
	add_envword ${_CHKPRG_VAR}

# XXX: -x check is also valid for directories
	SCRIPT=`cat <<EOF
${SCRIPT}
ochof "checking for ${_CHKPRG_NAME}... "
if [ -x "${S}{${_CHKPRG_VAR}}" ]; then
	FIND=${S}{${_CHKPRG_VAR}}
else
	FIND=""
	IFS=:
	for A in ${S}{PATH} ; do
		if [ -x "${S}{A}/${_CHKPRG_NAME}" ]; then
			FIND="${S}{A}/${_CHKPRG_NAME}"
			break;
		fi
	done
	unset IFS
fi
if [ -n "${S}{FIND}" ]; then
	ocho ${S}{FIND};
HAVE_${_CHKPRG_VAR}=1
	${_CHKPRG_VAR}=${S}{FIND}
else
	HAVE_${_CHKPRG_VAR}=0
	${_CHKPRG_VAR}=${_CHKPRG_NAME}
if [ "$QUIET" = 1 ]; then
echo "Cannot find ${_CHKPRG_NAME}" >&2
else
echo no
fi
EOF
`
	if [ "${_CHKPRG_REQUIRED}" = 1 ]; then
	SCRIPT=`cat <<EOF
${SCRIPT}
	echo "error: This program is required." >&2
	exit 1
EOF
`
	fi

	SCRIPT="${SCRIPT} ; fi"
	_CHKPRG_NAME=""
}
## }}}
## {{{ Vala
lang_vala() {
	LANG=1
	LANG_VALA=1
	LANG_VALA_ENV="VALA VAVAC HAVE_LANG_VALA"
	SCRIPT=`cat <<_EOF_
${SCRIPT}
printf "checking vala compiler... "
HAVE_LANG_VALA=1
HAVE_VALA=1
HAVE_VALAC=1
VALA_VERSION=""
echo "void main(){}" > .test.vala
${S}{VALAC} .test.vala 2>&1 >/dev/null
if [ ${S}? = 0 ]; then
	echo ${S}{VALAC};
	VALA_VERSION=${S}(${S}{VALAC} --version | cut -d " " -f 2 | cut -d . -f 1,2);
	printf "checking vala... ";
	${S}{VALA} .test.vala 2>&1 >/dev/null
	if [ ${S}? = 0 ]; then
		echo ${S}{VALA};
	else
		echo no ; HAVE_LANG_VALA=0 ; HAVE_VALA=0
	fi
else
	echo no ; HAVE_LANG_VALA=0 ; HAVE_VALAC=0
fi
_EOF_
`
	if [ "${LANG_VALA_REQUIRED}" = 1 ]; then
		SCRIPT="${SCRIPT}
if [ \"${S}{HAVE_LANG_VALA}\" = 0 ]; then
	echo \"ERROR: ${S}{VALAC} cannot create C code\"; exit 1
fi
"
	fi
	SCRIPT="${SCRIPT} rm -f .test.vala"
}
env_vala() {
	[ -n "${LANG_JAVA_ENV}" ] && return
	ACR_RMFILES="${ACR_RMFILES} .test.vala .test"
	add_lang_envwords "VALA"
	REPORT_LANGS="${REPORT_LANGS} vala"
	ENVHELP=`cat <<EOF
${ENVHELP}
  VALAC       Vala compiler to be used.
EOF
`
	add_flag with VALA vala "vala"  "Manually assign the vala program"
	add_flag with VALAC valac "valac"  "Manually assign the valac compiler program"

	lang_vala
}
## }}}
## {{{ Java
lang_java() {
	SCRIPT=`cat <<_EOF_
${SCRIPT}
printf "checking java compiler... "
HAVE_LANG_JAVA=1
echo "class test{public static void main(){}}" > test.java
if [ "${S}{JAVAC}" = "javac" ]; then if [ "${S}{JAVA_HOME}" ]; then
	JAVAC="${S}{JAVA_HOME}/bin/javac"
fi;fi
${S}{JAVAC} test.java 2>&1 >/dev/null
if [ ${S}? = 0 ]; then echo ${S}{JAVAC}; else
	echo no ; HAVE_LANG_JAVA=0
_EOF_
`
	if [ "${LANG_JAVA_REQUIRED}" = 1 ]; then
SCRIPT="${SCRIPT}
echo \"ERROR: ${S}{JAVAC} cannot create bytecode\"; exit 1"
	fi

	SCRIPT="${SCRIPT} ; fi"
}
env_java() {
	[ -n "${LANG_JAVA_ENV}" ] && return
	LANG=1
	LANG_JAVA=1
	LANG_JAVA_ENV="JAVA JAVAC HAVE_LANG_JAVA JAVA_HOME"
	ACR_RMFILES="${ACR_RMFILES} test.java test.class"
	add_lang_envwords "JAVA"
	REPORT_LANGS="${REPORT_LANGS} java"
	ENVHELP=`cat <<EOF
${ENVHELP}
  JAVAC       Java compiler to be used.
  JAVA_HOME   Path to the Java SDK prefix.
EOF
`
	add_flag with JAVAC javac "javac"  "Manually assign the javac program"
	add_flag with JAVAC jikes "jikes"  "Use jikes instead of javac"
	add_flag with JAVAC kjc   "kjc"    "Use the kaffe compiler"

	lang_java
}

use_jni() {
	add_envword "JAVA_HOME"
	add_envword "JCFLAGS"
	REPORT_REQUIRED="${REPORT_REQUIRED} JNI"
	SCRIPT=`cat <<EOF
${SCRIPT}
ochof "checking for java native interface (jni)... "
if [ -z "${S}{JAVA_HOME}" ]; then
	echo no
	echo "ERROR: JAVA_HOME not defined." >&2
	exit 1
fi
JCFLAGS="-I${S}{JAVA_HOME}/include -I${S}{JAVA_HOME}/include/linux"
echo "#include <jni.h>" > test.c
eval \\\\${S}${S}{COMPILER} test.c ${S}{JCFLAGS} -c >/dev/null 2>&1
if [ \"${S}?\" = 0 ]; then
	echo ok
else
	echo no
fi
EOF
`
}
## }}}
## {{{ Flag parsing (--enable-xxx)
add_flag() {
	# $1 = type (with,without,enable,disable)
	# $2 = affected variable
	# $3 = flag name (-static)
	# $4 = default value
	# $5 = description
	# $6 = argument name? ## XXX NOT USED XXX
	REPORT_FLAGS="${REPORT_FLAGS} --${1}-${3}"
	add_envword ${2}
	_TMP_=""
	TABS="`echo \"--${1}-${3}\" | ${AWK} '{ len=22-length($1); if (len<1) { print ""; len=25 } ; for (i=0;i<len;i++) { printf " "; } }'`"
	if [ -z "${4}" ]; then
		case $1 in
			"enable"|"with") value=1; echo "${2}=0"; ;;
			"disable"|"without") value=0; echo "${2}=1"; ;;
		esac
		FLAGSCRIPT=`printf "${FLAGSCRIPT}\n\"--${1}-${3}\") $2=\"${value}\"; ;;"`
	else
		REPORT_FLAGS="${REPORT_FLAGS}=${4}"
		echo "[ -z \"${S}{$2}\" ] && ${2}=\"${4}\""
		FLAGSCRIPT="`printf \"${FLAGSCRIPT}\n\"--${1}-${3}\") if [ -z \\\"${S}{value}\\\" ]; then $2=\\\"${4}\\\"; else $2=\\\"${S}{value}\\\" ; fi ;;\"`"
		_TMP_=" (${2}=${4})"
	fi
	_TMP_="`printf "  --${1}-${3}${6}${TABS}${5}${_TMP_}"`"
	FLAGHELP="${FLAGHELP}
${_TMP_}"
}
## }}}
## {{{ Bash
lang_bash() {
	LANG=1
	REPORT_LANGS="${REPORT_LANGS} bash"
	SCRIPT=`cat <<_EOF_
${SCRIPT}
ochof "checking for bash... "
type bash 2>&1 >/dev/null
if [ ${S}? = 0 ]; then echo yes ;
SH=\`sh -c "command -v bash 2>/dev/null"\`
else echo no ; fi
_EOF_
`
	add_envword "SH"
	ENVHELP=`cat <<EOF
${ENVHELP}
  SH          Environment for bash command
EOF
`
}
## }}}
## {{{ Ruby
env_ruby() {
	LANG_RUBY=1
	if [ "${LANG_RUBY_ENV}" ]; then return ; fi
	add_envword "RUBY"
	add_envword "RUBY_VERSION"
	add_envword "HAVE_LANG_RUBY"
	LANG_RUBY_ENV="RUBY RUBY_VERSION HAVE_LANG_RUBY" ## TODO no need to use this now.
	ENVHELP=`cat <<EOF
${ENVHELP}
  RUBY        Ruby interpreter path or binary name.
EOF
`
	REPORT_LANGS="${REPORT_LANGS} ruby"

## lang_ruby
	SCRIPT=`cat <<_EOF_
${SCRIPT}
ochof "checking for ruby... "
HAVE_LANG_RUBY=0
for A in \\\`echo ${S}{PATH} | sed -e 's,:, ,g'\\\`; do
RUBY=\\\`ls ${S}{A}/ruby* 2>/dev/null\\\`
 if [ "${S}{RUBY}" ]; then
   RUBY=\\\`echo ${S}{RUBY} | cut -d\  -f 1\\\`
   RUBY_VERSION=\\\`echo ${S}{RUBY} ${S}{A} | ${AWK} '{ print substr(${S}1,length(${S}2)+6) }'\\\`
   echo ${S}{RUBY} ; HAVE_LANG_RUBY=1 ; break
 fi
done
if [ -z "${S}{RUBY}" ]; then
echo no
_EOF_
`
	if [ "${LANG_RUBY_REQUIRED}" ]; then
	SCRIPT=`cat <<_EOF_
${SCRIPT}
	echo "ERROR: No ruby binaries found in PATH" >&2
	exit 1
_EOF
`
	fi
	SCRIPT="${SCRIPT} ; fi"
}
## }}}
## {{{ | Ruby Modules
check_ru() {
	HAVE_LIBS=1
	VAR=`echo "HAVE_RU_$1" | \
		sed -e 's, ,,g' | \
		sed -e 's,/,_,g' | \
		${AWK} '{ print toupper($0); }'`
	add_envword "${VAR}"

	SCRIPT=`cat <<EOF
${SCRIPT}
ochof "checking for ${1} ruby extension... "
if [ -z "${S}{RUBY}" ]; then
	false
else
\\\`echo "require '$1'" | ${S}{RUBY} 2> /dev/null\\\`
fi
if [ 0 = ${S}? ]; then
	${VAR}=1 ; echo ok
else
	${VAR}=0 ; echo no
EOF
`
if [ "${_REQUIRED}" = 1 ]; then
	SCRIPT=`cat <<EOF
${SCRIPT}
echo "This ruby extension is required."
exit 1
EOF
`
fi
	SCRIPT="${SCRIPT} ; fi"
}
## }}}
## {{{ Python

lang_python() {
	SCRIPT=`cat <<_EOF_
${SCRIPT}
ochof "checking for python... "
HAVE_LANG_PYTHON=0
for A in \\\`echo ${S}{PATH} | sed -e 's,:, ,g'\\\` ; do
PYTHON=\\\`ls ${S}{A}/python* 2>/dev/null\\\`
if [ "${S}{PYTHON}" ]; then
 PYTHON=\\\`echo ${S}{PYTHON} | ${AWK} '{print ${S}1; }'\\\`
 echo ${S}{PYTHON} ; HAVE_LANG_PYTHON=1 ; break ; fi
done
if [ -z "${S}{PYTHON}" ]; then
echo no
_EOF_
`
	if [ "${LANG_PYTHON_REQUIRED}" ]; then
	SCRIPT=`cat <<_EOF_
${SCRIPT}
echo "ERROR: No python binaries found in PATH" >&2
exit 1
_EOF_
`
	fi

	SCRIPT="${SCRIPT} ; fi"
}

env_python() {
	[ -n "${LANG_PYTHON_ENV}" ] && return
	LANG=1
	LANG_PYTHON=1
	LANG_PYTHON_ENV="PYTHON HAVE_LANG_PYTHON"
	add_lang_envwords "PYTHON"
	REPORT_LANGS="${REPORT_LANGS} python"
	lang_python
}
## }}}
## {{{ - modules
check_py() {
	HAVE_LIBS=1
	VAR=`var_filter "HAVE_PY_$1"`
	add_envword "${VAR}"
	SCRIPT=`cat <<EOF
${SCRIPT}
ochof "checking for ${1} python module... "
if [ -z "${PYTHON}" ]; then false ; else
\\\`echo import ${1} | ${S}{PYTHON} - 2>&1 >/dev/null\\\` 2>/dev/null
fi
if [ ${S}? = 0 ]; then
	${VAR}=1
	echo ok
else
	${VAR}=0
	echo no
EOF
`
if [ "${_REQUIRED}" = 1 ]; then
	SCRIPT="${SCRIPT}
echo \"This python module is required.\"
exit 1"
fi
	SCRIPT="${SCRIPT} ; fi"
}
## }}}
## {{{ Lua
lang_lua() {
	SCRIPT="${SCRIPT}
ochof \"checking for lua... \"
HAVE_LANG_LUA=0
${S}{LUA} -v > /dev/null 2>&1
if [ ${S}? = 0 ]; then
  echo ok ; HAVE_LANG_LUA=1 ;
ocho \"checking for lua libs... \""
if [ "${LANG_C}" = 1 ]; then
	check_lib lua
	check_lib lua5.1
	SCRIPT="${SCRIPT}
	if [ \"${S}{HAVE_LIB_LUA}\" = 1 ]; then
		LUA_LIBS=-llua
	fi
	if [ \"${S}{HAVE_LIB_LUA5_1}\" = 1 ]; then
		LUA_LIBS=-llua5.1
	fi
"
fi
	SCRIPT="${SCRIPT}
 else
echo no"
	if [ "${LANG_LUA_REQUIRED}" = 1 ]; then
	SCRIPT="${SCRIPT}
echo \"${LINENO}: You need lua.\" >&2
exit 1" ; fi
	SCRIPT="${SCRIPT} ; fi"
}

env_lua() {
	[ -n "${LANG_LUA_ENV}" ] && return
	LANG=1
	LANG_LUA=1
	LANG_LUA_ENV="LUA HAVE_LANG_LUA LUA_LIBS"
	add_lang_envwords "LUA"
	REPORT_LANGS="${REPORT_LANGS} lua"
	ENVHELP="${ENVHELP}
  LUA         path to lua interpreter"
	lang_lua
}
## }}}
## {{{ Perl
env_perl_threads() {
	add_envword "HAVE_LANG_PERL_THREADS"
	if [ -z "${LANG_PERL}" ]; then
		echo "`word` You must set LANG_PERL before calling USE_PERL_THREADS" >&2
		exit 1
	fi

	SCRIPT="${SCRIPT}
ochof \"checking for perl threads... \"
HAVE_LANG_PERL_THREADS=1
${S}{PERL} -e 'use threads;' 2>&1 > /dev/null
if [ ${S}? = 0 ]; then
  echo ok; else
  echo no ; HAVE_LANG_PERL_THREADS=0
"
	if [ "${_REQUIRED}" = 1 ]; then
		SCRIPT="${SCRIPT} echo \"ERROR: You need perl with threads.\" >&2 ; exit 1; fi"
	else
		SCRIPT="${SCRIPT} fi"
	fi
}

lang_perl() {
	SCRIPT="${SCRIPT}
ochof \"checking for perl... \"
HAVE_LANG_PERL=0
${S}{PERL} -e '' 2>&1 > /dev/null
if [ ${S}? = 0 ]; then
  ocho ok ; HAVE_LANG_PERL=1 ; else
  ocho no "
	if [ "${_REQUIRED}" = 1 ]; then
	SCRIPT="${SCRIPT}
echo \"${LINENO}: You need perl installed on your system.\" >&2
exit 1"
	fi

	SCRIPT="${SCRIPT} ; fi"
}

env_perl() {
	[ -n "${LANG_PERL_ENV}" ] && return
	LANG=1
	LANG_PERL=1
	LANG_PERL_ENV="PERL HAVE_LANG_PERL"
	ACR_RMFILES="${ACR_RMFILES} test.pl"
	add_lang_envwords "PERL"
	REPORT_LANGS="${REPORT_LANGS} perl"
	lang_perl
}
## }}}
## {{{ + modules
check_pm() {
	HAVE_LIBS=1
	VAR=`var_filter "HAVE_PM_$1"`
	add_envword "${VAR}"

	SCRIPT="${SCRIPT}
ochof \"checking for '${1}' perl module ... \"
perl -m${1} -e '' 2>/dev/null
if [ ${S}? = 0 ]; then
	${VAR}=1
	ocho ok
else
	${VAR}=0
	ocho no
"
if [ "${_REQUIRED}" = 1 ]; then
	SCRIPT="${SCRIPT}
echo 'This perl module is required.'
exit 1
"
fi
	SCRIPT="${SCRIPT} fi"
}
## }}}
## {{{ V
lang_v() {
SCRIPT=`cat <<_EOF_
${SCRIPT}
ochof "checking for V compiler... "
HAVE_LANG_V=1
if [ "${S}{CROSSBUILD}" = 1 ]; then
 (command -v ${S}{HOST}-${S}{V} >/dev/null 2>&1)
 if [ ${S}? = 0 ]; then V="${S}{HOST}-${S}{V}"; fi
fi
echo "fn main() {}" > .test.v
 (exec ${S}{V} ${S}{VFLAGS} .test.v >/dev/null 2>&1)
if [ ${S}? = 0 ]; then echo ${S}{V}; else
 echo no ; HAVE_LANG_V=0
_EOF_
`
	if [ "${LANG_V_REQUIRED}" = 1 ]; then
SCRIPT="${SCRIPT}
do_remove
echo \"ERROR: ${S}{V} cannot create executables\" >&2 ;
exit 1"
	fi
	SCRIPT="${SCRIPT} ; fi"
}

env_v() {
	[ -n "${LANG_V_ENV}" ] && return
	LANG=1
	LANG_V=1
	LANG_V_ENV="VFLAGS HAVE_LANG_V"
	ACR_RMFILES="${ACR_RMFILES} .test.v .test"
	REPORT_LANGS="${REPORT_LANGS} v"
	add_lang_envwords "V"
	ENVHELP="${ENVHELP}
  V           V compiler command
  VFLAGS      V compiler flags"
	lang_v
}
## }}}
## {{{ C
lang_c() {
SCRIPT=`cat <<_EOF_
${SCRIPT}
ochof "checking for c compiler... "
HAVE_LANG_C=1
if [ "${S}{CROSSBUILD}" = 1 ]; then
 (command -v ${S}{HOST}-${S}{CC} >/dev/null 2>&1)
 if [ ${S}? = 0 ]; then CC="${S}{HOST}-${S}{CC}"; fi
fi
echo "int main(int argc, char **argv){return 0;}" > test.c
 (exec ${S}{CC} -o a.out ${S}{CFLAGS} ${S}{CPPFLAGS} ${S}{LDFLAGS} test.c >/dev/null 2>&1)
if [ ${S}? = 0 ]; then echo ${S}{CC}; else
 echo no ; HAVE_LANG_C=0
_EOF_
`
	if [ "${LANG_C_REQUIRED}" = 1 ]; then
SCRIPT="${SCRIPT}
do_remove
echo \"ERROR: ${S}{CC} cannot create executables\" >&2 ;
exit 1"
	fi
	SCRIPT="${SCRIPT} ; fi"
	lang_cpp
}

env_cpp() {
	CURRENT_LANG="c"
SCRIPT="${SCRIPT}
COMPILER=CC"
	[ -n "${LANG_CPP_ENV}" ] && return
	LANG_CPP_ENV="CPPFLAGS CPP"
	ENVHELP="${ENVHELP}
  CPPFLAGS    C/C++ preprocessor flags, e.g. -I<include dir> if you have
              headers in a nonstandard directory <include dir>
  CPP         C preprocessor"
}

env_c() {
	[ -n "${LANG_C_ENV}" ] && return
	LANG=1
	LANG_C=1
	LANG_C_ENV="CC CFLAGS CPPFLAGS LDFLAGS HAVE_LANG_C"
	ACR_RMFILES="${ACR_RMFILES} test.c a.out a.exe"
	REPORT_LANGS="${REPORT_LANGS} c"
	add_lang_envwords "C"
	ENVHELP="${ENVHELP}
  CC          C compiler command
  CFLAGS      C compiler flags
  LDFLAGS     linker flags, e.g. -L<lib dir> if you have libraries in a
              nonstandard directory <lib dir>"
	env_cpp
	lang_c
}

lang_cpp() {
	LANG=1
	if [ "${LANG_CPP_ENV}" ]; then return ; fi
	add_envword "CPP"
	SCRIPT="`cat<<EOF
	$SCRIPT
	ochof \"checking for cpp... \"
	if [ \"${S}{CROSSBUILD}\" = 1 ]; then
	(exec ${S}{HOST}-${S}{CPP} --help >/dev/null 2>&1)
	[ ${S}? = 0 ] && CPP=\"${S}{HOST}-${S}{CPP}\"
	fi
	(echo \"int main(int ac, char **av){return 0;}\" | ${S}{CPP} ${CPPFLAGS} >/dev/null 2>&1)
	if [ \$? = 0 ]; then echo ${S}{CPP}; else
	echo \"ERROR: ${S}{CPP} cannot parse sources\"; fi
EOF
`"
}
## }}}
## {{{ C++
lang_cxx() {
	add_envword "CXX"
	SCRIPT="${SCRIPT}
HAVE_LANG_CXX=1
ochof \"checking for c++ compiler... \"
if [ \"${S}{CROSSBUILD}\" = 1 ]; then
(command -v ${S}{HOST}-${S}{CXX} 2>&1 >/dev/null)
if [ \"${S}?\" = 0 ]; then CXX=\"${S}{HOST}-${S}{CXX}\"; fi
fi
echo \"#include <iostream>\" > test.cxx
echo \"int main(){ std::cout << 1; return 0;}\" >> test.cxx
(exec ${S}{CXX} ${S}{CXXFLAGS} ${S}{LDFLAGS} test.cxx -lstdc++ >/dev/null 2>&1)
if [ \$? = 0 ]; then echo ${S}{CXX}; else
HAVE_LANG_CXX=0
echo no
"
	if [ "${LANG_CXX_REQUIRED}" = 1 ]; then
	SCRIPT="${SCRIPT}
echo \"ERROR: ${S}{CXX} cannot create executables\" >&2 ;
do_remove
exit 1
"
	fi
	SCRIPT="${SCRIPT} fi"
	lang_cpp
}

env_cxx() {
	CURRENT_LANG="c++"
	CURRENT_LANG="c"
	SCRIPT="${SCRIPT}
COMPILER=CXX"
	LANG=1
	[ "${LANG_CXX_ENV}" ] && return
	LANG_CXX=1
	REPORT_LANGS="${REPORT_LANGS} c++"
	ACR_RMFILES="${ACR_RMFILES} test.c test.cxx a.out a.exe"
	LANG_CXX_ENV="CXX CXXFLAGS LDFLAGS HAVE_LANG_CXX"
	add_lang_envwords "CXX"
	ENVHELP=`cat <<EOF
${ENVHELP}
  CXX         C++ compiler command
  CXXFLAGS    C++ compiler flags
EOF
`
lang_cxx
}

## }}}
## {{{ Objective C
env_objc() {
	# The Compiler
	[ "${LANG_OBJC_REQUIRED}" = 1 ] && LANG_C_REQUIRED=1

	_REQUIRED=1
	env_c
	check_lib objc

	SCRIPT=`cat <<EOF
${SCRIPT}
HAVE_LANG_OBJC=0
[ "${S}{HAVE_LIB_OBJC}${S}{HAVE_LANG_C}" = 11 ] && HAVE_LANG_OBJC=1
EOF
`

}
## }}}
## {{{ | Check Includes
_CHECK_INC=""
check_inc() {
	INC=$1
	VAR=`var_filter "HAVE_$1"`
	add_envword "${VAR}"
	get_current_compiler

	if [ -z "${_CHECK_INC}" ]; then
	SCRIPT="${SCRIPT}
check_include() {
VAR=${S}1
INC=${S}2
_REQUIRED=${S}3
ochof \"checking for ${S}2... \"
echo > test.c
"
	for A in `echo ${S}{INC} | sed -e 's,+, ,g'`; do
		SCRIPT="${SCRIPT}
echo \"#include <${A}>\" >>test.c"
	done
SCRIPT="${SCRIPT}
echo 'int main(int ac, char **av){return 0;}' >>test.c
eval \\\\${S}${S}{COMPILER} ${S}{CFLAGS} ${S}{CPPFLAGS} ${S}{CXXFLAGS} test.c >/dev/null 2>&1
if [ ${S}? = 0 ]; then
	eval ${S}{VAR}=1
	echo yes
else
	eval ${S}{VAR}=0
	echo no
	if [ 1 = \"${_REQUIRED}\" ]; then
		echo 'ERROR: required include not found.' >&2
		do_remove
		exit 1
	fi
fi
}
"
	_CHECK_INC=included
fi
	SCRIPT="${SCRIPT}
check_include ${VAR} ${INC} ${_REQUIRED}
"
}
## }}}
## {{{ | Endian
check_endian() {
	add_envword "LIL_ENDIAN"
	add_envword "BIG_ENDIAN"
	add_envword "BYTEORDER"
	SCRIPT="${SCRIPT}
printf \"checking host endianness... \"
LIL_ENDIAN=${S}(printf I|od|${S}{AWK}'{print substr(${S}2,6,1);exit}')
if [ $? != 0 -o \"${S}{LIL_ENDIAN}\" = 1 ]; then
	BYTEORDER=1234
	BIG_ENDIAN=0
	echo little
else
	BYTEORDER=4321
	BIG_ENDIAN=1
	LIL_ENDIAN=0
	echo big
fi
"
}
## }}}
## {{{ | PkgConfig Generator
PCGEN_INIT=0
check_pcgen() {
	if [ "${PCGEN_INIT}" = 0 ]; then
		PCGEN_INIT=1
		SCRIPT="${SCRIPT}
pcgen() {
	PCFILE=\"${S}{1}\" ; shift
	PCNAME=\"${S}{1}\" ; shift
	REQUIRES=\"${S}@\"
	PCVAR=\"\`echo \\\"${S}{PCNAME}\\\" | sed -e 's,:,,g' | tr '[a-z]' '[A-Z]' | tr '.\\\\-+/' '____'\`\"
	eval \"PCLIBS=\\${S}{PKGCFG_${S}{PCVAR}_LIBS-\\${S}{PKGCFG_LIBS_DEFAULT-\\${S}{DL_LIBS}}}\"
	eval \"PCLIBS_PRIVATE=\\${S}{PKGCFG_${S}{PCVAR}_LIBS_PRIVATE-\\${S}{PKGCFG_LIBS_PRIVATE_DEFAULT-}}\"
	eval \"PCCFLAGS=\\${S}{PKGCFG_${S}{PCVAR}_CFLAGS-\\${S}{PKGCFG_CFLAGS_DEFAULT-}}\"

	echo \"generating ${S}{PCFILE}\" >&2
	echo \"prefix=${S}{PREFIX}\" > ${S}{PCFILE}
	echo \"exec_prefix=\\${S}{prefix}\" >> ${S}{PCFILE}
	echo \"libdir=${S}{PKGCFG_LIBDIR}\" >> ${S}{PCFILE}
	echo \"includedir=${S}{PKGCFG_INCDIR}\" >> ${S}{PCFILE}
	echo \"\" >> ${S}{PCFILE}
	echo \"Name: ${S}{PCNAME}\" >> ${S}{PCFILE}
	if [ -n \"${S}{DESCRIPTION}\" ]; then
		echo \"Description: ${S}{DESCRIPTION}\" >> ${S}{PCFILE}
	fi
	echo \"Version: ${S}{VERSION}\" >> ${S}{PCFILE}
	echo \"Requires: ${S}{REQUIRES}\" >> ${S}{PCFILE}
	echo \"Cflags: -I\\${S}{includedir}/libr ${S}{PCCFLAGS}\" >> ${S}{PCFILE}
	echo \"Libs: -L\\${S}{libdir} -l${S}{PCNAME} ${S}{PCLIBS}\" >> ${S}{PCFILE}
	if [ -n \"${S}{PCLIBS_PRIVATE}\" ]; then
		echo \"Libs.private: ${S}{PCLIBS_PRIVATE}\" >> ${S}{PCFILE}
	fi
}
"
	fi
	SCRIPT="${SCRIPT}
pcgen $@"
}
## }}}
## {{{ | Check Libraries
check_lib() {
	_CHKLIB_NAME=$1
	HAVE_LIBS=1
	VAR=`echo "HAVE_LIB_$1" | sed -e 's,-,_,g' -e 's,+,_AND_,g' -e 's,\.,_,g' | awk '{ print toupper($0); }'`
	add_envword "${VAR}"

	get_current_compiler

	if [ -z "${_CHECK_LIB}" ]; then
	SCRIPT="${SCRIPT}
check_library() {
VAR=${S}1
S=\"$\"
_REQUIRED=${S}3
_CHKLIB_NAME=${S}2
_CHKLIB_LIBS=${S}(echo \"-l${S}{_CHKLIB_NAME}\" | sed 's,+, -l,g')
ochof \"checking for lib${S}{_CHKLIB_NAME} ... \"
echo \"int main(int ac, char **av){return 0;}\" > test.c
eval ${S}{S}${S}{COMPILER} ${S}{CFLAGS} ${S}{CPPFLAGS} ${S}{CXXFLAGS} ${S}{LDFLAGS} ${S}{_CHKLIB_LIBS} test.c >/dev/null 2>&1
if [ ${S}? = 0 ]; then
	eval ${S}{VAR}=1
	echo yes
else
	eval ${S}{VAR}=0
	echo no
	if [ \"${S}{_REQUIRED}\" = 1 ]; then
	  echo \"this library is required.\"
	  do_remove
	  exit 1
	fi
fi
}
"
fi
	if [ "${_REQUIRED}" = 1 ]; then
		REPORT_REQUIRED="${REPORT_REQUIRED} lib${_CHKLIB_NAME}"
	else
		REPORT_OPTIONAL="${REPORT_OPTIONAL} lib${_CHKLIB_NAME}"
	fi
	_CHECK_LIB=included

	SCRIPT="${SCRIPT}
check_library ${VAR} ${_CHKLIB_NAME} ${_REQUIRED}"
}
## }}}
## {{{ | Check Declarations
check_decl() {
	HAVE_DECL=1
	VAR=`var_filter "HAVE_DECL_$2"`
	add_envword "${VAR}"
	get_current_compiler
	SCRIPT=`cat <<EOF
${SCRIPT}
printf "checking declaration of ${_CHKDECL_NAME} in ${_CHKDECL_INC}... "
echo "#include <${_CHKDECL_INC}>" > test.c
echo "int main(int ac, char **av){ void *a = (void *)${_CHKDECL_NAME}; return 0; }" >> test.c
${S}{${COMPILER}} ${S}{CFLAGS} ${S}{CPPFLAGS} ${S}{LDFLAGS} test.c >/dev/null 2>&1
if [ ${S}? = 0 ]; then
	echo yes ; ${VAR}=1
else
	${VAR}=0 ; echo no
EOF
`
	if [ 1 = "${_REQUIRED}" ]; then
	SCRIPT=`cat <<EOF
${SCRIPT}
echo "this function is required." >&2
do_remove
exit 1
EOF
`
	fi
	SCRIPT="${SCRIPT} ; fi"
}
## }}}
## {{{ | Check CFLAGS
check_cflags() {
	HAVE_LIBS=1
	VAR=${_CHKFLG_VAR}
	add_envword "${VAR}"
	get_current_compiler
	SCRIPT=`cat <<EOF
${SCRIPT}
printf "checking cflags ${_CHKFLG_NAME}... "
echo "int main(int ac, char **av){ return 0; }" > .test.c
${S}{${COMPILER}} ${_CHKFLG_NAME} ${S}{CFLAGS} ${S}{CPPFLAGS} ${S}{LDFLAGS} $* .test.c >/dev/null 2>&1
if [ ${S}? = 0 ]; then
	echo yes ; ${VAR}=1
else
	${VAR}=0 ; echo no
EOF
`
	if [ 1 = "${_REQUIRED}" ]; then
	SCRIPT=`cat <<EOF
${SCRIPT}
echo "this compiler flag is required." >&2
do_remove
exit 1
EOF
`
	fi
	SCRIPT="${SCRIPT} ; fi"
}
## }}}
## {{{ | Check Symbol
check_sym() {
	HAVE_LIBS=1
	VAR=`var_filter "HAVE_$1"`
	add_envword "${VAR}"
	get_current_compiler
	SCRIPT=`cat <<EOF
${SCRIPT}
printf "checking symbol ${_CHKSYM_NAME}() in ${_CHKSYM_LIB}... "
echo "int main(int ac, char **av){ return (int)(size_t)&${_CHKSYM_NAME}; }" > test.c
${S}{${COMPILER}} ${S}{CFLAGS} ${S}{CPPFLAGS} ${S}{LDFLAGS} -l${_CHKSYM_LIB} test.c >/dev/null 2>&1
if [ ${S}? = 0 ]; then
	echo yes ; ${VAR}=1
else
	${VAR}=0 ; echo no
EOF
`
	if [ 1 = "${_REQUIRED}" ]; then
	SCRIPT=`cat <<EOF
${SCRIPT}
echo "this function is required." >&2
do_remove
exit 1
EOF
`
	fi
	SCRIPT="${SCRIPT} ; fi"
}
## }}}
## {{{ | Check Functions
check_func() {
	HAVE_LIBS=1
	VAR=`var_filter "HAVE_$1"`
	add_envword "${VAR}"
	get_current_compiler
	SCRIPT=`cat <<EOF
${SCRIPT}
printf "checking function ${_CHKFUNC_NAME}() in ${_CHKFUNC_LIB}... "
echo "extern int ${_CHKFUNC_NAME}(); int main(int ac, char **av){ ${_CHKFUNC_NAME}(0); return 0; }" > test.c
${S}{${COMPILER}} ${S}{CFLAGS} ${S}{CPPFLAGS} ${S}{LDFLAGS} -l${_CHKFUNC_LIB} test.c >/dev/null 2>&1
if [ ${S}? = 0 ]; then
	echo yes ; ${VAR}=1
else
	${VAR}=0 ; echo no
EOF
`
	if [ 1 = "${_REQUIRED}" ]; then
	SCRIPT=`cat <<EOF
${SCRIPT}
echo "this function is required." >&2
do_remove
exit 1
EOF
`
	fi
	SCRIPT="${SCRIPT} ; fi"
}
## }}}
## {{{ | Check sizeof
check_sizeof() {
	THAT=$1
	VAR=`echo "SIZEOF_$1" | sed -e 's,\*,_P,g' | awk '{ gsub(" ","_"); gsub("\\\.","_"); gsub("/","_"); print toupper($0); }'`

	add_envword "${VAR}"
 	## the $(./a.out) XXX this could be not portable
	SCRIPT="${SCRIPT}
printf 'checking size of $1... '
echo '#include <stdio.h>' > test.c
echo 'int main(int ac, char **av){printf(\"##d##\",sizeof($1));return 0;}' >>test.c
eval \\\\${S}${S}{COMPILER} ${S}{CFLAGS} ${S}{CPPFLAGS} ${S}{CXXFLAGS} -o a.out test.c >/dev/null 2>&1
if [ 0 = ${S}? ]; then
 ${VAR}=${S}(./a.out)
 echo ${S}{${VAR}}
 HAVE_${VAR}=1
else
	${VAR}=0 ; echo error
"
	if [ 1 = "${_REQUIRE}" ]; then
	SCRIPT="${SCRIPT}
do_remove
exit 1
"
	else
	SCRIPT="${SCRIPT}
HAVE_${VAR}=0
"
	fi
SCRIPT="${SCRIPT}
fi
"
}
## }}}
## {{{ | Pthreads
use_pthread() {
	HAVE_LIBS=1
	add_envword "PTHREAD_LIBS"
	add_envword "HAVE_PTHREAD"
	SCRIPT="${SCRIPT}
ochof \"checking for POSIX threads... \"
if [ \"${S}{HOST_OS}\" = \"freebsd\" ]; then
PTHREAD_LIBS=-pthread ; else
PTHREAD_LIBS=-lpthread ; fi
echo '#include <pthread.h>' > test.c
echo 'main(){pthread_testcancel();}' >> test.c
eval \\\\${S}${S}{COMPILER} ${S}{PTHREAD_LIBS} test.c 2>&1 >/dev/null
if [ ! \$? = 0 ]; then
HAVE_PTHREAD=0 ; echo no ; else HAVE_PTHREAD=1; echo yes ; fi
"
	if [ "${_REQUIRED}" = 1 ]; then
	REPORT_REQUIRED="${REPORT_REQUIRED} libpthread"
	SCRIPT="${SCRIPT}
if [ \"${S}{HAVE_PTHREAD}\" = 0 ]; then
	echo 'ERROR: You need the pthreads libraries.'
	do_remove
	exit 1
fi"
	else
	REPORT_OPTIONAL="${REPORT_OPTIONAL} libpthread"
	fi
}
## }}}
## {{{ TCL
lang_tcl() {
	LANG=1
	SCRIPT=`cat <<_EOF_
ochof "checking for tcl... "
HAVE_LANG_TCL=0
for A in ${S}{TCL_LIBDIR} ${S}{SEARCHPATH}; do
if [ -f \"${S}{A}/lib/tclConfig.sh\" ]; then
 . ${S}{A}/lib/tclConfig.sh
 HAVE_TCL=1
 TCL_CFLAGS="${S}{TCL_INCLUDE_SPEC}"
 TCL_LDFLAGS="${S}{TCL_LIB_SPEC}"
 echo "${S}{TCL_VERSION}"
 break
fi
	done

if [ "${S}{HAVE_LANG_TCL}" = 0 ]; then echo no
_EOF_
`

	[ "${LANG_TCL_REQUIRED}" = 1 ] && \
	SCRIPT="${SCRIPT} echo \"ERROR: You need TCL.\" ; exit 1"

	SCRIPT="${SCRIPT} ; fi"
}

env_tcl() {
	LANG=1
	LANG_TCL=1
	add_envword "TCL_VERSION"
	add_envword "HAVE_LANG_TCL"
	add_envword "TCL_LIBS"
	add_envword "TCL_CFLAGS"
	add_envword "TCL_LDFLAGS"
	REPORT_LANGS="${REPORT_LANGS} tcl"
	add_flag with TCL_BASEDIR tcl-basedir "=" "Sets the TCL basedir to find [/usr]/lib/tclConfig.sh"

	lang_tcl
}
## }}}
## {{{ PkgConfig
use_pkgconfig() {
	_USE_PKGCONFIG=1
	add_envword "PKGCONFIG"
	SCRIPT=`cat <<EOF
${SCRIPT}
ochof "checking for pkg-config... "
ENVWORDS="${S}{ENVWORDS} HAVE_PKGCONFIG"
if [ -n "${S}PKGCONFIG" ]; then
	if [ -x "${S}PKGCONFIG" ]; then
		_HAVE_PKGCONFIG="${S}{PKGCONFIG}"
	else
		ocho "PKGCONFIG env is not executable"
		PKGCONFIG=""
	fi
else
	IFS=:
	for A in ${S}{PATH} ; do
		if [ -x "${S}{A}/pkg-config" ]; then
			PKGCONFIG="${S}{A}/pkg-config"
		elif [ -x "${S}{A}/pkgconf" ]; then
			PKGCONFIG="${S}{A}/pkgconf"
		else
			continue
		fi
		break
	done
	unset IFS
fi
if [ -n "${S}{PKGCONFIG}" ]; then
	ocho ${S}{PKGCONFIG}
	HAVE_PKGCONFIG=1
else
	HAVE_PKGCONFIG=0
EOF
`
if [ "${_REQUIRED}" = 1 ]; then
SCRIPT=`cat <<EOF
${SCRIPT}
	echo "Cannot find pkg-config or pkgconf in PATH" >&2
	exit 1
EOF
`
fi
	SCRIPT="${SCRIPT}
ocho no; fi"
}
## }}}
## {{{ Libtool
use_libtool() {
	_USE_LIBTOOL=1
	check_prg "LIBTOOL" "libtool" ${_REQUIRED}
	case "${CURRENT_LANG}" in
	"c") add_envword "LIBTOOL_CC" ; ;;
	"c++") add_envword "LIBTOOL_CXX" ; ;;
	*) echo "Libtool depends on c or c++" >&2 ; exit 1 ; ;;
	esac

	add_envword "LIBTOOL_SHARED"
	add_envword "LIBTOOL_STATIC"
	add_envword "LIBTOOL_INSTALL"
}
## }}}
## {{{ TK
use_tk() {
	echo "ochof \"checking for tk... \""
	echo "HAVE_TK=0"
	echo "for A in ${S}{TK_BASEDIR} ${S}{SEARCHPATH}; do"
	echo "if [ -f \"${S}{A}/lib/tkConfig.sh\" ]; then"
	echo ". ${S}{A}/lib/tkConfig.sh"
	echo "  HAVE_TK=1"
	echo "  TK_LDFLAGS=\"${S}{TK_LIB_SPEC}\""
	echo "  echo \"${S}{TK_VERSION}\""
	echo "  break; fi"
	echo "done"
	echo "if [ \"${S}{HAVE_TK}\" = 0 ]; then echo no; fi"
}
## }}}
## {{{ X11
use_x11() {
	add_envword "HAVE_X11"
	SCRIPT=`cat <<_EOF_
${SCRIPT}
ochof "checking for X11... "
CFLAGS="${S}CFLAGS ${S}CPPFLAGS -I${S}{X11BASE}/include"
LDFLAGS="${S}LDFLAGS -L${S}{X11BASE}/lib"
[ "${X_INCLUDES}" ] && CFLAGS="${CFLAGS} -I${X_INCLUDES}"
[ "${X_LIBRARIES}" ] && CFLAGS="${CFLAGS} -L${X_LIBRARIES}"
if [ "${S}{HAVE_X11}" = 1 ]; then
	echo "#include <X11/Xlib.h>" > test.c
	echo "main(){XrmInitialize();}" >> test.c
	${S}{CC} ${S}{CFLAGS} ${S}{CPPFLAGS} ${S}{LDFLAGS} test.c -lX11 2> /dev/null
	if [ 0 = ${S}? ]; then
		echo ok ; HAVE_X11=1
	else
		HAVE_X11=0 ; echo "not found" >&2
	fi
else
	HAVE_X11=0 ; echo "disabled" >&2
fi
_EOF_
`
}
## }}}
## {{{ GNU make
env_gmake() {
	add_envword "MAKE"
	add_envword "GMAKE"
	REPORT_TOOLS="${REPORT_TOOLS} gmake"
}

use_gmake() {
	add_envword "MAKE"

	SCRIPT=`cat <<_EOF_
${SCRIPT}
ochof "checking for GNU make... "
GMAKE=0
for MAKE in make gmake ; do
 if [ -n "\\\`${S}{MAKE} -version 2>&1 | grep GNU\\\`" ]; then
  echo yes ; GMAKE=1 ; break
 fi
done
 if [ "${S}GMAKE" = 0 ]; then
  echo no
  echo "ERROR: No GNU make found." >&2
  exit 1
 fi
_EOF_
`
}
## }}}
## {{{ Generate Output file
report_h() {
	echo "for A in ${H_FILES}; do"
	echo " ocho \"creating h ${S}{VPATH}/${S}{A}\""
	echo "  ocho '' > ${S}{VPATH}/${S}{A}"
	echo "if [ ! \$? = 0 ]; then echo Cannot write target file ; control_c ; fi"
	echo " for B in ${S}{ENVWORDS} ; do"
	echo "  eval \"VAR=\\${S}${S}{B}\""
	if [ "${_REPORT_DEFINED}" = 1 ]; then
	#	echo "if [ ! \"\`echo ${S}{B} | grep -e HAVE -e VER -e PKG -e PACK\`\" = \"\" ]; then"
		echo "  if [ \"${S}{VAR}\" = 0 ]; then"
		echo "    echo \"/* #undef ${S}{B} */\" >> ${S}{VPATH}/${S}{A}"
		echo "  else"
		echo "  if [ -z \"${S}{VAR}\" ]; then"
		echo "    echo \"/* #undef ${S}{B} */\" >> ${S}{VPATH}/${S}{A}"
		echo "  else"
		echo "    echo \"#define ${S}{B} ${S}{VAR}\" >> ${S}{VPATH}/${S}{A}"
		echo "  fi"
		echo "  fi"
	#	echo "fi"
	else
		echo "  echo \"#define ${S}{B} ${S}{VAR}\" >> ${S}{VPATH}/${S}{A}"
	fi
	echo " done"
	echo "done"
	echo ""
}

report_sh() {
	echo "for A in ${SH_FILES}; do # SH_FILES"
	echo " ocho \"creating sh ${S}{VPATH}/${S}{A}\""
	echo "  echo '# automatically generated by acr' > ${S}{VPATH}/${S}{A}"
	echo "if [ ! \$? = 0 ]; then echo Cannot write target file >&2 ; control_c ; fi"
	echo " for B in ${S}{ENVWORDS} ; do"
	echo "  eval \"VAR=\\${S}${S}{B}\""
	echo "  echo ${S}{B}=${S}{VAR} >> ${S}{VPATH}/${S}{A}"
	echo " done"
	echo "done"
	echo ""
}

subst_tags() {
# if (crosspathbuild)
#  \
#   - create target directories (mkdir -p ${XX})
#   - target directori is localdir not VPATH

	# mkdirs
	echo "if [ \"${S}{WODIS}\" = \"crosspath\" ]; then"
	if [ "${S_MKDIR}" ]; then
	echo " mkdir -p ${S_MKDIR}"
	fi
	echo " for A in ${S_FILES} ; do"
	echo " echo \"filtering ${S}{A}\""
	echo "  cat ${S}{VPATH}/${S}{A} | eval sed ${S}{SEDFLAGS} > ${S}{A}"
	echo "if [ ! \$? = 0 ]; then echo Cannot write target file ; control_c ; fi"
	echo " done"
	echo "else #--"
	# move files
	echo "for A in ${S_FILES} ; do # SUBST_FILES"
	echo " echo \"filtering ${S}{A}\""
	echo " if [ -f \"${S}{VPATH}/${S}{A}.orig\" ]; then"
	if [ "${USE_BACKUP}" = 1 ]; then
		echo "   if [ -f \"${S}{VPATH}/${S}{A}.backup\" ]; then"
		echo "   if [ -f \"${S}{VPATH}/${S}{A}\" ]; then"
		echo "   if [ \"\`diff ${S}{VPATH}/${S}{A}.backup ${S}{VPATH}/${S}{A}\`\" ]; then"
		echo "   echo \"It seems that ${S}{VPATH}/${S}{A} has changed. Check .backup first.\""
		echo "   exit 1 ; fi ; fi ; fi"
		echo "   rm ${S}{VPATH}/${S}{A}.backup"
	fi
	echo "   mv -f ${S}{VPATH}/${S}{A}.orig ${S}{VPATH}/${S}{A}"
	echo " fi"
	echo " if [ -f \"${S}{VPATH}/${S}{A}\" ]; then"
	echo "   mv -f ${S}{VPATH}/${S}{A} ${S}{VPATH}/${S}{A}.orig"
	echo " else"
	echo "  if [ ! -f \"${S}{VPATH}${S}{A}.orig\" ]; then"
	echo "    echo \"ERROR: Cannot find ${S}{VPATH}/${S}{A}.orig\" >&2"
	echo "    exit 1"
	echo "  fi"
	echo " fi"
	echo " cat ${S}{VPATH}/${S}{A}.orig | eval sed ${S}{SEDFLAGS} > ${S}{VPATH}/${S}{A}"
	echo "if [ ! \$? = 0 ]; then echo Cannot write target file ; control_c ; fi"
	if [ "${USE_BACKUP}" = 1 ]; then
		echo " cp ${S}{VPATH}/${S}{A} ${S}{VPATH}/${S}{A}.backup"
		echo "if [ ! \$? = 0 ]; then echo Cannot write target file ; control_c ; fi"
	fi
	echo "done"
	echo "fi"
	echo ""
}

# TODO: too many echo's!
subst_dirs() {
	if [ "${SD_FILES}" = "%" ]; then
		echo "for A in \`find * | grep Makefile.acr | cut -d . -f 1\` ; do # SUBDIRS"
	else
		echo "for A in ${SD_FILES} ; do # SUBDIRS"
	fi
	cat <<EOF
if [ -f "${S}{VPATH}/${S}{A}.acr" ]; then
  SD_TARGET=${S}{A}
 else
  if [ -d "${S}{VPATH}/${S}{A}" ]; then
   SD_TARGET=${S}{A}/Makefile
   mkdir -p ${S}{A}
  else
   echo "ERROR: Cannot find ${S}{VPATH}/${S}{A}.acr" >&2
   exit 1
  fi
 fi
 ocho "creating ${S}{SD_TARGET}"
[ "${S}{VPATH}" != '.' ] && \
 mkdir -p \$(echo ${S}{A} | sed -e "s,/\`basename ${S}{A}\`\$,,g")
 cat ${S}{VPATH}/${S}{SD_TARGET}.acr | \\
eval sed -e "s,@VPATH@,${S}{VPATH}/${S}{A},g" ${S}{SEDFLAGS} > ${S}{SD_TARGET}.tmp
EOF
	# Support for ##MARKS##
	echo "
for A in ${S}{ENVWORDS}; do
VALUE=\`eval echo \"$\"${S}{A}\`
if [ \"${S}VALUE\" = 0 ]; then ## FALSE
 MARK=\"##${S}{A}##\"
if [ -n \"\`grep \\\"${S}{MARK}\\\" ${S}{SD_TARGET}.tmp\`\" ]; then
 mv ${S}{SD_TARGET}.tmp ${S}{SD_TARGET}.tmp2
 cat ${S}{SD_TARGET}.tmp2 | MARK=${S}MARK ${S}AWK 'BEGIN{a=0;}{if(${S}1==ENVIRON[\"MARK\"]){if(a)a=0;else a=1}else{if(!a)print;}}' > ${S}{SD_TARGET}.tmp
 fi
fi
done
 mv ${S}{SD_TARGET}.tmp ${S}{SD_TARGET} && rm -f ${S}{SD_TARGET}.tmp2
 if [ ! \$? = 0 ]; then echo Cannot write target file ; control_c ; fi
done
"
}
## }}}
## {{{ Body generation
create_body() {
	[ "${STRIP}" = 1 ] && return

	cat <<EOF
split_host() {
S="$"
while : ; do
ENVWORDS="${S}{ENVWORDS} ${S}1_CPU ${S}1_OS"
STR=\`eval "echo ${S}{S}${S}1"\`
SPLIT_CPU="\`echo "${S}STR" | cut -d - -f 1\`"
SPLIT_OS="\`echo "${S}STR" | ${S}AWK -F - '{
	if (${S}2=="unknown"){
		if (NF<3) { print ${S}2; } else { print ${S}3; }
	} else {
		if (${S}2=="linux") { print ${S}2; } else
		if (NF<3) { print ${S}2; } else { print ${S}3; }
	}
}'\`"
eval "${S}1_CPU=\"${S}SPLIT_CPU\""
eval "${S}1_OS=\"${S}SPLIT_OS\""
shift
[ -z "${S}1" ] && break
done
}

QUIET=0
be_quiet() {
	QUIET=1
}

VPATH=\`dirname ${S}{0}\`
if [ "${S}{VPATH}" = "." ]; then
	WODIS=current
else
	if [ "${S}{VPATH}" = "${S}{PWD}" ]; then
		VPATH=.
		WODIS=current
	else
		WODIS=crosspath
		CURDIR=${S}PWD
		cd ${S}VPATH
		VPATH="${S}{PWD}/"
		cd ${S}CURDIR
	fi
fi

guess_os() {
if [ -e "${S}{VPATH}/config.guess" ]; then
	sh ${S}{VPATH}/config.guess
	return
fi
CPU="\`uname -m | tr 'A-Z' 'a-z' | sed -e 's, ,,g' | cut -d - -f 1\`"
OS="\`uname -s|tr A-Z a-z\`"
uname -r | grep -qE "(Microsoft|WSL)" 2>/dev/null && OS="wsl"
GNU="\`uname --help 2>&1 | grep gnu\`"
[ "${S}{GNU}" ] && OS="${S}{OS}-gnu"
# normalize CPU
case "${S}{CPU}" in
	ppc*|powermac*|powermacintosh*|powerpc*)
		if echo "${S}{CPU}" | grep -q '64'; then
			CPU=powerpc64
		else
			CPU=powerpc
		fi
		;;
	*) ;;
esac
VENDOR="unknown"
if [ "${S}{OS}" = "darwin" ]; then
	VENDOR=apple
fi
echo "${S}{CPU}-${S}{VENDOR}-${S}{OS}"
}

SEARCHPATH="/usr /usr/local /usr/pkg /sw"

: ${S}{PREFIX:=/usr/local}
CROSSBUILD=0
BUILD=\`guess_os\`
HOST="${S}{BUILD}"
TARGET="${S}{HOST}"
ETCDIR="/etc"
SYSCONFDIR=""
DESCRIPTION="${DESCRIPTION}"

create_environ() {
: ${S}{EPREFIX:="${S}{PREFIX}"}
: ${S}{SPREFIX:="${S}{PREFIX}"}
: ${S}{BINDIR:="${S}{SPREFIX}/bin"}
: ${S}{SBINDIR:="${S}{PREFIX}/sbin"}
: ${S}{ETCDIR:="${S}{SPREFIX}/etc"}
: ${S}{LIBDIR:="${S}{SPREFIX}/lib"}
: ${S}{PKGCFG_LIBDIR:='${S}{exec_prefix}/lib'}
: ${S}{PKGCFG_INCDIR:='${S}{prefix}/include'}
: ${S}{LIBEXECDIR:="${S}{SPREFIX}/libexec"}
: ${S}{INCLUDEDIR:="${S}{SPREFIX}/include"}
: ${S}{DATADIR:="${S}{SPREFIX}/share"}
: ${S}{INFODIR:="${S}{DATADIR}/info"}
: ${S}{MANDIR:="${S}{DATADIR}/man"}
: ${S}{DOCDIR:="${S}{DATADIR}/doc/${PKGNAME}"}
: ${S}{LOCALSTATEDIR:="${S}{SPREFIX}/var"}
for A in \`echo ${S}{PATH} | sed -e 's,:, ,g'\` ; do
  [ -e "${S}A"/ginstall ] && : ${S}{INSTALL:="${S}A"/ginstall} && break
  [ -e "${S}A"/install ] && : ${S}{INSTALL:="${S}A"/install} && break
done
: ${S}{INSTALL_DIR:=${S}{INSTALL} -d}
: ${S}{INSTALL_DATA:=${S}{INSTALL} -m 644}
: ${S}{INSTALL_SCRIPT:=${S}{INSTALL} -m 755}
: ${S}{INSTALL_PROGRAM:=${S}{INSTALL} -m 755}
: ${S}{INSTALL_PROGRAM_STRIP:=${S}{INSTALL} -m 755 -s}
: ${S}{INSTALL_MAN:=${S}{INSTALL} -m 444}
: ${S}{INSTALL_LIB:=${S}{INSTALL} -m 755 -c}
${INIVARS}
}

show_usage() {
`cat <<EOF1
cat <<EOF2
'configure' configures ${PKGNAME}-${VERSION} to adapt to many kinds of systems.

Usage: ./configure [OPTION]... [VAR=VALUE]...

To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.

Defaults for the options are specified in brackets.

Configuration:
  -h, --help              display this help and exit
  -r, --report            show what libs/programs require to work
  -q, --quiet             be quiet, less verbose (see ./configure -qV)
  -V, --version           display version information and exit

Installation directories:
  --exec-prefix=EPREFIX  install arch-dependent files in EPREFIX [PREFIX]
  --prefix=PREFIX        install arch-independent files in PREFIX [/usr/local]
  --sandbox=SPREFIX      sandbox prefix directory: SPREFIX [PREFIX]

Fine tuning of the installation directories:
  --bindir=DIR           user executables [EPREFIX/bin]
  --datadir=DIR          read-only architecture-independent data [PREFIX/share]
  --docdir=DIR           documentation directory [DATADIR/doc/${PKGNAME}]
  --includedir=DIR       C header files [PREFIX/include]
  --infodir=DIR          info documentation [DATADIR/info]
  --libexecdir=DIR       program executables [EPREFIX/libexec]
  --libdir=DIR           object code libraries [EPREFIX/lib]
  --libpath=DIR          set rpath/libname-path to linker
  --localstatedir=DIR    modifiable single-machine data [PREFIX/var]
  --mandir=DIR           man documentation [DATADIR/man]
  --sbindir=DIR          system admin executables [EPREFIX/sbin]
  --etcdir=DIR           same as --sysconfdir (/etc)
  --sysconfdir=DIR       read-only single-machine data [PREFIX/etc]
  --sharedstatedir=DIR   modifiable architecture-independent data [PREFIX/com]

System types:
  --build=BUILD          configure for building on BUILD [guessed]
  --host=HOST            cross-compile to build programs to run on HOST [BUILD]
  --target=TARGET        configure for building compilers for TARGET [HOST]
EOF2
EOF1
`

EOF
[ "${FLAGHELP}" ] && \
	echo "printf \"\nOptional Features:${FLAGHELP}\n\""

[ "${ENVHELP}" ]  && \
	echo "printf \"\nSome influential environment variables:${ENVHELP}\n\""

[ "${AUTHOR_MAIL}" ] && \
	echo "printf \"\nAuthor: ${AUTHOR_NAME} <${AUTHOR_MAIL}>\""

[ "${CONTACT_MAIL}" ] && \
	echo "printf \"\nReport bugs to: ${CONTACT_NAME} <${CONTACT_MAIL}>\""

cat <<EOF
echo ""
exit 0
}

take_environ() {
: ${S}{SH:=/bin/sh}
EOF
if [ "${LANG_C}" = 1 ]; then
	echo ": ${S}{CPP:=cpp}"
	echo ": ${S}{CC:=gcc}"
fi
if [ "${LANG_V}" = 1 ]; then
	echo ": ${S}{V:=v}"
fi
if [ "${LANG_CXX}" = 1 ]; then
	echo ": ${S}{CPP:=cpp}"
	echo ": ${S}{CXX:=c++}"
fi
if [ "${LANG_JAVA}" = 1 ]; then
	echo ": ${S}{JAVAC:=javac}"
fi
if [ "${LANG_VALA}" = 1 ]; then
	echo ": ${S}{VALAC:=valac}"
fi
if [ "${LANG_PERL}" = 1 ]; then
	echo ": ${S}{PERL:=perl}"
fi
if [ "${LANG_LUA}" = 1 ]; then
	echo ": ${S}{LUA:=lua}"
fi
# XXX twice ?
cat <<EOF
: ${S}{PREFIX:=/usr/local/}
}

ochof() {
	[ "${S}QUIET" = 1 ] && return
	printf "${S}*"
}

ocho() {
	[ "${S}QUIET" = 1 ] && return
	echo "${S}*"
}

show_version() {
if [ "${S}QUIET" = 1 ]; then
	echo "${VERSION}"
	exit 0
fi
echo "${PKGNAME}-${VERSION} configuration script done with acr v${ACR_VERSION}.
The 'Free Software Foundation' message is only for autodetection.
Originally written by pancake <nopcode.org>."
exit 0
}

parse_options() {
flag=\`echo \$1| cut -d = -f 1\`
value=\`echo \$1| ${S}AWK 'BEGIN{FS="=";}{print \$2}'\`
flag2=\`echo \$flag|cut -f2- -d -| sed -e 's,-,_,g' -e 's,^_,,g'|tr '[a-z]' '[A-Z]'\`

if [ "\${TARGET_OS}" = "darwin" ]; then
	LIBPATH=-Wl,-install_name,
else
	LIBPATH=-Wl,-R
fi

case \$flag in
-h|--help|--hel|--h|--he|-help)
	show_usage ; ;;
-qV|-quiet-version|--quiet-version)
	be_quiet
	show_version ; ;;
-q|-quiet|--quiet)
	be_quiet ; ;;
-V|-version|--version)
	show_version ; ;;
-r|--r|--report)
EOF
	if [ ! "${PKGNAME}" = "this package" ]; then
		echo "echo \"PKGNAME:     ${PKGNAME}\"" ; fi
	if [ "${VERSION}" ]; then
		echo "echo \"VERSION:     ${VERSION}\"" ; fi
	if [ "${CONTACT_NAME}" ]; then
		echo "echo \"AUTHOR:      ${CONTACT_NAME}\"" ; fi
	if [ "${CONTACT_MAIL}" ]; then
		echo "echo \"EMAIL:       ${CONTACT_MAIL}\"" ; fi
	if [ "${DESCRIPTION}" ]; then
		echo "echo \"DESCRIPTION: ${DESCRIPTION}\"" ; fi
	if [ "${REPORT_LANGS}" ]; then
		echo "echo \"LANGS:      ${REPORT_LANGS}\"" ; fi
	if [ "${REPORT_REQUIRED}" ]; then
		echo "echo \"REQUIRED:   ${REPORT_REQUIRED}\"" ; fi
	if [ "${REPORT_OPTIONAL}" ]; then
		echo "echo \"OPTIONAL:   ${REPORT_OPTIONAL}\"" ; fi
	if [ "${REPORT_PKGCFG}" ]; then
		echo "echo \"PKGCONFIG:  ${REPORT_PKGCFG}\"" ; fi
	if [ "${REPORT_FLAGS}" ]; then
		echo "echo \"FLAGS:      ${REPORT_FLAGS}\"" ; fi
	if [ "${REPORT_TOOLS}" ]; then
		echo "echo \"TOOLS:      ${REPORT_TOOLS}\"" ; fi
# TODO
# We could probably reduce this case/switch because :
#	$ ./configure --prefix=/usr # flag=value
#	$ ./configure PREFIX=/usr   # flag2=value

cat <<EOF
	exit 0
	;;
--cache-file)
	# ignored: acr have no cache
	;;
--build)
	BUILD="\$value"; split_host BUILD ; ;;
--host)
	CROSSBUILD=1 # XXX
	HOST="\$value"; split_host HOST ; ;;
--target)
	TARGET="\$value"; split_host TARGET ; ;;
--prefix)
	PREFIX="\$value"; ;;
--exec-prefix)
	EPREFIX="\$value"; ;;
--sandbox|--sprefix)
	SPREFIX="\$value"; ;;
--bindir)
	BINDIR="\$value"; ;;
--sbindir)
	SBINDIR="\$value"; ;;
--libexecdir)
	LIBEXECDIR="\$value"; ;;
--docdir)
	DOCDIR="\$value"; ;;
--datadir)
	DATADIR="\$value"; ;;
--sysconfdir)
	SYSCONFDIR="\$value"
	ETCDIR="\$value"; ;;
--etcdir)
	SYSCONFDIR="\$value"
	ETCDIR="\$value"; ;;
--sharedstatedir)
	SHAREDSTATEDIR="\$value"; ;;
--localstatedir)
	LOCALSTATEDIR="\$value"; ;;
--libdir)
	LIBDIR="\$value"
	PKGCFG_LIBDIR="\$value"; ;;
--libpath)
	LDFLAGS="\${LDFLAGS} \${LIBPATH}\$value"; ;;
--includedir)
	PKGCFG_INCDIR="\$value"
	INCLUDEDIR="\$value"; CFLAGS="\${CFLAGS} -I\$value"; ;;
--infodir)
	INFODIR="\$value"; ;;
--mandir)
	MANDIR="\$value"; ;;
${FLAGSCRIPT}
*) if [ "\$value" ]; then eval "\`echo \$flag2=\$value\`" ;
else echo ; echo "WARNING: Unknown flag '\$flag'." >&2 ; echo ; fi ;;
esac
}

# MAIN #
take_environ
split_host BUILD HOST TARGET
[ -z "${S}ACRHOOK" ] && ACRHOOK=./configure.hook
[ -e "${S}ACRHOOK" ] && . ${S}{ACRHOOK}

while : ; do
[ -z "\$1" ] && break
parse_options "\$1"
shift
done

ENVWORDS="${ENVWORDS}"

create_environ
${EQUALSCR}
EOF
}

check_path() {
	[ "${SPLIT}" = 1 ] && return
	cat <<EOF
[ -n "${S}{prefix}" ] && PREFIX="${S}{prefix}"
ocho "checking for working directories... ${S}{WODIS}"
ocho "using prefix '${S}{PREFIX}'"
EOF
}

check_os() {
	[ "${STRIP}" = 1 ] && return
	cat <<EOF
ocho "checking build system type... ${S}{BUILD}"
ocho "checking host system type... ${S}{HOST}"
ocho "checking target system type... ${S}{TARGET}"
[ "${S}{CROSSBUILD}" = 1 ] && echo "using crosscompilation mode."

#split_host BUILD HOST TARGET
EOF
}

## - script output generation - ##
create_script() {
	create_body
	# checks
	check_os
	check_path

	echo "ACR_RMFILES=\"${ACR_RMFILES}\""
	[ "${USE_X11}" = 1 ] && use_x11
	[ "${USE_TK}" = 1 ] && use_tk
	[ "${USE_GMAKE}" = 1 ] && use_gmake

	echo "${SCRIPT}" | sed -e 's,##d##,%d,g'

	if [ "${_USE_LIBTOOL}" = 1 ]; then
	echo ": ${S}{LIBTOOL_CC:=${S}LIBTOOL --mode=compile ${S}CC -c}"
	echo ": ${S}{LIBTOOL_CXX:=${S}LIBTOOL --mode=compile ${S}CXX -c}"
	echo ": ${S}{LIBTOOL_SHARED:=${S}LIBTOOL --mode=link ${S}CC -rpath ${S}LIBDIR -release \\\$\{LIB_VERSION\} -o}"
	echo ": ${S}{LIBTOOL_STATIC:=${S}LIBTOOL --mode=link ${S}CC -static -rpath ${S}LIBDIR -release \\\$\{LIB_VERSION\} -o}"
	echo ": ${S}{LIBTOOL_INSTALL:=${S}LIBTOOL --mode=install ${S}INSTALL_LIB}"
	fi
## XXX ([ "${S}{A}" = srcdir ] && continue) is ugly wtf is srcdir?
	cat <<EOF
SEDFLAGS="${SEDFLAGS} -e '"
COUNT=0
for A in ${S}{ENVWORDS} ; do
	[ "${S}{A}" = VPATH ] && continue
	[ "${S}{A}" = srcdir ] && continue
	eval "VAR=\\${S}${S}{A}"
	case "${HOST_OS}" in
	mingw*|cygwin*|msys*)
		VAR="\`echo ${S}{VAR} | sed -e 's/\\\\/\\\\\\\\/g' -e 's/\,/\\\\\\,/g'\`"
		;;
	*)
		VAR="\`echo ${S}{VAR} | sed -e 's/\,/\\\\\,/g'\`"
		;;
	esac
	[ ${S}COUNT = 10 ] && COUNT=0 && SEDFLAGS="${S}{SEDFLAGS}' -e '"
	COUNT=${S}((${S}COUNT+1))
	SEDFLAGS="${S}{SEDFLAGS}s,@${S}{A}@,${S}{VAR},g;"
done
SEDFLAGS="${S}{SEDFLAGS}'"
EOF
	[ "${H_FILES}"  ] && report_h   ${H_FILES}
	[ "${SH_FILES}" ] && report_sh  ${SH_FILES}
	[ "${S_FILES}"  ] && subst_tags ${S_FILES}
	[ "${SD_FILES}" ] && subst_dirs ${SD_FILES}

	# acr_eof
	[ "${LANG}" = 1 ] && echo "do_remove"

	# subdirs
	[ "${SUBCONF_DIRS}" ] && \
	cat <<EOF
for A in ${SUBCONF_DIRS} ; do
if [ -x ${S}{A}/configure ]; then
	echo "=> running ./configure in ${S}{A}"
	cd ${S}{A}
	./configure ${S}@
	cd - > /dev/null
else
	echo "Cannot find ./configure on directory ${S}{A}." >&2
	exit 1
fi
done
EOF
}
## }}}
## {{{ Show report
show_report() {
	[ "${REPORT}" ] &&\
	cat <<EOF
if [ "${S}QUIET" = 0 ]; then
echo
echo "Final report:"
for A in ${REPORT} ; do
eval VAL="\\${S}${S}{A}"
[ -z "${S}{VAL}" ] && VAL="\"\""
echo " - ${S}{A} = ${S}{VAL}"
done
fi
EOF
}
# }}}

is_buggy() {
	WORD=$1
	[ "${MODE}" != "check" ] && [ "${WORD}" != ";" ] && [ -n "`echo $1 | grep ';'`" ] && \
		echo "`word` | Warning: keyword '$1' contains a ';'. Is this fine?" >&2
}

LAST_DOT=0
DOTS=0
show_progress_bar() {
	DOTS=`echo $WORDS_N $WORDS_T | awk '{printf ("%.d",$1*70/$2);}'`
	if [ "${LAST_DOT}" = "${DOTS}" ]; then return ; fi
	PCENT=`echo $WORDS_N $WORDS_T | awk '{printf ("%.d",$1*100/$2);}'`
	LAST_DOT=$DOTS
	[ -z "$PCENT" ] && PCENT=0
	dots=`printf "%.2d" $PCENT`
	N=0
	BAR=`echo $DOTS | awk '{ str=""; for(i=0;i<$1;i++) { str=str"#"; } print str;  }'`
	printf "     [------------------------------------------------------------------------]\r" >&2
	printf "  $dots%% [${BAR}\r" >&2
}

is_envword() {
	found=""
	for A in $ENVWORDS ; do
		[ $A = $1 ] && found=1 && break;
	done
	[ -z "$found" ] && \
	echo "`word` | Warning: Variable '$1' not defined." >&2
}

## string mode ##
enter_string_mode() {
	[ -n "${STRING}" ] && return 0
	MODE_OLD="${MODE}"
	MODE="string"
	STRING="${1}"
	return 1
}
alias wait_string_mode='[ "${MODE}" = "string" ] && continue'
alias leave_string_mode='STRING=""'

enter_mode() {
	MODE=$1
	STR=$2
	_REQUIRED=0
	case "${STR}" in
	*!) _REQUIRED=1  ; ;;
	esac
}

cat_file() {
	cat "$1" \
	 | sed -e 's,/\*,((,g' \
	 | sed -e 's,\*/,)),g' \
	 | awk '{ print $0" _n_";}'
}

# MAIN LOOP #

print_header # show /bin/sh stuff

COMMENT=0
WORDS=`cat_file "${ACRFILE}"`
WORDS_N=0 # number of current word (counter)
WORDS_K=0 # keyword word
WORDS_T=`echo ${WORDS} | wc -w ` # total number of words

[ "${DEBUG}" = 1 ] && echo "acr: parsing '${ACRFILE}'" >&2

for STR2 in ${WORDS} NOP ; do
	INSIDE_INCLUDE=0
	if [ "$MODE" = "include" ]; then
		[ "${DEBUG}" = 1 ] && echo "`word` < include $STR2"  >&2

		if [ -e "@DATADIR@/acr/modules/${STR2}.acr" ]; then
			FILE="@DATADIR@/acr/modules/${STR2}.acr";
		else	FILE="$STR2.acr"; fi
		
		if [ -e "$FILE" ]; then
			MODE=""
			INSIDE_INCLUDE=1
			#WORDS_N=$(($WORDS_N+1))
			WORDS2=`cat_file ${FILE}`
		else
			echo "`word` | ERROR: Cannot include file '$FILE'. File not found."  >&2
			exit 1
		fi
	else
		WORDS2="${STR2}"
	fi

	for STR in $WORDS2 ; do
	# inner loop for include workaround.

	# Progress bar
	if [ "${INSIDE_INCLUDE}" = 0 ]; then
		[ "${PBAR}" = 1 ] && show_progress_bar
	fi
	WORDS_N=$(($WORDS_N+1))

	IS_KEYWORD=1
	is_buggy ${STR}

	# XXX <- is deprecated! remove in 0.6
	if [ "${STR}" != "<-" ]; then
		if [ "`echo ${STR}| cut -c -2`" = "<-" ]; then
			VAR="`echo ${STR} | cut -c 3-`"
			TMP="${S}${VAR}"
			if [ "${DEBUG}" = 1 ]; then
				echo "`word` > get value" >&2
			fi
			STR=${TMP}
		fi
	fi

	# COMMENTS #
	[ -z "${STR}" ] && continue ## ???

	case ${COMMENT} in
	"1")
		case "${STR}" in
		"##"|"\\\\"|"))"|"*/")
			COMMENT=0
			;;
		esac
		continue
		;;
	"//")
		if [ "${STR}" = "_n_" ]; then
			COMMENT=0
		else
			continue
		fi
		;;
	*)
		case "${STR}" in
		"##"|"(("|"/*")
			COMMENT=1
			continue
			;;
		"//")
			COMMENT="//"
			continue
			;;
		esac
		;;
	esac

	[ "${STR}" = "_n_" ] && continue
	
	# one word comment
	[ "`echo $STR | cut -c 1`" = "#" ] && \
	echo "`word` | One word comment is deprecated." >&2 && \
	continue

	# CONDITION FLAGS #
	if [ -n "${MODE}" ]; then

	if [ "${MODE}" = "string" ]; then
		[ "${STR}" = "NOP" ] && STR=";"
		if [ "${STR}" = ";" ] ; then
			[ "${DEBUG}" = 1 ] && \
			echo "     |     $spaces\"${STRING}\"" >&2
			MODE="${MODE_OLD}"
		else
			STRING="`concat "${STRING}" ${STR}`"
			continue
		fi
	fi

	case "${MODE}" in
	"pkgconfig-do")
		enter_string_mode ${STR}
		wait_string_mode
		FILE_NAME="${STRING}"
		check_pcgen ${STRING}
		IS_KEYWORD=0
		MODE=""
		leave_string_mode
		;;
	"description")
		enter_string_mode ${STR}
		wait_string_mode
		DESCRIPTION="${STRING}"
		IS_KEYWORD=0
		MODE=""
		leave_string_mode
		;;
	"chkdecl")
		IS_KEYWORD=0
		if [ -z "${_CHKDECL_INC}" ]; then
			_CHKDECL_INC="${STR}"
		else
			MODE=""
			_CHKDECL_NAME=${STR}
			check_decl $_CHKDECL_INC ${_CHKDECL_NAME}
			_CHKDECL=0
			_CHKDECL_INC=""
			_CHKDECL_NAME=""
		fi
		;;
	"chkflg")
		IS_KEYWORD=0
		if [ -z "${_CHKFLG_VAR}" ]; then
			_CHKFLG_VAR="${STR}"
		else
			if [ "${STR}" = ";" ]; then
				check_cflags "${_CHKFLG_NAME}"
				_CHKFLG_NAME=""
				_CHKFLG_VAR=""
				_CHKFLG=0
				MODE=""
			else
				_CHKFLG_NAME="${_CHKFLG_NAME} ${STR}"
			fi
		fi
		;;
	"chksym")
		IS_KEYWORD=0
		if [ -z "${_CHKSYM_LIB}" ]; then
			_CHKSYM_LIB="${STR}"
		else
			MODE=""
			_CHKSYM_NAME=${STR}
			check_sym $_CHKLIB_LIB ${_CHKSYM_NAME}
			_CHKSYM=0
			_CHKSYM_LIB=""
			_CHKSYM_NAME=""
		fi
		;;
	"chkfunc")
		IS_KEYWORD=0
		if [ -z "${_CHKFUNC_LIB}" ]; then
			_CHKFUNC_LIB="${STR}"
		else
			MODE=""
			_CHKFUNC_NAME=${STR}
			check_func $_CHKLIB_LIB ${_CHKFUNC_NAME}
			_CHKFUNC=0
			_CHKFUNC_LIB=""
			_CHKFUNC_NAME=""
		fi
		;;
	"chkcp")
		MODE=""
		IS_KEYWORD=0
		REPORT_REQUIRED="${REPORT_REQUIRED} ${STR}"
		VAR=`var_filter "HAVE_CLASSPATH_${STR}"`
		SCRIPT="${SCRIPT}
ochof \"checking for classpath ${STR}... \"
echo \"import ${STR}.*;\" > test.java
${S}{JAVAC} test.java 2>&1 > /dev/null
if [ \"${S}?\" = 0 ]; then echo yes
${VAR}=1
else echo no
${VAR}=0
"
		if [ "${_REQUIRED}" = 1 ]; then
			SCRIPT="${SCRIPT}
echo 'This classpath is required.'
exit 1
"
		fi
		SCRIPT="${SCRIPT}fi"
		;;
	"chklib")
		IS_KEYWORD=0
		check_lib ${STR}
		MODE=""
		_CHKLIB_NAME=""
		;;
	"chkpy")
		IS_KEYWORD=0
		check_py ${STR}
		MODE=""
		;;
	"chkru")
		IS_KEYWORD=0
		check_ru ${STR}
		MODE=""
		;;
	"chkpm")
		IS_KEYWORD=0
		check_pm ${STR}
		MODE=""
		;;
	"check_exec")
		IS_KEYWORD=0
		if [ -z "${_IF_VAR}" ]; then	
			_IF_VAR=${STR};
		elif [ "${STR}" = ";" ]; then
SCRIPT=`cat <<EOF
${SCRIPT}
printf "running ${_IF_VALUE}... "
${_IF_VALUE}
${_IF_VAR}=${S}?
if [ "${S}${_IF_VAR}" = 0 ]; then
	${_IF_VAR}=1
	echo yes
else
	${_IF_VAR}=0
	echo no
fi
EOF
`
			reset_vars
		else
			if [ -z "${_IF_VALUE}" ]; then
				_IF_VALUE="${STR}"
			else
				_IF_VALUE="${_IF_VALUE} ${STR}"
			fi
		fi
		;;
	"check")
		IS_KEYWORD=0
		if [ -z "${CHECK_VAR}" ]; then	
			TMP_VAR=""
			SET_VAR=""
			CHECK_VAR=${STR};
		elif [ -z "${CHECK_EOF}" ]; then
			CHECK_EOF=${STR}
		elif [ "${STR}" = "${CHECK_EOF}" ]; then
SCRIPT=`cat <<EOF
${SCRIPT}
ochof "checking for ${CHECK_VAR}... "
${TMP_VAR}
if [ "${S}${CHECK_VAR}" = 0 ]; then
	${CHECK_VAR}=1
	echo yes
else
	${CHECK_VAR}=0
	echo no
fi

EOF
`
			add_envword ${CHECK_VAR}
			MODE=""
			CHECK_VAR=""
			CHECK_EOF=""
			TMP_VAR=""
		else
			if [ "${STR}" = "\\" ]; then
				TMP_VAR="${TMP_VAR} ;"
			else
				TMP_VAR="${TMP_VAR} ${STR}"
			fi
		fi
		;;
	"chkgrp")
		IS_KEYWORD=0;
		if [ -z "${CHKUSR_NAME}" ]; then
		CHKUSR_NAME=":${STR}"
		CHKUSR_VAR=`var_filter "HAVE_GROUP_$STR"`
		CHKUSR_TARGET="group"
		MODE="chkusr"
		fi
		;;
	"chkusr")
		IS_KEYWORD=0;
		if [ -z "${CHKUSR_NAME}" ]; then
			CHKUSR_NAME=${STR}
			CHKUSR_VAR=`var_filter "HAVE_USER_$CHKUSR_NAME"`
		else
		ACR_RMFILES="${ACR_RMFILES} .usrgrp.tmp"
		# HAVE_USER_WWWDATA=0
		add_envword ${CHKUSR_VAR}

		SCRIPT=`cat <<EOF
${CHKUSR_VAR}=0
ochof "checking for ${CHKUSR_TARGET} '${CHKUSR_NAME}'... "
touch .usrgrp.tmp && chown ${CHKUSR_NAME} .usrgrp.tmp 2>/dev/null
if [ \\$? = 0 ]; then echo "ok" ; $CHKUSR_VAR=1; else echo "no"
EOF
`
		if [ 1 = "${_REQUIRED}" ]; then
			SCRIPT="${SCRIPT} ; echo \"*** This ${CHKUSR_TARGET} is required!.\" ; do_remove ; exit 1"
		fi
		SCRIPT="${SCRIPT} ; fi"
		MODE=""
		CHKUSR_NAME=""
		CHKUSR_TARGET=""
		IS_KEYWORD=1
		fi
		;;
	"chkver")
		IS_KEYWORD=0
		if [ -z "${_CHKVER_LIB}" ]; then
			_CHKVER_LIB="${STR}"
		else

		if [ "${_REQUIRED}" = 1 ]; then
			REPORT_REQUIRED="${REPORT_REQUIRED} lib${_CHKVER_LIB}>=${STR}"
		else
			REPORT_OPTIONAL="${REPORT_OPTIONAL} lib${_CHKVER_LIB}>=${STR}"
		fi
	
		# HAVE_GLIB_2_0_VERSION_2_18
		VAR=`var_filter HAVE_${_CHKVER_LIB}_VERSION_${STR}`
		add_envword ${VAR}
		SCRIPT=`cat <<EOF
${SCRIPT}
${VAR}=0
printf "checking version of library ${_CHKVER_LIB} >= ${STR}... "
${S}{PKGCONFIG} --atleast-version=${STR} ${_CHKVER_LIB} >/dev/null 2>&1
if [ ${S}? = 0 ]; then ${VAR}=1 ; echo ok; else
echo "no"
EOF
`
		if [ 1 = ${_REQUIRED} ]; then
			SCRIPT=`cat <<EOF
${SCRIPT}
echo "ERROR: You need ${_CHKVER_LIB} >= ${STR}." >&2
exit 1; fi
EOF
`
		else
			SCRIPT="${SCRIPT} ; fi"
		fi
			_CHKVER=0
			_CHKVER_LIB=""
			MODE=""
		fi
		;;
	"useiconv")
		MODE=""
		add_envword "HAVE_LIB_ICONV"
		add_envword "ICONV_LIBS"
		REPORT_REQUIRED="${REPORT_REQUIRED} libiconv"
		SCRIPT=`cat <<EOF
${SCRIPT}
ochof "checking for iconv... "
HAVE_LIB_ICONV=0
ICONV_LIBS=""
for HOST_OS in solaris linux netbsd ; do
	HAVE_LIB_ICONV=1
	echo libc
	break;
done
if [ "${HAVE_LIB_ICONV}" = 0 ]; then
	echo 'main(){iconv_open}' > test.c
	${S}{CC} ${S}{CFLAGS} ${S}{CPPFLAGS} ${S}{LDFLAGS} test.c 2> /dev/null
	if [ ${S}? = 0 ]; then
		echo yes
		HAVE_LIB_ICONV=1
	else
		echo no
	fi
fi
EOF
`
		;;
	"exec"|"exec!")
		IS_KEYWORD=0
		if [ -z "${_EXEC_VAR}" ]; then
			_EXEC_VAR="${STR}"
		else
			enter_string_mode ${STR}
			wait_string_mode
			add_envword "${_EXEC_VAR}"
			append "`cat<<EOF
printf "checking exec ${STRING}... "
${_EXEC_VAR}="\\\`${STRING} 2>/dev/null\\\`"
if [ ${S}? = 0 ]; then
	echo "ok"
else
	${_EXEC_VAR}=""
	echo "not found"
EOF
`"
			[ "$MODE" = 'exec!' ] && \
				append "echo 'required'; exit 1"
			append "fi"
			MODE=""
			_EXEC_VAR=""
			leave_string_mode
		fi
		;;
	"usedl")
		MODE=""
		REPORT_REQUIRED="${REPORT_REQUIRED} libdl"
		add_envword "HAVE_LIB_DL"
		add_envword "DL_LIBS"
		# Linux Darwin BeOS Solaris need libdl
		SCRIPT=`cat <<EOF
${SCRIPT}
ochof "checking for dynamic library... "
HAVE_LIB_DL=0
for OS in gnulinux linux gnu/kfreebsd syllable serenityos vinix sunos darwin beos solaris wsl ; do
if [ "${S}{HOST_OS}" = "${S}{OS}" ]; then
	HAVE_LIB_DL=1
	break;
fi
done
if [ "${S}{HAVE_LIB_DL}" = 1 ]; then
	DL_LIBS="-ldl"
	echo "required"
else
	DL_LIBS=""
	echo "libc"
fi
EOF
`
		MODE=""
		;;
	"export")
		IS_KEYWORD=0;
		if [ "${STR}" = ";" ]; then
			MODE=""
		else
			add_envword $STR
		fi
		;;
	"not")
		IS_KEYWORD=0
		MODE=""
		SCRIPT="${SCRIPT} ; if [ \"${S}{STR}\" = 0 ]; then ${STR}=1 ; else ${STR}=0 ; fi"
		_NOT=0
		;;
	"echo")
		IS_KEYWORD=0;
		enter_string_mode ${STR}
		wait_string_mode
		SCRIPT="${SCRIPT}
echo \"   ${STRING}\" >&2 "
		MODE=""
		leave_string_mode
		;;
	"die")
		IS_KEYWORD=0;
		if [ -z "${_DIE_VAR}" ]; then
			_DIE_VAR=${STR}
		else
			enter_string_mode ${STR}
			wait_string_mode
			if [ "${_DIE_BOOL}" = 0 ]; then
			SCRIPT="${SCRIPT}
case \"${S}${_DIE_VAR}\" in 0|\"\") echo ; echo \"ERROR: ${STRING}\" >&2 ; echo ; exit 1 ;; esac"
			else
			SCRIPT="${SCRIPT}
case \"${S}${_DIE_VAR}\" in 0|\"\") ;; *) echo ; echo \"ERROR: ${STRING}\" >&2 ; echo ; exit 1 ; ;; esac"
			fi
			MODE=""
			_DIE_VAR="";
			leave_string_mode
		fi
		;;
	"if")
		IS_KEYWORD=0
		if [ -z "${_IF_VAR}" ]; then
			_IF_VAR=${STR}
			is_envword "${STR}"
		elif [ -z "${_IF_SETVAR}" ]; then
			if [ "${STR}" = "{" ]; then
				NKEYS=$(($NKEYS+1))
				SCRIPT="${SCRIPT}
if [ \"${S}${_IF_VAR}\" = \"${_IF_BOOL}\" ]; then"
				MODE=""
				_IF_VAR=""
				_IF_SETVAR=""
				_IF_OP=""
			else
				_IF_SETVAR=${STR}
				is_envword ${STR}
				_IF_SETVAL=""
			fi
		elif [ -z "${_IF_OP}" ]; then
			_IF_OP=${STR}
			check_ifop ${_IF_OP}
		else
			enter_string_mode ${STR}
			wait_string_mode
			add_envword ${_IF_SETVAR}
			SCRIPT="${SCRIPT}
if [ \"${S}${_IF_VAR}\" = \"${_IF_BOOL}\" ]; then"
			# check_ifop TODO -> use ifop instead of that.
			case ${_IF_OP} in
			"=") SCRIPT="${SCRIPT} ${_IF_SETVAR}=\"${_IF_SETVAL}\" ; fi"
				;;
			"+=") SCRIPT="${SCRIPT} ${_IF_SETVAR}=\"${S}{${_IF_SETVAR}} ${_IF_SETVAL}\" ; fi"
				;;
			"?=") SCRIPT="${SCRIPT} if [ -z \"${S}${_IF_SETVAR}\" ]; then ${_IF_SETVAR}=\"${_IF_SETVAL}\"; fi"
				;;
			"<-") SCRIPT="${SCRIPT} ${_IF_SETVAR}=\"\`eval 'echo \$${_IF_SETVAL}'\`\"; fi"

				# XXX deprecated in 0.6
				echo "`word` | The '<-' keyword is deprecated. Use $VAR instead" >&2
				;;
			esac
			reset_vars
			leave_string_mode
		fi
		;;
	"ifeqval")
		echo "IFEQVAL: deprecated keyword. Please use IFEQ VAR \$VAR2 ;"  >&2
		IS_KEYWORD=0
		if [ -z "${_IF_VAR}" ]; then
			_IF_VAR=$STR
			is_envword "${STR}"
		elif [ -z "${_IF_VAR2}" ]; then
			_IF_VAR2=$STR
		elif [ -z "${_IF_SETVAR}" ]; then
			if [ "${STR}" = "{" ]; then
				NKEYS=$(($NKEYS+1))

				if [ "${_IF_BOOL}" = 1 ]; then
					SCRIPT="${SCRIPT}
if ["
				else
					SCRIPT="${SCRIPT}
if [ !"
				fi
				SCRIPT="${SCRIPT} \\\"${S}${_IF_VAR}\\\" = \\\"${S}${_IF_VAR2}\\\" ]; then"
				_IFEQVAL=0
				reset_vars
			else
				_IF_SETVAR=${STR}
			fi
		elif [ -z "${_IF_OP}" ]; then
			_IF_OP=${STR}
			check_ifop ${_IF_OP}
		elif [ "${STR}" = ";" ]; then
			if [ "${_IF_BOOL}" = 1 ]; then
				SCRIPT="${SCRIPT}
if ["
			else
				SCRIPT="${SCRIPT}
if [ !"
			fi
			SCRIPT="${SCRIPT} \"${S}${_IF_VAR}\" = \"${S}${_IF_VAR2}\" ]; then"

			# TODO this must be in a external function
			case ${_IF_OP} in
			"=")  SCRIPT="${SCRIPT} ${_IF_SETVAR}=\"${_IF_SETVAL}\" ; fi"
				;;
			"+=") SCRIPT="${SCRIPT} ${_IF_SETVAR}=\"${S}{${_IF_SETVAR}} ${_IF_SETVAL}\" ; fi"
				;;
			"?=") SCRIPT="${SCRIPT} if [ -z \"${S}${_IF_SETVAR}\" ]; then ${_IF_SETVAR}=\"${_IF_SETVAL}\"; fi"
				;;

			"<-") SCRIPT="${SCRIPT} ${_IF_SETVAR}=\"\`eval 'echo \$${_IF_SETVAL}'\`\"; fi"
				# XXX this is deprecated
				echo "`word` | The '<-' keyword is deprecated. Use $VAR instead" >&2
				;;
			esac
			MODE=""
			_IFEQVAL=0
			_IF_BOOL=0
			reset_vars
		else
			_IF_SETVAL="`concat "${_IF_SETVAL}" "${STR}"`"
		fi
		;;
	"ifeq")
		IS_KEYWORD=0
		if [ -z "${_IF_VAR}" ]; then
			_IF_VAR=${STR}
			is_envword "${STR}"
		elif [ "${_IF_VALUE_EOF}" = 0 ]; then
			if [ "${STR}" = ";" ]; then
				_IF_VALUE_EOF=1
			elif [ "${STR}" = "{" ]; then
				echo "`word` | Missing ; in ifeq expression" >&2
				exit 1
			else
				_IF_VALUE="`concat "${_IF_VALUE}" "${STR}"`"
			fi
		elif [ -z "${_IF_SETVAR}" ]; then
			if [ "${STR}" = "{" ]; then
				if [ "${_IF_BOOL}" = 1 ]; then
					SCRIPT="`printf \"${SCRIPT}\nif [\"`"
				else
					SCRIPT="`printf \"${SCRIPT}\nif [ !\"`"
				fi
				SCRIPT="`printf \"${SCRIPT} \\\"${S}${_IF_VAR}\\\" = \\\"${_IF_VALUE}\\\" ]; then\"`"
				NKEYS=$(($NKEYS+1))
				MODE=""
				_IF_VALUE_EOF=0
				reset_vars
			else
				_IF_SETVAR=${STR}
			fi
		elif [ -z "${_IF_OP}" ]; then
			_IF_OP="${STR}"
			check_ifop ${_IF_OP}
		elif [ "${STR}" = ";" ]; then
			# bool
			if [ "${_IF_BOOL}" = 1 ]; then
				SCRIPT="`echo -n \"${SCRIPT}
if [ \"`"
			else
				SCRIPT="`echo -n \"${SCRIPT}
if [ ! \"`"
			fi
			SCRIPT="`echo \"${SCRIPT} \\\"${S}${_IF_VAR}\\\" = \\\"${_IF_VALUE}\\\" ]; then\"`"

			# set
			# TODO create a new function that concats SCRIPT checking IF_OP
			case ${_IF_OP} in
			"=") SCRIPT="${SCRIPT} ${_IF_SETVAR}=\"${_IF_SETVAL}\" ; fi ;"
				;;
			"+=") SCRIPT="${SCRIPT} ${_IF_SETVAR}=\"${S}{${_IF_SETVAR}} ${_IF_SETVAL}\" ; fi ;"
				;;
			"?=") SCRIPT="${SCRIPT} if [ -z \"${S}${_IF_SETVAR}\" ]; then ${_IF_SETVAR}=\"${_IF_SETVAL}\"; fi ; fi ;"
				;;
			"<-") SCRIPT="${SCRIPT} ${_IF_SETVAR}=\"\`eval 'echo \$${_IF_SETVAL}'\`\"; fi"
				echo "`word` | The '<-' keyword is deprecated. Use $VAR instead" >&2
				;;
			esac
			reset_vars
			_IFEQ=0
			_IF_VALUE_EOF=0
		elif [ -z "${_IF_SETVAL}" ]; then
			_IF_SETVAL="${STR}"
		else
			_IF_SETVAL="${_IF_SETVAL} ${STR}"
		fi
		;;
	"ifand")
		IS_KEYWORD=0
		if [ -z "${_IF_VAR}" ]; then
			_IF_VAR=${STR}
			is_envword "${STR}"
		elif [ -z "${_IF_VAR2}" ]; then
			_IF_VAR2="${STR}"
			is_envword "${STR}"
		elif [ -z "${_IF_SETVAR}" ]; then
			if [ "${STR}" = "{" ]; then
				NKEYS=$(($NKEYS+1))
				[ "${_IF_BOOL}" = 1 ] && OGLE=11 || OGLE=00
				SCRIPT="${SCRIPT}
if [ ${OGLE} = \"${S}${_IF_VAR}${S}${_IF_VAR2}\" ]; then
"
				_IF_VALUE_EOF=0
				reset_vars
			else
				_IF_SETVAR=${STR}
				is_envword ${STR}
			fi
		elif [ -z "${_IF_OP}" ]; then
			_IF_OP="${STR}"
			check_ifop ${_IF_OP}
		elif [ ! "${STR}" = ";" ]; then
			if [ -z "${_IF_SETVAL}" ]; then
				_IF_SETVAL="${STR}"
			else
				_IF_SETVAL="${_IF_SETVAL} ${STR}"
			fi
		else
			[ "${_IF_BOOL}" = 1 ] && OGLE=1 || OGLE=0

			case ${_IF_OP} in
			"=") STR="${_IF_SETVAR}=\"${_IF_SETVAL}\""
				;;
			"+=") STR="${_IF_SETVAR}=\"${S}{${_IF_SETVAR}} ${_IF_SETVAL}\""
				;;
			"?=") STR="if [ -z \"${S}${_IF_SETVAR}\" ]; then ${_IF_SETVAR}=\"${_IF_SETVAL}\"; fi"
				;;
			"<-") SCRIPT="${SCRIPT} ${_IF_SETVAR}=\"\`eval 'echo \$${_IF_SETVAL}'\`\"; fi"
				echo "`word` | The '<-' keyword is deprecated. Use $VAR instead" >&2
				;;
			esac
			SCRIPT="${SCRIPT}
if [ ${OGLE} = \"${S}${_IF_VAR}\" ]; then
if [ ${OGLE} = \"${S}${_IF_VALUE}\" ]; then
${STR} ; fi ; fi"
## TODO XXX this must be other command (IFEQVAR..or something similar
#if [ ${OGLE}"${S}${_IF_VAR}" = "${S}${_IF_VALUE}" ]; then ${_IF_SETVAR}="${STR}" ; fi ;
			reset_vars
		fi
		;;
	"ifor")
		IS_KEYWORD=0
		if [ -z "${_IF_VAR}" ]; then
			_IF_VAR=${STR}
			is_envword "${STR}"
		elif [ -z "${_IF_VAR2}" ]; then
			_IF_VAR2="${STR}"
			is_envword "${STR}"
		elif [ -z "${_IF_SETVAR}" ]; then
			if [ "${STR}" = "{" ]; then
				NKEYS=$(($NKEYS+1))

				SCRIPT="${SCRIPT}
if [ ${_IF_BOOL} = \"${S}${_IF_VAR}\" ] || [ ${_IF_BOOL} = \"${S}${_IF_VAR2}\" ]; then
"
				_IF_VALUE_EOF=0
				reset_vars
			else
				_IF_SETVAR=${STR}
				is_envword ${STR}
			fi
		elif [ -z "${_IF_OP}" ]; then
			_IF_OP="${STR}"
			check_ifop ${_IF_OP}
		elif [ ! "${STR}" = ";" ]; then
			if [ -z "${_IF_SETVAL}" ]; then
				_IF_SETVAL="${STR}"
			else
				_IF_SETVAL="${_IF_SETVAL} ${STR}"
			fi
		else
			[ "${_IF_BOOL}" = 1 ] && OGLE=1 || OGLE=0

			case ${_IF_OP} in
			"=")  STR="${_IF_SETVAR}=\"${_IF_SETVAL}\""
				;;
			"+=") STR="${_IF_SETVAR}=\"${S}{${_IF_SETVAR}} ${_IF_SETVAL}\""
				;;
			"?=") STR="if [ -z \"${S}${_IF_SETVAR}\" ]; then ${_IF_SETVAR}=\"${_IF_SETVAL}\"; fi"
				;;
			"<-") SCRIPT="${SCRIPT} ${_IF_SETVAR}=\"\`eval 'echo \$${_IF_SETVAL}'\`\"; fi"
				echo "`word` | The '<-' keyword is deprecated. Use $VAR instead" >&2
				;;
			esac
			SCRIPT="${SCRIPT}
if [ ${_IF_BOOL} = \"${S}${_IF_VAR}$\" ] || [ ${_IF_BOOL} = \"${S}${_IF_VAR2}\" ]; then
${STR} ; fi"
			reset_vars
		fi
		;;
	"valapkg")
		IS_KEYWORD=0
		TMP_VAR=`var_filter "HAVE_VALAPKG_${STR}"`
		add_envword ${TMP_VAR}
		# TODO: Use EOF or F="$F B"
		REPORT_PKGCFG="${REPORT_PKGCFG} ${STR}"
		SCRIPT="`printf \"${SCRIPT}\nprintf 'checking valac --pkg ${STR}... '\"`"
		## - flags - ##
		SCRIPT="`printf \"${SCRIPT}\ntmp=\\\`echo 'void main(){}'>.test.vala ; ${S}{VALAC} --pkg ${STR} .test.vala 2>/dev/null\\\`\"`"
		SCRIPT="`printf \"${SCRIPT}\nif [ ${S}? != 0 ]; then echo no ; ${TMP_VAR}=0; \"`"
		if [ "${_REQUIRED}" = 1 ]; then
		SCRIPT="`printf \"${SCRIPT}\necho 'This vala package is required'\nexit 1\"`"
		fi
		SCRIPT="`printf \"${SCRIPT}\nelse \"`"
		SCRIPT="`printf \"${SCRIPT}\necho yes; ${TMP_VAR}=1; \";`"
		SCRIPT="`printf \"${SCRIPT}\nfi;rm -f .test.vala\"`"
		## -eof- ##
		_REQUIRED=0
		MODE=""
		;;
	"pkgconfig")
		if [ "${_USE_PKGCONFIG}" = 0 ]; then
			echo "PKGCFG directive requires USE_PKGCONFIG" >&2
			exit 1
		fi
		IS_KEYWORD=0
		if [ -z "${_PC_CFLAGS}" ]; then
			_PC_CFLAGS="${STR}"
		elif [ -z "${_PC_LDFLAGS}" ]; then
			_PC_LDFLAGS="${STR}"
		else
			add_envword ${_PC_CFLAGS}
			add_envword ${_PC_LDFLAGS}
			TMP_VAR=`var_filter "HAVE_PKGCFG_${STR}"`
			add_envword ${TMP_VAR}
			# TODO: Use EOF or F="$F B"
			REPORT_PKGCFG="${REPORT_PKGCFG} ${STR}"
			SCRIPT="`printf \"${SCRIPT}\nif [ -z \\\"${S}{PKGCONFIG}\\\" ]; then pkg-config --version >/dev/null 2>&1 ; if [ $? = 0 ]; then PKGCONFIG=pkg-config ; else PKGCONFIG=pkgconf ; fi; fi\"`"
			if [ -z "${ONCE_PKGCONFIG}" ]; then
				SCRIPT="`printf \"${SCRIPT}\ntype ${S}{PKGCONFIG} > /dev/null 2>&1 || echo \\\"ERROR: Cannot find valid PKGCONFIG, pkg-config or pkgconf in PATH\\\"\"`"
				SCRIPT="`printf \"${SCRIPT}\necho 'Using PKGCONFIG: '${S}{PKGCONFIG}\"`"
				ONCE_PKGCONFIG=1
			fi
			SCRIPT="`printf \"${SCRIPT}\nprintf 'checking pkg-config flags for ${STR}... '\"`"
			## - flags - ##
			SCRIPT="`printf \"${SCRIPT}\ntmp=\\\`${S}{PKGCONFIG} --cflags ${STR} 2>/dev/null\\\`\"`"
			SCRIPT="`printf \"${SCRIPT}\nif [ ${S}? != 0 ]; then echo no ; ${TMP_VAR}=0;\"`"
			SCRIPT="`printf \"${SCRIPT}\n${_PC_CFLAGS}='';${_PC_LDFLAGS}='';\"`"
			if [ "${_REQUIRED}" = 1 ]; then
			SCRIPT="`printf \"${SCRIPT}\necho 'This package is required'\nexit 1\"`"
			fi
			SCRIPT="`printf \"${SCRIPT}\nelse\"`"
			if [ "${_PKGCONFIG_PLUS}" = 1 ]; then
			SCRIPT="`printf \"${SCRIPT}\n${_PC_CFLAGS}=\\\"${S}${_PC_CFLAGS} ${S}tmp\\\";\"`"
			else
			SCRIPT="`printf \"${SCRIPT}\n${_PC_CFLAGS}=${S}tmp;\"`"
			fi
			## - libs - ##
			SCRIPT="`printf \"${SCRIPT}\ntmp=\\\`${S}{PKGCONFIG} --libs ${STR} 2>/dev/null\\\`\"`"
			SCRIPT="`printf \"${SCRIPT}\nif [ ${S}? = 0 ]; then\"`"
			SCRIPT="`printf \"${SCRIPT}\necho yes; ${TMP_VAR}=1;\";`"
			if [ "${_PKGCONFIG_PLUS}" = 1 ]; then
			SCRIPT="`printf \"${SCRIPT}\n${_PC_LDFLAGS}=\\\"${S}${_PC_LDFLAGS} ${S}tmp\\\"; fi; fi\"`"
			else
			SCRIPT="`printf \"${SCRIPT}\n${_PC_LDFLAGS}=${S}tmp; fi; fi\"`"
			fi
			## -eof- ##
			_PC_CFLAGS=""
			_PC_LDFLAGS=""
			_REQUIRED=0
			MODE=""
		fi
		;;
	"report")
		IS_KEYWORD=0
		if [ "${STR}" = ";" ]; then
			MODE=""
		else
			REPORT="${REPORT} ${STR}"
		fi
		;;
	"chkinc")
		check_inc ${STR}
		IS_KEYWORD=0
		MODE=""
		;;
	"subconf")
		IS_KEYWORD=0
		if [ "${STR}" = ";" ]; then
			MODE=""
		else
			SCRIPT="`printf \"${SCRIPT}\n(cd ${STR} && sh configure )\"`"
		#	SUBCONF_DIRS="${SUBCONF_DIRS} ${STR}"
		fi
		;;
	"subdirs")
		IS_KEYWORD=0
		case "${STR}" in
		";") MODE="" ; ;;
		#"%") SD_FILES="%" ; ;; ## WTF
		*) SD_FILES="${SD_FILES}${STR} " ; ;;
		esac
		;;
	"chkprg")
		IS_KEYWORD=0
		if [ "${STR}" = "NOP" ]; then
			echo "ERROR: Expected argument after CHKPRG keyword"  >&2
			exit 1
		fi
		if [ -z "${_CHKPRG_VAR}" ]; then
			_CHKPRG_VAR=${STR}
		else
			_CHKPRG_NAME=${STR}
			check_prg ${_CHKPRG_VAR} ${_CHKPRG_NAME} ${_REQUIRED}
			MODE=""
			_CHKPRG_VAR=""
			_CHKPRG_NAME=""
		fi
		;;
	"outputh")
		if [ "${STR}" = ";" ]; then
			MODE=""
		else
			H_FILES="${H_FILES} ${STR}"
		fi
		IS_KEYWORD=0
		;;
	"outputsh")
		if [ "${STR}" = ";" ]; then
			MODE=""
		else
			SH_FILES="${SH_FILES} ${STR}"
		fi
		IS_KEYWORD=0
		;;
	"argument")
		if [ "${STR}" = ";" ]; then
			# enable DEBUG debug "="
			add_flag ${ARGUMENT} ${ARG_NAME} \
				${ARG_FLAG} "${ARG_VALUE}" "${ARG_DESC}"
			MODE=""
			ARGUMENT=""
			ARG_NAME=""
			ARG_FLAG=""
			ARG_VALUE=""
			ARG_DESC=""
		else
			if [ -z "${ARG_NAME}" ]; then
				ARG_NAME=`echo ${STR} | cut -d = -f 1`
				ARG_VALUE=`echo ${STR} | awk -F = '{ print $2 }'`
			elif [ -z "${ARG_FLAG}" ]; then
				ARG_FLAG=${STR}
			else
				ARG_DESC="${ARG_DESC} ${STR}"
			fi
		fi
		IS_KEYWORD=0
		;;
	"equal")
		# TODO: use string mode
		if [ "${STR}" = ";" ]; then
			for A in 1 2 3 4 ; do
				if [ "${A}" = "${EQUAL}" ]; then
				echo "`word` | There's a null body assignation to variable ${SET_VAR}. Please type \"\" or ''" >&2
				exit 1
				IS_KEYWORD=0
				fi
			done

			[ "${DEBUG}" = 1 ] && \
			echo "     |     $spaces\"${STRING}\"" >&2
			STRING=""
			SCRIPT="${SCRIPT}
${EQUAL}\""

			MODE=""
	#		eval ${EQUAL}\" # XXX ?
			EQUAL=""
			_EQUAL_3=0
		else
			if [ -n "${SET_VAR}" ]; then
				STRING=${STR}
				STR=${SET_VAR}
				SET_VAR=""
			fi
			case "${STRING}" in
			"''"|"\"\"")
				STRING=""
				;;
			esac
			case "${EQUAL}" in
			1)  EQUAL="${STR}=\"${STRING}" ;;                               # =
			2)  EQUAL="${STR}=\"${S}{${STR}} ${STRING}" ;;                  # +=
			3)  EQUAL="[ -z \"${S}{${STR}}\" ]  && ${STR}=\"${STRING}" ;; # ?=
			4)  EQUAL="${STR}=\"${S}${STRING}" ; ;;                # <- XXX deprecated!!
			*)  #STRING=`concat "${STRING}" "${STR}"`
			    EQUAL=`concat "${EQUAL}" "${STR}"` ; ;;          # .concat()
			esac
			#STRING=""
			continue
		fi
		IS_KEYWORD=0
		;;
	"pkgname")
		add_envword "PKGNAME"
		add_envword "VPATH"
		INIVARS="${INIVARS} PKGNAME='${STR}' ;"
		PKGNAME=${STR}
		MODE=""
		IS_KEYWORD=0
		;;
	"version")
		if [ "${PKGNAME}" = "this package" ]; then
			echo "`word` | PKGNAME must be defined before VERSION" >&2
			exit 1
		fi
		if [ x != "x`echo \"$STR\" | grep ^@`" ]; then
			F="`echo \"$STR\" | cut -c 2-`"
			STR=$(cat $F)
		fi
		add_envword "VERSION"
		INIVARS="${INIVARS} VERSION='${STR}' ;"
		VERSION="${STR}"
		VERSION_MAJOR="`semver ${STR} 1`"
		VERSION_MINOR="`semver ${STR} 2`"
		VERSION_PATCH="`semver ${STR} 3`"
		VERSION_NUMBER="`printf "%d%02d%02d\n" ${VERSION_MAJOR} ${VERSION_MINOR} ${VERSION_PATCH}`"
		INIVARS="${INIVARS} VERSION_MAJOR=${VERSION_MAJOR};"
		INIVARS="${INIVARS} VERSION_MINOR=${VERSION_MINOR};"
		INIVARS="${INIVARS} VERSION_PATCH=${VERSION_PATCH};"
		INIVARS="${INIVARS} VERSION_NUMBER=${VERSION_NUMBER};"
		MODE=""
		IS_KEYWORD=0
		;;
	"contact")
		IS_KEYWORD=0
		if [ "${_CONTACT}" = 1 ]; then
			if [ "${STR}" = ";" ]; then
				_CONTACT=2
			else
				if [ -z "${CONTACT_NAME}" ]; then
					CONTACT_NAME="${STR}"
				else
					CONTACT_NAME="${CONTACT_NAME} ${STR}"
				fi
			fi
		else
			CONTACT_MAIL=${STR};
			MODE=""
			_CONTACT=0 ## XXX ugly hack
			add_envword "CONTACT"
			add_envword "CONTACT_NAME"
			add_envword "CONTACT_MAIL"
			INIVARS="${INIVARS} CONTACT_MAIL=\"${CONTACT_MAIL}\" ;"
			INIVARS="${INIVARS} CONTACT_NAME=\"${CONTACT_NAME}\" ;"
			INIVARS="${INIVARS} CONTACT=\"${CONTACT_NAME} <${CONTACT_MAIL}>\" ;"
		fi
		;;
	"arg_ignore")
		FLAGSCRIPT=`printf "${FLAGSCRIPT}\n\"${STR}\") : ; ;;"`
		IS_KEYWORD=0
		MODE=""
		;;
	"author")
		IS_KEYWORD=0
		if [ "${_AUTHOR}" = 1 ]; then
			if [ "${STR}" = ";" ]; then
				_AUTHOR=2
			else
				if [ -z "${AUTHOR_NAME}" ]; then
					AUTHOR_NAME="${STR}"
				else
					AUTHOR_NAME="${AUTHOR_NAME} ${STR}"
				fi
			fi
		else
			AUTHOR_MAIL=${STR};
			MODE=""
			_AUTHOR=0 ## XXX ugly hack
			add_envword "AUTHOR"
			add_envword "AUTHOR_NAME"
			add_envword "AUTHOR_MAIL"
			INIVARS="${INIVARS} AUTHOR_MAIL=\"${AUTHOR_MAIL}\" ;"
			INIVARS="${INIVARS} AUTHOR_NAME=\"${AUTHOR_NAME}\" ;"
			INIVARS="${INIVARS} AUTHOR=\"${AUTHOR_NAME} <${AUTHOR_MAIL}>\" ;"
		fi
		;;
	"requireacrversion")
		IS_KEYWORD=0
		MODE=""
		_OK="`echo ${STR} ${ACR_VERSION} | awk '{ if ($1 <= $2) {print "ok";} }'`"
		if [ -z "${_OK}" ]; then
			echo "`word` | Invalid ACR version. You need at least v${STR}" >&2
			exit 1
		fi
		;;
	"sizeof")
		enter_string_mode ${STR}
		wait_string_mode

		check_sizeof "${STRING}"
		MODE=""
		IS_KEYWORD=0
		
		leave_string_mode
		;;
	"substfiles")
		IS_KEYWORD=0
		if [ "${STR}" = ";" ]; then
			MODE=""
		else
			S_FILES="${S_FILES} ${STR}"
			# XXX may be buggy on >1 dir. must be checked
			DIR="`echo ${STR} | awk -F / '{ print substr($0,0,length($0)-length($NF)) }'`"
			S_MKDIR="${S_MKDIR} ${DIR}"
		fi
		;;
	*)
		echo "Invalid mode '${MODE}'"  >&2
		exit 1
		;;
	esac
	fi

	##KEYWORDS##
	## debug output {{{
	if [ "${DEBUG}" = 1 ]; then
		spaces="`echo $NKEYS | awk '{ str=""; for(i=0;i<$1;i++) { str=str"  "; } print str; }'`"
		[ -n "${MODE}" ] && spaces=", ${spaces}"
	fi

	if [ "${IS_KEYWORD}" = 0 ]; then
		if [ "${DEBUG}" = 1 ]; then
			if [ ! "${STR}" = ";" ]; then
				printf "%04d |$spaces " $WORDS_N >&2
				echo "${STR}" >&2
			fi
		fi
		continue
	fi

	WORDS_K=$WORDS_N

	if [ "${DEBUG}" = 1 ]; then
		if [ ! "${WORDS_N}" = "${WORDS_T}" ]; then
			printf "%04d |$spaces " $WORDS_N >&2
			echo "${STR}" >&2
		fi
	fi
	## }}}

	if [ -n "${SET_VAR}" ]; then
		MODE="equal"
		case $STR in
		"=")  EQUAL=1 ;;
		"+=") EQUAL=2 ;;
		"?=") EQUAL=3 _EQUAL_3=1 ;;
		"<-") EQUAL=4
			echo "`word` | The '<-' keyword is deprecated. Use $VAR instead" >&2
;; ### XXX deprecated!!
		*)
			check_ifop $STR
			EQUAL=1 ;; ## will crash in check_ifop :)
		esac
		continue
	fi

	# clear required flag
	_REQUIRED=0

	case $STR in #------------ keyword tokens
	";")
		echo "`word` < found ';' character out of context." >&2
		exit 1
		;;
	"}{"|"}ELSE{")
		if [ $NKEYS -le 0 ]; then
			echo "`word` < '}{' unmatching ELSE key." >&2
			exit 1
		fi
		SCRIPT="${SCRIPT}
else"
		;;
	"}")
		if [ $NKEYS -lt 0 ]; then
			echo "`word` < unmatching '}' key." >&2
			exit 1
		fi
		NKEYS=$(($NKEYS-1))
		SCRIPT="${SCRIPT}; fi"
		;;
					#------------- segmentation keywords
	"LOCAL:")
		echo "LOCAL: deprecated keyword."  >&2
		;;
	"GLOBAL:")
		echo "GLOBAL: deprecated keyword."  >&2
		;;
					#------------- command keywords
	"NOP")
		IS_KEYWORD=0 # Do Nothing Keyword
		;;
	##LANGS##
	"LANG_C")               env_c ; ;;
	"LANG_C!")              LANG_C_REQUIRED=1; env_c ; ;;
	"LANG_V")               env_v ; ;;
	"LANG_V!")              LANG_V_REQUIRED=1; env_v ; ;;
	"LANG_OBJC")		env_objc ; ;;
	"LANG_OBJC!")		LANG_OBJC_REQUIRED=1 ; env_objc ; ;;
	"LANG_CC"|"LANG_CXX")   env_cxx ; ;;
	"LANG_CC!"|"LANG_CXX!") LANG_CXX_REQUIRED=1; env_cxx ; ;;
	"LANG_BASH")            LANG_BASH=1; lang_bash ; ;;
	"LANG_TCL")             env_tcl ; ;;
	"LANG_TCL!")            LANG_TCL_REQUIRED=1 ; env_tcl ; ;;
	"LANG_PYTHON")          env_python ; ;;
	"LANG_PYTHON!")         LANG_PYTHON_REQUIRED=1 ; env_python ; ;;
	"LANG_JAVA")            env_java ; ;;
	"LANG_JAVA!")           LANG_JAVA_REQUIRED=1 ; env_java ; ;;
	"LANG_VALA")            env_vala ; ;;
	"LANG_VALA!")           LANG_VALA_REQUIRED=1 ; env_vala ; ;;
	"LANG_LUA")             env_lua ; ;;
	"LANG_LUA!")            LANG_LUA_REQUIRED=1 ; env_lua ; ;;
	"LANG_PERL")            env_perl ; ;;
	"LANG_PERL!")           LANG_PERL_REQUIRED=1 ; env_perl ; ;;
	"USE_PERL_THREADS") 	env_perl_threads ; ;;
	"USE_PERL_THREADS!")	_REQUIRED=1 env_perl_threads ; ;;
	"LANG_RUBY")		env_ruby ; ;;
	"LANG_RUBY!")		LANG_RUBY_REQUIRED=1 ; env_ruby ; ;;
	"USE_TK")
		add_envword "TK_VERSION"
		add_envword "HAVE_TK"
		add_envword "TK_LIBS"
		add_envword "TK_LDFLAGS"
		add_flag with TK_BASEDIR tk-basedir "/usr/" "Sets the TK basedir to find [/usr]/lib/tkConfig.sh"
		USE_TK=1 ; ;;
	"USE_X11")
		REPORT_OPTIONAL="${REPORT_OPTIONAL} X11"
		add_envword "HAVE_X11"
		add_envword "X11BASE"
		SCRIPT="${SCRIPT} ; X11BASE=/usr/X11R6/"
		add_flag without HAVE_X11 x "" "Disable the X11 window system"
		add_flag with X11BASE x11base "=" "Sets the X11BASE directory [/usr/X11R6]"
		add_flag x X_INCLUDES includes "=" "X include files are in DIR" "=DIR"
		add_flag x X_LIBRARIES libraries "=" "Points to the X11 library directory" "=DIR"
		USE_X11=1   ; ;;
	"USE_GMAKE") USE_GMAKE=1 ; env_gmake ; ;;
	"PKGNAME") MODE="pkgname" ; ;;
	"VERSION") MODE="version" ; ;;
	"CONTACT") MODE="contact" _CONTACT=1 ; ;;
	"AUTHOR")  MODE="author" _AUTHOR=1 ; ;;
	"DESC"|"DESCRIPTION") MODE="description" ; ;;
	"PKGCFG_DO"|"PKGCONFIG_DO") MODE="pkgconfig-do" ; ;;
	"ARG_IGNORE") MODE="arg_ignore" ; ;;
	"INCLUDE")
		if [ "${INSIDE_INCLUDE}" = 1 ]; then
			echo "ERROR: Cannot nest includes." >&2
			exit 1
		fi
		MODE="include"
		;;
	"REQUIRE_ACR_VERSION") MODE="requireacrversion" ; ;;
	"CHECK_INCLUDE"|"CHECK_INCLUDE!"|"CHKINC"|"CHKINC!")
		if [ -z "${LANG_C}${LANG_CXX}" ]; then
			echo "`word` | Define LANG_C or LANG_CXX before use CHECK_INC" >&2
			exit 1
		fi
		enter_mode "chkinc" ${STR}
		;;
	"CHK_CP"|"CHK_CP!"|"CHECK_CLASSPATH"|"CHECK_CLASSPATH!")
		if [ -z "${LANG_JAVA}" ]; then
			echo "`word` | Define LANG_JAVA before use CHECK_CLASSPATH" >&2
			exit 1
		fi
		enter_mode "chkcp" ${STR}
		;;
	"CHKDECL"|"CHKDECL!"|"CHKDEC"|"CHKDEC!"|"CHECK_DECLARATION"|"CHECK_DECLARATION!")
		if [ -z "${LANG_C}" ]; then
			echo "`word` | Define LANG_C before use CHECK_FUNC" >&2
			exit 1
		fi
		enter_mode "chkdecl" ${STR}
		;;
	"CHKFLG"|"CHKFLG!"|"CHECK_CFLAGS"|"CHECK_CFLAGS!")
		if [ -z "${LANG_C}" ]; then
			echo "`word` | Define LANG_C before use CHKFLG" >&2
			exit 1
		fi
		enter_mode "chkflg" ${STR}
		;;
	"CHKSYM"|"CHKSYM!"|"CHECK_SYMBOL"|"CHECK_SYMBOL!")
		if [ -z "${LANG_C}" ]; then
			echo "`word` | Define LANG_C before use CHKSYM" >&2
			exit 1
		fi
		enter_mode "chksym" ${STR}
		;;
	"CHKFNC"|"CHKFNC!"|"CHECK_FUNCTION"|"CHECK_FUNCTION!")
		if [ -z "${LANG_C}" ]; then
			echo "`word` | Define LANG_C before use CHECK_FUNC" >&2
			exit 1
		fi
		enter_mode "chkfunc" ${STR}
		;;
	"CHK_PY"|"CHK_PY!"|"CHECK_PYTHON_MODULE"|"CHECK_PYTHON_MODULE!")
		if [ -z "${LANG_PYTHON}" ]; then
			echo "`word` | Define LANG_PYTHON before CHECK_PYTHON_MODULE." >&2
			exit 1
		fi
		enter_mode "chkpy" ${STR}
		;;
	"CHK_RU"|"CHK_RU!"|"CHECK_RUBY_EXTENSION"|"CHECK_RUBY_EXTENSION")
		if [ -z "${LANG_RUBY}" ]; then
			echo "`word` | Define LANG_RUBY before CHECK_RUBY_EXTENSION." >&2
			exit 1
		fi
		enter_mode "chkru" ${STR}
		;;
	"CHK_PM"|"CHK_PM!"|"CHECK_PERL_MODULE"|"CHECK_PERL_MODULE!")
		if [ -z "${LANG_PERL}" ]; then
			echo "`word` | Define LANG_PERL before using CHK_PM directive" >&2
			exit 1
		fi
		enter_mode "chkpm" ${STR}
		;;
	"CHKLIB"|"CHKLIB!"|"CHECK_LIBRARY"|"CHECK_LIBRARY!")
		if [ -z "${LANG_C}${LANG_CXX}" ]; then
			echo "`word` | Define LANG_C or LANG_CXX before use CHECK_LIB" >&2
			exit 1
		fi
		enter_mode "chklib" ${STR}
		;;
	"CHKPRG"|"CHKPRG!"|"CHECK_PROGRAM"|"CHECK_PROGRAM!")
		enter_mode "chkprg" ${STR}
		;;
	"CHKUSR"|"CHKUSR!")
		enter_mode "chkusr" ${STR}
		;;
	"CHKGRP"|"CHKGRP!")
		enter_mode "chkgrp" ${STR}
		;;
	"ENDIAN"|"CHECK_ENDIAN")
		if [ -z "${LANG_C}${LANG_CXX}" ]; then
			echo "`word` | Define LANG_C or LANG_CXX before use CHECK_ENDIAN" >&2
			exit 1
		fi
		check_endian
		;;
	"SIZEOF")
		if [ -z "${LANG_C}${LANG_CXX}" ]; then
			echo "`word` | Define LANG_C or LANG_CXX before use SIZEOF" >&2
			exit 1
		fi
		enter_mode "sizeof" ${STR}
		;;
	"PKGCONFIG!"|"PKGCFG!")
		MODE="pkgconfig"
		_PKGCONFIG_PLUS=0
		_REQUIRED=1
		;;
	"PKGCONFIG"|"PKGCFG")
		MODE="pkgconfig"
		_PKGCONFIG_PLUS=0
		;;
	"VALAPKG")
		MODE="valapkg"
		_REQUIRED=0
		;;
	"VALAPKG!")
		MODE="valapkg"
		_REQUIRED=1
		;;
	"PKGCONFIG+"|"PKGCFG+")
		MODE="pkgconfig"
		_PKGCONFIG_PLUS=1
		;;
	"USE_LIBTOOL"|"USE_LIBTOOL!")
		if [ -z "${LANG_C}${LANG_CXX}" ]; then
			echo "`word` | USE_LIBTOOL requires LANG_C or LANG_CXX." >&2
			exit 1
		fi
		# TODO: Use mode for libtool
		if [ "`echo ${STR}| grep '!'`" ]; then
			_REQUIRED=1
		else
			_REQUIRED=0
		fi
		use_libtool
		;;
	"USE_PKGCONFIG"|"USE_PKGCONFIG!")
		if [ "`echo ${STR}| grep '!'`" ]; then
			_REQUIRED=1
		else
			_REQUIRED=0
		fi
		use_pkgconfig
		;;
	"USE_PTHREAD"|"USE_PTHREAD!")
		if [ -z "${LANG_C}${LANG_CXX}" ]; then
			echo "`word` | USE_PTHREAD requires LANG_C or LANG_CXX." >&2
			exit 1
		fi
		if [ "`echo ${STR}| grep '!'`" ]; then
			_REQUIRED=1
		else
			_REQUIRED=0
		fi
		use_pthread
		;;
	"USE_BACKUP")
		echo "`word` | USE_BACKUP is deprecated. It may not work on further version." >&2
		USE_BACKUP=1
		;;
	"REPORT_DEFINED"|"RD")
		_REPORT_DEFINED=1
		;;
	"USE_JNI")
		if [ -z "${LANG_C}${LANG_CXX}${LANG_JAVA}" ]; then
			echo "`word` | USE_JNI requires LANG_C or LANG_CXX and LANG_JAVA" >&2
			exit 1
		fi
		use_jni
		;;
	"REPORT")    MODE="report" ;;
	"OUTPUT_H")  MODE="outputh" ;;
	"OUTPUT_SH") MODE="outputsh" ;;
	"SUBCONF")   MODE="subconf" ;;
	"SUBDIRS")   MODE="subdirs" ;;
	"EXPORT")
		echo "`word` | EXPORT keyword is deprecated. No need to use." >&2
		MODE="export" ;;
	"SUBST"|"SUBST_FILES")
		MODE="substfiles" ;;
	"IF")
		MODE="if"
		_IF_BOOL=1 ;;
	"IFNOT")
		MODE="if"
		_IF_BOOL=0 ;;
	"IFNULL")
		MODE="if"
		_IF_BOOL="" ;;
	"ECHO")
		MODE="echo" ;;
	"IFNOTNULL")
		MODE="ifeq"
		_IF_BOOL=0
		_IF_VALUE=""
		_IF_VALUE_EOF=1
		;;
	"IFEQ")
		MODE="ifeq"
		_IF_BOOL=1 ;;
	"IFNOTEQ")
		MODE="ifeq"
		_IF_BOOL=0 ;;
	"IFEQVAL")
		MODE="ifeqval"
		_IF_BOOL=1 ;;
	"IFNOTEQVAL")
		MODE="ifeqval"
		_IF_BOOL=0 ;;
	"IFAND")
		MODE="ifand"
		_IF_BOOL=1 ;;
	"IFNOTAND")
		MODE="ifand"
		_IF_BOOL=0 ;;
	"IFOR")
		MODE="ifor"
		_IF_BOOL=1 ;;
	"IFNOTOR")
		MODE="ifor"
		_IF_BOOL=0 ;;
	"NOT")
		MODE="not"
		_NOT=1 ;;
	"DIE"|"DIEIF")
		MODE="die"
		_DIE_BOOL=1 ;;
	"DIENOT"|"DIEIFNOT")
		MODE="die"
		_DIE_BOOL=0 ;;
	"DIENOW")
		MODE="die"
		_DIE_VAR=undefined_variable
		_DIE_BOOL=0 ;;
	"CHKVER!"|"CHECK_VERSION!"|"CHKVER"|"CHECK_VERSION")
		enter_mode "chkver" ${STR} ;;
	"USE_DL")
		if [ "${_USE_DL_USED}" = 0 ]; then
			MODE="usedl"
			_USE_DL_USED=`word`
		else
			echo "`word` | ignored twice USE_DL found at ${_USE_DL_USED}." >&2
		fi ;;
	"EXEC")
		MODE="exec"
		;;
	"EXEC!")
		MODE="exec!"
		;;
	"USE_ICONV")
		MODE="useiconv" ;;
	"ARG_WITH")
		if [ $NKEYS -gt 0 ]; then
			echo "cant use ARG_WITH inside bracket blocks" >&2
			exit 1
		fi
		MODE="argument"
		ARGUMENT=with ;;
	"ARG_WITHOUT")
		if [ $NKEYS -gt 0 ]; then
			echo "cant use ARG_WITHOUT inside bracket blocks" >&2
			exit 1
		fi
		MODE="argument"
		ARGUMENT=without ;;
	"ARG_ENABLE")
		if [ $NKEYS -gt 0 ]; then
			echo "cant use ARG_ENABLE inside bracket blocks" >&2
			exit 1
		fi
		MODE="argument"
		ARGUMENT=enable ;;
	"ARG_DISABLE")
		if [ $NKEYS -gt 0 ]; then
			echo "cant use ARG_DISABLE inside bracket blocks" >&2
			exit 1
		fi
		MODE="argument"
		ARGUMENT=disable ;;
	"CHKEXE" | "CHECK_EXEC")
		MODE="check_exec"
		;;
	"CHECK")
		MODE="check"
		;;
	"="|"+="|"?="|"<-")
		echo "The *old* format <op> <var> <value> is deprecated."  >&2
		echo "Please use the standard format <var> <op> <value>."  >&2
		exit 1
		;;
	*)
		add_envword "$STR"
		SET_VAR=$STR
		;;
	esac
done # inner loop

done

if [ "${IS_KEYWORD}" = 1 ]; then
	echo "`word` | Unexpected end of file." >&2
	exit 1
fi

if [ -n "$MODE" ]; then
	echo "`word` | Unexpected end of file." >&2
	exit 1
fi

if [ "${COMMENT}" = 1 ]; then
	echo "`word` | Comment tag not closed." >&2
	exit 1
fi

# Final stage
if [ "${MAKEFILE}" = 1 ]; then
if [ -f Makefile.acr ]; then
	echo "ERROR: Makefile.acr already exists. Remove't manually." >&2
	exit 1
fi

if [ -z "${VERSION}" ]; then
	echo "ERROR: no VERSION defined." >&2
	exit 1
fi
if [ "${PKGNAME}" = "this package" ]; then
	echo "ERROR: no PKGNAME defined." >&2
	exit 1
fi

if [ $NKEYS -lt 0 ]; then
	echo "`word` < unexpected eof: unmatching final '}' key." >&2
	exit 1
fi

cat > Makefile.acr <<EOF
.PHONY: all install dist clean mrproper

VPATH=@VPATH@
EOF

echo "PKGNAME=@""PKGNAME@" >> Makefile.acr
echo "VERSION=@""VERSION@" >> Makefile.acr
echo "INSTALL_DATA=@""INSTALL_DATA@" >> Makefile.acr
echo "INSTALL_PROGRAM=@""INSTALL_PROGRAM@" >> Makefile.acr
echo "INSTALL_PROGRAM_STRIP=@""INSTALL_PROGRAM_STRIP@" >> Makefile.acr

if [ "${LANG_JAVA}" = 1 ]; then
cat >> Makefile.acr <<EOF
JAVAC=@JAVAC@

%.class: %.java
	${S}{JAVAC} $<
EOF
fi

[ "${LANG_V}" = 1 ] &&\
cat >> Makefile.acr <<EOF
V=@V@
VFLAGS=@VFLAGS@
EOF

[ "${LANG_C}" = 1 ] &&\
cat >> Makefile.acr <<EOF
CC=@CC@
CFLAGS=@CFLAGS@
CPPFLAGS=@CPPFLAGS@
EOF

[ "${LANG_CXX}" = 1 ] &&\
cat >> Makefile.acr <<EOF
CXX=@CXX@
CXXFLAGS=@CXXFLAGS@
EOF

# XXX this will be deprecated!
if [ "${USE_BACKUP}" = 1 ]; then
cat >> Makefile.acr <<EOF
define unsubst
   @echo unsubst ${S}{1}
   @if [ -f "${S}{1}.backup" ]; then\\
      if [ ! -f "${S}{1}" ]; then\\
         FOO="";\\
      else\\
         FOO="\`diff -u ${S}{1} ${S}{1}.backup\`";\\
      fi;\\
      if [ "${S}{FOO}" ]; then\\
         echo "Looks like '${S}{1}' has changed. Check .backup";\\
         exit 1;\\
      else\\
         rm -f ${S}{1}.backup;\\
         mv -f ${S}{1}.orig ${S}{1};\\
      fi\\
   fi
endef

EOF
fi
echo "" >> Makefile.acr
echo "all:" >> Makefile.acr
for A in ${SD_FILES} ; do
	if [ ! "${A}" = "." ]; then
	printf "\tcd ${A} && ${S}{MAKE}\n" >> Makefile.acr
	fi
done

echo "" >> Makefile.acr
echo "install:" >> Makefile.acr
for A in ${SD_FILES} ; do
	if [ ! "${A}" = "." ]; then
	printf "\tcd ${A} && ${S}{MAKE} install\n" >> Makefile.acr
	fi
done
cat >> Makefile.acr << EOF

clean:
EOF
for A in ${SD_FILES} ; do
	if [ ! "${A}" = "." ]; then
	printf "\tcd ${A} && ${S}{MAKE} clean\n" >> Makefile.acr
	fi
done

if [ "${USE_BACKUP}" = 1 ]; then
	for A in ${S_FILES}; do
	printf "\t${S}(call unsubst,${A})\n" >> Makefile.acr
	done
else
	for A in ${S_FILES}; do
	printf "\tmv -f ${A}.orig ${A}\n" >> Makefile.acr
	done
fi

if [ "${H_FILES}" ]; then
	printf "\trm -f ${H_FILES}\n" >> Makefile.acr
fi
if [ "${SH_FILES}" ]; then
	printf "\trm -f ${SH_FILES}\n" >> Makefile.acr
fi
cat >> Makefile.acr <<EOF

mrproper: clean
	rm Makefile

dist: mrproper
	cd ..  && \\
	mv -f ${S}{PKGNAME} ${S}{PKGNAME}-${S}{VERSION} && \\
	tar czvf ${S}{PKGNAME}-${S}{VERSION}.tar.gz ${S}{PKGNAME}-${S}{VERSION} && \\
	mv -f ${S}{PKGNAME}-${S}{VERSION} ${S}{PKGNAME} && \\
	echo "dist done"
EOF
	echo "acr: Makefile.acr created successfully." >&2
else
	if [ "${PBAR}" = 1 ]; then
	echo >&2 ; fi
	create_script
	
	if [ $NKEYS -lt 0 ]; then
		echo "`word` < unexpected eof: unmatching '}' key." >&2
		exit 1
	fi
	show_report
	if [ "${NOPE}" = 0 ]; then
		echo "acr: ${CONFIGURE} script created successfully." >&2
	fi
fi

makedist_failed() {
	echo "ERROR: $1"  >&2
	exit 1
}

if [ "${MAKEDIST}" = 1 ]; then
	if [ -z "$PKGNAME" ]; then
		makedist_failed "PKGNAME not defined"
	fi
	if [ -z "$VERSION" ]; then
		VERSION=`date +%Y%m%d`
		echo "VERSION not defined. Using current date" >&2
	fi
	
	# if ( basename `pwd` != $PKGNAME ) { failed!! }
	DIRNAME="`expr $PWD : '.*\/\(.*\)$'`"
	if [ ! "$DIRNAME" = "$PKGNAME" ]; then
		makedist_failed "Invalid directory name '$DIRNAME' must be '$PKGNAME'."  >&2
	fi
##
	echo "[2/4] Running configure." >&2
	./configure 2>&1 | ${AWK} '{ printf "."; }' >&2
	echo  >&2
	if [ ! -e "Makefile.acr" ]; then
		makedist_failed "No Makefile.acr found in current directory. Cannot clean"
	fi
	echo "[3/4] Cleaning (make clean)" >&2
	make clean 2>&1 | ${AWK} '{ printf "."; }' >&2
	echo  >&2
	echo "[4/4] Disting project into a tarball." >&2
	P=${PKGNAME}-${VERSION}
	if [ -d .hg ]; then
		rm -rf .tmp >&2
		mkdir -p .tmp >&2
		hg clone . .tmp/${P} >&2
		rm -rf .tmp/${P}/.hg
		cd .tmp/
		tar czvf ../${P}.tar.gz $P 2>&1 | ${AWK} '{ printf "."; }' >&2
		rm -rf .tmp
	elif [ -d .git ]; then
		rm -rf .tmp >&2
		mkdir -p .tmp >&2
		git clone . .tmp/${P} >&2
		rm -rf .tmp/${P}/.git
		cd .tmp/
		tar czvf ../${P}.tar.gz $P 2>&1 | ${AWK} '{ printf "."; }' >&2
		rm -rf .tmp
	elif [ -d .svn ]; then
		URL=$(svn info| grep URL|cut -d : -f 2-)
		rm -rf .tmp >&2
		mkdir -p .tmp >&2
		cd .tmp
		svn co ${URL} ${P}
		rm -rf ${P}/.svn
		tar czvf ../${P}.tar.gz $P 2>&1 | ${AWK} '{ printf "."; }' >&2
		rm -rf .tmp
	else
		cd ..
		mv $PKGNAME $PKGNAME-$VERSION
		tar czvf $PKGNAME-$VERSION.tar.gz $PKGNAME-$VERSION 2>&1 | ${AWK} '{ printf "."; }' >&2
		if [ ! $? = 0 ]; then
			mv $PKGNAME-$VERSION $PKGNAME
			makedist_failed "Cannot create ../$PKGNAME-$VERSION"
		fi
		mv $PKGNAME-$VERSION $PKGNAME
		cd -
		mv ../$PKGNAME-$VERSION.tar.gz .
	fi
	echo >&2
	echo "Tarball created successfully at $PKGNAME-$VERSION.tar.gz"  >&2
fi

# vim:set foldmethod=marker
