#!/bin/bash

if [ "x$(id -u)" != 'x0' ]; then
	echo 'Error: this script can only be executed by root'
	exit 1
elif [ -f /etc/os-release ]; then
    . /etc/os-release
    OS=$NAME
elif type lsb_release >/dev/null 2>&1; then
    OS=$(lsb_release -si)
elif [ -f /etc/lsb-release ]; then
    . /etc/lsb-release
    OS=$DISTRIB_ID
elif [ -f /etc/debian_version ]; then
    OS=Debian
elif [ -f /etc/redhat-release ]; then
    OS=RHEL
else
    OS=$(uname -s)
fi

if [ "$OS" == "Ubuntu" ] || [ "$OS" == "Debian" ] || [ "$OS" == "Debian GNU/Linux" ]; then 
	if [ ! -z "$(ls -A ./)" ]; then
		printf "Error: Directory not empty.\nVWI must be installed in clean directory. Exiting ...\n"
		exit 1
	fi
	printf "Checking for required packages ...\n"
	if dpkg -s wget &> /dev/null
        then
                echo "wget found"
        else
                echo "wget not found. Installing ..."
                apt-get install -y wget > /dev/null
        fi
        if dpkg -s tar &> /dev/null
        then
                echo "tar found"
        else
                echo "tar not found. Installing ..."
                apt-get install -y tar > /dev/null
        fi
        if dpkg -s git &> /dev/null
        then
                echo "git found"
        else
                echo "git not found. Installing ..."
                apt-get install -y git > /dev/null
        fi

	printf "\nInstalling Vesta Web Interface frontend ...\n"
	git clone --quiet https://github.com/cdgco/VestaWebInterface . > /dev/null

  	git checkout --quiet 4e69e98 > /dev/null

	if [ -f README.md ] ; then
		rm README.md
	fi
	if [ -f 'VWI Banner.png' ] ; then
		rm 'VWI Banner.png'
	fi
	chmod 777 includes
   	chmod 777 tmp
   	chmod 777 plugins/images/uploads
	printf "Installing Vesta Web Interface backend ...\n"
	sleep .5
	if [ -d /usr/local/vesta/softaculous/enduser/themes/default ] ; then
		cp /usr/local/vesta/softaculous/enduser/themes/default /usr/local/vesta/softaculous/enduser/themes/original-default -r
	fi
	if [ -d /usr/local/vesta/softaculous/enduser/themes/simple ] ; then
		cp /usr/local/vesta/softaculous/enduser/themes/simple /usr/local/vesta/softaculous/enduser/themes/original-simple -r
	fi
	wget -q https://github.com/cdgco/VestaWebInterface/releases/download/v2.2.0/backend.tar.gz
	if [ -f backend.tar.gz ] ; then
		tar -xzf backend.tar.gz -C /usr/local/
		rm backend.tar.gz
	fi
	read -p "Enter the full web address of your installation: "
        echo "<?php \$vwipanel = '$REPLY'; ?>" > /usr/local/vesta/web/vwi/config.php
	printf "\nInstallation Complete! Please visit your website online to finish configuration.\n"
elif [ "$OS" == "CentOS Linux" ] || [ "$OS" == "RHEL" ]; then
	if [ ! -z "$(ls -A ./)" ]; then
		printf "Error: Directory not empty.\nVWI must be installed in clean directory. Exiting ...\n"
		exit 1
	fi
	printf "Checking for required packages ...\n"
        if rpm -q wget &> /dev/null
	then
		echo "wget found"
	else
		echo "wget not found. Installing ..."
		yum -y install wget
	fi
	if rpm -q tar &> /dev/null
	then
		echo "tar found"
	else
		echo "tar not found. Installing ..."
		yum -y install tar
	fi
	if rpm -q git &> /dev/null
	then
		echo "git found"
	else
		echo "git not found. Installing ..."
		yum -y install git
	fi
	printf "\nInstalling Vesta Web Interface frontend ...\n"
	git clone --quiet https://github.com/cdgco/VestaWebInterface . > /dev/null

  	git checkout --quiet 4e69e98 > /dev/null

	if [ -f README.md ] ; then
		rm README.md
	fi
	if [ -f 'VWI Banner.png' ] ; then
		rm 'VWI Banner.png'
	fi
	chmod 777 includes
   	chmod 777 tmp
   	chmod 777 plugins/images/uploads
	printf "Installing Vesta Web Interface backend ...\n"
	sleep .5
	if [ -d /usr/local/vesta/softaculous/enduser/themes/default ] ; then
		cp /usr/local/vesta/softaculous/enduser/themes/default /usr/local/vesta/softaculous/enduser/themes/original-default -r
	fi
	if [ -d /usr/local/vesta/softaculous/enduser/themes/simple ] ; then
		cp /usr/local/vesta/softaculous/enduser/themes/simple /usr/local/vesta/softaculous/enduser/themes/original-simple -r
	fi
	wget -q https://github.com/cdgco/VestaWebInterface/releases/download/v2.2.0/backend.tar.gz
	if [ -f backend.tar.gz ] ; then
		tar -xzf backend.tar.gz -C /usr/local/
		rm backend.tar.gz
	fi
	read -p "Enter the full web address of your installation: "
        echo "<?php \$vwipanel = '$REPLY'; ?>" > /usr/local/vesta/web/vwi/config.php
	printf "\nInstallation Complete! Please visit your website online to finish configuration.\n"
else
	echo 'Error: VWI can only be installed on Debian, Ubuntu, CentOS, or RHEL. Exiting ...\n'
	exit 1
fi
