#!/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)

if [ -z "$1" ]; then
  echo "Usage: $0 <branch-name>"
  exit 0
fi

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

# the following environment variables can be set to "yes" to enable optional features:
# KEEP_GIT_DIRS: don't delete cloned git directories after update check
# BUMP_PKG_REV: bump PKG_REV if PKG_VERSION has changed

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-${KODI_BRANCH}.git"

. "${MY_DIR}/update_common_functions"

if [ ! -d ${KODI_DIR} ] ; then
  git_clone https://github.com/xbmc/xbmc ${KODI_BRANCH} ${KODI_DIR}
fi

# kodi-platform
REPO=$(cat $KODI_DIR/cmake/addons/depends/common/kodi-platform/kodi-platform.txt | awk '{print $2}')
GIT_HASH=$(cat $KODI_DIR/cmake/addons/depends/common/kodi-platform/kodi-platform.txt | awk '{print $3}')
PKG_NAME="kodi-platform"

git_clone $REPO master $PKG_NAME.git $GIT_HASH
if [ -f "${ROOT}/packages/mediacenter/kodi-platform/package.mk" ] ; then
  # update package.mk
  RESOLVED_HASH=$(resolve_hash $PKG_NAME.git $GIT_HASH)
  update_pkg "${ROOT}/packages/mediacenter/kodi-platform" ${PKG_NAME} ${RESOLVED_HASH}
fi
if [ "${KEEP_GIT_DIRS}" != "yes" ]; then
  rm -rf $PKG_NAME.git
fi

# addons
for addontxt in $KODI_DIR/cmake/addons/bootstrap/repositories/*-addons.txt ; do
  ADDONS=$(cat $addontxt | awk '{print $1}')
  ADDONREPO=$(cat $addontxt | awk '{print $2}')
  GIT_HASH=$(cat $addontxt | awk '{print $3}')
  git_clone $ADDONREPO ${KODI_BRANCH} $ADDONS.git $GIT_HASH
  for addon in $ADDONS.git/*.*/ ; do
    ADDON=$(basename $addon)

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

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

    REPO=$(cat $addon/$ADDON.txt | awk '{print $2}')
    GIT_HASH=$(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" "$REPO" || continue

      update_to_latest_tag "${ADDON_PATH}" ${ADDON} ${REPO} ${KODI_BRANCH} ${GIT_HASH}
    else
      echo "[mkpkg] Skipped $ADDON"
      SKIPPED_ADDONS="$SKIPPED_ADDONS $ADDON"
    fi
    PROCESSED="${PROCESSED}${ADDON}\n"
  done

  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

  # Obtain git url - ignore if not a suitable repo
  REPO="$(geturl "${ADDON}")" || continue

  git_clone $REPO ${KODI_BRANCH} $ADDON.git HEAD

  # update package.mk for stale github.com packages
  RESOLVED_HASH=$(resolve_hash ${ADDON}.git HEAD) || continue
  echo "Resolving hash for ${ADDON}: HEAD => ${RESOLVED_HASH}"
  update_pkg "${ROOT}/packages/mediacenter/kodi-binary-addons/$ADDON" ${ADDON} ${RESOLVED_HASH}

  if [ "${KEEP_GIT_DIRS}" != "yes" ]; then
    rm -rf $ADDON.git
  fi
done

rm -rf "${TMPDIR}"
