#!/bin/sh
# This script was automatically generated by ACR v0.4
# @author: pancake <pancake@phreaker.net>
# @url: http://www.nopcode.org/?t=acr
#

do_remove() {
  rm -f ${ACR_RMFILES}
}
control_c() {
  printf "\n\n^C control-c : script execution interrupted.\n"
  do_remove
  exit 1
}
trap control_c 2
DEBUG=0
ENDSCRIPT=""
split_host() {
	DOL="$"
	while : ; do
	ENVWORDS="${ENVWORDS} $1_CPU $1_OS"
	STR=`eval "echo ${DOL}$1"`
	SPLIT_CPU="`echo $STR | awk -F - '{ print $1 }'`"
	SPLIT_OS="`echo $STR | awk -F - '{ print $3 }'`"
	eval "$1_CPU=$SPLIT_CPU"
	eval "$1_OS=$SPLIT_OS"
	# go next
	shift
	if [ -z "$1" ]; then break; fi
	done
}

guess_os() {
	CPU="`uname -m`"
	OS="`uname -s|awk '{print(tolower($0))}'`"
	echo "${CPU}-unknown-${OS}"
}

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

test -z "${PREFIX}" && PREFIX=/usr/local
HOST=`guess_os`
BUILD="${HOST}"
TARGET="${HOST}"
SYSCONFDIR=""

create_environ() {
	test -z "${EPREFIX}"    && EPREFIX="${PREFIX}";
	test -z "${SPREFIX}"    && SPREFIX="${PREFIX}";
	test -z "${BINDIR}"     && BINDIR="${SPREFIX}/bin";
	test -z "${SBINDIR}"    && SBINDIR="${SPREFIX}/sbin";
	test -z "${SYSCONFDIR}" && SYSCONFDIR="${SPREFIX}/etc";
	test -z "${LIBDIR}"     && LIBDIR="${SPREFIX}/lib";
	test -z "${LIBEXECDIR}" && LIBEXECDIR="${SPREFIX}/libexec";
	test -z "${DATADIR}"    && DATADIR="${SPREFIX}/share";
	test -z "${INCLUDEDIR}" && INCLUDEDIR="${SPREFIX}/include";
	test -z "${INFODIR}"    && INFODIR="${SPREFIX}/info";
	test -z "${MANDIR}"     && MANDIR="${SPREFIX}/man";
	test -z "${LOCALSTATEDIR}"  && LOCALSTATEDIR="${SPREFIX}/var";
	test -z "${INSTALL}"    && INSTALL="/usr/bin/install";
	test -z "${INSTALL_DIR}" && INSTALL_DIR="${INSTALL} -d";
	test -z "${INSTALL_DATA}" && INSTALL_DATA="${INSTALL} -m 644";
	test -z "${INSTALL_SCRIPT}" && INSTALL_SCRIPT="${INSTALL}";
	test -z "${INSTALL_PROGRAM}" && INSTALL_PROGRAM="${INSTALL} -s";

 PKGNAME=pag ; VERSION=0.92 ; CONTACT_MAIL="pancake@phreaker.net" ; CONTACT_NAME="pancake" ; CONTACT="pancake <pancake@phreaker.net>" ;
}
                
                
show_usage() {
cat <<EOF2
'configure' configures pag-0.92 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
  -V, --version           display version information and exit
  -r, --report            show what libs/programs require to work

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

Fine tuning of the installation directories:
  --bindir=DIR           user executables [EPREFIX/bin]
  --sbindir=DIR          system admin executables [EPREFIX/sbin]
  --libexecdir=DIR       program executables [EPREFIX/libexec]
  --datadir=DIR          read-only architecture-independent data [PREFIX/share]
  --sysconfdir=DIR       read-only single-machine data [PREFIX/etc]
  --sharedstatedir=DIR   modifiable architecture-independent data [PREFIX/com]
  --localstatedir=DIR    modifiable single-machine data [PREFIX/var]
  --libdir=DIR           object code libraries [EPREFIX/lib]
  --includedir=DIR       C header files [PREFIX/include]
  --infodir=DIR          info documentation [PREFIX/info]
  --mandir=DIR           man documentation [PREFIX/man]

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

printf "\nOptional Features:
  --enable-debug         builds pag with debugging support \n"
printf "\nSome influential environment variables:
  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>
  CPPFLAGS    C/C++ preprocessor flags, e.g. -I<include dir> if you have
              headers in a nonstandard directory <include dir>
  CPP         C preprocessor\n"
printf "\nReport bugs to: pancake <pancake@phreaker.net>"
echo ""
exit 0
}

