#!/bin/bash
#
# [Quick Box :: Install Headphones 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) 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.

YELLOW='\e[93m'
RED='\e[91m'
ENDCOLOR='\033[0m'
CYAN='\e[96m'
GREEN='\e[92m'

OUTTO=/srv/rutorrent/home/db/output.log
USERNAME=$(cat /srv/rutorrent/home/db/master.txt)
PASSWD=$(cat /root/$USERNAME.info | cut -d ":" -f 3 | cut -d "@" -f 1)
local_setup=/etc/QuickBox/setup/


APPNAME='headphones'
APPSHORTNAME='hp'
APPPATH='/home/'$USERNAME'/.headphones'
APPTITLE='Headphones'
APPDEPS='git-core python python-cheetah python-pyasn1'
APPGIT='https://github.com/rembo10/headphones.git'
APPDPORT='8181'
APPSETTINGS=$APPPATH'/config.ini'
PORTSEARCH='http_port = '
USERSEARCH='http_username = '
PASSSEARCH='http_password = '
# New password encrypted
NEWPASS='$PASSWD'
# New password unencrypted
APPNEWPASS='$PASSWD'

echo
sleep 1
echo -e "Refreshing packages list ..." >>"${OUTTO}" 2>&1;
echo -e $YELLOW'--->Refreshing packages list...'$ENDCOLOR
sudo apt-get update

echo
sleep 1
echo -e "Installing prerequisites for $APPTITLE ..." >>"${OUTTO}" 2>&1;
echo -e $YELLOW'--->Installing prerequisites for '$APPTITLE'...'$ENDCOLOR
sudo apt-get -y install $APPDEPS

echo
sleep 1
echo -e "Downloading latest $APPTITLE ... " >>"${OUTTO}" 2>&1;
echo -e $YELLOW'--->Downloading latest '$APPTITLE'...'$ENDCOLOR
git clone $APPGIT $APPPATH || { echo -e $RED'Git not found.'$ENDCOLOR ; exit 1; }

echo
sleep 1
echo -e "Setting $APPTITLE permissions for $USERNAME ... " >>"${OUTTO}" 2>&1;
echo -e $YELLOW'--->Setting '$APPTITLE' permissions for '$USERNAME'...'$ENDCOLOR
chown www-data: /etc/apache2/sites-enabled/$APPNAME.conf  >/dev/null 2>&1
sudo chown -R $USERNAME:$USERNAME $APPPATH >/dev/null 2>&1
sudo chmod -R 775 $APPPATH >/dev/null 2>&1
sudo chmod -R g+s $APPPATH >/dev/null 2>&1

echo
sleep 1
echo -e "Configuring $APPTITLE Install ..." >>"${OUTTO}" 2>&1;
echo -e $YELLOW'--->Configuring '$APPTITLE' Install...'$ENDCOLOR
APPSHORTNAMEU="${APPSHORTNAME^^}"
DEFAULTFILE='/tmp/'$APPNAME'_default'
echo $APPSHORTNAMEU"_HOME="$APPPATH"/" >> $DEFAULTFILE || { echo 'Could not create '$APPTITLE' default file.' ; exit 1; }
echo $APPSHORTNAMEU"_DATA="$APPPATH"/" >> $DEFAULTFILE
echo -e 'Enabling user '$CYAN$USERNAME$ENDCOLOR' to run '$APPTITLE'...'
echo $APPSHORTNAMEU"_USER="$USERNAME >> $DEFAULTFILE
sudo mv $DEFAULTFILE "/etc/default/"$APPNAME || { echo 'Could not move '$APPTITLE' default file.' ; exit 1; }
/etc/init.d/$APPNAME start >/dev/null 2>&1
/etc/init.d/$APPNAME stop >/dev/null 2>&1
sudo cp $APPPATH/init-scripts/init.ubuntu /etc/init.d/headphones || { echo $RED'Creating init file failed.'$ENDCOLOR ; exit 1; }

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

echo
sleep 1
echo -e "Enabling $APPTITLE Autostart at Boot ... " >>"${OUTTO}" 2>&1;
echo -e $YELLOW'--->Enabling '$APPTITLE' Autostart at Boot...'$ENDCOLOR
sudo chown $USERNAME:$USERNAME /etc/init.d/$APPNAME
sudo chmod +x /etc/init.d/$APPNAME
sudo update-rc.d $APPNAME defaults

echo
sleep 1
echo -e "Starting $APPTITLE ... " >>"${OUTTO}" 2>&1;
echo -e $YELLOW'--->Starting '$APPTITLE$ENDCOLOR
/etc/init.d/$APPNAME start >/dev/null 2>&1
/etc/init.d/$APPNAME stop >/dev/null 2>&1

sudo sed -i 's@http_host = localhost@http_host = 0.0.0.0@g' $APPSETTINGS  || { echo -e $RED'Modifying http_host in config file failed.'$ENDCOLOR; exit 1; }

/etc/init.d/$APPNAME start

touch /install/.$APPNAME.lock
echo

echo "$APPTITLE Install Complete!" >>"${OUTTO}" 2>&1;
sleep 5
echo >>"${OUTTO}" 2>&1;
echo >>"${OUTTO}" 2>&1;
echo "Close this dialog box to refresh your browser" >>"${OUTTO}" 2>&1;
service apache2 reload > /dev/null 2>&1

exit
