BUILDTHREADS=${BUILDTHREADS:-1}
SDK_VERSION=${SDK_VERSION:-"Default"}
NDK_VERSION=${NDK_VERSION:-"Default"}
NDK_API=${NDK_API:-"Default"}
Configuration=${Configuration:-"Default"}
XBMC_DEPENDS_ROOT=${XBMC_DEPENDS_ROOT:-"Default"}
DARWIN_ARM_CPU=${DARWIN_ARM_CPU:-"Default"}
XCODE_APP=${XCODE_APP:-"Default"}
PATH_CHANGE_REV_FILENAME=".last_success_revision"
FAILED_BUILD_FILENAME=".last_failed_revision"
#TARBALLS ENV-VAR is only used by android scripts atm
TARBALLS=${TARBALLS:-"/opt/xbmc-tarballs"}

BINARY_ADDONS_ROOT=tools/depends/target
BINARY_ADDONS="binary-addons"
DEPLOYED_BINARY_ADDONS="-e /addons"

#set platform defaults
#$XBMC_PLATFORM_DIR matches the platform subdirs!
case $XBMC_PLATFORM_DIR in
  ios)
    DEFAULT_SDK_VERSION=11.0
    DEFAULT_XBMC_DEPENDS_ROOT=$WORKSPACE/tools/depends/xbmc-depends
    DEFAULT_CONFIGURATION="Debug"
    DEFAULT_DARWIN_ARM_CPU="armv7"
    DEFAULT_XCODE_APP="Xcode9.0.app"
    ;;

  osx64)
    DEFAULT_SDK_VERSION=10.13
    DEFAULT_XBMC_DEPENDS_ROOT=$WORKSPACE/tools/depends/xbmc-depends
    DEFAULT_CONFIGURATION="Debug"
    DEFAULT_XCODE_APP="Xcode9.0.app"
    ;;

  android)
    DEFAULT_NDK_VERSION="18"
    DEFAULT_NDK_API="21"
    DEFAULT_XBMC_DEPENDS_ROOT=$WORKSPACE/tools/depends/xbmc-depends
    DEFAULT_CONFIGURATION="Debug"
    ;;

  linux*)
    DEFAULT_XBMC_DEPENDS_ROOT=$WORKSPACE/tools/depends/xbmc-depends
    DEFAULT_CONFIGURATION="Debug"
  ;;

  rbpi)
    JENKINS_RBPI_DEVENV=${JENKINS_RBPI_DEVENV:-"/home/jenkins/rbpi-dev"}
    DEFAULT_XBMC_DEPENDS_ROOT=$WORKSPACE/tools/depends/xbmc-depends
    DEFAULT_CONFIGURATION="Debug"
  ;;

  freebsd)
    DEFAULT_CONFIGURATION="Debug"
  ;;
esac

if [ "$SDK_VERSION" == "Default" ]
then
  SDK_VERSION=$DEFAULT_SDK_VERSION
fi

if [ "$NDK_VERSION" == "Default" ]
then
  NDK_VERSION=$DEFAULT_NDK_VERSION
fi

if [ "$NDK_API" == "Default" ]
then
  NDK_API=$DEFAULT_NDK_API
fi

if [ "$XBMC_DEPENDS_ROOT" == "Default" ]
then
  XBMC_DEPENDS_ROOT=$DEFAULT_XBMC_DEPENDS_ROOT
fi

if [ "$DARWIN_ARM_CPU" == "Default" ]
then
  DARWIN_ARM_CPU=$DEFAULT_DARWIN_ARM_CPU
fi

if [ "$XCODE_APP" == "Default" ]
then
  XCODE_APP=$DEFAULT_XCODE_APP
fi

# make osx environment aware of the selected xcode app
DEVELOPER_DIR=/Applications/$XCODE_APP/Contents/Developer

if [ "$Configuration" == "Default" ]
then
  Configuration=$DEFAULT_CONFIGURATION
fi

if [ "$Configuration" == "Release" ]
then
  DEBUG_SWITCH='--disable-debug'
