#!/bin/sh

set -e
glpk_ver=4.37

wgetcmd=wget
wgetcount=`which wget 2>/dev/null | wc -w`
if test ! $wgetcount = 1; then
  echo "Utility wget not found in your PATH."
  if test `uname` = Darwin; then
    wgetcmd=ftp
    echo "Using ftp command instead."
  else
    exit -1
  fi
fi

echo " "
echo "Running script to download the source code for GLPK $glpk_ver."
echo " "

rm -f glpk*.tar.gz

echo "Downloading the source code from ftp.gnu.org..."
$wgetcmd ftp://ftp.gnu.org/gnu/glpk/glpk-${glpk_ver}.tar.gz

echo "Uncompressing the tarball..."
gunzip -f glpk-${glpk_ver}.tar.gz

if test -d glpk ; then
  echo "Moving current glpk to glpk.OLD."
  if test -d glpk.OLD ; then
    rm -rf glpk.OLD
  fi
  mv glpk glpk.OLD
fi

echo "Unpacking the source code..."
tar xf glpk-${glpk_ver}.tar

echo "Deleting the tar file..."
rm glpk-${glpk_ver}.tar

mv glpk-${glpk_ver} glpk

echo " "
echo "Done downloading the source code for GLPK."
echo "Applying patch file."

patch -p0 < glpk.patch

echo "Touch every source file to force rebuild of whole package."

touch glpk/src/*.c glpk/include/*.h

echo " "
echo "Verify that there are no error message in the output above."
