#!/bin/bash


#############################################################################################
# ASTP v1.2.0  installer for ASTPP version 6
#
#
# This installation script is partly based on the iNetrix Technologies Pvt. Ltd.
# installtion script for installing ASTPP.
#
# This script will only install version 6 of ASTPP, and for Ububtu.
#
# The purpose of this script is for multiple reasons compared to the one
# providedby iNetrix:
#
# 1) Compiles FreeSwitch from source instead of using the SignalWire repo
#    which requires a SignalWire account with a token attached to that account
#
# 2) Uses MariaDB from repos instead of MySQL from their repo.
#    This has caused issues in the past
#
# 3) Does not use RemiRepo for PHP 7.3+. iNetrix for unknown reasons uses the RemiRepo
#
# 4) No longer support of CentOS 7/8 as those are End of Life
#    CentOS Stream is now the default distro, and not stable for production
#    May consider Rocky Linux 8 (CentOS 8 fork) in the future
#
# 5) Option to install Postfix instead of sendmail which provides better logging
#
# 6) Uses legacy IP Tables instead of UFW
#
# 7) No telemetry sent
#
# License https://www.gnu.org/licenses/agpl-3.0.html
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
#############################################################################################



# Sofia-Sip Version
sofia_version=1.13.7                        # release-version for sofia-sip to use

# FreeSWITCH Version
switch_version=1.10.9                       # which source code to download, only for source


#General Congifuration
TEMP_USER_ANSWER="no"
ASTPP_SOURCE_DIR=/opt/ASTPP
ASTPP_HOST_DOMAIN_NAME="host.domain.tld"
os_codename=$(lsb_release -cs)


#ASTPP Configuration
ASTPPDIR=/var/lib/astpp/
ASTPPEXECDIR=/usr/share/astpp/
ASTPPLOGDIR=/var/log/astpp/


#HTML and MariaDB Configuraition
WWWDIR=/var/www/html
ASTPP_DATABASE_NAME="astpp"
ASTPP_DB_USER="astppuser"

#Freeswich Configuration
FS_DIR=/usr/share/freeswitch/
FS_SOUNDSDIR=${FS_DIR}/sounds/en/us/callie




#Introduction
introduction()
{

printf "\033c"
echo ""
echo "**************** INTRODUCTION *******************"
echo ""
echo "This is an alternative to the iNetrix ASTPP installer."
echo "Their installer uses third party package repositories (repos) which has been having a lot of issues "
echo "in the past due to the use of those third party repos."
echo ""
echo "This installer instead will use only the Debian repo to install most of the needed requirements"
echo ""
echo "Do note, since this will compile* FreeSwitch from source, there will be a lot of packages which will"
echo "need to be locally installed. This process of installing packages and compiling of FreeSwitch"
echo "will take much longer than the iNetrix installer, but without the need for third party packages,"
echo "nor having to obtain an account with SignalWire."
echo ""
echo "Enjoy!"
echo ""
echo ""
echo ""
echo "*Compile time of FreeSwitch varies depending on performance of your server, and number of CPUs. "
echo ""
echo ""
echo ""
read -n 1 -s -r -p "Press any key to continue"




}


#Verify freeswitch token
verification ()
{
        output bold
        echo "                       Authentication required !!!!!!
Personal Access Tokens (PAT)s are required to access FreeSWITCH install packages."
        echo ""

        echo "VISIT below link to generate Personal Access Token
https://freeswitch.org/confluence/display/FREESWITCH/HOWTO+Create+a+SignalWire+Personal+Access+Token"
        sleep 3s
        echo "" && echo ""
        read -p "Enter your Personal Access Token: ${FS_TOKEN}"
        tput sgr0
        FS_TOKEN=${REPLY}
        echo ""
        if [ ."$os_codename" = ."bullseye" ]; then
                wget --http-user=signalwire --http-password=$FS_TOKEN -O /usr/share/keyrings/signalwire-freeswitch-repo.gpg https://freeswitch.signalwire.com/repo/deb/debian-release/signalwire-freeswitch-repo.gpg
                verify_debian10="$?"
                if [ $verify_debian10 = 0 ]; then
                        tput bold
                        echo "******************************************************************************"
                        echo ""
                        echo "You have entered valid token"
                        echo ""
                        echo "******************************************************************************"
                        sleep 4s
                        tput sgr0
                else
                        echo ""
                        tput bold
                        echo "Invalid token"
                        echo "******************************************************************************"
                        echo ""
                        echo "For more information go to https://id.signalwire.com/personal_access_tokens "
                        echo ""
                        echo "******************************************************************************"
                        sleep 3s
                        tput sgr0
                        exit 0
                fi

        fi
}