take_environ() {
	if [ -z "${SH}" ]; then SH=sh ; fi
if [ -z "${CC}" ]; then CC=gcc ; fi
if [ -z "${CPP}" ]; then CPP=cpp ; fi
	if [ -z "${PREFIX}" ]; then PREFIX="/usr/local/" ; fi
}
                
show_version() {
	echo "This is the ACR 0.4 : Auto Conf Replacement."
	echo "This 'Free Software Foundation' message is only for autodetection."
	echo "Originally written by pancake <pancake@phreaker.net>."
	exit 0
}

parse_options() {
	flag=`echo $1| awk -F = '{ print $1 }'`
	value=`echo $1| awk -F = '{ print $2 }'`
	#if [ -z "$value" ]; then value=0; fi # XXX

	case $flag in
	"-h"|"--help"|"--hel"|"--h"|"--he"|"-help")
		show_usage ; ;;
	"-V"|"--version")
		show_version ; ;;
	"-r"|"--report")
echo "PKGNAME:   pag"
echo "VERSION:   0.92"
echo "LANGS:     c"
echo "REQUIRED:  libncurses"
echo "OPTIONAL:  libaa"
echo "FLAGS:     --enable-debug"
		exit 0
		;;
	"--cache-file") 
		# ignored: acr doesnt needs cache
		;;
	"--build")
		BUILD=$value; ;;
	"--host")
		HOST=$value; ;;
	"--target")
		TARGET=$value; ;;
	"--prefix")
		PREFIX=$value; ;;
	"--exec-prefix")
		EPREFIX=$value; ;;
	"--sandbox"|"--sprefix")
		SPREFIX=$value; ;;
	"--bindir")
		BINDIR=$value; ;;
	"--sbindir")
		SBINDIR=$value; ;;
	"--libexecdir")
		LIBEXECDIR=$value; ;;
	"--datadir")
		DATADIR=$value; ;;
	"--sysconfdir")
		SYSCONFDIR=$value; ;;
	"--sharedstatedir")
		SHAREDSTATEDIR=$value; ;;
	"--localstatedir")
		LOCALSTATEDIR=$value; ;;
	"--libdir")
		LIBDIR=$value; ;;
	"--includedir")
		INCLUDEDIR=$value; ;;
	"--infodir")
		INFODIR=$value; ;;
	"--mandir")
		MANDIR=$value; ;;

--enable-debug) DEBUG="1" ENDSCRIPT="${ENDSCRIPT} DEBUG=1 ;";;
	*) if [ -n "$value" ]; then eval '$flag="$value"' ; fi
		;;
	esac
}


# MAIN #
take_environ

while : ; do
        if [ -z "$1" ]; then break; fi
        parse_options $1
        shift;
done

ENVWORDS="MANDIR INFODIR LIBDIR LOCALSTATEDIR SYSCONFDIR DATADIR LIBEXECDIR SBINDIR BINDIR EPREFIX PREFIX SPREFIX TARGET HOST BUILD INSTALL INSTALL_PROGRAM INSTALL_DIR INSTALL_SCRIPT INSTALL_DATA PKGNAME VPATH VERSION CONTACT CONTACT_NAME CONTACT_MAIL CC CFLAGS LDFLAGS HAVE_LANG_C HAVE_LIB_NCURSES HAVE_LIB_AA DEBUG CFLAGS INTRO INTRO_FLAGS INTRO INTRO_FLAGS"

create_environ


 INTRO="intro"
 INTRO_FLAGS="-laa"
	echo "checking build system type... ${BUILD}"
	echo "checking host system type... ${HOST}"
	echo "checking target system type... ${TARGET}"

	split_host HOST BUILD TARGET

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

