#!/bin/sh

# Copyright (C) 2008 Andrey Nazarov
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
#
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

# -----------------------------
# Q2PRO configuration script
# -----------------------------

die() {
    echo "ERROR: $1"
    exit 1
}

# build directories
sources=`dirname $0`
[ "$sources" != "." ] || sources=`pwd`
[ -f $sources/src/common.c ] || die "this does not look like Q2PRO source tree"
if [ -f $sources/VERSION ] ; then
    version=`cat $sources/VERSION`
    revision=`tr -d -c [:digit:] < $sources/VERSION`
else
    [ -x `which git` ] || die "VERSION is missing and git suite is not installed"
    [ -d "$sources/.git" ] || die "VERSION is missing and this is not a git working tree"
    version=`git --git-dir "$sources/.git" describe --match "r[0-9]*" --abbrev=4 HEAD`
    revision=`git --git-dir "$sources/.git" describe --match "r[0-9]*" --abbrev=0 HEAD | tr -d -c [:digit:]`
fi
echo "$revision" | grep -q -E "^[[:digit:]]+$" || die "invalid revision"
outdir=`pwd`
config_mk="config.mk"
config_h="config.h"

# defaults
help="no"
exesuffix=""
libsuffix=".so"
targets=""
cpu=""
singleuser="no"
asmflags=""
mingw="no"
wince="no"
[ ! -z "$CFLAGS" ] || CFLAGS="-O2 -g -Wall -Wstrict-prototypes"
have_endian_h="no"
curl_cflags=""

# build tools
cross_prefix=""
cc="gcc"
make="make"
windres="windres"
strip="strip"
pngconfig="libpng-config"
sdlconfig="sdl-config"

# install paths
prefix="/usr/local"
datadir="share/games/q2pro"
libdir="lib/games/q2pro"
bindir="games"
mandir="share/man/man6"
appdir="share/applications"
pixdir="share/pixmaps"
homedir="~/.q2pro"
sitecfg="/etc/default/q2pro"

# use flags
use_snddma="yes"
use_openal="no"
use_dsound="no"
use_dinput="no"
use_udev="no"
use_lirc="no"
use_zlib="yes"
use_curl="no"
use_tga="yes"
use_png="no"
use_jpg="no"
use_md3="yes"
use_sdl="yes"
use_dl="no"
use_x11="yes"
use_client="yes"
use_ref="gl"
use_ui="yes"
use_server="no"
use_anticheat="no"
use_mvd_server="yes"
use_mvd_client="yes"
use_asm="???"
use_baseq2="no"
use_syscon="yes"
use_winsvc="no"
use_dbghelp="no"
use_icmp="???"
use_fps="no"

# constants
basegame="baseq2"
defgame=""
cfgfile="config.cfg"
defcfg="default.cfg"
autocfg="autoexec.cfg"
histfile=".conhistory"
democache=".democache"
screenshots="screenshots"
scoreshots="scoreshots"
gldriver="libGL.so.1"
aldriver="libopenal.so.1"
indriver=""
vid_modelist="640x480 800x600 1024x768"
vid_geometry="640x480"

