#!/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.5 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 "*                   Check OS Version                       *"
echo -e "************************************************************"
os_codename=`cat /etc/os-release | grep -e VERSION_CODENAME | awk -F '=' '{print $2}' | xargs`
if [ "$os_codename" != "bookworm" ]; then
        echo -e "${red}The current OS isn't compatible with VitalPBX...${txtrst}"
        exit ;
fi

echo -e "************************************************************"
echo -e "*              install sudo, curl and pgd                  *"
echo -e "************************************************************"
apt install sudo -y
sudo DEBIAN_FRONTEND=noninteractive apt install curl gpg -y

echo -e "************************************************************"
echo -e "*                        Disable UFW                       *"
echo -e "************************************************************"
if [ -x "$(command -v ufw)" ]; then
	sudo ufw disable
fi

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

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

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 net-tools systemd-timesyncd"
PACKAGES="$PACKAGES nginx php-fpm ssl-cert apache2-utils"

echo -e "************************************************************"
echo -e "*              Web Pabackages & MariaDB...                 *"
echo -e "************************************************************"
PACKAGES="$PACKAGES 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"
sudo DEBIAN_FRONTEND=noninteractive apt install -y $PACKAGES

echo -e "************************************************************"
echo -e "*                 Web Server Configuration                 *"
echo -e "************************************************************"
systemctl enable nginx.service
systemctl restart nginx.service

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

echo -e "************************************************************"
echo -e "*     Installing VitalPBX/Security Packages pacakges       *"
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"
sudo 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 "*    Disable IPTables to Avoid Conflicts with FirewallD    *"
echo -e "************************************************************"
systemctl disable iptables 2>/dev/null
sed -i 's/^#bind-interfaces/bind-interfaces/g' /etc/dnsmasq.conf

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 Initial setup                    *"
echo -e "************************************************************"
systemctl enable vpbx-setup.service

echo -e "************************************************************"
echo -e "*                  Configure Packages                      *"
echo -e "************************************************************"
dpkg --configure -a

echo -e "************************************************************"
echo -e "*                       Delete me                          *"
echo -e "************************************************************"
rm /etc/profile.d/install_vpbx45.sh

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