#!/bin/bash
#
##
## Laxkit configure
##
#
#
# --help
# --prefix=/blah/blah


LAXKITVERSION=0.0.8.1
LAXKITCONFIGDIRBASE="~/.laxkit"
#LAXKITCONFIGDIR="$LAXKITCONFIGDIRBASE/$LAXKITVERSION"
PREFIX="/usr/local"

#---capabilities
EXTRAS=
OPTIONAL=
OPTIONALLIBS=

 #USEXLIB to compile functions for the xlib rendering backend
USEXLIB="yes"
USEIMLIB="yes"
USECAIRO="yes"
USEXINPUT2="yes"
USEGL="yes"
USESQLITE="yes"

ONAMAC="no"
FORCE="no"

 #x11, or qt
PLATFORM="x11"

LAXKITBACKEND="cairo"


if [ ! -e /usr/include/X11/extensions/XInput2.h ] ; then unset USEXINPUT2; fi


 ##
 ## Parse options
 ##
while true ; do
  ARG=$1;
  if [ x$ARG == x ] ; then break; fi
  
  option=`expr "x$ARG" : 'x\([^=]*\)=.*'`;
  if [ x$option == x ] ; then
    option="$ARG"; 
  fi
  
  optarg=`expr "x$ARG" : 'x[^=]*=\(.*\)'`;
  #echo "option=$option,   optarg=$optarg"

  case "$option" in
    -h | --help) 
        echo "Whizbang Laxkit configuration script options."
#		echo "Please note that at least one of Imlib2 or Cairo must be used. Both"
#		echo "are built by default."
		echo ""
        echo " --help                       Show this stuff"
        echo " --prefix=/where/to/install   Default /usr/local"
        echo " --configdir=/path/to/config  Default ~/.laxkit/(version)"
		echo " --platform=x11               Only x11 is implemented. Someday also headless or qt."
#		echo " --disable-gl                 Do not build opengl components. Default is to built them."
        echo " --disable-imlib              Do not build Imlib2 components. Default is to built them."
        echo " --disable-cairo              Do not build cairo components. Default is to built them."
        echo " --disable-xinput2            Use this option if you do not have at least Xorg 7.5 for MPX."
        echo " --disable-sqlite             Optional. Used to get font tags in Fontmatrix database (if it exists)"
		echo " --backend=cairo              Use xlib or cairo. cairo is default. xlib is deprecated."
#       echo " --onamac                     Include this if you are compiling on a Mac."
		echo " --force                      Try to compile even when libraries are not detected by ./configure"
		echo " --version=OtherVersionNum    You may force Laxkit to compile with a different"
		echo "                                version string."
        echo ""
		exit 1 ;;

    --force)
		FORCE="yes"
		shift ;;

    --onamac)
		ONAMAC="yes"
		shift ;;

	--disable-gl)
		unset USEGL
		shift;;

	--disable-imlib)
		unset USEIMLIB
		shift;;

	--disable-cairo)
		unset USECAIRO
		shift;;

	--disable-xinput2)
		unset USEXINPUT2
		shift;;

	--disable-sqlite)
		unset USESQLITE
		shift;;

    -p | --prefix)  
        if [ -z $optarg ] ; then 
          optarg=$2
          shift
        fi
		PREFIX="$optarg"
        #echo prefix $optarg ;
        shift ;;

	--configdir)
        if [ -z $optarg ] ; then 
          optarg=$2
          shift
        fi
		LAXKITCONFIGDIR="$optarg"
		shift ;;

    --backend | -b)  
        if [ -z $optarg ] ; then 
          optarg=$2
          shift
        fi
		LAXKITBACKEND="$optarg"
        #echo version $optarg ;
        shift ;;

    --platform | -P)  
        if [ -z $optarg ] ; then 
          optarg=$2
          shift
        fi
		PLATFORM="$optarg"
        #echo version $optarg ;
        shift ;;

    --version | -v)  
        if [ -z $optarg ] ; then 
          optarg=$2
          shift
        fi
		LAXKITVERSION="$optarg"
        #echo version $optarg ;
        shift ;;

    *) echo "Unrecognized option $option $optarg" ; exit 1 ;;
  esac
done


#if [ x$USEGL    != x ] ;   then EXTRAS="$EXTRAS -DLAX_USES_GL";   fi
#if [ x$USEIMLIB != x ] ;   then EXTRAS="$EXTRAS -DLAX_USES_IMLIB";   fi
#if [ x$USECAIRO != x ] ;   then EXTRAS="$EXTRAS -DLAX_USES_CAIRO";   fi
#if [ x$USEXINPUT2 != x ] ; then EXTRAS="$EXTRAS -DLAX_USES_XINPUT2"; fi

