#!/bin/bash
# znc-install
scriptversion="0.5.0"
scriptname="znc-install"
# adamaze
#
# wget -qO ~/znc-install.sh http://git.io/hrKo && bash ~/znc-install.sh
#
############################
#### Script Notes Start ####
############################
#
# Todo:
#	add configuring help?
#
############################
##### Script Notes End #####
############################
#
############################
## Version History Starts ##
############################
#
# 0.5.0 - initial commit
#
############################
### Version History Ends ###
############################
#
############################
###### Variable Start ######
############################
#
updaterenabled="0"
#
scripturl="https://raw.githubusercontent.com/frankthetank7254/feral/master/znc-install.sh"
#
############################
####### Variable End #######
############################
#
############################
#### Self Updater Start ####
############################
#
if [[ "$updaterenabled" -eq 1 ]]
then
    [[ ! -d ~/bin ]] && mkdir -p ~/bin
    [[ ! -f ~/bin/"$scriptname" ]] && wget -qO ~/bin/"$scriptname" "$scripturl"
    #
    wget -qO ~/.000"$scriptname" "$scripturl"
    #
    if [[ $(sha256sum ~/.000"$scriptname" | awk '{print $1}') != $(sha256sum ~/bin/"$scriptname" | awk '{print $1}') ]]
    then
        echo -e "#!/bin/bash\nwget -qO ~/bin/$scriptname $scripturl\ncd && rm -f $scriptname{.sh,}\nbash ~/bin/$scriptname\nexit" > ~/.111"$scriptname"
        bash ~/.111"$scriptname"
        exit
    else
        if [[ -z $(ps x | fgrep "bash $HOME/bin/$scriptname" | grep -v grep | head -n 1 | awk '{print $1}') && $(ps x | fgrep "bash $HOME/bin/$scriptname" | grep -v grep | head -n 1 | awk '{print $1}') -ne "$$" ]]
        then
            echo -e "#!/bin/bash\ncd && rm -f $scriptname{.sh,}\nbash ~/bin/$scriptname\nexit" > ~/.222"$scriptname"
            bash ~/.222"$scriptname"
            exit
        fi
    fi
    cd && rm -f .{000,111,222}"$scriptname"
    chmod -f 700 ~/bin/"$scriptname"
else
    echo
    echo "The Updater has been disabled"
fi
#
############################
##### Self Updater End #####
############################
#
############################
#### Core Script Starts ####
############################
#
#echo
#echo -e "Hello $(whoami), you have the latest version of the" "\033[36m""$scriptname""\e[0m" "script. This script version is:" "\033[31m""$scriptversion""\e[0m"
#echo
#read -ep "The script has been updated, enter [y] to continue or [q] to exit: " -i "y" updatestatus
#echo
#if [[ "$updatestatus" =~ ^[Yy]$ ]]
#then
#
############################
#### User Script Starts ####
############################
#
echo
REPLY="n"
read -p "Do you need the extra charset available in ZNC? (This is for non-english characters) [y/N]" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
	if [ "$(dpkg-query -s libicu-dev 2>&1 | grep -c 'ok installed')" -eq 0 ]; then
		echo -e "\033[33m""It looks like libicu-dev is not installed on your server.""\e[0m"
		echo -e Please open a ticket: "\033[34m"https://www.feralhosting.com/manager/tickets/new"\e[0m"
		echo "Paste the following into the ticket:"
		echo
		echo "Please can you install the ZNC 1.6 dependancy for charset

https://packages.debian.org/wheezy/libicu-dev

apt-get install libicu-dev

Thank you."
		echo
		echo -e "\033[33m""Once you are notified that the dependancy has been installed, you can re-run this script""\e[0m"
		exit
	fi
fi

echo -e "\033[33m""This script will download and install znc on your slot. It may take a few minutes...""\e[0m"
sleep 3
mkdir -p ~/bin
wget -qO ~/znc.tar.gz http://znc.in/releases/znc-latest.tar.gz
cd ~/
tar xf ~/znc.tar.gz && cd ~/znc-1.*
./configure --prefix=$HOME
make && make install
cd && rm -rf znc{-1.*,.tar.gz}
echo
echo -e "\033[33m""\nNow it is time for initial configuration...""\e[0m"
echo
~/bin/znc --makeconf
#
echo -e "\033[33m""\nNow that ZNC has been installed, configured, and started, we will make sure it starts if/when your server reboots.""\e[0m"
#
# adding to cron
tmpcron=$(mktemp)
        if [ "$(crontab -l 2> /dev/null | grep -c znc)" == "0" ]; then
        echo "appending znc to crontab."
        crontab -l 2> /dev/null > "$tmpcron"
        echo "@reboot ~/bin/znc" >> "$tmpcron"
        crontab "$tmpcron"
        rm "$tmpcron"
else
        echo "znc is already in crontab"
fi
# give user the full URL
echo -e "\nClick on the URL below to do additional configuration if needed"
echo -e "\033[33m""https://$(hostname -f):$(grep Port ~/.znc/configs/znc.conf | awk '{print $3}')""\e[0m"
echo -e "\nSearch the ZNC wiki for help connecting with different clients. http://wiki.znc.in/"
#
#
############################
##### User Script End  #####
############################
#
#else
#    echo -e "You chose to exit after updating the scripts."
#    echo
#    cd && bash
#    exit 1
#fi
#
############################
##### Core Script Ends #####
############################
#
