#!/bin/bash

# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2009-2016 Stephan Raue (stephan@openelec.tv)
# Copyright (C) 2018-present Team LibreELEC (https://libreelec.tv)

BUMP_PKG_REV=""
KEEP_GIT_DIRS="yes"
UPDATE_TO_HASH="yes"

usage() {
  echo "Usage: $0 [options] <kodi-branch> [<unofficial-addon-branch>]"
  echo " -b, --bump-pkg-rev: bump PKG_REV if package was not updated"
  echo " -d, --delete-git-dirs: delete cloned git dirs after update"
  echo " -x, --no-update-to-hash: do not update to hash if tag is not set"
  echo " -h, --help: display help and exit"
  exit 1
}

while [ $# -ne 0 ]; do
  case "$1" in
    -b | --bump-pkg-rev)
      BUMP_PKG_REV="yes"
      shift
      ;;
    -d | --delete-git-dirs)
      KEEP_GIT_DIRS=""
      shift
      ;;
    -x | --no-update-to-hash)
      UPDATE_TO_HASH="no"
      shift
      ;;
    -h | --help)
      usage
      exit 1
      ;;
    -*)
      echo "illegal option $1"
      usage
      exit 1
      ;;
    *)
      break
      ;;
  esac
done

if [ $# -eq 0 -o $# -gt 2 ]; then
  usage
  exit 1
fi

# list of packages to exclude from update
EXCLUDED_PACKAGES="vfs.sacd"

MY_DIR="$(dirname "$0")"
ROOT="$(cd "${MY_DIR}"/../.. && pwd)"
TMPDIR="$(pwd)/.update-binary-addons-tmp"
TMP_PKG_FILE="${TMPDIR}/package.tar.gz"
TMP_PKG_DIR="${TMPDIR}/package"

rm -rf "${TMPDIR}"
mkdir -p "${TMPDIR}"

KODI_BRANCH="$1"
KODI_DIR="kodi.git"

if [ $# -eq 1 ]; then
  UNOFFICIAL_ADDON_BRANCH="${KODI_BRANCH}"
else
  UNOFFICIAL_ADDON_BRANCH="$2"
fi

. "${MY_DIR}/update_common_functions"

get_gh_token

if [ -z "${GITHUB_API_TOKEN}" ]; then
  git_clone https://github.com/xbmc/xbmc ${KODI_DIR} ${KODI_BRANCH}
  ADDONS_REPO_LOCATION="${KODI_DIR}/cmake/addons/bootstrap/repositories"
else
  # check if depends are available
  command -v curl >/dev/null 2>&1 || die "please install curl"
  command -v jq >/dev/null 2>&1 || die "please install jq"

  TMP_REPO_DIR="${TMPDIR}/bootstrap-repos"

  curl -s -L -H "Authorization: token ${GITHUB_API_TOKEN}" \
    "https://api.github.com/repos/xbmc/xbmc/contents/cmake/addons/bootstrap/repositories?ref=${KODI_BRANCH}" | awk \
    '/download_url/ { gsub("\"|,", "", $2); system("mkdir -p '${TMP_REPO_DIR}'; (cd '${TMP_REPO_DIR}'; curl -O "$2")"); }' \
    2>/dev/null
  ADDONS_REPO_LOCATION=${TMP_REPO_DIR}
fi

if ! ls ${ADDONS_REPO_LOCATION}/*-addons.txt &>/dev/null; then
  echo "No binary addon repo description found, nothing to do!"
  echo "Path searched: ${ADDONS_REPO_LOCATION}"
  exit 0
fi

# addons
for addontxt in ${ADDONS_REPO_LOCATION}/*-addons.txt; do
  ADDONS=$(cat $addontxt | awk '{print $1}')
  ADDONS_GIT_DIR="${ADDONS}.git"
  ADDONS_GIT_REPO=$(cat $addontxt | awk '{print $2}')
  ADDONS_GIT_BRANCH=$(cat $addontxt | awk '{print $3}')
  git_clone $ADDONS_GIT_REPO $ADDONS_GIT_DIR ${ADDONS_GIT_BRANCH}

  for addon in $ADDONS_GIT_DIR/*.*/; do
    ADDON=$(basename $addon)

    [[ ${ADDON} =~ ^game.* ]] && continue # ignore game.* addons - handled by update_retroplayer-addons

    check_package_excluded "${ADDON}" "${EXCLUDED_PACKAGES}" && continue

    GIT_DIR="${ADDON}.git"
    GIT_REPO=$(cat ${addon}/${ADDON}.txt | awk '{print $2}')
    GIT_BRANCH=$(cat ${addon}/${ADDON}.txt | awk '{print $3}')

    if ! grep -q all ${addon}/platforms.txt && ! grep -q linux ${addon}/platforms.txt && ! grep -q ! ${addon}/platforms.txt; then
      continue
    fi

    ADDON_PATH="${ROOT}/packages/mediacenter/kodi-binary-addons/${ADDON}/"
    if [ -f "${ADDON_PATH}/package.mk" ]; then
      # Verify the Kodi repo matches our package repo
      # If different, ignore the addon and process it later as an "unofficial" addon
      validate_pkg_url "${ADDON}" "${GIT_REPO}" || continue

      NO_TAG=""
      if [ -z "${GITHUB_API_TOKEN}" ]; then
        git_clone ${GIT_REPO} ${GIT_DIR} ${GIT_BRANCH}
        PARAMS="resolve_tag_in_branch ${GIT_DIR} ${GIT_BRANCH}"
      else
        REPO=$(basename "${GIT_REPO}")
        OWNER=$(basename "${GIT_REPO%/${REPO}*}")
        PARAMS="resolve_tag_on_gh ${OWNER} ${REPO} ${GIT_BRANCH}"
      fi
      NEW_VERSION=$(${PARAMS})

      if [ -z "${NEW_VERSION}" ]; then
        NO_TAG="yes"
        if [ -z "${GITHUB_API_TOKEN}" ]; then
          PARAMS="resolve_hash_in_branch ${GIT_DIR} ${GIT_BRANCH}"
        else
          PARAMS="resolve_hash_on_gh ${GIT_REPO} ${GIT_BRANCH}"
        fi
        echo "========================================================================"
        if [ "${UPDATE_TO_HASH}" != "yes" ]; then
          msg_warn "WARNING: no tag found for addon ${ADDON}, and UPDATE_TO_HASH=no so not updating"
          NEW_VERSION=""
        else
          msg_warn "WARNING: no tag found for addon ${ADDON}, falling back to HEAD"
          NEW_VERSION=$(${PARAMS})
        fi
        echo "========================================================================"
      fi

      if [ ! -z "${NEW_VERSION}" ]; then
        echo "Resolved version for ${ADDON}: ${GIT_BRANCH} => ${NEW_VERSION}"

        if update_pkg "${ADDON_PATH}" ${ADDON} ${NEW_VERSION}; then
          if [ -n "${NO_TAG}" ]; then
            # always bump PKG_REV on updates as we have no info if version changed
            bump_pkg_rev "${ADDON_PATH}" "${ADDON}"
          else
            reset_pkg_rev "${ADDON_PATH}" "${ADDON}"
          fi
        else
          [ "${BUMP_PKG_REV}" = "yes" ] && bump_pkg_rev "${ADDON_PATH}" "${ADDON}"
        fi

        if [ "${KEEP_GIT_DIRS}" != "yes" ]; then
          [ -d "${GIT_DIR}" ] && rm -rf "${GIT_DIR}"
        fi
      fi
    else
      echo "[mkpkg] Skipped $ADDON"
      SKIPPED_ADDONS="$SKIPPED_ADDONS $ADDON"
    fi
    PROCESSED="${PROCESSED}${ADDON}\n"
  done

  if [ "${KEEP_GIT_DIRS}" != "yes" ]; then
    [ -d "${ADDONS_GIT_DIR}" ] && rm -rf "${ADDONS_GIT_DIR}"
  fi

  echo
  echo "The following addons were skipped, please add packages for these addons:"
  echo "[${SKIPPED_ADDONS}]"
  echo
done

# finally, any other unofficial addons
for ADDON in $(ls -1 "${ROOT}/packages/mediacenter/kodi-binary-addons"); do
  [[ ${ADDON} =~ ^game.* ]] && continue # ignore game.* addons - handled by update_retroplayer-addons

  # ignore already processed addons
  echo -e "${PROCESSED}" | grep -qE "^${ADDON}$" && continue

  check_package_excluded "${ADDON}" "${EXCLUDED_PACKAGES}" && continue

  # always continue when UPDATE_TO_HASH=no as the below code does not work on tags
  [ "${UPDATE_TO_HASH}" != "yes" ] && continue

  ADDON_PATH="${ROOT}/packages/mediacenter/kodi-binary-addons/${ADDON}/"
  # Obtain git url - ignore if not a suitable repo
  GIT_DIR="${ADDON}.git"
  GIT_REPO="$(geturl "${ADDON}")" || continue

  if [ -z "${GITHUB_API_TOKEN}" ]; then
    git_clone ${GIT_REPO} ${GIT_DIR}
    RESOLVE_UNOFFICIAL_BRANCH="resolve_hash_in_branch ${ADDON}.git ${UNOFFICIAL_ADDON_BRANCH}"
    RESOLVE_HEAD="resolve_hash_in_branch ${ADDON}.git HEAD"
  else
    RESOLVE_UNOFFICIAL_BRANCH="resolve_hash_on_gh ${GIT_REPO%.git} ${UNOFFICIAL_ADDON_BRANCH}"
    RESOLVE_HEAD="resolve_hash_on_gh ${GIT_REPO%.git} HEAD"
  fi

  if RESOLVED_HASH=$(${RESOLVE_UNOFFICIAL_BRANCH}); then
    echo "Resolved hash for ${ADDON}: ${UNOFFICIAL_ADDON_BRANCH} => ${RESOLVED_HASH}"
  elif RESOLVED_HASH=$(${RESOLVE_HEAD}); then
    echo "Resolved hash for ${ADDON}: HEAD => ${RESOLVED_HASH}"
  else
    msg_warn "WARNING: Could not resolve hash for ${ADDON}"
    continue
  fi

  if update_pkg "${ADDON_PATH}" "${ADDON}" "${RESOLVED_HASH}"; then
    # always bump PKG_REV when updating untagged addons
    bump_pkg_rev "${ADDON_PATH}" "${ADDON}"
  else
    [ "${BUMP_PKG_REV}" = "yes" ] && bump_pkg_rev "${ADDON_PATH}" "${ADDON}"
  fi

  if [ "${KEEP_GIT_DIRS}" != "yes" ]; then
    [ -d "${GIT_DIR}" ] && rm -rf ${GIT_DIR}
  fi
done

rm -rf "${TMPDIR}"

if [ -d "${TMP_REPO_DIR}" ]; then
  rm -rf "${TMP_REPO_DIR}"
fi
