#!/bin/bash

package=chromaprint
version=-v1.5.0

progname=$0

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

prefix=$GEXT_MARS_INSTALLED

#export CFLAGS="$CFLAGS -fPIC"
#export CXXFLAGS="$CXXFLAGS -fPIC"
#export LDFLAGS="$LDFLAGS -fPIC"

export CFLAGS="$CFLAGS -I$GEXT_MARS_INSTALLED/include"
#export CPPFLAGS="$CPPFLAGS -I$GEXT_MARS_INSTALLED/include"
export CXXFLAGS="$CXXFLAGS -I$GEXT_MARS_INSTALLED/include"
export LDFLAGS="$LDFLAGS -L$GEXT_MARS_INSTALLED/lib"
#export LD_LIBRARY_PATH="$GEXT_MARS_INSTALLED/lib"


# The following is a workaround that lets 'fpcalc' (the final executable generate) to compile.
#
# In theory this shouldn't be needed, but it would appear the 'cmake' rule to Chromaprint
#   don't correctly work out that linking in a static -lavcodec means that -lz also need to be present.  
# Awkward to try and change the rule within cmake, so the decision was made to include it through
#   the LDFLAGS environment variable, even though that means it's presented to all the linking
#   commands in the project.

# How did we get here?
#
# The fix is considered a small price to pay in working through a more complicated sequence of
# errors we had been getting.
#
# 1. With default params, chromaprint generates both static and shared libraries.  However, compilation
#     failed trying to generate the shared library, as it could not link against the static
#     libavcodec.a (-fPIC issue).
# 2. So, next we went back to 'ffmpeg' and changed its configure statement to enable shared libraries.
#     However, this lead to a new linking error to do with 'inflate' (in libz) not being found.
#     (It wasn't clear at that point which libz it should be looking for.  It was thought that it
#     was probably a system-wide installed one, and perhaps one that was perhaps just the .so file)
# 3. The decision at that point was made to bundle a version of libz within the 'devel' folder
#     to this extension, and point packages known to need it explicity at that.
#
# Step 3 resulted in successful compilation, notwithstanding, the little bugbear that 'fpcalc'
#   fails to include -lz in its linking command (hence the workaround used here!)

export LDFLAGS="$LDFLAGS -lz"

if [ "x$PKG_CONFIG_PATH" = "x" ] ; then
    export PKG_CONFIG_PATH="$GEXT_MARS_INSTALLED/lib/pkgconfig"
else
    export PKG_CONFIG_PATH="$GEXT_MARS_INSTALLED/lib/pkgconfig:$PKG_CONFIG_PATH"

fi

opt_run_untar $force_untar $auto_untar $package $version

#opt_run_configure $force_config $auto_config $package $version $prefix 
if [ $force_config = "1" ] ; then
    echo "[pushd $package$version]"
    cd $package$version ; 
    
    if [ ! -f Makefile ] ; then \
	#cmake -D CMAKE_BUILD_TYPE=RELEASE \
	#      -D CMAKE_INSTALL_PREFIX=$prefix ..;
	cmake -DCMAKE_BUILD_TYPE=Release \
	      -DCMAKE_INSTALL_PREFIX=$prefix \
	      -DBUILD_TOOLS=ON . \
	      -DBUILD_SHARED_LIBS=OFF \
	      -DCMAKE_C_FLAGS="$CFLAGS" \
	      -DCMAKE_CXX_FLAGS="$CXXFLAGS"  \
	      -DCMAKE_EXE_LINKER_FLAGS="$LDFLAGS" 

    fi ;
    cd ..
    
fi

opt_run_make $compile $package $version
opt_run_make $install $package $version "install"

opt_run_make $clean $package $version "clean"
opt_run_make $distclean $package $version "distclean"

opt_run_tarclean $tarclean $package $version
