#!/bin/sh
################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2016 Stephan Raue (stephan@openelec.tv)
#
#  OpenELEC is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 2 of the License, or
#  (at your option) any later version.
#
#  OpenELEC is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with OpenELEC.  If not, see <http://www.gnu.org/licenses/>.
################################################################################

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

KODI_DIR="kodi-$1.git"

git_clone() {
  # git_clone https://repo.url branch ./target_dir [githash]
  echo "[mkpkg] Checking out $1 ..."
  if [ ! -d "$3" ]; then
    git clone "$1" -b $2 "$3"
  else
    if [ -d "$3" ] ; then
      cd "$3"
      git checkout $2 >/dev/null 2>/dev/null
      git pull
      cd ..
    fi
  fi
  if [ ! -z "$4" ] ; then
    cd "$3"
    git fetch >/dev/null 2>/dev/null
    git branch -D $4 >/dev/null 2>/dev/null
    git checkout $4 >/dev/null 2>/dev/null
    git checkout -b ref-$4 >/dev/null 2>/dev/null
    cd ..
  fi
}

resolve_hash() {
  if [ -d "$1" ] ; then
    cd "$1"
    git rev-parse --short $2 2>/dev/null
  fi
}

# Get url in git:// notation for a package.mk, assuming it is a github.com url
# Return 1 if not a github domain
geturl() {
  local addon="$1"
  local domain owner repo

  . ../../packages/mediacenter/kodi-binary-addons/${addon}/package.mk 1>/dev/null 2>/dev/null

  domain="$(echo "${PKG_URL}" | cut -d/ -f3)"
  [ "${domain}" = "github.com" ] || return 1

  owner="$(echo "${PKG_URL}" | cut -d/ -f4)"
  repo="$(echo "${PKG_URL}" | cut -d/ -f5)"

  echo "git://${domain}/${owner}/${repo}.git"
  return 0
}

# For the specified addon, verify that the package url
# matches the url retrieved from Kodi
validate_pkg_url() {
  local addon="$1" url1="$2"
  local domain owner repo url2

  domain="$(echo "${url1}" | cut -d/ -f3)"
  owner="$(echo "${url1}" | cut -d/ -f4)"
  repo="$(echo "${url1}" | cut -d/ -f5)"

  url1="git://${domain}/${owner}/${repo}.git"
  url2="$(geturl "${addon}")"

  [ "${url1}" = "${url2}" ] && return 0 || return 1
}

if [ ! -d $KODI_DIR ] ; then
  echo "meh.. $KODI_DIR does not exist"
  exit 127
fi

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

git_clone $REPO master $PKG_NAME.git $GIT_HASH
if [ -f ../../packages/mediacenter/p8-platform/package.mk ] ; then
  # update package.mk
  RESOLVED_HASH=$(resolve_hash $PKG_NAME.git $GIT_HASH)
  sed -i "s|PKG_VERSION=.*|PKG_VERSION=\"$RESOLVED_HASH\"|g" ../../packages/mediacenter/p8-platform/package.mk
fi
rm -rf $PKG_NAME.git

# kodi-platform
REPO=$(cat $KODI_DIR/project/cmake/addons/depends/common/kodi-platform/kodi-platform.txt | awk '{print $2}')
GIT_HASH=$(cat $KODI_DIR/project/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 ../../packages/mediacenter/kodi-platform/package.mk ] ; then
  # update package.mk
  RESOLVED_HASH=$(resolve_hash $PKG_NAME.git $GIT_HASH)
  sed -e "s|PKG_VERSION=.*|PKG_VERSION=\"$RESOLVED_HASH\"|g" \
      -e "s|PKG_GIT_URL=.*|PKG_GIT_URL=\"$REPO\"|g" \
      -i ../../packages/mediacenter/kodi-platform/package.mk
fi
rm -rf $PKG_NAME.git

# addons
for addontxt in $KODI_DIR/project/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 master $ADDONS.git $GIT_HASH
  for addon in $ADDONS.git/*.*/ ; do
    ADDON=$(basename $addon)
    REPO=$(cat $addon/$ADDON.txt | awk '{print $2}')
    GIT_HASH=$(cat $addon/$ADDON.txt | awk '{print $3}')
    PKG_NAME="$ADDON"

    # 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

    PROCESSED="${PROCESSED}${PKG_NAME}\n"

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

    if [ -f ../../packages/mediacenter/kodi-binary-addons/$ADDON/package.mk ] ; then
      git_clone $REPO master $PKG_NAME.git $GIT_HASH

      # update package.mk
      RESOLVED_HASH=$(resolve_hash $PKG_NAME.git $GIT_HASH)
      sed -e "s|PKG_VERSION=.*|PKG_VERSION=\"$RESOLVED_HASH\"|g" \
          -e "s|PKG_GIT_URL=.*|PKG_GIT_URL=\"$REPO\"|g" \
          -i ../../packages/mediacenter/kodi-binary-addons/$ADDON/package.mk

      rm -rf $PKG_NAME.git
    else
      echo "[mkpkg] Skipped $ADDON"
      SKIPPED_ADDONS="$SKIPPED_ADDONS $ADDON"
    fi
  done
  echo "followed addons was skipped, please add packages for this:"
  echo "$SKIPPED_ADDONS"
done

# finally, any other unofficial addons
for ADDON in $(ls -1 ../../packages/mediacenter/kodi-binary-addons); do
  # ignore already processed addons
  echo -e "${PROCESSED}" | grep -qE "^${ADDON}$" && continue

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

  git_clone $REPO master $ADDON.git HEAD

  # update package.mk for stale github.com packages
  RESOLVED_HASH=$(resolve_hash ${ADDON}.git HEAD) || continue

  sed -e "s|PKG_VERSION=.*|PKG_VERSION=\"$RESOLVED_HASH\"|g" \
    -i ../../packages/mediacenter/kodi-binary-addons/$ADDON/package.mk

  rm -rf $ADDON.git
done