#parse options
for opt do
    case $opt in
    --help|-h) help="yes"
    ;;
    --sources=*) sources=`echo $opt | cut -d '=' -f 2`
    ;;
    --prefix=*) prefix=`echo $opt | cut -d '=' -f 2`
    ;;
    --cross-prefix=*) cross_prefix=`echo $opt | cut -d '=' -f 2`
    ;;
    --enable-mingw) mingw="yes" ; sdl="no"
    ;;
    --enable-wince) wince="yes"
    ;;
    --cpu=*) cpu=`echo $opt | cut -d '=' -f 2`
    ;;
    --cc=*) cc=`echo $opt | cut -d '=' -f 2`
    ;;
    --make=*) make=`echo $opt | cut -d '=' -f 2`
    ;;
    --windres=*) windres=`echo $opt | cut -d '=' -f 2`
    ;;
    --strip=*) strip=`echo $opt | cut -d '=' -f 2`
    ;;
    --disable-client) use_client="no"
    ;;
    --enable-server) use_server="yes"
    ;;
    --enable-baseq2) use_baseq2="yes"
    ;;
    --enable-dbghelp) use_dbghelp="yes"
    ;;
    --disable-snddma) use_snddma="no"
    ;;
    --enable-openal) use_openal="yes"
    ;;
    --enable-dsound) use_dsound="yes"
    ;;
    --enable-dinput) use_dinput="yes"
    ;;
    --enable-lirc) use_lirc="yes"
    ;;
    --disable-zlib) use_zlib="no"
    ;;
    --enable-curl) use_curl="yes"
    ;;
    --disable-tga) use_tga="no"
    ;;
    --enable-png) use_png="yes"
    ;;
    --enable-jpg) use_jpg="yes"
    ;;
    --disable-md3) use_md3="no"
    ;;
    --disable-ui) use_ui="no"
    ;;
    --use-ref=*) use_ref=`echo $opt | cut -d '=' -f 2`
    ;;
    --disable-asm) use_asm="no"
    ;;
    --enable-anticheat) use_anticheat="yes"
    ;;
    --disable-mvd-server) use_mvd_server="no"
    ;;
    --disable-mvd-client) use_mvd_client="no"
    ;;
    --disable-icmp) use_icmp="no"
    ;;
    --enable-fps) use_fps="yes"
    ;;
    --single-user) singleuser="yes"
    ;;
    --datadir=*) datadir=`echo $opt | cut -d '=' -f 2`
    ;;
    --libdir=*) libdir=`echo $opt | cut -d '=' -f 2`
    ;;
    --bindir=*) bindir=`echo $opt | cut -d '=' -f 2`
    ;;
    --mandir=*) mandir=`echo $opt | cut -d '=' -f 2`
    ;;
    --appdir=*) appdir=`echo $opt | cut -d '=' -f 2`
    ;;
    --pixdir=*) pixdir=`echo $opt | cut -d '=' -f 2`
    ;;
    --homedir=*) homedir=`echo $opt | cut -d '=' -f 2`
    ;;
    *) die "Unknown option: $opt (try --help)"
    ;;
    esac
done

if [ "$help" = "yes" ]; then
echo "Usage: $0 [options]"
echo ""
echo "Options:"
echo "  --help                 print this message"
echo "  --cross-prefix=PREFIX  add PREFIX to compile tools [$cross_prefix]"
echo "  --cc=CC                use C compiler [$cc]"
echo "  --make=MAKE            use MAKE processor [$make]"
echo "  --windres=WINDRES      use RC compiler [$windres]"
echo "  --disable-client       do not build graphical client"
echo "  --enable-server        build dedicated server"
echo "  --enable-baseq2        build baseq2 game module"
echo "  --enable-mingw         enable Windows build"
echo "  --enable-dbghelp       enable Windows crash dumps"
echo "  --disable-snddma       disable DMA sound engine"
echo "  --enable-openal        enable OpenAL sound engine"
echo "  --enable-dsound        enable direct sound driver"
echo "  --enable-dinput        enable direct input driver"
echo "  --enable-lirc          enable LIRC input interface"
echo "  --disable-zlib         disable linking with zlib"
echo "  --enable-curl          enable linking with libcurl"
echo "  --disable-tga          disable TGA images support"
echo "  --enable-png           enable PNG images support"
echo "  --enable-jpg           enable JPG images support"
echo "  --disable-md3          disable MD3 models support"
echo "  --disable-ui           disable menu user interface"
echo "  --disable-asm          disable i386 assembly optimizations"
echo "  --enable-anticheat     enable r1ch.net anticheat server interface"
echo "  --disable-mvd-server   disable MVD/GTV server"
echo "  --disable-mvd-client   disable MVD/GTV client"
echo "  --disable-icmp         disable ICMP error handling"
echo "  --enable-fps           enable variable server FPS support"
echo "  --single-user          assume to be installed in home dir"
echo "  --prefix=PREFIX        install in PREFIX [$prefix]"
echo "  --datadir=DIR          path to game data tree [$datadir]"
echo "  --libdir=DIR           path to game libs tree [$libdir]"
echo "  --bindir=DIR           executables directory [$bindir]"
echo "  --mandir=DIR           manpages directory [$mandir]"
echo "  --appdir=DIR           directory for .desktop files [$appdir]"
echo "  --pixdir=DIR           directory for pixmaps [$pixdir]"
echo "  --homedir=DIR          home directory [$homedir]"
echo ""
echo "Object files are built in the directory from which configure is run."
exit 1
fi

