#!/bin/bash
#
# [Quick Box :: Install AutoDL-IRSSI package]
#
# QUICKLAB REPOS
# QuickLab _ packages  :   https://github.com/QuickBox/quickbox_packages
# LOCAL REPOS
# Local _ packages   :   /etc/QuickBox/packages
# Author             :   QuickBox.IO
# 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.
#
#   QuickBox.IO does not grant the end-user the right to distribute this
#   code in a means to supply commercial monetization. If you would like
#   to include QuickBox in your commercial project, write to echo@quickbox.io
#   with a summary of your project as well as its intended use for moentization.
#
function _string() { perl -le 'print map {(a..z,A..Z,0..9)[rand 62] } 0..pop' 15 ; }

MASTER=$(cat /srv/rutorrent/home/db/master.txt)
IRSSIIP=$(curl -s http://ipecho.net/plain || curl -s http://ifconfig.me/ip ; echo)

OUTTO="/root/quick-box.log"
local_setup=/etc/QuickBox/setup/


function _installautodl() {
  rutorrent="/srv/rutorrent/";
  PLUGINVAULT="/etc/QuickBox/rtplugins/"; cd "${rutorrent}plugins"
  PLUGIN="autodl-irssi"
    for i in $PLUGIN; do
      cp -R "${PLUGINVAULT}$i" .
      chown -R www-data: ${rutorrent}plugins/$PLUGIN
    done
    for u in "$(cat /etc/htpasswd | cut -d ':' -f 1 | uniq)"; do
      IRSSI_PASS=$(_string)
      IRSSI_PORT=$(shuf -i 20000-61000 -n 1)
      mkdir -p "/home/${u}/.irssi/scripts/autorun/" >>"${OUTTO}" 2>&1
      cd "/home/${u}/.irssi/scripts/"

      autodlver=$(wget -q https://github.com/autodl-community/autodl-irssi/releases/latest -O - | grep -E \/tag\/ | grep -v repository | awk -F "[><]" '{print $3}')
      wget -q https://github.com/autodl-community/autodl-irssi/releases/download/$autodlver/autodl-irssi-v$autodlver.zip
      unzip -o autodl-irssi-v$autodlver.zip
      rm -rf autodl-irssi-v$autodlver.zip

      #curl -sL http://git.io/vlcND | grep -Po '(?<="browser_download_url": ")(.*-v[\d.]+.zip)' | xargs wget --quiet -O autodl-irssi.zip >/dev/null 2>&1
      #unzip -o autodl-irssi.zip >>"${OUTTO}" 2>&1
      #rm autodl-irssi.zip

      cp autodl-irssi.pl autorun/
      mkdir -p "/home/${u}/.autodl" >>"${OUTTO}" 2>&1
      touch "/home/${u}/.autodl/autodl.cfg"
      cat >"/home/${u}/.autodl/autodl.cfg"<<ADC
[options]
gui-server-port = ${IRSSI_PORT}
gui-server-password = ${IRSSI_PASS}
ADC

#cat >"/home/${u}/.autodl/autodl2.cfg"<<ADC
#[options]
#gui-server-port = ${IRSSI_PORT}
#gui-server-password = ${IRSSI_PASS}
#ADC

CONFPATH="/srv/rutorrent/conf/users/${u}/plugins/autodl-irssi/"
mkdir -p $CONFPATH
cat >"/srv/rutorrent/conf/users/${u}/plugins/autodl-irssi/conf.php"<<"ADI"
<?php
$autodlPort = IRSSI_PORT;
$autodlPassword = IRSSI_PASS;
?>
ADI
sed -i "s/IRSSI_PORT/${IRSSI_PORT}/g" /srv/rutorrent/conf/users/${u}/plugins/autodl-irssi/conf.php
sed -i "s/IRSSI_PASS/${IRSSI_PASS}/g" /srv/rutorrent/conf/users/${u}/plugins/autodl-irssi/conf.php

      chown -R "${u}.${u}" "/home/${u}/.irssi/"
      chown -R "${u}.${u}" "/home/${u}"
    done

  touch /install/.autodlirssi.lock
    cp ${local_setup}templates/sysd/autodlirssi.template /etc/systemd/system/irssi@.service
    systemctl enable irssi@${username} > /dev/null 2>&1
    systemctl start irssi@${username} > /dev/null 2>&1
  cd
}

_installautodl
