#!/bin/bash
##############################################################
# Execute as
#
# $ bash get_latest_version.sh prefix=<target>
#
# where <target> is the (existing) target directory, to which
# the  hameren-avhlib-x  and  hameren-katie-x  directories
# will be downloaded.
#
##############################################################

set -e

case "`uname`" in
  Darwin*)
    sedi='sed -i ""'
    ;;
  *)
    sedi='sed -i'
    ;;
esac

PATHavhlib=examplePathAVHLIB
PATHkatie=examplePathKatie
searchDir=/

function infoExecute {
  echo " You must give a target directory:"
  echo " $ bash $0 prefix=<target>"
  echo " where <target> is the (existing) target directory, to which"
  echo " the  hameren-avhlib-x  and  hameren-katie-x  directories"
  echo " will be downloaded."
#
}
function infoInstall {
  echo " Check if the necessary paths in"
  echo " $PATHkatie/settings.py "
  echo " are correct. You can use this script to find paths:"
  echo " $ bash $0 path=<path> find=<file>"
  echo " will look for <file> starting from <path> (default path=/)."
  echo ""
  echo " When all paths are set, execute "
  echo " $ $PATHkatie/config.py lib"
  echo ""
  echo " Use the script $PATHkatie/run.sh"
  echo " to run this version of KaTie."
  echo " Most convenient is to link it somewhere, eg. execute"
  echo " $ ln -s $PATHkatie/run.sh"
  echo " in your sandbox, so it is available there as run.sh."
}

function getzipfile {
  local __result=$1
  local key=`wget -O - https://bitbucket.org/hameren/$2/downloads/ \
            | grep "/hameren/$2/get/" \
            | sed -e"s|.*/hameren/$2/get/||" \
            | sed -e"s|.zip.*||"`
  eval $__result="'$key'"
  echo ""
  echo "Executing: wget https://bitbucket.org/hameren/$2/get/$key.zip"
  echo ""
  wget https://bitbucket.org/hameren/$2/get/$key.zip
}

function getfilepath {
  echo $(dirname `find $1 -name $2 2>&1 | grep -v "Permission denied" | tail -1`)
}

function setpath {
  $sedi -e"s|^ *$1 *= *.*|$1 = '$2'|" settings.py
}

here=`pwd`
if [ "$#" -eq 0 ] ;then 
  echo ""
  infoExecute
  echo ""
  echo " For more info, execute"
  echo " $ bash $0 help"
  echo ""
  exit 0
fi

task=getLatest

for var in "$@"
do
    lhs=`echo $var | sed s~=.*$~~`
    rhs=`echo $var | sed s~^$lhs=~~`
    case $lhs in
    "prefix"|"-prefix"|"--prefix")
        targetDir=$rhs
        ;;
    "path"|"-path"|"--path")
        searchDir=$rhs
        ;;
    "find"|"-find"|"--find")
        fileName=$rhs
        task=findFile
        ;;
    *)
        task=info
    esac
done

case $task in
"getLatest")
    cd $targetDir
    
    getzipfile KEYavhlib 'avhlib'
    getzipfile KEYkatie 'katie'
    
    unzip $KEYavhlib.zip
    unzip $KEYkatie.zip
    
    cd hameren-avhlib-$KEYavhlib
    PATHavhlib=`pwd`
    cd ../hameren-katie-$KEYkatie
    PATHkatie=`pwd`
    
    echo ""
    echo " Finding paths..."
    setpath 'AVHLIBpath' $PATHavhlib
    setpath 'LHAPDFpath' $(getfilepath '/usr' 'libLHAPDF.so')
#    setpath 'TMDLIBpath' $(getfilepath '/usr' 'libTMDlib.so')
    setpath 'TMDLIBpath' $(getfilepath '/usr' 'TMDlib-config')
    setpath 'GSLpath'    $(getfilepath '/usr' 'libgsl.so')
    echo " ...done."
    
    echo ""
    echo " FURTHER INSTALLATION INSTRUCTIONS:"
    infoInstall
    echo ""
    ;;
"findFile")
    find $searchDir -name $fileName 2>&1 | grep -v "Permission denied"
    ;;
"info")
    echo ""
    infoExecute
    echo ""
    echo " After successful execution of the script, you need to"
    echo " go through the following:"
    infoInstall
    echo ""
esac