pre_install()
{

printf "\033c"

echo ""
echo "********************************"
echo "!!!!!!!!!!!!! NOTICE !!!!!!!!!!!"
echo "********************************"
echo ""
echo "This part of the script will give the option to install Postfix instead of Sendmail. "
echo "Postifx is preferred as it produces better logs than Sendmail in case for the need to troubleshoot sending email issues. "
echo "During installation of postfix, it will require some manual configurations, but the defaults will be ready to select."
echo ""
echo "You will only need to press the enter key to continue for the two questions, HOWEVER, it is reccomended you have the server" 
echo "Fully Qualified Domain Name (FQDN)/hosname already set (e.g. astpp.myserver.com) before continuing beyond this point."
echo ""
echo "If you have not done so, the FQDN/hostname can be easily set, as an example, with the command: "
echo "hostnamectl set-hostname astpp.yourhostname.com"
echo ""
echo "You will have to reboot/relog to make sure changes have been made."
echo ""
echo "As an alternative, you can select 'n' to instead install Sendmail. No additonal configuration is needed for it."
echo ""



read -n 1 -p "Do you wish to continue with installation of Postfix? (y/n/q[uit]) "
                if [ "$REPLY"   = "y" ]; then

			echo ""
			echo "Now installing Postfix"
			echo ""
			apt -y install postfix
		elif [ "$REPLY"   = "n" ]; then
			echo ""
			echo "Now installing Sendmail"
			echo ""
			apt -y install sendmail
		elif [ "$REPLY"   = "q" ]; then
			echo ""
			echo "Exiting"
			echo ""
			exit 0

		fi

} #end of pre_install










