#!/bin/sh

set -e

prefix=/usr/local
mandir=\$\{PREFIX\}/share/man
bindir=\$\{PREFIX\}/bin
docdir=\$\{PREFIX\}/share/doc/stripclub
browser=mozilla-firefox
skiptests=0

usage="\
Usage: configure [--(prefix|bindir|mandir|docdir)=DIR] [--browser=<browser command>] [--skip-tests]"

while test $# -gt 0; do
  case "$1" in
  -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
  *) optarg= ;;
  esac

  case $1 in
    --prefix=*)
      prefix=$optarg
      ;;
    --bindir=*)
      bindir=$optarg
      ;;
    --mandir=*)
      mandir=$optarg
      ;;
    --docdir=*)
      docdir=$optarg
      ;;
    --browser=*)
      browser=$optarg
      ;;
    --skip-tests)
      skiptests=1
      ;;
    *)
      echo "${usage}"
      exit 1
      ;;
  esac
  shift
done

if [ $skiptests != "1" ] ; then \
	echo -n "Checking for FLTK..."
	if !(c++ -o /dev/null tests/fltktest.cpp `fltk-config --ldflags --use-images` ) ; \
		then echo "Couldn't compile fltktest.cpp, is FLTK 1.1.x installed?" >&2 ; exit 1 ; \
		else echo "success" ; \
	fi
	echo -n "Checking for fluid..."
	if [ ! -x `command -v fluid` ] ; \
		then echo "Couldn't find fluid, is FLTK 1.1.x installed?" >&2 ; exit 1 ; \
		else echo "success" ; \
	fi
	echo -n "Checking for PCRE..."
	if !(cc -o /dev/null tests/pcretest.c `pcre-config --cflags --libs` ) ; \
		then echo "Couldn't compile pcretest.c, is PCRE 4.5 or greater installed?" >&2 ; exit 1 ; \
		else echo "success" ; \
	fi
	echo -n "Checking for libjpeg..."
	if !(cc -o /dev/null tests/jpegtest.c -ljpeg) ; \
		then echo "Couldn't compile jpegtest.c, is libjpeg installed?" >&2 ; exit 1 ; \
		else echo "success" ; \
	fi
	echo -n "Checking for libpng..."
	if !(cc -o /dev/null tests/pngtest.c -lpng) ; \
		then echo "Couldn't compile pngtest.c, is libpng installed?" >&2 ; exit 1 ; \
		else echo "success" ; \
	fi
	echo -n "Checking for libXpm..."
	if !(cc -o /dev/null tests/xpmtest.c -L/usr/X11R6/lib -lXpm) ; \
		then echo "Couldn't compile xpmtest.c, is libXpm installed?" >&2 ; echo "This may not be an error depending on your system" ; \
		else echo "success" ; \
	fi
fi
	
echo "Writing Makefile.inc..."
sed \
-e "s%@prefix@%$prefix%" -e "s%@mandir@%$mandir%" \
-e "s%@bindir@%$bindir%" -e "s%@docdir@%$docdir%" \
-e "s%@browser@%$browser%" \
Makefile.inc.in >Makefile.inc

echo "Success! Run 'make' to compile."
