#!/bin/bash

# --help
# --prefix=/blah/blah

CONFIGCOMMAND="./configure $@"


LAIDOUTVERSION=0.096.1

 # finalprefix is where things eventually get installed. For deb packaging, this is different than prefix
FINALPREFIX=""
PREFIX="/usr/local"

##Use the first if you are using an installed LAXKIT.
##Use the second if laxkit is located in same dir as this file, which is true for releases.
#LAXDIR="/usr/local/include/lax"
LAXDIR="`pwd`/laxkit/lax"

GS="/usr/bin/gs"

LAIDOUT_NOGL=""
USINGGL="yes"
USESQLITE="yes"
ONAMAC="no"
FORCE="no"

EXTRA_CPPFLAGS=""
EXTRA_LDFLAGS=""
EXTRA_PKG=

 ##
 ## Parse options
 ##
while true ; do
  ARG=$1;
  echo "arg: $ARG"

  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"
  echo

  case "$option" in
    -h | --help) 
        echo "Whizbang Laidout configuration script options:" ;
		echo ""
        echo " --help                       Show this stuff"
        echo " --prefix=/where/to/install   Default /usr/local"
        echo " --gs=/path/to/gs             Where is your ghostscript executable? This is quite"
        echo "                                optional, and is currently used only to generate"
        echo "                                previews of EPS files, and to render files it understands"
        echo "                                to image files that Laidout understands."
        echo " --laxkit=/laxkit/headers     Default in releases is ./laxkit, since the Laxkit"
        echo "                                is currently included in release downloads"
		echo " --version=OtherVersionNum    You may force Laidout to compile with a different"
		echo "                                version string. This will make ~/.config/laidout/(version)"
		echo "                                and the executable becomes laidout-(version) and will"
		echo "                                save system stuff in prefix/share/laidout-(version)."
		echo " --language=path/to/langs     The path where translation files should be installed."
		echo "                                This will normally be 'share/locale', and the files"
		echo "                                themselves then get put in prefix/share/locale/*."
		echo " --extra-cppflags             Any extra compile flags, such as location of includes not"
		echo "                                otherwise detected"
		echo " --extra-ldflags              Any extra linking flags, such as  -I/usr/lib64/glib-2.0/include/"
		echo "                                for Fedora includes"
		echo " --pkg-config-path            Any directories to tack onto environment PKG_CONFIG_PATH,"
		echo "                                for configuring convenience. Should be a colon separated list."
        echo " --disable-sqlite             Optional. Used to get font tags in Fontmatrix database (if it exists)"
		echo " --nogl                       Do not compile with gl based features"
		echo " --force                      Try to compile even if libraries are not detected"
        echo ""
		exit 1 ;;

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

	--extra-cppflags)
        if [ -z $optarg ] ; then 
          optarg=$2
          shift
        fi
		EXTRA_CPPFLAGS=$optarg
		shift ;;

	--extra-ldflags)
        if [ -z $optarg ] ; then 
          optarg=$2
          shift
        fi
		EXTRA_LDFLAGS=$optarg
		shift ;;

	--pkg-config-path)
        if [ -z $optarg ] ; then 
          optarg=$2
          shift
        fi
		EXTRA_PKG=$optarg
		echo "extra_pkg = $EXTRA_PKG"
		shift ;;

	--disable-sqlite)
		unset USESQLITE
		shift;;

    --finalprefix)  
        if [ -z $optarg ] ; then 
          optarg=$2
          shift
        fi
		FINALPREFIX="$optarg"
        #echo prefix $optarg ;
        shift ;;
    -p | --prefix)  
        if [ -z $optarg ] ; then 
          optarg=$2
          shift
        fi
		PREFIX="$optarg"
        #echo prefix $optarg ;
        shift ;;
    --version | -v)  
        if [ -z $optarg ] ; then 
          optarg=$2
          shift
        fi
		LAIDOUTVERSION="$optarg"
        #echo version $optarg ;
        shift ;;
    --gs) 
        if [ -z $optarg ] ; then 
          optarg=$2
          shift
        fi
		GS="$optarg"
		echo gs $optarg; 
		shift ;;
    --laxkit)
        if [ -z $optarg ] ; then 
          optarg=$2
          shift
        fi
		LAXDIR="$optarg"
		#echo laxkit $optarg; 
		shift ;;
    --language)
        if [ -z $optarg ] ; then 
          optarg=$2
          shift
        fi
		LANGUAGE_PATH="$optarg"
		echo language path $optarg; 
		shift ;;
    --nogl)
		LAIDOUT_NOGL="#define LAIDOUT_NOGL"
		USINGGL="no"
		shift ;;
    --onamac)
		ONAMAC="yes"
		shift ;;
    *) echo "Unrecognized option $option $optarg" ; break ;;
  esac