echo "checking for working directories... ${WODIS}"
ACR_RMFILES=" test.c a.out"
printf "checking for c compiler... "
HAVE_LANG_C=1
(command -v ${TARGET}-${CC} >/dev/null 2>&1)
if [ $? = 0 ]; then CC="${TARGET}-${CC}"; fi
echo "main(){}" > test.c
(exec ${CC} ${CFLAGS} ${LDFLAGS} test.c >/dev/null 2>&1)
if [ $? = 0 ]; then echo ${CC}; else
echo no ; HAVE_LANG_C=0
fi

ENVWORDS="${ENVWORDS} CPP"
(exec ${TARGET}-${CPP} --help >/dev/null 2>&1)
if [ $? = 0 ]; then CPP="${TARGET}-${CPP}"; fi
printf "checking for cpp... "
echo "main(){}" > test.c
(exec ${CPP}  test.c >/dev/null 2>&1)
if [ $? = 0 ]; then echo ${CPP}; else
echo "ERROR: ${CPP} cannot parse sources"; fi


 printf "checking for libncurses ... "
 echo "main(){ }" > test.c
 ${CC} ${CFLAGS} ${LDFLAGS} -lncurses test.c >/dev/null 2>&1
 if [ "$?" = "0" ]; then
	HAVE_LIB_NCURSES=1
	echo yes
 else
	HAVE_LIB_NCURSES=0
	echo no
  echo "ERROR: this library is required." > /dev/stderr
  exit 1
 fi
 printf "checking for libaa ... "
 echo "main(){ }" > test.c
 ${CC} ${CFLAGS} ${LDFLAGS} -laa test.c >/dev/null 2>&1
 if [ "$?" = "0" ]; then
	HAVE_LIB_AA=1
	echo yes
 else
	HAVE_LIB_AA=0
	echo no
 fi
eval "${ENDSCRIPT}"

if [ "$DEBUG" = 1 ]; then CFLAGS="${CFLAGS} -g" ; fi
if [ "$HAVE_LIB_AA" = 0 ]; then INTRO="null" ; fi
if [ "$HAVE_LIB_AA" = 0 ]; then INTRO_FLAGS="" ; fi

SEDFLAGS=""
for A in ${ENVWORDS} ; do
	if [ "${A}" = "VPATH" ]; then continue; fi
	if [ "${A}" = "srcdir" ]; then continue; fi ## XXX ugly fix
	eval "VAR=\$${A}"
	VAR="`echo ${VAR} | sed -e 's/\,/\\\,/g'`"
	SEDFLAGS="${SEDFLAGS} -e 's,@${A}@,${VAR},g;'"
done

for A in  src/version.h; do
 echo "filtering ${A}"
 if [ -f "${VPATH}/${A}.orig" ]; then
   mv -f ${VPATH}/${A}.orig ${VPATH}/${A}
 fi
 if [ -f "${VPATH}/${A}" ]; then
   mv -f ${VPATH}/${A} ${VPATH}/${A}.orig
 else
  if [ ! -f "${VPATH}${A}.orig" ]; then
    echo "ERROR: Cannot find ${VPATH}/${A}.orig" > /dev/stderr
    exit 1
  fi
 fi
 cat ${VPATH}/${A}.orig | eval sed ${SEDFLAGS} > ${VPATH}/${A}
done

for A in . ; do
 mkdir -p ${A}
 echo "creating ${A}/Makefile"
  if [ ! -f "${VPATH}/${A}/Makefile.acr" ]; then
    echo "ERROR: Cannot find ${VPATH}/${A}/Makefile.acr" > /dev/stderr
    exit 1
  fi
 cat ${VPATH}/${A}/Makefile.acr | \
eval sed -e "s,@VPATH@,${VPATH}/${A},g" ${SEDFLAGS} > ${A}/Makefile
done

printf "cleaning temporally files... "
do_remove
rm -f test.c a.out
echo "done"
echo ""
echo "Final report:"
for A in  HAVE_LIB_AA HAVE_LIB_NCURSES ; do
  eval "VAL=\$${A}"
  if [ -z "${VAL}" ]; then VAL="(null)"; fi
  echo " - ${A} = ${VAL}"
done