fi

#helper functions

#hash a dir based on the git revision, Configuration, SDK_PATH, NDK_PATH, NDK_VERSION, SDK_VERSION, TOOLCHAIN and XBMC_DEPENDS_ROOT
#param1 path to be hashed
function getBuildHash ()
{
  local checkPath
  checkPath="$1"
  local hashStr
  hashStr="$(git rev-list HEAD --max-count=1  -- $checkPath)"
  hashStr="$hashStr $Configuration $SDK_PATH $NDK_PATH $NDK_VERSION $SDK_VERSION $TOOLCHAIN $XBMC_DEPENDS_ROOT $XCODE_APP"
  echo $hashStr
}

#param1 path to be checked for changes
function pathChanged ()
{
  local ret
  local checkPath
  ret="0"

  checkPath="$1"
  if [ -e $checkPath/$PATH_CHANGE_REV_FILENAME ]
  then
    if [ "$(cat $checkPath/$PATH_CHANGE_REV_FILENAME)" != "$(getBuildHash $checkPath)" ]
    then
      ret="1"
    fi
  else
    ret="1"
  fi

  echo $ret
}

#param1 path to be tagged with hash
function tagSuccessFulBuild ()
{
  local pathToTag
  pathToTag="$1"
  # tag last successful build with revisions of the given dir
  # needs to match the checks in function getBuildHash
  echo "$(getBuildHash $pathToTag)" > $pathToTag/$PATH_CHANGE_REV_FILENAME
}

#param1 path to be tagged with hash
function tagFailedBuild ()
{
  local pathToTag
  pathToTag="$1"
  # tag last failed build with revisions of the given dir
  # needs to match the checks in function getBuildHash
  echo "$(getBuildHash $pathToTag)" > $pathToTag/$FAILED_BUILD_FILENAME
}

function getBranchName ()
{
  local branchName
  branchName=`git symbolic-ref HEAD 2>/dev/null || echo detached`

  if [ "$branchName" != "detached" ] # if we are not detached
  then
    #we are attached - use the branchname then
    if echo $branchName | grep /pr/ 2>&1 > /dev/null
    then
      #if this is a pull request branch - fetch the pr number and prefix with "PR"
      #refs/heads/number/head
      echo PR$(echo $branchName | awk '{gsub(".*/pr/","");print $1}' | awk '{gsub("/","-");print $1}')
    else
      #if we are on a normal branch - fetch branchname
      #refs/heads/branchname
      echo $branchName | awk '{gsub("^([^/]*/){2}","");print $1}' | awk '{gsub("/","-");print $1}'
    fi
  else
    #if we are in detached head state
    #get all branches pointing at HEAD which are from current GITHUB_REPO
    #prefer non-pullrequest branches
    branches=`(git branch -r --points-at HEAD 2> /dev/null || git branch -r --contains HEAD) | grep $GITHUB_REPO/`
    if echo "$branches" | grep -v $GITHUB_REPO/pr/ 2>&1 > /dev/null
    then
      echo "$branches" | sed "/$GITHUB_REPO\/pr\//d" | head -n1 | awk '{gsub("^([^/]*/)","");print $1}' | awk '{gsub("/","-");print $1}'
    else
      echo PR$(echo "$branches" | head -n1 | awk '{gsub(".*/pr/","");print $1}' | awk '{gsub("/","-");print $1}')
    fi
  fi
}

function getBuildRevDateStr ()
{
  local revStr
  #fetch date-rev
  revStr=`git --no-pager show -s --abbrev=8 --date=short --pretty=format:"%cd %h" | awk '{gsub("-", ""); print $1"-"$2}' 2>/dev/null`
  if [ "$?" == "0" ]
  then
    #fetch the first branch containing head
    revStr=$revStr"-"$(getBranchName)
    if [ "$?" == "0" ]
    then
      echo $revStr
    else
      echo "Unknown"
    fi
  else
    echo "Unknown"
  fi
}