if [ x$USEIMLIB$USECAIRO == x ] ; 
	then echo "You must use at least one of Imlib or Cairo."; 
	exit 1;
fi


if [ x$LAXKITCONFIGDIR == x ] ; then
	LAXKITCONFIGDIR="$LAXKITCONFIGDIRBASE/$LAXKITVERSION"
fi

LAXDIR="$PREFIX/include/lax"

# do check for required libraries:
#   NEEDED      libX11.so.6
#   NEEDED      libpng12.so.0
#   NEEDED      libcups.so.2
#   NEEDED      libImlib2.so.1
#   NEEDED      libfreetype.so.6
#   NEEDED      libz.so.1
#   NEEDED      libXext.so.6

 ## The stuff in NEED can be checked with pkg-config. Not all libraries
 ## can be checked this way! (notably cups, apparently)
#NEED='x11 cupsys freetype2 imlib2 xext'
NEED='x11  freetype2  harfbuzz'
if [ x$USEIMLIB != x ] ; then NEED="$NEED imlib2"; fi
if [ x$USECAIRO != x ] ; then NEED="$NEED cairo"; fi
NUM='1'

for NAME in $NEED; do
    echo -n Checking for $NAME......;
    if [ "`pkg-config --exists --print-errors --errors-to-stdout $NAME`"xxx != "xxx" ] ; then 
        echo "You need to install the development package of $NAME."; 
        NUM="";
    else 
        echo "ok"; 
    fi
done
#echo NUM=$NUM

OPTIONAL='sqlite3'
for NAME in $OPTIONAL; do
    echo -n Checking for $NAME......;
    if [ "`pkg-config --exists --print-errors --errors-to-stdout $NAME`"xxx != "xxx" ] ; then 
		if [ $NAME == 'sqlite3' ] ; then
	        echo "Didn't find $NAME. Install that to get access to Fontmatrix tags."; 
		else 
			echo "Couldn't find optional $NAME. Skipping.";
		fi
    else 
		OPTIONALLIBS="$OPTIONALLIBS $NAME"
        echo "ok"; 
    fi
done

 ## cups doesn't seem to have a pkg-config entry, so do this hackier check
echo -n Checking for cups......;
if [ ! -e /usr/include/cups ] ; then 
    echo "You need to install the development package of Cups."; 
    NUM="";
else echo "ok"; 
fi

echo



if [ "$NUM" != "1" -a $FORCE=="no" ]; then
    echo "Too many problems to proceed! Use --force to override."
	exit 1
fi


#if [[ $PLATFORM != "x11" && $PLATFORM != "mac" && $PLATFORM != "windows" ]] ;
if [ $PLATFORM != "x11" -a $PLATFORM != "headless" -a $PLATFORM != "qt" ] ;
	then PLATFORM="x11";
fi


 ##
 ## generate lax/version.h
 ## 
echo "Generating lax/version.h..."

echo " // ---- THIS FILE IS AUTOMATICALLY GENERATED -----" > lax/version.h
echo " // ----          BY ../configure             -----" >> lax/version.h
echo "#ifndef _LAX_VERSION_H" >> lax/version.h
echo "#define _LAX_VERSION_H" >> lax/version.h
echo "" >> lax/version.h
echo "#define LAXKIT_VERSION  \"$LAXKITVERSION\"" >> lax/version.h
echo "" >> lax/version.h
echo "#endif" >> lax/version.h
echo "" >> lax/version.h



 ##
 ## generate lax/configured.h by putting install_prefix where the code can access it!
 ##
echo "Generating lax/configured.h..."

echo " // ---- THIS FILE IS AUTOMATICALLY GENERATED -----" > lax/configured.h
echo " // ----          BY ../configure             -----" >> lax/configured.h
echo "#ifndef _LAX_CONFIGURED_H" >> lax/configured.h
echo "#define _LAX_CONFIGURED_H" >> lax/configured.h
echo "" >> lax/configured.h
echo "#define LAX_INSTALL_PREFIX   \"$PREFIX\"" >> lax/configured.h
echo "#define LAX_SHARED_DIRECTORY \"$PREFIX/share/laxkit/$LAXKITVERSION/\"" >> lax/configured.h
echo "#define LAX_CONFIG_DIRECTORY \"$LAXKITCONFIGDIR\"" >> lax/configured.h
echo "" >> lax/configured.h