# detect host CPU
[ ! -z "$cpu" ] || cpu=`uname -m`
case $cpu in
i?86)
cpu="i386"
[ "$use_asm" = "no" ] || use_asm="yes"
;;
amd64)
cpu="x86_64"
;;
arm*)
cpu="arm"
;;
ppc*)
cpu="powerpc"
;;
mips*)
cpu="mips"
;;
esac

# detect host OS
targetos=`uname -s`
case $targetos in
CYGWIN*)
mingw="yes"
CFLAGS="-mno-cygwin $CFLAGS"
;;
MINGW*)
mingw="yes"
;;
*BSD)
make="gmake"
;;
Linux)
use_dl="yes"
have_endian_h="yes"
[ "$use_icmp" = "no" ] || use_icmp="yes"
[ "$use_dinput" = "yes" ] && use_udev="yes"
;;
esac

if [ "$mingw" = "yes" ]; then
    if [ "$cpu" = "i386" ]; then
        cpu="x86"
        targetos="Win32"
    else
        targetos="Win64"
    fi
    pathsep='\\'
    exesuffix=".exe"
    libsuffix=".dll"
    use_sdl="no"
    gldriver="opengl32"
    aldriver="openal32"
    exported="__attribute__((dllexport))"
    homedir=""
    asmflags="-DUNDERSCORES"
    cfgfile="q2config.cfg"
    [ ! -z "$LDFLAGS" ] || LDFLAGS="-static"
    curl_cflags="-DCURL_STATICLIB"

    if [ "$wince" = "yes" ]; then
        vid_modelist="240x320"
        vid_geometry="240x320"
        targetos="WinCE"
        use_syscon="no"
        use_winsvc="no"
        use_dbghelp="no"
    else
        use_winsvc="yes"
        [ "$use_icmp" = "no" ] || use_icmp="yes"
    fi
    have_endian_h="no"
else
    pathsep="/"
    exported="__attribute__((visibility(\"default\")))"

    if [ "$singleuser" = "yes" ]; then
        prefix=""
        datadir="."
        libdir="."
        bindir=""
        mandir=""
        appdir=""
        pixdir=""
        homedir=""
    elif [ ! -z "$prefix" ]; then
        datadir="$prefix/$datadir"
        libdir="$prefix/$libdir"
        bindir="$prefix/$bindir"
        mandir="$prefix/$mandir"
        appdir="$prefix/$appdir"
        pixdir="$prefix/$pixdir"
    fi

# check if -fvisibility is supported
    tmpc=`mktemp /tmp/q2pro-XXXXXXXXXX`
    tmpo=`mktemp /tmp/q2pro-XXXXXXXXXX`
    echo "$tmpc" | grep -q "^/tmp/q2pro-" || die "mktemp failed"
    echo "$tmpo" | grep -q "^/tmp/q2pro-" || die "mktemp failed"
    echo "int main(){return 0;}" > $tmpc
    $cc -o $tmpo -x c -fvisibility\=hidden $tmpc 2>/dev/null && \
        CFLAGS="-fvisibility=hidden $CFLAGS"
    rm -f $tmpc $tmpo

    LDFLAGS="-Wl,--no-undefined $LDFLAGS"
fi

if [ "$use_ref" = "soft" ]; then
    use_tga="no"
    use_png="no"
    use_jpg="no"
    use_md3="no"
fi

if [ "$use_snddma" = "no" ]; then
    use_dsound="no"
    use_openal="yes" # FIXME
fi

gamelib="game$cpu$libsuffix"

vpath="\$(SRCDIR)/src"
[ "$use_asm" = "yes" ] && vpath="$vpath \$(SRCDIR)/src/i386"

if [ ! -z "$cross_prefix" ]; then
    cc="$cross_prefix-$cc"
    windres="$cross_prefix-$windres"
    strip="$cross_prefix-$strip"
    pngconfig="$cross_prefix-$pngconfig"
fi