done

if [ xxx$FINALPREFIX == xxx ] ; then FINALPREFIX=$PREFIX; fi


if [ xxx$EXTRA_PKG != xxx ] ; then
#	if [ xxx$PKG_CONFIG_PATH != xxx ] ; then
#		export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$EXTRA_PKG;
#	else 
		export PKG_CONFIG_PATH=$EXTRA_PKG;
#	fi
fi


# do check for required libraries:

 ## todo:
 ##   to make this actually useful, sometimes need to do a version check, or override the default link.
 ##   so, each lib needs [libname, minimum version, linking text]
 ##
 ## The stuff in NEED can be checked with pkg-config. Not all libraries
 ## can be checked this way! (notably cups, apparently)
NEED='x11 xext freetype2 libssl imlib2 cairo harfbuzz'
NEEDGL='ftgl GraphicsMagick++'
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 may need to install the development package of $NAME."; 
        NUM="";
    else 
        echo "ok"; 
    fi
done
#echo NUM=$NUM

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

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 optional $NAME. Install that to get access to Fontmatrix tags."; 
		fi
    else 
        echo "ok"; 
    fi
done

#EXTRAINCLUDEPATHS=("/usr/local/include/" "/usr/include/")

 ## readline doesn't seem to have a pkg-config entry, so do this hackier check
echo -n Checking for readline......;
#---attempt 3, check for existence of cups-config program:
if [ ! -e /usr/local/include/readline/readline.h -a \
	 ! -e /usr/include/readline/readline.h  ] ; then 
    echo "Can't find libreadline headers."; 
    NUM="";
else echo "ok";
fi

 ## cups doesn't seem to have a pkg-config entry, so do this hackier check
echo -n Checking for cups......;
#---attempt 3, check for existence of cups-config program:
if [ ! `builtin type -p cups-config` ] ; then 
    echo "Can't find Cups headers."; 
    NUM="";

else echo "ok"; 
fi

echo



if [ "$NUM" != "1" -a $FORCE=="no" ]; then
    echo "Too many problems to proceed!"
	echo "Use --force to override. You may have to adjust CPPFLAGS and LDFLAGS in Makefiles,"
	echo "or pass in includes with --extra-cppflags."
	echo
	echo "On debian based systems, you might try installing all dependencies with this:"
	echo "    apt-get install g++ pkg-config libpng12-dev libreadline-dev libx11-dev libxext-dev libxi-dev libxft-dev libcups2-dev libimlib2-dev libfontconfig-dev libfreetype6-dev libssl-dev xutils-dev libgraphicsmagick++1-dev mesa-common-dev libglu1-mesa-dev libftgl-dev libcairo2-dev libharfbuzz-dev"
	exit 1
fi

if [ x$LANGUAGE_PATH == x ]; then
	LANGUAGE_PATH=share/locale
fi


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

echo " // ---- THIS FILE IS AUTOMATICALLY GENERATED -----" > src/version.h
echo " // ----          BY ../configure             -----" >> src/version.h
echo "#ifndef VERSION_H" >> src/version.h
echo "#define VERSION_H" >> src/version.h
echo "" >> src/version.h
echo "#define LAIDOUT_VERSION  \"$LAIDOUTVERSION\"" >> src/version.h
echo "" >> src/version.h
echo "#endif" >> src/version.h
echo "" >> src/version.h



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