if [ $PLATFORM == "x11"      ] ; then echo "#define _LAX_PLATFORM_XLIB"     >> lax/configured.h ; fi
if [ $PLATFORM == "qt"       ] ; then echo "#define _LAX_PLATFORM_QT"       >> lax/configured.h ; fi
if [ $PLATFORM == "headless" ] ; then echo "#define _LAX_PLATFORM_HEADLESS" >> lax/configured.h ; fi

echo "#define LAX_DEFAULT_BACKEND \"$LAXKITBACKEND\"" >> lax/configured.h
if [ x$USEGL      != x ] ; then echo "#define LAX_USES_GL" >> lax/configured.h; fi
if [ x$USEXLIB    != x ] ; then echo "#define LAX_USES_XLIB" >> lax/configured.h; fi
if [ x$USEXINPUT2 != x ] ; then echo "#define LAX_USES_XINPUT2" >> lax/configured.h; fi
if [ x$USEIMLIB   != x ] ; then echo "#define LAX_USES_IMLIB" >> lax/configured.h; fi
if [ x$USECAIRO   != x ] ; then echo "#define LAX_USES_CAIRO" >> lax/configured.h; fi
if [ x$USESQLITE  != x ] ; then echo "#define LAX_USES_SQLITE" >> lax/configured.h; fi
echo "" >> lax/configured.h
echo "#endif" >> lax/configured.h
echo "" >> lax/configured.h


 ##
 ## generate Makefile-toinclude
 ##
echo "Generating ./Makefile-toinclude..."

echo " # ---- THIS FILE IS AUTOMATICALLY GENERATED -----" > Makefile-toinclude
echo " # ----          BY ./configure             -----" >> Makefile-toinclude
echo "" >> Makefile-toinclude
echo "PREFIX=$PREFIX" >> Makefile-toinclude
echo "LAXKITVERSION=$LAXKITVERSION" >> Makefile-toinclude
echo "EXTRAS=$EXTRAS" >> Makefile-toinclude
echo "OPTIONALLIBS=$OPTIONALLIBS" >> Makefile-toinclude
if [ x$USESQLITE  != x ] ; then echo "LAX_USES_SQLITE=yes" >> Makefile-toinclude; fi
echo "" >> Makefile-toinclude

 ##
 ## write out config.log
 ##
echo \
"       Configuration Summary
" `date` "  
----------------------------------
  Laxkit version:  $LAXKITVERSION
  Lax Config dir:  $LAXKITCONFIGDIR
    Install here:  $PREFIX
" >> config.log
if [ x$USEXINPUT2 != x ] ; 
then echo "     Use XInput2:  yes" >> config.log; 
else echo "     Use XInput2:  no"  >> config.log; 
fi
if [ x$USEIMLIB != x ] ; 
then echo "      Use Imlib2:  yes" >> config.log; 
else echo "      Use Imlib2:  no"  >> config.log; 
fi
if [ x$USECAIRO != x ] ; 
then echo "       Use Cairo:  yes" >> config.log; 
else echo "       Use Cairo:  no"  >> config.log; 
fi
if [ x$USEGL != x ] ; 
then echo "          Use GL:  yes" >> config.log; 
else echo "          Use GL:  no"  >> config.log; 
fi
if [ x$USESQLITE != x ] ; 
then echo "      Use Sqlite:  yes" >> config.log; 
else echo "      Use Sqlite:  no"  >> config.log; 
fi
echo " Default backend:  $LAXKITBACKEND" >> config.log
echo "        Platform:  $PLATFORM" >> config.log



echo 
echo "       Configuration Summary"
echo "  " `date`
echo "----------------------------------"
echo "  Laxkit version:  $LAXKITVERSION"
echo "  Lax Config dir:  $LAXKITCONFIGDIR"
echo "    Install here:  $PREFIX"
if [ x$USEXINPUT2 != x ] ; 
then echo "     Use XInput2:  yes"; 
else echo "     Use XInput2:  no"; 
fi
if [ x$USEIMLIB != x ] ; 
then echo "      Use Imlib2:  yes"; 
else echo "      Use Imlib2:  no"; 
fi
if [ x$USECAIRO != x ] ; 
then echo "       Use Cairo:  yes"; 
else echo "       Use Cairo:  no"; 
fi
if [ x$USEGL != x ] ; 
then echo "          Use GL:  yes"; 
else echo "          Use GL:  no"; 
fi
if [ x$USESQLITE != x ] ; 
then echo "      Use Sqlite:  yes"; 
else echo "      Use Sqlite:  no" ; 
fi
echo " Default backend:  $LAXKITBACKEND"
echo "        Platform:  $PLATFORM"
echo
echo "Now type make to build.";