# echo configuration info
echo "Build client       $use_client"
echo "Build server       $use_server"
echo "Build baseq2       $use_baseq2"
if [ "$mingw" = "no" ]; then
    if [ "$singleuser" = "yes" ]; then
        echo "Single user        $singleuser"
    else
        echo "Data directory     $datadir"
        echo "Game libraries     $libdir"
        echo "Binaries           $bindir"
        echo "Man pages          $mandir"
        echo "Desktop files      $appdir"
        echo "Pixmap files       $pixdir"
        echo "Home directory     $homedir"
    fi
fi
echo "zlib support       $use_zlib"
if [ "$use_client" = "yes" ]; then
    echo "Refresh type       $use_ref"
    echo "Direct input       $use_dinput"
    echo "Direct sound       $use_dsound"
    echo "DMA sound engine   $use_snddma"
    echo "OAL sound engine   $use_openal"
    [ "$mingw" = "no" ] && echo "LIRC support       $use_lirc"
    echo "CURL support       $use_curl"
    if [ "$use_ref" = "gl" ]; then
        echo "TGA support        $use_tga"
        echo "JPG support        $use_jpg"
        echo "PNG support        $use_png"
        echo "MD3 support        $use_md3"
    fi
    echo "UI support         $use_ui"
fi
[ "$cpu" = "i386" ] && echo "i386 assembly      $use_asm"

CFLAGS="-I\$(OUTDIR) -MF \$*.d -MMD -pipe $CFLAGS -Wno-strict-aliasing"
#LDFLAGS="$LDFLAGS"

# determine what needs to be built
if [ "$use_client" = "yes" ]; then
    targets="$targets q2pro"
else
    use_snddma="no"
    use_openal="no"
    use_dsound="no"
    use_dinput="no"
    use_udev="no"
    use_lirc="no"
    use_curl="no"
    use_tga="no"
    use_png="no"
    use_jpg="no"
    use_md3="no"
    use_sdl="no"
    use_ref="no"
    use_ui="no"
fi

if [ "$use_server" = "yes" ]; then
    targets="$targets q2proded"
else
    use_anticheat="no"
fi

if [ "$use_baseq2" = "yes" ]; then
    targets="$targets baseq2"
fi

if [ "$use_png" = "yes" ]; then
    [ -x `which $pngconfig` ] || die "$pngconfig not found"
    png_ldflags=`$pngconfig --libs`
    png_cflags=`$pngconfig --cflags`
fi

if [ "$use_sdl" = "yes" ]; then
    [ -x `which $sdlconfig` ] || die "$sdlconfig not found"
    sdl_ldflags=`$sdlconfig --libs`
    sdl_cflags=`$sdlconfig --cflags`
fi

# build configuration files
echo "# Generated by configure - do not modify" > $config_mk
echo "// Generated by configure - do not modify" > $config_h

[ "$mingw" = "yes" ] && echo "MINGW=$mingw" >> $config_mk
[ "$wince" = "yes" ] && echo "WINCE=$wince" >> $config_mk
echo "CC=$cc" >> $config_mk
echo "WINDRES=$windres" >> $config_mk
echo "STRIP=$strip" >> $config_mk
echo "MAKE=$make" >> $config_mk
echo "SRCDIR=$sources" >> $config_mk
echo "OUTDIR=$outdir" >> $config_mk
echo "EXESUFFIX=$exesuffix" >> $config_mk
echo "LIBSUFFIX=$libsuffix" >> $config_mk
echo "#define LIBSUFFIX \"$libsuffix\"" >> $config_h
echo "VPATH=$vpath" >> $config_mk
echo "CFLAGS=$CFLAGS" >> $config_mk
echo "LDFLAGS=$LDFLAGS" >> $config_mk
[ "$mingw" = "yes" ] && echo "RESFLAGS=-I\$(OUTDIR) -I\$(SRCDIR)/src" >> $config_mk
[ "$use_asm" = "yes" ] && echo "ASMFLAGS=$asmflags" >> $config_mk

echo "TARGETS=$targets" >> $config_mk

echo "REVISION=$revision" >> $config_mk
echo "#define REVISION $revision" >> $config_h
echo "#define VERSION \"$version\"" >> $config_h