echo " // ---- THIS FILE IS AUTOMATICALLY GENERATED -----" > src/configured.h
echo " // ----          BY ../configure             -----" >> src/configured.h
echo "#ifndef CONFIGURED_H" >> src/configured.h
echo "#define CONFIGURED_H" >> src/configured.h
echo "" >> src/configured.h
echo "#define INSTALL_PREFIX   \"$FINALPREFIX\"" >> src/configured.h
echo "#define BIN_DIRECTORY    \"$FINALPREFIX/bin\"" >> src/configured.h
echo "#define BIN_PATH         \"$FINALPREFIX/bin/laidout\"" >> src/configured.h
echo "#define SHARED_DIRECTORY \"$FINALPREFIX/share/laidout/$LAIDOUTVERSION/\"" >> src/configured.h
echo "#define ICON_DIRECTORY   \"$FINALPREFIX/share/laidout/$LAIDOUTVERSION/icons\"" >> src/configured.h
echo "#define LANGUAGE_PATH    \"$FINALPREFIX/$LANGUAGE_PATH\"" >> src/configured.h
echo "#define GHOSTSCRIPT_BIN  \"$GS\"" >> src/configured.h 
echo $LAIDOUT_NOGL >> src/configured.h
echo "" >> src/configured.h
echo "#endif" >> src/configured.h
echo "" >> src/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 "LAIDOUTVERSION=$LAIDOUTVERSION"       >> Makefile-toinclude
echo "LAXDIR=$LAXDIR"                       >> Makefile-toinclude
echo "LAXIDIR=$LAXDIR/interfaces"           >> Makefile-toinclude
echo "LANGUAGE_PATH=$PREFIX/$LANGUAGE_PATH" >> Makefile-toinclude
echo "POLYPTYCHBASEDIR="`pwd`"/src"         >> Makefile-toinclude
echo "LAIDOUT_USING_GL=$USINGGL"            >> Makefile-toinclude
if [ x$USESQLITE  != x ] ; then echo "LAIDOUT_USES_SQLITE=yes" >> Makefile-toinclude; fi
echo "EXTRA_CPPFLAGS=$EXTRA_CPPFLAGS"       >> Makefile-toinclude
echo "EXTRA_LDFLAGS=$EXTRA_LDFLAGS"         >> Makefile-toinclude
echo "EXTRA_PKG=$EXTRA_PKG"                 >> Makefile-toinclude
echo ""                                     >> Makefile-toinclude
echo 'ifeq ($(PKG_CONFIG_PATH)xxx, xxx)'    >> Makefile-toinclude
echo 'export PKG_CONFIG_PATH=$(EXTRA_PKG)'  >> Makefile-toinclude
echo "else"                                 >> Makefile-toinclude
echo 'PKG_CONFIG_PATH+=:$(EXTRA_PKG)'       >> Makefile-toinclude
echo 'export PKG_CONFIG_PATH'               >> Makefile-toinclude
echo "endif"                                >> Makefile-toinclude

echo "" >> Makefile-toinclude



if [ x$USESQLITE  != x ] ; then USESQLITE="yes"; else USESQLITE="no"; fi

 ##
 ## generate config.log
 ##
echo "$CONFIGCOMMAND"                              > config.log
echo                                              >> config.log
echo "       Configuration Summary"               >> config.log
echo "      generated by ./configure"             >> config.log
echo "----`date`---"                              >> config.log
echo "-----------------------------------"        >> config.log
echo " Laidout version:  $LAIDOUTVERSION"         >> config.log
echo "    Install here:  $PREFIX"                 >> config.log
echo "   Language Path:  $PREFIX/$LANGUAGE_PATH"  >> config.log
echo "  Laxkit headers:  $LAXDIR"                 >> config.log
echo "     Ghostscript:  $GS"                     >> config.log
echo "       Enable GL:  $USINGGL"                >> config.log
echo "          Sqlite:  $USESQLITE"              >> config.log
echo "  Extra cppflags:  $EXTRA_CPPFLAGS"         >> config.log
echo "   Extra ldflags:  $EXTRA_LDFLAGS"          >> config.log
echo "  Pkgconfig path:  $EXTRA_PKG"              >> config.log


echo 
echo "       Configuration Summary"
echo "----------------------------------"
echo " Laidout version:  $LAIDOUTVERSION"
echo "    Install here:  $PREFIX"
echo "   Language Path:  $PREFIX/$LANGUAGE_PATH"
echo "  Laxkit headers:  $LAXDIR"
echo "     Ghostscript:  $GS"
echo "       Enable GL:  $USINGGL"
echo "          Sqlite:  $USESQLITE"             
echo "  Extra cppflags:  $EXTRA_CPPFLAGS"
echo "   Extra ldflags:  $EXTRA_LDFLAGS"
echo "  Pkgconfig path:  $EXTRA_PKG"
echo
echo "If compiling from git, please follow \"COMPILING FROM SOURCE\" in README.md.";
echo
echo "Otherwise, now type make to build.";

