#!/bin/sh
##
## fbv configuration script
##

# See TGT's ./configure script for in-depth comments, becouse this
# one is delivered from it...

rm -f config.h

BASENAME=$(basename "$0")

unset prefix
unset infodir
unset mandir
unset srcdir
unset bindir
unset libs
unset ungif
unset jpeg
unset png
unset bmp
unset dfb

help(){
cat << EOF >&2
Usage: ./configure [options]
Options: [defaults in brackets after descriptions]

If a long option shows an argument as mandatory, then it is mandatory
for the equivalent short option also.  Similarly for optional arguments.

General:
  --help	print this message
  
Directory and file names:
  --prefix=PREFIX	install files in PREFIX [/usr/local]
  --bindir=DIR		binary executable in DIR [PREFIX/lib]
  --infodir=DIR		info documentation in DIR [PREFIX/info]
  --mandir=DIR		man documentation in DIR [PREFIX/man]

Features and packages:
  --without-libungif	disable libungif support even if found
  --without-libjpeg	disable libjpeg support even if found
  --without-libpng	disable libpng support even if found
  --without-bmp		disable bmp support
EOF
}

# get options
TEMP=$(getopt -o h \
--long help,\
prefix:,srcdir:,bindir:,\
infodir:,mandir:,\
without-libungif,without-libjpeg,without-libpng,without-bmp \
-n "$BASENAME" -- "$@")

if [ $? != 0 ] ; then help ; exit 1 ; fi
#
eval set -- "$TEMP"

# process options
while true ; do
	case "$1" in
		-h|--help) help ; exit 0 ;;

		--prefix) prefix="$2" ; shift 2 ;;
		--srcdir) srcdir="$2" ; shift 2 ;;
		--bindir) bindir="$2" ; shift 2 ;;
		--infodir) infodir="$2" ; shift 2 ;;
		--mandir) mandir="$2" ; shift 2 ;;
		--without-libungif) ungif="disabled" ; shift ;;
		--without-libjpeg) jpeg="disabled" ; shift ;;      #the default, else I have an error linking
		--without-libpng) png="disabled" ; shift ;;
		--without-bmp) bmp="disabled" ; shift ;;

		--) shift ; break ;;
		*) help ; exit 1 ;;
	esac
done

[ -z "$prefix" ] && prefix="/usr/local"
[ -z "$bindir" ] && bindir="${prefix}/bin"
[ -z "$mandir" ] && mandir="${prefix}/man"
[ -z "$infodir" ] && infodir="${prefix}/info"

cat << EOF | tee ./config.log >Make.conf
prefix	= $prefix
bindir	= $bindir
mandir	= $mandir
infodir	= $infodir
EOF

# tests
rm -f \$\$~test \$\$~test.c
cat > \$\$~test.c << EOF
main()
{
}
EOF
###
echo -n "checking for libungif presence... "
if [ "$ungif" != "disabled" ]; then
xdir="/usr/X11R6"
ungif="no"
echo "libungif check" >>./config.log
echo "  1st:" >>./config.log
cc 2>>./config.log >>./config.log -o \$\$~test \$\$~test.c -lungif
if [ -e \$\$~test ]; then
	libs="-lungif $libs" ; ungif="yes"
else
	echo "  2nd: -lX11 -L$xdir/lib" >>./config.log
	cc 2>>./config.log >>./config.log -o \$\$~test \$\$~test.c -lungif -lX11 -L$xdir/lib
	if [ -e \$\$~test ]; then
	libs="-lungif -lX11 -L$xdir/lib $libs" ; ungif="yes"
	fi
fi
rm -f \$\$~test
fi
echo $ungif
echo "libungif: $ungif" >> ./config.log
###
echo -n "checking for libjpeg presence... "
if [ "$jpeg" != "disabled" ]; then
jpeg="no"
cc 2>>./config.log >>./config.log -o \$\$~test \$\$~test.c -ljpeg
if [ -e \$\$~test ]; then
        echo -e "yes, but I disable it becouse I have a ugly link error, with it"
        echo "perhaps only an 'extern "C" { #include <filename.h>}' is needed" 
	jpeg="disabled"
	#libs="-ljpeg $libs" ; jpeg="yes"    #uncomment this if you want jpeg and it works
else
  echo "$jpeg"
fi
fi
#echo $jpeg
echo "libjpeg: $jpeg" >> ./config.log
###
echo -n "checking for libpng presence... "
if [ "$png" != "disabled" ]; then
png="no"
cc 2>>./config.log >>./config.log -o \$\$~test \$\$~test.c -lpng
if [ -e \$\$~test ]; then
	libs="-lpng $libs" ; png="yes"
fi
fi
echo $png
echo "libpng: $png" >> ./config.log
###
echo -n "building with bmp support... "
if [ "$bmp" != "disabled" ]; then
bmp="yes"
fi
echo $bmp
echo "bmp: $bmp" >> ./config.log
###
rm -f \$\$~test \$\$~test.c
###
echo -n "checking for DEFAULT_FRAMEBUFFER... "
if [ -n "$FRAMEBUFFER" ]; then
	dfb="$FRAMEBUFFER"
else
	dfb="/dev/fb0"
fi
echo $dfb
echo "fb: $dfb" >> ./config.log
#
echo >>Make.conf
echo "LIBS	= $libs" | tee -a ./config.log >>Make.conf
echo "#define DEFAULT_FRAMEBUFFER \"$dfb\"" | tee -a ./config.log >>config.h
[ "$ungif" != "disabled" ] && echo "#define FBV_SUPPORT_GIF" | tee -a ./config.log >>config.h
[ "$jpeg" != "disabled" ] && echo "#define FBV_SUPPORT_JPEG" | tee -a ./config.log >>config.h
[ "$png" != "disabled" ] && echo "#define FBV_SUPPORT_PNG" | tee -a ./config.log >>config.h
[ "$bmp" != "disabled" ] && echo "#define FBV_SUPPORT_BMP" | tee -a ./config.log >>config.h
#echo "installation dir: $bindir"              #not currently used, you can change if you want
#echo "manuals dir: $mandir"                   #not currently used, you can change if you want


if [ "$ungif" == "yes" ];then
  echo "good, you are ok, becouse you can unpack the .gif images of the game!!!wow"
  echo "now make it"
else
  echo -e "bah, you haven't ungif library, you cannot use the .gif images of the game\n"
       "you can change to another image format or boh. :("
fi

exit 0
## EOF
