#!/bin/bash
#
# [Quick Box :: Install Radarr package]
#
# GITHUB REPOS
# GitHub _ packages  :   https://github.com/QuickBox/QB
# LOCAL REPOS
# Local _ packages   :   /etc/QuickBox/packages
# Author             :   PastaGringo | KarmaPoliceT2
# URL                :   https://quickbox.io
#
# QuickBox Copyright (C) 2017 QuickBox.io
# 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.
#################################################################################
function _string() { perl -le 'print map {(a..z,A..Z,0..9)[rand 62] } 0..pop' 15 ; }
#################################################################################

function _installRadarrIntro() {
  echo "Radarr will now be installed." >>"${OUTTO}" 2>&1;
  echo "This process may take up to 2 minutes." >>"${OUTTO}" 2>&1;
  echo "Please wait until install is completed." >>"${OUTTO}" 2>&1;
  # output to box
  echo "Radarr will now be installed."
  echo "This process may take up to 2 minutes."
  echo "Please wait until install is completed."
  echo
  sleep 5
}

function _installRadarrDependencies() {
  # output to box
  echo "Installing dependencies ... "
if [[ ! -f /etc/apt/sources.list.d/mono-official-stable.list ]]; then
  if [[ $distribution == "Ubuntu" ]]; then
    apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF >/dev/null 2>&1
  elif [[ $distribution == "Debian" ]]; then
      exit
  fi
  echo "deb https://download.mono-project.com/repo/ubuntu stable-xenial main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list >/dev/null 2>&1
fi

apt-get update -y >/dev/null 2>&1
apt-get install -y mono-devel apt-transport-https >/dev/null 2>&1
}

function _installRadarrCode() {
  # output to box
  echo "Installing Radar ... "
  cd /opt
  wget $( curl -s https://api.github.com/repos/Radarr/Radarr/releases | grep linux.tar.gz | grep browser_download_url | head -1 | cut -d \" -f 4 ) > /dev/null 2>&1
  tar -xvzf Radarr.*.linux.tar.gz >/dev/null 2>&1
  rm -rf /opt/Radarr.*.linux.tar.gz
  touch /install/.radarr.lock
}

function _installRadarrConfigure() {
  # output to box
  echo "Configuring Radar ... "
cat > /etc/systemd/system/radarr.service <<EOF
[Unit]
Description=Radarr Daemon
After=syslog.target network.target

[Service]
User=${username}
Group=${username}
Type=simple
ExecStart=/usr/bin/mono /opt/Radarr/Radarr.exe -nobrowser
TimeoutStopSec=20
KillMode=process
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

  cat > /etc/apache2/sites-enabled/radarr.conf <<EOF
<Location /radarr>
ProxyPass http://localhost:7878/radarr
ProxyPassReverse http://localhost:7878/radarr
AuthType Digest
AuthName "rutorrent"
AuthUserFile '/etc/htpasswd'
Require user ${username}
</Location>
EOF

  mkdir -p /home/${username}/.config
  chown -R ${username}:${username} /home/${username}/.config
  chmod 775 /home/${username}/.config
  chown -R ${username}:${username} /opt/Radarr/
  chown www-data:www-data /etc/apache2/sites-enabled/radarr.conf
  systemctl daemon-reload
  systemctl enable radarr.service > /dev/null 2>&1
  systemctl start radarr.service
  sleep 10

  cp ${local_setup}configs/Radarr/config.xml /home/${username}/.config/Radarr/config.xml
  chown ${username}:${username} /home/${username}/.config/Radarr/config.xml

  systemctl stop radarr.service
  sleep 10

  if [[ -f /home/${username}/.config/Radarr/config.xml ]]; then
    #sed -i "s/<UrlBase>.*/<UrlBase>radarr<\/UrlBase>/g" /home/${username}/.config/Radarr/config.xml
    #sed -i "s/<BindAddress>.*/<BindAddress>127.0.0.1<\/BindAddress>/g" /home/${username}/.config/Radarr/config.xml
    service apache2 reload
  else
    # output to dashboard
    echo "ERROR INSTALLING - COULD NOT FIND config.xml in /home/${username}/.config/Radarr/config.xml" >> "${OUTTO}" 2>&1
    # output to box
    echo "ERROR INSTALLING - COULD NOT FIND config.xml in /home/${username}/.config/Radarr/config.xml"
  fi
}

function _installRadarrStart() {
  # output to box
  echo "Starting Radar ... "
  systemctl start radarr.service
}

function _installRadarrFinish() {
  # output to dashboard
  echo "Radarr Install Complete!" >>"${OUTTO}" 2>&1;
  echo "You can access it at  : http://$ip/radarr" >>"${OUTTO}" 2>&1;
  echo >>"${OUTTO}" 2>&1;
  echo >>"${OUTTO}" 2>&1;
  echo "Close this dialog box to refresh your browser" >>"${OUTTO}" 2>&1;
  # output to box
  echo "Radarr Install Complete!"
  echo "You can access it at  : http://$ip/radarr"
  echo
  echo "Close this dialog box to refresh your browser"
}

function _installRadarrExit() {
	exit 0
}

OUTTO=/srv/rutorrent/home/db/output.log
local_setup=/etc/QuickBox/setup/
local_packages=/etc/QuickBox/packages/
username=$(cat /srv/rutorrent/home/db/master.txt)
distribution=$(lsb_release -is)
ip=$(curl -s http://whatismyip.akamai.com)

_installRadarrIntro
echo "Installing dependencies ... " >>"${OUTTO}" 2>&1;_installRadarrDependencies
echo "Installing Radar ... " >>"${OUTTO}" 2>&1;_installRadarrCode
echo "Configuring Radar ... " >>"${OUTTO}" 2>&1;_installRadarrConfigure
echo "Starting Radar ... " >>"${OUTTO}" 2>&1;_installRadarrStart
_installRadarrFinish
_installRadarrExit