#Generate random password
genpasswd()
{
        length=$1
        digits=({1..9})
        lower=({a..z})
        upper=({A..Z})
        CharArray=(${digits[*]} ${lower[*]} ${upper[*]})
        ArrayLength=${#CharArray[*]}
        password=""
        for i in `seq 1 $length`
        do
                index=$(($RANDOM%$ArrayLength))
                char=${CharArray[$index]}
                password=${password}${char}
        done
        echo $password
}




get_linux_distribution()
{


        if [ $os_codename != 'bullseye' ] && [ $os_codename != 'focal' ]; then

                echo -e 'Sorry, but this script is only for Debian 11, and Unbuntu.'
				exit 1

        fi

} # endget_linux_distribution

#User Response Gathering
get_user_response ()
{

        echo ""
        echo ""
        read -p "Enter a FQDN: example (i.e ${ASTPP_HOST_DOMAIN_NAME}), or an IP Address for this server: "
        ASTPP_HOST_DOMAIN_NAME=${REPLY}
        echo "Your entered FQDN is : ${ASTPP_HOST_DOMAIN_NAME} "
        echo ""
        echo ""
        read -n 1 -p "Press any key to continue ... "
        NAT1=$(dig +short myip.opendns.com @resolver1.opendns.com)
        NAT2=$(curl http://ip-api.com/json/)
        INTF=$(ifconfig $1|sed -n 2p|awk '{ print $2 }'|awk -F : '{ print $2 }')
        if [ "${NAT1}" != "${INTF}" ]; then
                echo "Server is behind NAT";
                NAT="True"
        fi

}

install_prerequisties()
{

# install dependencies


apt update && apt -y upgrade

if [ ."$os_codename" = ."focal" ]; then

apt -y install wget lsb-release  systemd-sysv ca-certificates dialog nano net-tools openssl libssl-dev \
autoconf automake devscripts g++ git libncurses5-dev libtool make libjpeg-dev pkg-config flac libgdbm-dev \
libdb-dev gettext sudo equivs mlocate git dpkg-dev libpq-dev liblua5.2-dev libtiff5-dev libperl-dev \
libcurl4-openssl-dev libsqlite3-dev libpcre3-dev devscripts libspeexdsp-dev libspeex-dev libldns-dev \
libedit-dev libopus-dev libmemcached-dev libshout3-dev libmpg123-dev libmp3lame-dev yasm nasm libsndfile1-dev \
libuv1-dev libvpx-dev libavformat-dev libswscale-dev libvlc-dev libavformat-dev libswscale-dev \
wget curl git dnsutils ntpdate systemd net-tools whois sensible-mda mlocate vim imagemagick \
php-pear php-imagick libreoffice ghostscript sngrep software-properties-common lsb-release \
apt-transport-https ca-certificates unixodbc cmake uuid-dev sqlite3 unzip mariadb-server \
nginx ntpdate ntp lua5.1 bc libxml2-dev ed mcrypt



#if [ $os_codename = 'buster' ] || [ $os_codename = 'bullseye' ]; then

else

apt -y install wget lsb-release  systemd-sysv ca-certificates dialog nano net-tools openssl libssl-dev \
autoconf automake devscripts g++ git libncurses5-dev libtool make libjpeg-dev pkg-config flac libgdbm-dev \
libdb-dev gettext sudo equivs mlocate git dpkg-dev libpq-dev liblua5.2-dev libtiff5-dev libperl-dev \
libcurl4-openssl-dev libsqlite3-dev libpcre3-dev devscripts libspeexdsp-dev libspeex-dev libldns-dev \
libedit-dev libopus-dev libmemcached-dev libshout3-dev libmpg123-dev libmp3lame-dev yasm nasm libsndfile1-dev \
libuv1-dev libvpx-dev libavformat-dev libswscale-dev libvlc-dev libavformat-dev libswscale-dev libsndfile-dev \
wget curl git dnsutils ntpdate systemd net-tools whois sensible-mda mlocate vim imagemagick \
php-pear php-imagick libreoffice ghostscript sngrep software-properties-common lsb-release \
apt-transport-https ca-certificates unixodbc unixodbc cmake uuid-dev sqlite3 unzip mariadb-server \
nginx ntpdate ntp lua5.1 bc libxml2-dev ed mcrypt



fi


#Ion Cube Loader

cd /usr/src/
wget http://downloads3.ioncube.com/loader_downloads/ioncube_loaders_lin_x86-64.tar.gz
tar -xzvf ioncube_loaders_lin_x86-64.tar.gz

#sed -i "s|PermitRootLogin without-password\+|PermitRootLogin yes|g" /etc/ssh/sshd_config
#systemctl restart sshd

} #end install_prerequisties

#install dependencies that depend on the operating system version


os_dependencies()
{

os_codename=$(lsb_release -cs)


#Ubuntu 20.04.x
if [ ."$os_codename" = ."focal" ]; then

	apt update
       	apt install -y libvpx6 swig4.0 python3-distutils
	apt install -y php7.4 php7.4-fpm php7.4-mysql php7.4-cli php7.4-json php7.4-readline php7.4-xml php7.4-curl
	apt install -y php7.4-gd php7.4-json php7.4-mbstring php7.4-mysql php7.4-opcache php7.4-imap




#Debian 11
#if [ ."$os_codename" = ."bullseye" ]; then

elif [ ."$os_codename" = ."bullseye" ]; then

        #Need to add backports for IP tables 1.8.3 and monit.
	printf "%s\n" "deb http://ftp.de.debian.org/debian buster-backports main" | \
	tee /etc/apt/sources.list.d/buster-backports.list

	apt install -t buster-backports iptables -y 
	apt install -y libvpx6 swig4.0 python3-distutils mcrypt
	apt install -y php7.4 php7.4-fpm php7.4-mysql php7.4-cli php7.4-json php7.4-readline php7.4-xml php7.4-curl
	apt install -y php7.4-gd php7.4-json php7.4-mbstring php7.4-mysql php7.4-opcache php7.4-imap php7.4-geoip
	
	
	
elif [ ."$os_codename" = ."buster" ]; then
	
	   printf "%s\n" "deb http://ftp.de.debian.org/debian buster-backports main" | \
        tee /etc/apt/sources.list.d/buster-backports.list

	apt update

        apt install -t buster-backports iptables -y 

	apt install -y libvpx5 swig3.0 python3-distutils 
	apt install -y php7.3 php7.3-fpm php7.3-mysql php7.3-cli php7.3-json php7.3-readline php7.3-xml php7.3-curl
	apt install -y php7.3-gd php7.3-json php7.3-mbstring php7.3-mysql php7.3-opcache php7.3-imap
	
fi

} #end os_dependencies




install_freeswitch_packages()
{


#if [ ."$os_codename" = ."buster" ]; then
   
       


#elif  [ ."$os_codename" = ."bullseye" ]; then
                echo "Installing FREESWITCH"
                sleep 6s
                apt-get update && apt-get install -y gnupg2 wget lsb-release
                echo "machine freeswitch.signalwire.com login signalwire password $FS_TOKEN" > /etc/apt/auth.conf
                echo "deb [signed-by=/usr/share/keyrings/signalwire-freeswitch-repo.gpg] https://freeswitch.signalwire.com/repo/deb/debian-release/ `lsb_release -sc` main" > /etc/apt/sources.list.d/freeswitch.list
                echo "deb-src [signed-by=/usr/share/keyrings/signalwire-freeswitch-repo.gpg] https://freeswitch.signalwire.com/repo/deb/debian-release/ `lsb_release -sc` main" >> /etc/apt/sources.list.d/freeswitch.list
                apt-get update -y
                sleep 2s
                apt-get install freeswitch-meta-all -y
                
#fi        
        mv -f ${FS_DIR}/scripts /tmp/.
        ln -s /opt/ASTPP/freeswitch/fs ${WWWDIR}
        ln -s /opt/ASTPP/freeswitch/scripts ${FS_DIR}
        cp -rf /opt/ASTPP/freeswitch/sounds/*.wav ${FS_SOUNDSDIR}/
        cp -rf /opt/ASTPP/freeswitch/conf/autoload_configs/* /etc/freeswitch/autoload_configs/


}






normalize_freeswitch()
{
        systemctl start freeswitch
        systemctl enable freeswitch
        sed -i "s#max-sessions\" value=\"1000#max-sessions\" value=\"2000#g" /etc/freeswitch/autoload_configs/switch.conf.xml
        sed -i "s#sessions-per-second\" value=\"30#sessions-per-second\" value=\"50#g" /etc/freeswitch/autoload_configs/switch.conf.xml
        sed -i "s#max-db-handles\" value=\"50#max-db-handles\" value=\"500#g" /etc/freeswitch/autoload_configs/switch.conf.xml
        sed -i "s#db-handle-timeout\" value=\"10#db-handle-timeout\" value=\"30#g" /etc/freeswitch/autoload_configs/switch.conf.xml
        rm -rf  /etc/freeswitch/dialplan/*
        touch /etc/freeswitch/dialplan/astpp.xml
        rm -rf  /etc/freeswitch/directory/*
        touch /etc/freeswitch/directory/astpp.xml
        rm -rf  /etc/freeswitch/sip_profiles/*
        touch /etc/freeswitch/sip_profiles/astpp.xml
        chmod -Rf 755 ${FS_SOUNDSDIR}
        chmod -Rf 777 /usr/local/freeswitch/scripts/astpp/lib

        cp -rf /opt/ASTPP/web_interface/nginx/deb_fs.conf /etc/nginx/conf.d/fs.conf
        chown -Rf root.root ${WWWDIR}/fs
        chmod -Rf 755 ${WWWDIR}/fs
        /bin/systemctl restart freeswitch
        /bin/systemctl enable freeswitch
        
        #change 
        sed -i "s#<load module=\"mod_pgsql\"/>#<load module=\"mod_mariadb\"/>#g" /etc/freeswitch/autoload_configs/pre_load_modules.conf.xml
             
} #end normalize_freeswitch


normalize_mariadb()
{
cp /opt/ASTPP/misc/odbc/deb_odbc.ini /etc/odbc.ini
sed -i '1i wait_timeout=600' /etc/mysql/conf.d/mysql.cnf
sed -i '1i interactive_timeout = 600' /etc/mysql/conf.d/mysql.cnf
sed -i '1i sql_mode=""' /etc/mysql/conf.d/mysql.cnf
sed -i '1i [mysqld]' /etc/mysql/conf.d/mysql.cnf

systemctl restart mariadb
systemctl enable mariadb

} #end normalize_mariadb

#Fetch ASTPP Source
get_astpp_source()
{
        cd /opt
        
        git clone -b V6.0 https://github.com/iNextrix/ASTPP/
        
        
       
} #end get_astpp_source


install_fail2ban()
{
                read -n 1 -p "Do you want to install and configure Fail2ban ? (y/n) "
                if [ "$REPLY"   = "y" ]; then

                            sleep 2s
                            apt update -y
                            sleep 2s
                            apt install fail2ban -y
                            sleep 2s
                            echo ""
                            read -p "Enter fail2ban client's Notification email address: ${NOTIEMAIL}"
                            NOTIEMAIL=${REPLY}
                            echo ""
                            read -p "Enter sender email address: ${NOTISENDEREMAIL}"
                            NOTISENDEREMAIL=${REPLY}
                            cd /usr/src
                            wget --no-check-certificate --max-redirect=0 https://latest.astppbilling.org/fail2ban_Deb.tar.gz
                            tar xzvf fail2ban_Deb.tar.gz
                            mv /etc/fail2ban /tmp/
                            cd /opt/ASTPP/misc/
                            tar -xzvf fail2ban_deb10.tar.gz
                            cp -rf /opt/ASTPP//misc/fail2ban_deb10 /etc/fail2ban
                            #cp -rf /usr/src/fail2ban /etc/fail2ban
                            #cp -rf ${ASTPP_SOURCE_DIR}/misc/deb_files/fail2ban/jail.local /etc/fail2ban/jail.local

                            sed -i -e "s/{INTF}/${INTF}/g" /etc/fail2ban/jail.local
                            sed -i -e "s/{NOTISENDEREMAIL}/${NOTISENDEREMAIL}/g" /etc/fail2ban/jail.local
                            sed -i -e "s/{NOTIEMAIL}/${NOTIEMAIL}/g" /etc/fail2ban/jail.local

                        ################################# JAIL.CONF FILE READY ######################
                        echo "################################################################"
                        mkdir /var/run/fail2ban
                        systemctl restart fail2ban
                        systemctl enable fail2ban
                        echo "################################################################"
                        echo "Fail2Ban for FreeSwitch & IPtables Integration completed"
                        else
                        echo ""
                        echo "Fail2ban installation is aborted !"
                fi
} #end install_fail2ban



install_astpp()
{
echo "Creating neccessary locations and configuration files ..."
mkdir -p ${ASTPPDIR}
mkdir -p ${ASTPPLOGDIR}
mkdir -p ${ASTPPEXECDIR}
mkdir -p ${WWWDIR}
cp -rf /opt/ASTPP/config/astpp-config.conf ${ASTPPDIR}astpp-config.conf
cp -rf /opt/ASTPP/config/astpp.lua ${ASTPPDIR}astpp.lua
ln -s /opt/ASTPP/web_interface/astpp ${WWWDIR}
ln -s /opt/ASTPP//freeswitch/fs ${WWWDIR}



} #end install_astpp


normalize_astpp()
{
mkdir -p /etc/nginx/ssl
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt






###################################################
#: << 'COMMENT'

##if [ ."$os_codename" = ."bullseye" ] || [ ."$os_codename" = ."focal" ]; then
/bin/cp /usr/src/ioncube/ioncube_loader_lin_7.4.so /usr/lib/php/20190902/
sed -i '2i zend_extension ="/usr/lib/php/20190902/ioncube_loader_lin_7.4.so"' /etc/php/7.4/fpm/php.ini
sed -i '2i zend_extension ="/usr/lib/php/20190902/ioncube_loader_lin_7.4.so"' /etc/php/7.4/cli/php.ini
cp -rf /opt/ASTPP/web_interface/nginx/deb_astpp.conf /etc/nginx/conf.d/astpp.conf
systemctl start nginx
systemctl enable nginx
systemctl start php7.4-fpm
systemctl enable php7.4-fpm
chown -Rf root.root ${ASTPPDIR}
chown -Rf www-data.www-data ${ASTPPLOGDIR}
chown -Rf root.root ${ASTPPEXECDIR}
chown -Rf www-data.www-data ${WWWDIR}/astpp
chown -Rf www-data.www-data ${ASTPP_SOURCE_DIR}/web_interface/astpp
chmod -Rf 755 ${WWWDIR}/astpp
sed -i "s/;request_terminate_timeout = 0/request_terminate_timeout = 300/" /etc/php/7.4/fpm/pool.d/www.conf
sed -i "s#short_open_tag = Off#short_open_tag = On#g" /etc/php/7.4/fpm/php.ini
sed -i "s#;cgi.fix_pathinfo=1#cgi.fix_pathinfo=1#g" /etc/php/7.4/fpm/php.ini
sed -i "s/max_execution_time = 30/max_execution_time = 3000/" /etc/php/7.4/fpm/php.ini
sed -i "s/upload_max_filesize = 2M/upload_max_filesize = 20M/" /etc/php/7.4/fpm/php.ini
sed -i "s/post_max_size = 8M/post_max_size = 20M/" /etc/php/7.4/fpm/php.ini
sed -i "s/memory_limit = 128M/memory_limit = 512M/" /etc/php/7.4/fpm/php.ini
systemctl restart php7.4-fpm

#fi

#COMMENT

###################################################

CRONPATH='/var/spool/cron/crontabs/astpp'

echo "# To call all crons
* * * * * cd /opt/ASTPP/web_interface/astpp/cron/ && php cron.php crons
" > $CRONPATH
chmod 600 $CRONPATH
crontab $CRONPATH
touch /var/log/astpp/astpp.log
touch /var/log/astpp/astpp_email.log
chmod -Rf 755 $ASTPP_SOURCE_DIR
chmod 777 /var/log/astpp/astpp.log
chmod 777 /var/log/astpp/astpp_email.log
sed -i "s#dbpass = <PASSSWORD>#dbpass = ${ASTPPUSER_MYSQL_PASSWORD}#g" ${ASTPPDIR}astpp-config.conf
sed -i "s#DB_PASSWD=\"<PASSSWORD>\"#DB_PASSWD = \"${ASTPPUSER_MYSQL_PASSWORD}\"#g" ${ASTPPDIR}astpp.lua
sed -i "s#base_url=https://localhost:443/#base_url=https://${ASTPP_HOST_DOMAIN_NAME}/#g" ${ASTPPDIR}/astpp-config.conf
sed -i "s#PASSWORD = <PASSWORD>#PASSWORD = ${ASTPPUSER_MYSQL_PASSWORD}#g" /etc/odbc.ini



###### Temp change
sed -i "s#debug = 0#debug = 1#g" ${ASTPPDIR}/astpp-config.conf


#Make changes for ASTPP to use php7.4-fpm in Debian 11 

#if [ ."$os_codename" = ."bullseye" ] || [ ."$os_codename" = ."focal" ]; then
sed -i -e 's!fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;!fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;!g' /etc/nginx/conf.d/fs.conf
printf '%s\n'  'g/^/s|fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;|fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;|g' 'w' 'q' | ed -s /etc/nginx/conf.d/astpp.conf
#fi

systemctl restart nginx
} #end normalize_astpp





install_database ()
{


        mysqladmin -u root -p${MYSQL_ROOT_PASSWORD} create ${ASTPP_DATABASE_NAME}
        mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "CREATE USER 'astppuser'@'localhost' IDENTIFIED BY '${ASTPPUSER_MYSQL_PASSWORD}';"
        mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "ALTER USER 'astppuser'@'localhost' IDENTIFIED BY '${ASTPPUSER_MYSQL_PASSWORD}';"
        mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "GRANT ALL PRIVILEGES ON \`${ASTPP_DATABASE_NAME}\` . * TO 'astppuser'@'localhost' WITH GRANT OPTION;FLUSH PRIVILEGES;"

        #Need to make modifications to .sql file for compatibility with MariaDB
        #printf '%s\n' 'g/^/s|\(/\*!50003 SET collation_connection  = \)utf8mb4_0900_ai_ci\( \*/ ;\)|\1utf8mb4_unicode_520_ci\2|g' 'w' 'q' | ed -s ${ASTPP_SOURCE_DIR}/database/astpp-6.0.sql
        #printf '%s\n' 'g/^/s|\(/\*!50001 SET collation_connection      = \)utf8mb4_0900_ai_ci\( \*/;\)|\1utf8mb4_unicode_520_ci\2|g' 'w' 'q' | ed -s ${ASTPP_SOURCE_DIR}/database/astpp-6.0.sql
        
        sed -i -e 's/utf8mb4_0900_ai_ci/utf8_unicode_ci/g' /opt/ASTPP/database/astpp-6.0.sql
        
        sed -i -e 's/utf8mb4/utf8/g' /opt/ASTPP/database/astpp-6.0.1.sql
        
        sed -i -e 's/utf8_0900_ai_ci/utf8_unicode_ci/g' /opt/ASTPP/database/astpp-6.0.1.sql
        sed -i -e 's/utf8mb4_0900_ai_ci/utf8_unicode_ci/g' /opt/ASTPP/database/astpp-6.0.1.sql
        
        #sed -i -e 's/utf8mb4_0900_ai_ci/1utf8mb4_unicode_520_ci/g' /opt/ASTPP/database/astpp-6.0.1.sql
        #utf8_unicode_ci
                       
        mysql -uroot -p${MYSQL_ROOT_PASSWORD} astpp < ${ASTPP_SOURCE_DIR}/database/astpp-6.0.sql
        mysql -uroot -p${MYSQL_ROOT_PASSWORD} astpp < ${ASTPP_SOURCE_DIR}/database/astpp-6.0.1.sql

	#database cpnnector
        cd /opt/ASTPP/misc/
        tar -xzvf odbc.tar.gz
	mkdir -p /usr/lib/x86_64-linux-gnu/odbc/.
        cp -rf odbc/libmyodbc8* /usr/lib/x86_64-linux-gnu/odbc/.



} #end install_database


#Firewall Configuration
configure_firewall()
{


echo "Configuring IPTables"

#defaults to nftables by default this enables iptables
if [ ."$os_codename" = ."buster" ]; then
	update-alternatives --set iptables /usr/sbin/iptables-legacy
	update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
fi
if [ ."$os_codename" = ."bullseye" ]; then
	apt-get install -y iptables
	update-alternatives --set iptables /usr/sbin/iptables-legacy
	update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
fi

#remove ufw
ufw reset
ufw disable
apt-get remove -y ufw firewalld


#run iptables commands
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -j DROP -p udp --dport 5060:5091 -m string --string "friendly-scanner" --algo bm --icase
iptables -A INPUT -j DROP -p tcp --dport 5060:5091 -m string --string "friendly-scanner" --algo bm --icase
iptables -A INPUT -j DROP -p udp --dport 5060:5091 -m string --string "sipcli/" --algo bm --icase
iptables -A INPUT -j DROP -p tcp --dport 5060:5091 -m string --string "sipcli/" --algo bm --icase
iptables -A INPUT -j DROP -p udp --dport 5060:5091 -m string --string "VaxSIPUserAgent/" --algo bm --icase
iptables -A INPUT -j DROP -p tcp --dport 5060:5091 -m string --string "VaxSIPUserAgent/" --algo bm --icase
iptables -A INPUT -j DROP -p udp --dport 5060:5091 -m string --string "pplsip" --algo bm --icase
iptables -A INPUT -j DROP -p tcp --dport 5060:5091 -m string --string "pplsip" --algo bm --icase
iptables -A INPUT -j DROP -p udp --dport 5060:5091 -m string --string "system " --algo bm --icase
iptables -A INPUT -j DROP -p tcp --dport 5060:5091 -m string --string "system " --algo bm --icase
iptables -A INPUT -j DROP -p udp --dport 5060:5091 -m string --string "exec." --algo bm --icase
iptables -A INPUT -j DROP -p tcp --dport 5060:5091 -m string --string "exec." --algo bm --icase
iptables -A INPUT -j DROP -p udp --dport 5060:5091 -m string --string "multipart/mixed;boundary" --algo bm --icase
iptables -A INPUT -j DROP -p tcp --dport 5060:5091 -m string --string "multipart/mixed;boundary" --algo bm --icase
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -p tcp --dport 7443 -j ACCEPT
iptables -A INPUT -p tcp --dport 5060:5091 -j ACCEPT
iptables -A INPUT -p udp --dport 5060:5091 -j ACCEPT
iptables -A INPUT -p udp --dport 16384:32768 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables -A INPUT -p udp --dport 1194 -j ACCEPT
iptables -t mangle -A OUTPUT -p udp -m udp --sport 16384:32768 -j DSCP --set-dscp 46
iptables -t mangle -A OUTPUT -p udp -m udp --sport 5060:5091 -j DSCP --set-dscp 26
iptables -t mangle -A OUTPUT -p tcp -m tcp --sport 5060:5091 -j DSCP --set-dscp 26
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

#answer the questions for iptables persistent
echo iptables-persistent iptables-persistent/autosave_v4 boolean true | debconf-set-selections
echo iptables-persistent iptables-persistent/autosave_v6 boolean true | debconf-set-selections
apt-get install -y iptables-persistent





} #end configure_firewall



#Install Monit for service monitoring
install_monit ()
{


#Monit is not in repo is Buster Backports

# if [ $os_codename != 'focal' ] ; then

#apt install monit -y  

#else 

#Monit is not in repo is Buster Backports
#apt install -t buster-backports monit -y 



#fi

read -p "Enter a Notification email address for sytem monitor: ${EMAIL}"



#elif [ ."$os_codename" = ."bullseye" ] || [ $os_codename != 'focal' ]; then


apt  -y install monit
sed -i -e 's/# set mailserver mail.bar.baz,/set mailserver localhost/g' /etc/monit/monitrc
sed -i -e '/# set mail-format { from: monit@foo.bar }/a set alert '$EMAIL /etc/monit/monitrc
sed -i -e 's/##   subject: monit alert on --  $EVENT $SERVICE/   subject: monit alert --  $EVENT $SERVICE/g' /etc/monit/monitrc
sed -i -e 's/##   subject: monit alert --  $EVENT $SERVICE/   subject: monit alert on '${INTF}' --  $EVENT $SERVICE/g' /etc/monit/monitrc
sed -i -e 's/## set mail-format {/set mail-format {/g' /etc/monit/monitrc
sed -i -e 's/## }/ }/g' /etc/monit/monitrc
echo '
#------------MySQL
check process mysqld with pidfile /var/run/mysqld/mysqld.pid
    start program = "/bin/systemctl start mariadb"
    stop program = "/bin/systemctl stop mariadb"
if failed host 127.0.0.1 port 3306 then restart
if 5 restarts within 5 cycles then timeout

#------------Fail2ban
check process fail2ban with pidfile /var/run/fail2ban/fail2ban.pid
    start program = "/bin/systemctl start fail2ban"
    stop program = "/bin/systemctl stop fail2ban"

# ---- FreeSWITCH ----
check process freeswitch with pidfile /var/run/freeswitch/freeswitch.pid
    start program = "/bin/systemctl start freeswitch"
    stop program  = "/bin/systemctl stop freeswitch"

#-------nginx----------------------
check process nginx with pidfile /var/run/nginx.pid
    start program = "/bin/systemctl start nginx" with timeout 30 seconds
    stop program  = "/bin/systemctl stop nginx"

#-------php-fpm----------------------
check process php7.4-fpm with pidfile /var/run/php/php7.4-fpm.pid
    start program = "/bin/systemctl start php7.4-fpm" with timeout 30 seconds
    stop program  = "/bin/systemctl stop php7.4-fpm"

#--------system
check system localhost
    if loadavg (5min) > 8 for 4 cycles then alert
    if loadavg (15min) > 8 for 4 cycles then alert
    if memory usage > 80% for 4 cycles then alert
    if swap usage > 20% for 4 cycles then alert
    if cpu usage (user) > 80% for 4 cycles then alert
    if cpu usage (system) > 20% for 4 cycles then alert
    if cpu usage (wait) > 20% for 4 cycles then alert

check filesystem "root" with path /
    if space usage > 80% for 1 cycles then alert' >> /etc/monit/monitrc


systemctl restart monit
systemctl enable monit

#fi



} #End of Monit


#Configure logrotation for maintain log size
logrotate_install ()
{


if [ ."$os_codename" = ."buster" ]; then
sed -i -e 's/daily/size 30M/g' /etc/logrotate.d/rsyslog
sed -i -e 's/weekly/size 30M/g' /etc/logrotate.d/rsyslog
sed -i -e 's/rotate 7/rotate 5/g' /etc/logrotate.d/rsyslog
sed -i -e 's/weekly/size 30M/g' /etc/logrotate.d/php7.3-fpm
sed -i -e 's/rotate 12/rotate 5/g' /etc/logrotate.d/php7.3-fpm
sed -i -e 's/weekly/size 30M/g' /etc/logrotate.d/nginx
sed -i -e 's/rotate 52/rotate 5/g' /etc/logrotate.d/nginx
sed -i -e 's/weekly/size 30M/g' /etc/logrotate.d/fail2ban
sed -i -e 's/weekly/size 30M/g' /etc/logrotate.d/monit


elif [ ."$os_codename" = ."bullseye" ] || [ $os_codename != 'focal' ]; then
sed -i -e 's/daily/size 30M/g' /etc/logrotate.d/rsyslog
sed -i -e 's/weekly/size 30M/g' /etc/logrotate.d/rsyslog

sed -i -e 's/rotate 7/rotate 5/g' /etc/logrotate.d/rsyslog
sed -i -e 's/weekly/size 30M/g' /etc/logrotate.d/php7.4-fpm
sed -i -e 's/rotate 12/rotate 5/g' /etc/logrotate.d/php7.4-fpm
sed -i -e 's/weekly/size 30M/g' /etc/logrotate.d/nginx
sed -i -e 's/rotate 52/rotate 5/g' /etc/logrotate.d/nginx
sed -i -e 's/weekly/size 30M/g' /etc/logrotate.d/fail2ban
sed -i -e 's/weekly/size 30M/g' /etc/logrotate.d/monit

fi




}




#Remove all downloaded and temp files from server
clean_server ()
{
        cd /usr/src
#        rm -Rf libks
#        rm -Rf spansp
#        rm -Rf sofia-sip-1.13.7
#        rm -Rf freeswitch-$switch_version.-release

        echo "FS restarting...!"
        systemctl restart freeswitch
        echo "FS restarted...!"
}



letsencrypt()
{

#For use with Nginx
apt install snapd -y
snap install core
snap refresh core
snap install --classic certbot
ln -s /snap/bin/certbot /usr/bin/certbot

sed -i -e 's/server_name _;/server_name ${ASTPP_HOST_DOMAIN_NAME};/g' /etc/nginx/conf.d/astpp.conf
certbot --nginx -d ${ASTPP_HOST_DOMAIN_NAME}
 

 systemctl reload nginx




}


#Installation Information Print
start_installation ()
{

	introduction
	MYSQL_ROOT_PASSWORD=`echo "$(genpasswd 20)" | sed s/./*/5`
        ASTPPUSER_MYSQL_PASSWORD=`echo "$(genpasswd 20)" | sed s/./*/5`

        ## Just making sure password is generated
        echo $MYSQL_ROOT_PASSWORD
        echo $ASTPPUSER_MYSQL_PASSWORD

        
        verification
	
	echo ""
        read -n 1 -p "Do you wish to continue with installation [y/n] "
                if [ "$REPLY"   = "n" ]; then

		exit
		
		fi
        
        
        
        
        
        
        
        
        
        pre_install
        
        echo ""
        read -n 1 -p "Do you wish to continue with installation [y/n] "
                if [ "$REPLY"   = "n" ]; then

		exit
		
		fi
	
		
        install_prerequisties
        
        echo ""
        read -n 1 -p "Do you wish to continue with installation [y/n] "
                if [ "$REPLY"   = "n" ]; then

		exit
		
		fi
        
        get_user_response
	
	echo ""
	read -n 1 -p "Do you wish to continue with installation [y/n] "
                if [ "$REPLY"   = "n" ]; then

		exit
		
		fi
	
	
	os_dependencies
        
        echo ""
        read -n 1 -p "Do you wish to continue with installation [y/n] "
                if [ "$REPLY"   = "n" ]; then

		exit
		
		fi
        
       
        
        get_astpp_source
	
	echo ""
	read -n 1 -p "Do you wish to continue with installation [y/n] "
                if [ "$REPLY"   = "n" ]; then

		exit
		
		fi
	
	 
	
	install_freeswitch_packages
        
        echo ""
        echo "######### FreeSwitch Packages"
        echo ""
        read -n 1 -p "Do you wish to continue with installation [y/n] "
                if [ "$REPLY"   = "n" ]; then

		exit
		
		fi
        
        install_astpp
        
        echo ""
        echo "Install ASTPP"
        read -n 1 -p "Do you wish to continue with installation [y/n] "
                if [ "$REPLY"   = "n" ]; then

		exit
		
		fi
        
        normalize_mariadb
        
        echo ""
        echo "Normalize MariaDB"
        read -n 1 -p "Do you wish to continue with installation [y/n] "
                if [ "$REPLY"   = "n" ]; then

		exit
		
		fi
		
        install_database
        
        echo ""
        read -n 1 -p "Do you wish to continue with installation [y/n] "
                if [ "$REPLY"   = "n" ]; then

		exit
		
		fi
        normalize_freeswitch
        
        echo ""
        echo "Normalize FreeSwitch"
        read -n 1 -p "Do you wish to continue with installation [y/n] "
                if [ "$REPLY"   = "n" ]; then

		exit
		
		fi
		
       
        
        
        echo "Now Normalizing ASTPP"
        echo ""
        
        
        normalize_astpp
        
        echo ""
        read -n 1 -p "Normalized ATSPP Completed. Do you wish to continue with installation [y/n] "
                if [ "$REPLY"   = "n" ]; then

		exit
		
		fi
        
        configure_firewall
        
        echo ""
        read -n 1 -p "Do you wish to continue with installation [y/n] "
                if [ "$REPLY"   = "n" ]; then

		exit
		
		fi
        
        
        install_fail2ban
        
        echo ""
        read -n 1 -p "Do you wish to continue with installation [y/n] "
                if [ "$REPLY"   = "n" ]; then

		exit
		
		fi
        
        install_monit
        
        echo ""
        read -n 1 -p "Do you wish to continue with installation [y/n] "
                if [ "$REPLY"   = "n" ]; then

		exit
		
		fi
		
		
		
	read -n 1 -p "Install Certificate from Let's Encrypt? [y/n] "
                if [ "$REPLY"   = "y" ]; then

		letsencrypt
		
		else
		echo ""
    		echo "Skipping Let's Encrypt certificate installation."
		
		fi
        
        logrotate_install
        
        
        clean_server

        clear
        echo "******************************************************************************************"
        echo "******************************************************************************************"
        echo "******************************************************************************************"
        echo "**********                                                                      **********"
        echo "**********           Your ASTPP is installed successfully                       **********"
        echo "                     Browse URL: https://${ASTPP_HOST_DOMAIN_NAME}"
        echo "                     Username: admin"
        echo "                     Password: admin"
        echo ""
        echo "                     MySQL root user password:"
        echo "                     ${MYSQL_ROOT_PASSWORD}"
        echo ""
        echo "                     MySQL astppuser password:"
        echo "                     ${ASTPPUSER_MYSQL_PASSWORD}"
        echo ""
        echo "**********           IMPORTANT NOTE: Please reboot your server once.            **********"
        echo "**********                                                                      **********"
        echo "******************************************************************************************"
        echo "******************************************************************************************"
        echo "******************************************************************************************"
}
start_installation