#!/bin/bash
#
# [Quick Box :: Install pyLoad package]
#
# QUICKLAB REPOS
# QuickLab _ 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) 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 _installpyLoad1() {
  apt-get install -y tesseract-ocr gocr rhino pyqt4-dev-tools python-imaging >/dev/null 2>&1
  apt-get -y autoremove >/dev/null 2>&1
}

function _installpyLoad2() {
  mkdir /home/${MASTER}/.pip && cd /home/${MASTER}/.pip
  wget https://bootstrap.pypa.io/get-pip.py >/dev/null 2>&1
  python get-pip.py >/dev/null 2>&1
}

function _installpyLoad3() {
  pip install wheel --upgrade >/dev/null 2>&1
  pip install setuptools --upgrade >/dev/null 2>&1
  pip install ply --upgrade >/dev/null 2>&1
  pip install cryptography --upgrade >/dev/null 2>&1
  pip install distribute >/dev/null 2>&1
  pip install pyOpenSSL >/dev/null 2>&1
  pip install cffi --upgrade >/dev/null 2>&1
  pip install pycurl >/dev/null 2>&1
  pip install django >/dev/null 2>&1
  pip install pyimaging >/dev/null 2>&1
  pip install web2py >/dev/null 2>&1
  pip install beaker >/dev/null 2>&1
  pip install thrift >/dev/null 2>&1
  pip install pycrypto >/dev/null 2>&1
  pip install feedparser >/dev/null 2>&1
  pip install beautifulsoup >/dev/null 2>&1
  pip install tesseract >/dev/null 2>&1
}

function _installpyLoad4() {
  mkdir /home/${MASTER}/.pyload
  cd /home/${MASTER} && git clone https://github.com/pyload/pyload.git .pyload >/dev/null 2>&1
  printf "/home/${MASTER}/.pyload" > /home/${MASTER}/.pyload/module/config/configdir
  mkdir -p /var/run/pyload
}

function _installpyLoad5() {
cat >/etc/systemd/system/pyload@.service<<PYSV
[Unit]
Description=pyLoad
After=network.target

[Service]
Type=forking
KillMode=process
User=%I
ExecStart=/usr/bin/python /home/${MASTER}/.pyload/pyLoadCore.py --config=/home/${MASTER}/.pyload --pidfile=/var/run/pyload/pid --daemon
PIDFile=/var/run/pyload/pid
ExecStop=-/bin/kill -HUP
WorkingDirectory=/home/%I/

[Install]
WantedBy=multi-user.target

PYSV

}

function _installpyLoad6() {
  chown -R ${MASTER}.${MASTER} /home/${MASTER}/.pip
  chown -R ${MASTER}.${MASTER} /home/${MASTER}/.pyload
  chown -R ${MASTER}.${MASTER} /var/run/pyload
}

function _installpyLoad7() {
  touch /install/.pyload.lock
  systemctl daemon-reload >/dev/null 2>&1
  systemctl enable pyload@${MASTER}.service >/dev/null 2>&1
  systemctl start pyload@${MASTER}.service >/dev/null 2>&1
}

function _installpyLoad8() {
  echo "pyLoad Install Complete!" >>"${OUTTO}" 2>&1;
  echo "Please type: 'setup-pyLoad' in ssh to complete your pyload installation" >>"${OUTTO}" 2>&1;
  sleep 2
  echo >>"${OUTTO}" 2>&1;
  echo >>"${OUTTO}" 2>&1;
  echo "Close this dialog box to refresh your browser" >>"${OUTTO}" 2>&1;
}

function _installpyLoad9() {
  exit
}


MASTER=$(cat /srv/rutorrent/home/db/master.txt)
OUTTO=/srv/rutorrent/home/db/output.log


echo "Installing any additional dependencies needed for pyLoad ... " >>"${OUTTO}" 2>&1;_installpyLoad1
echo "Setting up python package management system in /home/${MASTER}/.pip ... " >>"${OUTTO}" 2>&1;_installpyLoad2
echo "Installing pyLoad packages ... " >>"${OUTTO}" 2>&1;_installpyLoad3
echo "Grabbing latest stable pyLoad repository ... " >>"${OUTTO}" 2>&1;_installpyLoad4
echo "Building pyLoad systemd template ... " >>"${OUTTO}" 2>&1;_installpyLoad5
echo "Adjusting permissions ... " >>"${OUTTO}" 2>&1;_installpyLoad6
echo "Enabling and starting pyLoad services ... " >>"${OUTTO}" 2>&1;_installpyLoad7
_installpyLoad8
_installpyLoad9
