#!/bin/bash
#
# [Quick Box :: Install spectrogram plugin]
#
# GITHUB REPOS
# GitHub _ plugins   :   https://github.com/QuickBox/QB/tree/master/rtplugins
# LOCAL REPOS
# Local _ packages   :   /etc/QuickBox/packages
# Local _ plugins    :   /etc/QuickBox/rtplugins
# Author             :   QuickBox.IO | JMSolo
# URL                :   https://quickbox.io
#
# QuickBox Copyright (C) 2018 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.
#

# To get most output from commands that are run, set to nothing
MIN_OUTPUT=  #yes

# To print to shell, set to nothing, otherwise output to LOGFILE
USE_LOGFILE=yes
LOGFILE=/srv/rutorrent/home/db/output.log # QuickBox logfile

# To print to shell, set to nothing, otherwise output to LOGFILE
rutorrent=/srv/rutorrent/

# Location of QuickBox ruTorrent plugins on localhost
PLUGINVAULT=/etc/QuickBox/rtplugins/

# Plugin to install
PLUGIN=spectrogram

################################################################################
#####          Should not need to change anything below this line          #####
################################################################################

_print() {
  if [[ -z "${USE_LOGFILE}" ]]; then
    echo $1
  else
    echo $1 >>"${LOGFILE}" 2>&1
  fi
}

_do() {
  if [[ ! -z "${MIN_OUTPUT}" ]]; then
    ($1 >/dev/null 2>&1)
  else
    ($1)
  fi
}
_sudo() {
  _do "sudo $1"
}

_installBaseDeps() {
  _print "Installing plugin dependencies: sox ..."

  if [[ ! -n `which sox` ]]; then
    _sudo "apt-get -yqq install sox"
  else
    _print "OK, sox found"
  fi
}

_installplugin-spectrogram() {
  username=$(whoami);
  _print "Accessing $PLUGIN in the plugin vault ... "
  cd ${rutorrent}plugins
  sleep 5

  _print "Installing $PLUGIN plugin ... "
    for i in $PLUGIN; do
      cp -R "${PLUGINVAULT}$i" .
    done
  sleep 5

  _print "Adjusting permissions ... "
  chown -R www-data: ${rutorrent}plugins/$PLUGIN
  _print "$PLUGIN Plugin Install Complete!"

  sleep 5
}

_done() {
  _print
  _print
  _print "Close this dialog box to refresh your browser"
  exit
}

_installBaseDeps
_installplugin-spectrogram
_done
