#!/bin/bash

# only going to make xpdf-tools, not xpdf-reader. So not making all of xpdf
package=xpdf
version=-4.00

progname=$0

source ../cascade-make/lib/cascade-lib.bash GEXT_XPDFTOOLS ../.. $*

# GEXT XDFTOOLS INSTALLED location is this GEXT's GSDLOS subdir
prefix=$GEXTXPDFTOOLS_INSTALLED

# When trying to compile xpdf-tools on a 32 bit linux LSB, we get the error
# "undefined reference to `__sync_add_and_fetch_4'" unless we follow the instructions at
# https://stackoverflow.com/questions/130740/link-error-when-compiling-gcc-atomic-operation-in-32-bit-mode
# and append to CXXFLAGS "-march=i486", which is the earliest architecture for which compilation
# succeeds and the xpdftool binaries produced work (even though our 32 bit LSB is i686).
arch=`uname -m`
# Don't set -march if 64 bit (if uname -m comes back with anything that contains 64)
# https://stackoverflow.com/questions/229551/string-contains-a-substring-in-bash
if [[ $arch = *"64"* ]]; then
    arch=
else
    echo "@@@ 32 bit unix, passing in -march=i486 to avoid certain linking errors"
    arch="-march=i486"
fi

# Use C/CPP/CXX FLAGS and LDFLAGS for further setting up
# libpng and libz for compiling xpdftools
export CFLAGS="$CFLAGS -I$GEXTXPDFTOOLS_INSTALLED/include -I$GEXTXPDFTOOLS_INSTALLED/include/libpng15"
export CPPFLAGS="$CPPFLAGS -I$GEXTXPDFTOOLS_INSTALLED/include -I$GEXTXPDFTOOLS_INSTALLED/include/libpng15"
export CXXFLAGS="$CXXFLAGS -I$GEXTXPDFTOOLS_INSTALLED/include -I$GEXTXPDFTOOLS_INSTALLED/include/libpng15 $arch"
export LDFLAGS="$LDFLAGS -L$GEXTXPDFTOOLS_INSTALLED/lib"

# See section D, 4c of the GS-README of this extension.
if [ "x$GSDLOS" == "xdarwin" ] ; then
    static_flag=-Bstatic
else
    static_flag=-static
fi

opt_run_untar $force_untar $auto_untar $package $version

# patch the original tarball with our custom CMake configure/makefile
if [[ -d "$package$version/xpdf" && -f "gs-CMakeLists.txt" ]]; then
    echo "*******************************************************************"
    echo "Using our custom gs-CMakeLists.txt instead of the one included in the xpdf subfolder of $package$version"
    echo "Renaming gs-CMakeLists.txt to $package$version/xpdf/CMakeLists.txt"
    echo "*******************************************************************"

    cp "gs-CMakeLists.txt" "$package$version/xpdf/CMakeLists.txt"
fi

# Whether force_config is set for this package depends on CMake test
# to see if 'release' directory has been created or not
if [ $auto_config = "1" ] ; then
  if [ ! -d $package$version/release ] ; then
    force_config=1
  fi
fi

echo "Using cmake: "
echo `which cmake`

