#!/bin/bash

echo -e "\n"
echo -e "************************************************************"
echo -e "*          Welcome to the VitalPBX  installation           *"
echo -e "*   For this process it is necessary that you have an      *"
echo -e "*      internet connection, since the latest version of    *"
echo -e "*                VitalPBX 4 will be installed              *"
echo -e "************************************************************"
while [[ $veryfy_info != yes && $veryfy_info != no ]]
do
    read -p "Are you sure to continue with the installation?? (yes,no) > " veryfy_info 
done

if [ "$veryfy_info" = yes ] ;then
	echo -e "************************************************************"
	echo -e "*              Starting installation process               *"
	echo -e "************************************************************"
else
    	exit;
fi

echo -e "************************************************************"
echo -e "*            Install Dependencies & SUDO                   *"
echo -e "************************************************************"
DEBIAN_FRONTEND=noninteractive apt install curl gpg sudo -y

echo -e "************************************************************"
echo -e "*                        Disable UFW                       *"
echo -e "************************************************************"
#Disable UFW
if [ -x "$(command -v ufw)" ]; then
	echo -e "${green}Disabling UFW...${txtrst}"
	sudo ufw disable
fi

echo -e "************************************************************"
echo -e "*                   Update & Upgrade System                *"
echo -e "************************************************************"
apt -y update
DEBIAN_FRONTEND=noninteractive apt -y upgrade
apt -y clean

echo -e "************************************************************"
echo -e "*                  Installing VitalPBX Repo                *"
echo -e "************************************************************"
curl -fsSL https://repo.vitalpbx.com/vitalpbx/v4/apt/setup_repo | bash -

echo -e "************************************************************"
echo -e "*           Update after setup VPBX Repo...                *"
echo -e "************************************************************"
apt -y update
DEBIAN_FRONTEND=noninteractive apt -y upgrade
apt -y clean

echo -e "************************************************************"
echo -e "*              Installing dependencies...                  *"
echo -e "************************************************************"
PACKAGES="acl cron lame sox ffmpeg aptitude postfix nmap"

echo -e "************************************************************"
echo -e "*              Web Pabackages & MariaDB...                 *"
echo -e "************************************************************"
PACKAGES="$PACKAGES apache2 php8.1-fpm php-common php-xml php-intl php-pear php-bcmath php-cli php-mysql php-zip"
PACKAGES="$PACKAGES php-gd php-mbstring php-json php-imagick php-curl php-ioncube-loader openvpn unzip"
PACKAGES="$PACKAGES mariadb-server unixodbc odbcinst unixodbc-dev"

echo -e "************************************************************"
echo -e "*           Installing MariaDB, apache2 and php 8          *"
echo -e "************************************************************"
DEBIAN_FRONTEND=noninteractive apt install -y $PACKAGES

echo -e "************************************************************"
echo -e "*                   Configuring Apache...                  *"
echo -e "************************************************************"
a2dismod -q php8.1 || :
a2enconf -q php8.1-fpm || :
a2enmod -q proxy_fcgi || :
a2enmod -q proxy_http || :
a2enmod -q ssl || :
a2enmod -q headers || :
a2enmod -q rewrite || :

echo -e "************************************************************"
echo -e "*         Enabling HTTP/2 support for Apache...            *"
echo -e "************************************************************"
a2dismod -q mpm_prefork || :
a2enmod -q mpm_event || :
a2enmod -q ssl || :
a2enmod -q http2 || :

echo -e "************************************************************"
echo -e "*             Starting & Enabling Services...              *"
echo -e "************************************************************"
systemctl enable mariadb.service
systemctl restart mariadb.service

systemctl enable apache2.service
systemctl restart apache2.service

echo -e "************************************************************"
echo -e "*      Installing VitalPBX packages and Firewall...        *"
echo -e "************************************************************"
PACKAGES="asterisk-pbx asterisk-pbx-modules asterisk-pbx-g729 sngrep vitalpbx-asterisk-config vitalpbx-fail2ban-config vitalpbx-api"
PACKAGES="$PACKAGES firewalld fail2ban iptables iptables-persistent"
PACKAGES="$PACKAGES vitalpbx-i18n vitalpbx-helper vitalpbx-assets vitalpbx-scripts vitalpbx-monitor vitalpbx"
DEBIAN_FRONTEND=noninteractive apt install -y $PACKAGES

echo -e "************************************************************"
echo -e "*                  Configuring Firewall...                 *"
echo -e "************************************************************"
sed -i 's/^FirewallBackend=.*/FirewallBackend=iptables/g' /etc/firewalld/firewalld.conf
update-alternatives --set iptables /usr/sbin/iptables-legacy
update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy

systemctl enable firewalld.service
systemctl enable fail2ban.service

echo -e "************************************************************"
echo -e "*                    Set Permissions                       *"
echo -e "************************************************************"
chown -R asterisk.asterisk /etc/asterisk /var/lib/asterisk /var/log/asterisk /var/spool/asterisk /usr/lib/asterisk
chown -R www-data:root /etc/asterisk/vitalpbx

echo -e "************************************************************"
echo -e "*             Running the initial setup...                 *"
echo -e "************************************************************"
systemctl enable vpbx-setup.service

#Configure Packages
dpkg --configure -a

# Delete me
rm /etc/profile.d/install_vpbx4.sh

echo -e "************************************************************"
echo -e "*                Rebooting the System...                   *"
echo -e "************************************************************"
reboot
