#!/bin/sh

#
# configure
#
# This script should be started before 'make'.
# It should detect your personal configuration.
#



rm -f ./conftest ./Makefile
printf "Searching gcc ... ";
GCCPATH=`which gcc`
if test -z "$GCCPATH" ; then
   printf "FAILED" ;
fi
printf "%s\n" $GCCPATH
gcc -Wall -Wstrict-prototypes -o conftest conftest.c
./conftest

case $? in
0)   printf "gcc: test FAILED.\n";exit 1;;
1)   ENDIANSTR="";;
2)   ENDIANSTR="-DUSE_LITTLE_ENDIAN";;
esac

OS=`uname`
case $OS in
*BSD)  printf "Using -pthread.\n"; PTHREAD="-pthread";;
    *) printf "Using -lpthread.\n"; PTHREAD="-lpthread";;
esac

printf "Searching special header files ...\n"
printf "fb.h: "
if test -r /usr/include/linux/fb.h ; then
    printf "found.\n";
    if test ! -e "/dev/fb0" ; then
    printf "Warning: /dev/fb0 doesn't exist.\n";
    fi
    FBSTR="-DFRAMEBUFFER"
else
    printf "not found.\n";
    unset FBSTR;
fi

printf "joystick.h: "
if test -r /usr/include/linux/joystick.h || test -r /usr/include/sys/joystick.h ; then
    printf "found.\n";
    if test ! -e "/dev/js0" ; then
	if test ! -e "/dev/joy0"; then
	    printf "Warning: /dev/js0 and /dev/joy0 don't exist.\n";
	    printf "         Setting device to /dev/js0.\n";
	    JOYSTR="-DJOYSTICK"
	else
	    JOYSTR="-DBSD_JOYSTICK"
	fi
    else
	JOYSTR="-DJOYSTICK"
    fi
    printf "Compiling joy-button configuration ... "
    JOYCONF="conf"
    JOYINST="install -m 755 -s .\/cingb_conf \/usr\/local\/bin\/cingb_conf"
    printf "ok\n";
else
    printf "not found.\n";
    unset JOYSTR;
fi

printf "soundcard.h: "
if test -r /usr/include/linux/soundcard.h || test -r /usr/include/sys/soundcard.h ; then
    printf "found.\n";
    SNDSTR="-DSOUND";
else
    printf "not found.\n";
    printf "audioio.h: "
    if test -r /usr/include/sys/audioio.h ; then
	printf "found.\n";
	SNDSTR="-DSOUND -DAUDIO";
    else
	printf "not found.\n";
	unset SNDSTR;
    fi
fi


printf "XShm.h: "
if test -r /usr/X11R6/include/X11/extensions/XShm.h ; then
    printf "found.\n";
    SHMSTR="-DMIT_SHM";
    LIBSHM="-lXext";
else
    printf "not found.\n";
    unset SHMSTR;
    unset LIBSHM;
fi

printf "Checking libraries  ...\n"
printf "libsocket ... "
if test -r /lib/libsocket.a || test -r /usr/lib/libsocket.a ; then
    printf "found.\n";
    LIBSOCK="-lsocket";
else
    printf "not found.\n"
    unset LIBSOCK;
fi

printf "libnsl ... "
if test -r /lib/libnsl.a || test -r /usr/lib/libnsl.a ; then
    printf "found.\n";
    LIBNSL="-lnsl";
else
    printf "not found.\n";
    unset LIBNSL;
fi


printf "Creating Makefile ... "
sed "s/%%%endian%%%/$ENDIANSTR/;s/%%%joystick%%%/$JOYSTR/;s/%%%framebuffer%%%/$FBSTR/;s/%%%sound%%%/$SNDSTR/;s/%%%libnsl%%%/$LIBNSL/;s/%%%libsocket%%%/$LIBSOCK/;s/%%%mitshm%%%/$SHMSTR/;s/%%%libext%%%/$LIBSHM/;s/%%%pthread%%%/$PTHREAD/;s/%%%joyconf%%%/$JOYCONF/;s/%%%joyinst%%%/$JOYINST/" Makefile.templ > Makefile

if test "$?"="0"; then
	printf "ok\n";
else
        printf "FAILED\n";
	exit 1;
fi

if test -n "$JOYPATH" ; then
    printf "\n................................................\n";
    printf "Now run 'make'.\n";
fi
