#!/bin/bash
#
# [Quick Box :: Remove Sonarr-NzbDrone package]
#
# GITHUB REPOS
# GitHub _ packages  :   https://github.com/QuickBox/quickbox_packages
# LOCAL REPOS
# Local _ packages   :   /etc/QuickBox/packages
# Author             :   QuickBox.IO | JMSolo
# URL                :   https://quickbox.io
#
# QuickBox Copyright (C) 2016
# Licensed under GNU General Public License v3.0 GPL-3 (in short)
#
#   You may copy, distribute and modify the software as long as you track
#   changes/dates in source files. Any modifications to our software
#   including (via compiler) GPL-licensed code must also be made available
#   under the GPL along with build & install instructions.
#
username=$(cat /srv/rutorrent/home/db/master.txt)
local_setup=/etc/QuickBox/setup/

function _removeSonarr() {
  systemctl stop sonarr@{username}
  systemctl disable sonarr@{username}
  sudo apt-get remove -y nzbdrone >/dev/null 2>&1
  sudo apt-get -y autoremove >/dev/null 2>&1
  rm -f /etc/apt/sources.list.d/sonarr.list
  rm -f /etc/apache2/sites-enabled/sonarr.conf
if [[ -f /install/.jackett.lock ]]; then
if ls mono-* >/dev/null 2>&1; then
    echo "mono sources list was found"
    ls mono-*
fi
for list in /etc/apt/sources.list.d/mono-*; do
    if [[ -f $list ]]; then
        echo "removing and replacing with stable mono"
        rm -rf /etc/apt/sources.list.d/mono-*
        sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
        echo "deb http://download.mono-project.com/repo/ubuntu stable-xenial main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list
        break
    fi
done
fi
if [[ -f /install/.lidarr.lock ]]; then
if ls mono-* >/dev/null 2>&1; then
    echo "mono sources list was found"
    ls mono-*
fi
for list in /etc/apt/sources.list.d/mono-*; do
    if [[ -f $list ]]; then
        echo "removing and replacing with stable mono"
        rm -rf /etc/apt/sources.list.d/mono-*
        sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
        echo "deb http://download.mono-project.com/repo/ubuntu stable-xenial main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list
        break
    fi
done
fi
if [[ -f /install/.radarr.lock ]]; then
if ls mono-* >/dev/null 2>&1; then
    echo "mono sources list was found"
    ls mono-*
fi
for list in /etc/apt/sources.list.d/mono-*; do
    if [[ -f $list ]]; then
        echo "removing and replacing with stable mono"
        rm -rf /etc/apt/sources.list.d/mono-*
        sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
        echo "deb http://download.mono-project.com/repo/ubuntu stable-xenial main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list
        break
    fi
done
fi
  apt-get -y autoremove && apt-get -y autoclean
  if [[ -f /etc/init.d/sonarr ]]; then
    sudo update-rc.d -f sonarr remove >/dev/null 2>&1
    sudo rm /etc/default/sonarr
    sudo rm /etc/init.d/sonarr
  fi
    sudo rm /install/.sonarr.lock
    apt-get -y autoremove && apt-get -y autoclean
    service apache2 reload
}

_removeSonarr
