#!/bin/bash

#This used to be a package-app, but it turns out the 'kodi' package conflicts with 'kodi-data' and 'kodi-bin' in an annoying way. Using a script allows us to remove these packages first, to avoid causing apt errors.
if package_installed kodi-data ;then
  status "First removing kodi-data package to avoid errors..."
  apt_lock_wait
  sudo apt purge kodi-data -y --autoremove
fi

if package_installed kodi-bin ;then
  status "First removing kodi-bin package to avoid errors..."
  apt_lock_wait
  sudo apt purge kodi-bin -y --autoremove
fi

# check for bad PPA use
ppa_name="team-xbmc/ppa"
ppa_grep="$ppa_name"
[[ "${ppa_name}" != */ ]] && ppa_grep="${ppa_name}/"
ppa_added=$(grep ^ /etc/apt/sources.list /etc/apt/sources.list.d/* | grep -v list.save | grep -v deb-src | grep deb | grep -v '#' | grep "$ppa_grep" | wc -l)
if [[ $ppa_added -gt "0" ]]; then
  warning "A BAD PPA from the XBCM Kodi Team has been found on your system."
  warning "There is no ARM support in this PPA and it causes installation errors so it has been removed."
  warning "This is a user error. Do NOT add PPAs without ARM support to your system."
  status "Removing $ppa_name PPA"
  install_packages ppa-purge || exit 1
  apt_lock_wait
  sudo ppa-purge "ppa:$ppa_name" -y
fi

install_packages kodi || exit 1