if [ "$mingw" = "no" ]; then
    echo "#define PRIz \"zu\"" >> $config_h

    echo "DATADIR=$datadir" >> $config_mk
    echo "#define DATADIR \"$datadir\"" >> $config_h

    echo "LIBDIR=$libdir" >> $config_mk
    echo "#define LIBDIR \"$libdir\"" >> $config_h

    echo "#define HOMEDIR \"$homedir\"" >> $config_h

    if [ "$singleuser" = "yes" ]; then
        echo "SINGLEUSER=$singleuser" >> $config_mk
    else
        echo "BINDIR=$bindir" >> $config_mk
        echo "MANDIR=$mandir" >> $config_mk
        echo "APPDIR=$appdir" >> $config_mk
        echo "PIXDIR=$pixdir" >> $config_mk
    fi
else
    echo "#define PRIz \"u\"" >> $config_h # FIXME: change this for x86_64
fi

echo "#define EXPORTED $exported" >> $config_h
echo "#define QDECL" >> $config_h

echo "GAMELIB=$gamelib" >> $config_mk
echo "#define GAMELIB \"$gamelib\"" >> $config_h

echo "#define BASEGAME \"$basegame\"" >> $config_h
echo "#define DEFGAME \"$defgame\"" >> $config_h
echo "#define COM_CONFIG_NAME \"$cfgfile\"" >> $config_h
echo "#define COM_DEFAULTCFG_NAME \"$defcfg\"" >> $config_h
echo "#define COM_AUTOEXECCFG_NAME \"$autocfg\"" >> $config_h
echo "#define COM_HISTORYFILE_NAME \"$histfile\"" >> $config_h

if [ "$use_client" = "yes" ]; then
    echo "#define COM_DEMOCACHE_NAME \"$democache\"" >> $config_h
    echo "#define SCREENSHOTS_DIRECTORY \"$screenshots\"" >> $config_h
    echo "#define SCORESHOTS_DIRECTORY \"$scoreshots\"" >> $config_h
fi

if [ "$mingw" = "no" ]; then
    echo "#define SYS_SITECFG_NAME \"$sitecfg\"" >> $config_h
    echo "SITECFG=$sitecfg" >> $config_mk
fi

echo "#define CPUSTRING \"$cpu\"" >> $config_h
echo "#define BUILDSTRING \"$targetos\"" >> $config_h

echo "#define PATH_SEP_CHAR '$pathsep'" >> $config_h
echo "#define PATH_SEP_STRING \"$pathsep\"" >> $config_h

[ "$use_dl" = "yes" ] && echo "USE_DL=yes" >> $config_mk

if [ "$use_zlib" = "yes" ]; then
    echo "USE_ZLIB=yes" >> $config_mk
    echo "ZLIB_LDFLAGS=-lz" >> $config_mk
    echo "ZLIB_CFLAGS=" >> $config_mk
    echo "#define USE_ZLIB 1" >> $config_h
    [ "$wince" = "yes" ] && echo "#define NO_ERRNO_H" >> $config_h
else
    echo "#define USE_ZLIB 0" >> $config_h
fi

if [ "$use_ref" != "no" ]; then
    echo "USE_REF=$use_ref" >> $config_mk
    echo "#define REF_SOFT 1" >> $config_h
    echo "#define REF_GL 2" >> $config_h
    if [ "$use_ref" = "gl" ]; then
        echo "#define DEFAULT_OPENGL_DRIVER \"$gldriver\"" >> $config_h
    fi
    echo "#define USE_LIGHTSTYLES 1" >> $config_h
    echo "#define USE_DLIGHTS 1" >> $config_h
    echo "#define VID_REF \"$use_ref\"" >> $config_h
    echo "#define VID_MODELIST \"$vid_modelist\"" >> $config_h
    echo "#define VID_GEOMETRY \"$vid_geometry\"" >> $config_h
fi

[ "$use_tga" = "yes" ] && echo "#define USE_TGA 1" >> $config_h

if [ "$use_png" = "yes" ]; then
    echo "USE_PNG=yes" >> $config_mk
    echo "PNG_LDFLAGS=$png_ldflags" >> $config_mk
    echo "PNG_CFLAGS=$png_cflags" >> $config_mk
    echo "#define USE_PNG 1" >> $config_h
fi

if [ "$use_jpg" = "yes" ]; then
    echo "USE_JPG=yes" >> $config_mk
    echo "JPG_LDFLAGS=-ljpeg" >> $config_mk
    echo "JPG_CFLAGS=" >> $config_mk
    echo "#define USE_JPG 1" >> $config_h
