#!/bin/bash

package=cmake
#version=-3.11.4
version=-3.9.6

progname=$0

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

prefix=$GEXTXPDFTOOLS_INSTALLED

opt_run_untar $force_untar $auto_untar $package $version

# For configuring cmake, run ./bootstrap <args> instead of ./configure <args>
# See https://cmake.org/install/
#opt_run_configure $force_config $auto_config $package $version $prefix
if [ $force_config = "1" ] ; then
    echo "[pushd $package$version]"
    ( cd $package$version ; \
	echo $CROSSCONFIGURE_VARS ./bootstrap --prefix="$prefix" $CROSSCONFIGURE_ARGS $@ ; 
	eval $CROSSCONFIGURE_VARS ./bootstrap --prefix="$prefix" $CROSSCONFIGURE_ARGS $@ )
    if [ $? != 0 ] ; then
        echo "      Error encountered running *bootstrap* stage of $progname"
        exit 1
    fi
    echo "[popd]"
else
    if [ $auto_config = "1" ] ; then
	echo "Found top-level for ${progname%.*} => no need to run ./bootstrap"
    fi
fi

# Check that CMake products weren't just moved out of the distribution area,
# before recompiling all of CMake
if [[ ! -e "$GEXTXPDFTOOLS_INSTALLED/bin/cmake" && -e "$GEXT_XPDFTOOLS/devel/$GSDLOS/bin/cmake" ]]; then
    echo "**************************************"
    echo "Moving intermediary CMake products back for development"
    $GEXT_XPDFTOOLS/move-cmake.sh back
    echo "**************************************"
fi

# Only compile if necessary - we don't edit these source files:
# Only compile up OUR cmake, and only if we haven't already got OUR cmake.
# But don't use any system cmake: xpdf-tools is dependent on cmake version (>= 2.8.8)
if [[ -x "$GEXTXPDFTOOLS_INSTALLED/bin/cmake" ]]; then
  echo "found cmake in $GEXTXPDFTOOLS_INSTALLED/bin"
  _CMAKE="$GEXTXPDFTOOLS_INSTALLED/bin/cmake"
fi

if [[ "$_CMAKE" ]]; then
  _VERSION=$("$_CMAKE" --version 2>&1 | sed 's/.*cmake version \([0-9]*\.[0-9]*\.[0-9]*\).*/\1/;1q')
  echo "cmake version $_VERSION"
fi

if [[ ! "$_CMAKE" ]]; then
  opt_run_make $compile   $package $version
  opt_run_make $install   $package $version "install"
else
  echo "$package$version already available => no need to recompile"
fi

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

opt_run_tarclean $tarclean $package $version
