#!/bin/bash
#
# $Id: get-tools 28436 2016-08-03 19:50:05Z edo $
#

unalias -a
export NWCHEM_TOP=${NWCHEM_TOP:-"`pwd`/../"}
if test "x$NWCHEM_TOP" = x
then
    echo You must set NWCHEM_TOP to use this script.
    exit 1
fi
ga_version=0
while [ $# -gt 0 ]
do
   case "$1" in
      --ga-version) ga_version=1;;
      *)
         echo >&2 "usage: $0 [--ga-version]"
         exit 1;;
   esac
   shift 
done

TOOLDIR=`pwd`
TOOLGIT=`which git`
CONFIG=$NWCHEM_TOP/src/config/makefile.h
if test "x$DEV_GA" != x
then
    GA_DIR=ga-5.6.3
    GA_BRANCH=hotfix/5.6.3
fi
if test "x$GA_BRANCH" = x 
then
USE_GA_RELEASE=1
fi
if test "x$USE_GA_RELEASE" != x
then
if test "x$GA_RELEASE_NO" = x
then
    GA_RELEASE_NO=5.6.3
fi
    GA_DIR=ga-"$GA_RELEASE_NO"
if [ ! -f ga-"$GA_RELEASE_NO".tar.gz ]; then
TAR_URL=https://github.com/GlobalArrays/ga/releases/download/v"$GA_RELEASE_NO"/ga-"$GA_RELEASE_NO".tar.gz
# TAR_URL=https://github.com/edoapra/ga/releases/download/v"$GA_RELEASE_NO"/ga-"$GA_RELEASE_NO".tar.gz
#         https://github.com/edoapra/ga/releases/download/v5.6.3/ga-5.6.3.tar.gz
 echo "downloading" ga-"$GA_RELEASE_NO".tar.gz  "from" "$TAR_URL"
  WGET_YES=`wget  -O 2>&1 | head -1  | awk ' /require/ {print "Y";exit};{print "N"}'`
if test "x$WGET_YES" = "Y"
then
 wget  -O ga-"$GA_RELEASE_NO".tar.gz "$TAR_URL"
else
#try curl because wget is not there
 curl -L  "$TAR_URL" -o ga-"$GA_RELEASE_NO".tar.gz
fi
    if [ $? -ne 0 ]
    then
	rm -f ga-"$GA_RELEASE_NO".tar.gz
	echo "wget failed for"  ga-"$GA_RELEASE_NO".tar.gz
	echo " " 
	exit 1
    fi
fi
if [ ! -f ga-"$GA_RELEASE_NO".tar.gz ]; then 
    echo "Failed to download" ga-$GA_RELEASE_NO.tar.gz
    exit 1
else
if [ ! -f "$GA_DIR"/configure ]; then 
 tar xvzf ga-"$GA_RELEASE_NO".tar.gz
fi
fi
else #USE_GA_RELEASE
if test "x$DEV_GA" != x
then
#DEV_GA=1 gives the current "best" dev branch, not develop
#to get develop, set GA_BRANCH-develop
    GA_DIR=ga-5.6.3
    GA_BRANCH=hotfix/5.6.3
else
  if test "x$GA_BRANCH" != x
  then
    if test "x$GA_DIR" = x
    then
       GA_DIR0=ga-"$GA_BRANCH"
       GA_DIR=`echo $GA_DIR0 | sed -e 's/\//_/g'`
    fi
  else
     GA_DIR=ga-5-6-3
     GA_BRANCH=hotfix/5.6.3
  fi
fi
if test "x$GA_URL" != x
then
    echo "Using GA_URL" $GA_URL
else
GA_URL="https://github.com/GlobalArrays/ga.git"
fi
GA_GIT="$GA_URL  -b $GA_BRANCH"
echo "Using GA_GIT" $GA_GIT "and GA_DIR" $GA_DIR

if [ $ga_version == 1 ] ; then
  echo $GA_DIR
  exit 0
fi

if [ ${#TOOLGIT} -eq 0 ] ; then
  echo "No Git found!"
  echo "Giving up and hoping for the best..."
  exit 0
fi
if test -d $GA_DIR
then
    echo "Updating existing $GA_DIR"
    cd $GA_DIR
#    git cleanup
    if git fetch 
    then
        echo "Finished updating existing $GA_DIR"
    else
        echo "Failed updating existing $GA_DIR"
        exit 1
    fi
    cd $TOOLDIR
else
    echo "Checking out $GA_GIT into dir $GA_DIR"
    git clone $GA_GIT $GA_DIR 2> /dev/null
    if [ $? -ne 0 ]
    then
	echo "Git clone failed" >&2
	exit 1
    fi
fi
fi #USE_GA_RELEASE
# check if configure is there
if test -e $GA_DIR/configure
then
        echo "configure present. no autogen.sh required"
else
        echo "configure missing. running autogen.sh"
        echo $GA_DIR/configure $GA_DIR/autogen.sh
        cd  $GA_DIR;sh ./autogen.sh
fi
exit 0