# The xpdf-tools src code's INSTALL instructions say we need to create a 'build' dir,
# cd into there and then run cmake from there, providing the xpdf-tools src dir
# However, doing so breaks the way cascade-lib.bash runs a CMake compile sequence.
# cascade-lib.bash wants a 'release' directory, so do it the cascade-make way as
# that works.
# TO AVOID building xpdf(reader), turn off finding Qt libs with the cmake flags below
if [ $force_config = "1" ] ; then
  echo "[pushd $package$version]"
  ( cd $package$version ; \
    if [ ! -d release ] ; then \
      echo "Creating 'release' directory" ; \
      mkdir release ; \
    fi ; \
    cd release ; \

    # see xpdf-tools src code's INSTALL file for these options
    if [ ! -f Makefile ] ; then \
      #cmake -D CMAKE_BUILD_TYPE=RELEASE \
      #      -D CMAKE_INSTALL_PREFIX=$prefix ..;
	cmake -DCMAKE_BUILD_TYPE=Release \
	    -DCMAKE_INSTALL_PREFIX=$prefix \
	    -DTIFF_INCLUDE_DIR=$prefix/include \
	    -DJPEG_INCLUDE_DIR=$prefix/include \
	    -DZLIB_LIBRARY=$prefix/lib/libz.a \
	    -DTIFF_LIBRARY=$prefix/lib/libtiff.a \
	    -DPNG_LIBRARY=$prefix/lib/libpng15.a \
	    -DJPEG_LIBRARY=$prefix/lib/libjpeg.a \
	    -DFREETYPE_LIBRARY=$prefix/lib/libfreetype.a \
	    -DCMAKE_DISABLE_FIND_PACKAGE_Qt4=1 \
	    -DCMAKE_DISABLE_FIND_PACKAGE_Qt5Widgets=1 \
	    -DCMAKE_C_FLAGS="$CFLAGS" \
	    -DCMAKE_CXX_FLAGS="$CXXFLAGS" \
	    -DCMAKE_EXE_LINKER_FLAGS="$LDFLAGS" \
 	    -DGSDLFLAG_STATIC="$static_flag" \
	    $GEXT_XPDFTOOLS/packages/$package$version
    fi ; \
    PKG_CONFIG_PATH=$prefix/lib/pkgconfig:${PKG_CONFIG_PATH}; \
    export PKG_CONFIG_PATH; \
   )
  if [ $? != 0 ] ; then
      echo "      Error encountered running *configure* stage of $progname"
      exit 1
  fi
  echo "[popd]"
  opt_run_cmake $compile $package $version "release"
  opt_run_cmake $install $package $version "release" "install"
else
  if [ $auto_config = "1" ] ; then
    echo "Found top-level for ${progname%.*} => no need to run configure/initialize CMake"
  fi
fi


## If compiling statically make sure the above CMake command contains the following
## with these values:
#	    -DZLIB_LIBRARY=$prefix/lib/libz.a \
#	    -DTIFF_LIBRARY=$prefix/lib/libtiff.a \
#	    -DPNG_LIBRARY=$prefix/lib/libpng15.a \
#	    -DJPEG_LIBRARY=$prefix/lib/libjpeg.a \
#	    -DFREETYPE_LIBRARY=$prefix/lib/libfreetype.a \
# and
#	    -DGSDLFLAG_STATIC="$static_flag" \


## For building dynamic xpdf-tools executables, change the above to have the following instead
## The .so for LIBJPEG gets generated for libjpeg version 9c (but not 6b). Correct the .so version number below.
#	    -DZLIB_LIBRARY=$prefix/lib/libz.so.1.2.7 \
#	    -DTIFF_LIBRARY=$prefix/lib/libtiff.so.5.2.2 \
#	    -DPNG_LIBRARY=$prefix/lib/libpng15.so.15.30.0 \
#	    -DJPEG_LIBRARY=$prefix/lib/libjpeg.so.PUT_THE_NUMBER_HERE \
#	    -DFREETYPE_LIBRARY=$prefix/lib/libfreetype.so.6.3.20 \
# (The 3rd line can alternatively be "-DFREETYPE_DIR=$prefix")
# And REMOVE the following line. Don't comment it out, nor set it to "", but remove it:
#	    -DGSDLFLAG_STATIC="$static_flag" \


# If compilation was successful, then we don't need cmake binaries anymore when
# packaging up xpdf-tools. Move them away from the distribution area:

if [ -f "$GEXTXPDFTOOLS_INSTALLED/bin/pdftohtml" ]; then
    echo "**************************************"
    echo "Moving intermediary CMake products out of the distribution area"
    $GEXT_XPDFTOOLS/move-cmake.sh away
    echo "**************************************"
fi