fi

[ "$use_md3" = "yes" ] && echo "#define USE_MD3 1" >> $config_h

if [ "$use_ui" = "yes" ]; then
    echo "USE_UI=yes" >> $config_mk
    echo "#define USE_UI 1" >> $config_h
fi

if [ "$use_sdl" = "yes" ]; then
    echo "USE_SDL=yes" >> $config_mk
    echo "SDL_LDFLAGS=$sdl_ldflags" >> $config_mk
    echo "SDL_CFLAGS=$sdl_cflags" >> $config_mk
    echo "#define USE_SDL 1" >> $config_h
    if [ "$use_x11" = "yes" ]; then
        echo "USE_X11=yes" >> $config_mk
        echo "X11_LDFLAGS=-lX11" >> $config_mk
        echo "X11_CFLAGS=" >> $config_mk
        echo "#define USE_X11 1" >> $config_h
    fi
fi

if [ "$use_snddma" = "yes" ]; then
    echo "USE_SNDDMA=yes" >> $config_mk
    echo "#define USE_SNDDMA 1" >> $config_h
fi

if [ "$use_openal" = "yes" ]; then
    echo "USE_OPENAL=yes" >> $config_mk
    echo "#define DEFAULT_OPENAL_DRIVER \"$aldriver\"" >> $config_h
    echo "#define USE_OPENAL 1" >> $config_h
fi

if [ "$use_dsound" = "yes" ]; then
    echo "USE_DSOUND=yes" >> $config_mk
    echo "#define USE_DSOUND 1" >> $config_h
fi

if [ "$use_dinput" = "yes" ]; then
    echo "USE_DINPUT=yes" >> $config_mk
    echo "#define USE_DINPUT 1" >> $config_h
fi

if [ "$use_udev" = "yes" ]; then
    echo "USE_UDEV=yes" >> $config_mk
    echo "#define USE_UDEV 1" >> $config_h
fi

if [ "$use_lirc" = "yes" ]; then
    echo "USE_LIRC=yes" >> $config_mk
    echo "#define USE_LIRC 1" >> $config_h
fi

if [ "$use_curl" = "yes" ]; then
    echo "USE_CURL=yes" >> $config_mk
    echo "CURL_LDFLAGS=-lcurl" >> $config_mk
    echo "CURL_CFLAGS=$curl_cflags" >> $config_mk
    echo "#define USE_CURL 1" >> $config_h
fi

if [ "$use_asm" = "yes" ]; then
    echo "USE_ASM=yes" >> $config_mk
    echo "#define USE_ASM 1" >> $config_h
fi

if [ "$use_anticheat" = "yes" ]; then
    echo "USE_AC_SERVER=yes" >> $config_mk
    echo "#define USE_AC_SERVER !USE_CLIENT" >> $config_h
fi

if [ "$use_mvd_server" = "yes" ]; then
    echo "USE_MVD_SERVER=yes" >> $config_mk
    echo "#define USE_MVD_SERVER 1" >> $config_h
fi

if [ "$use_mvd_client" = "yes" ]; then
    echo "USE_MVD_CLIENT=yes" >> $config_mk
    echo "#define USE_MVD_CLIENT 1" >> $config_h
fi

if [ "$use_dbghelp" = "yes" ]; then
    echo "USE_DBGHELP=yes" >> $config_mk
    echo "#define USE_DBGHELP 1" >> $config_h
fi

[ "$use_syscon" = "yes" ] && echo "#define USE_SYSCON 1" >> $config_h
[ "$use_winsvc" = "yes" ] && echo "#define USE_WINSVC !USE_CLIENT" >> $config_h
[ "$use_icmp" = "yes" ] && echo "#define USE_ICMP 1" >> $config_h
[ "$use_fps" = "yes" ] && echo "#define USE_FPS 1" >> $config_h
echo "#define USE_MAPCHECKSUM 1" >> $config_h
echo "#define USE_AUTOREPLY 1" >> $config_h
[ "$have_endian_h" = "yes" ] && echo "#define HAVE_ENDIAN_H 1" >> $config_h
echo "#define HAVE_STDINT_H 1" >> $config_h
echo "#define HAVE_INTTYPES_H 1" >> $config_h

for t in $targets ; do
    mkdir -p .$t ;
done

