#!/bin/bash

package=jpeg
#version=-6b
# version 9c is from 2018, see www.ijg.org. Version 6b is from 2008
version=-9c

# renamed the downloaded jpegsrc.v9c.tar.gz to jpeg$version.tar.gz
# similarly for the earlier 6b version.

progname=$0

(cd .. ; \
 source setup.bash silent; )

# This syntax is from the realistic books extension:
#source $GEXT_XPDFTOOLS/cascade-make/lib/cascade-lib.bash
source ../cascade-make/lib/cascade-lib.bash GEXT_XPDFTOOLS ../.. $*

prefix=$GEXTXPDFTOOLS_INSTALLED

# Unpack the libjpeg tarball
opt_run_untar $force_untar $auto_untar $package $version

# Patch the unpacked tarball with our fixed up config.sub to get --enable-static to work
# GS: added x86_64-* to config.sub's $basic_machine list,
# since configuring produced the following error without it
#   checking host system type... Invalid configuration `x86_64-unknown-linux-gnu': machine `x86_64-unknown' not recognized
# in which case running make on the libjpeg package then failed with the error:
#   ./libtool --mode=compile gcc -I/home/greenstone/gs3-svn-26Mar2018/gs2build/ext/xpdf-tools/linux/include -fPIC  -I/home/greenstone/gs3-svn-26Mar2018/gs2build/ext/xpdf-tools/linux/include  -I. -c ./jcapimin.c
#   make: ./libtool: Command not found
#   make: *** [jcapimin.lo] Error 127
#        Error encountered running *make * stage of ./CASCADE-MAKE/LIBJPEG.sh
# The solution is taken from https://sourceforge.net/p/libjpeg/bugs/12/
##if [[ -d "$package$version" && -f "gs-libjpeg-config.sub" ]]; then
##    echo "*******************************************************************"
##    echo "Using our custom gs-libjpeg-config.sub instead of the one included in $package$version"
##    echo "Copying across gs-libjpeg-config.sub as $package$version/config.sub"
##    echo "*******************************************************************"
##    echo `pwd`
##    cp "gs-libjpeg-config.sub" "$package$version/config.sub"
##fi

# This also seems to make only the static libjpeg.a
#opt_run_configure $force_config $auto_config $package $version "$prefix"
# Similarly, despite turning on --enable-shared as well as explained at 
# http://www.linuxfromscratch.org/blfs/view/6.3/general/libjpeg.html
# only the libjpeg.a is produced with this command
opt_run_configure $force_config $auto_config $package $version "$prefix" --disable-shared
#opt_run_configure $force_config $auto_config $package $version "$prefix" \
#    --enable-static --enable-shared
#         --host=x86_64-linux-gnu --build=x86_64-linux-gnu --target=x86_64-linux-gnu --enable-static --enable-shared
# The following didn't fix the configuration and subsequent libtool error described above
# and with or without it, there's still reference to host=x86_64-unknown(-unknown)-linux-gnu in configure output:
#         --host=x86_64-linux-gnu --build=x86_64-linux-gnu --target=x86_64-linux-gnu --disable-shared --enable-static

opt_run_make $compile   $package $version
#opt_run_make $install   $package $version "install-lib"
# Contrary to the install.txt in version jpeg version 9c, install-lib is an unrecognised make command
# I found there was an install-libLTLIBRARIES target in the Makefile, however. And that seems to at least copy over libjpeg.a
opt_run_make $install   $package $version "install-libLTLIBRARIES"
opt_run_make $clean     $package $version "clean"
opt_run_make $distclean $package $version "distclean"

opt_run_tarclean $tarclean $package $version

# according to jpeg version 9c's install.txt, these 4 header files need to be copied into the include folder (besides installing libjpeg.a)
# if building something against the libjpeg library, as we do with xpdf-tools 
cp $package$version/jpeglib.h $package$version/jerror.h $package$version/jconfig.h $package$version/jmorecfg.h $GEXTXPDFTOOLS_INSTALLED/include/.

