#!/bin/bash

if [ "$1" == "" ]
then
    start="Repo";
else
    start=$1;
fi

#version=`rpm -qa \*-release | grep -Ei "oracle|redhat|centos" | cut -d"-" -f3 | cut -d"." -f1`
version=`rpm -E %{rhel}`

setenforce 0

while [ "$start" != "END" ] 
do
    echo "Installing $start..."
    case $start in
    Repo)
	if [ -e /etc/redhat-release ]
	then
	    yum -y upgrade
	    yum -y install wget

	    if [ "$version" ==  "6" ]
		then
		wget http://dl.fedoraproject.org/pub/epel/6/`uname -i`/epel-release-6-8.noarch.rpm
		wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
		rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm
#		wget http://apt.sw.be/redhat/el6/en/x86_64/rpmforge/RPMS/rpmforge-release-0.5.2-2.el6.rf.`uname -i`.rpm
		wget http://ftp.tu-chemnitz.de/pub/linux/dag/redhat/el6/en/x86_64/dag/RPMS/rpmforge-release-0.5.3-1.el6.rf.`uname -i`.rpm
		rpm -Uvh rpmforge-release-0.5.3-1.el6.rf.`uname -i`.rpm
		rpm --import http://ftp.tu-chemnitz.de/pub/linux/dag/RPM-GPG-KEY.dag.txt
		rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm
		rpm --import https://mirror.webtatic.com/yum/RPM-GPG-KEY-webtatic-andy
	    elif [ "$version" == "7" ]
	    then
		wget http://dl.fedoraproject.org/pub/epel/7/`uname -i`/Packages/e/epel-release-7-14.noarch.rpm
#		yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
#		wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.3-1.el7.rf.`uname -i`.rpm
		wget http://ftp.tu-chemnitz.de/pub/linux/dag/redhat/el7/en/x86_64/dag/RPMS/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm
		rpm -Uvh rpmforge-release-0.5.3-1.el7.rf.`uname -i`.rpm
		rpm --import http://ftp.tu-chemnitz.de/RPM-GPG-KEY.dag.txt
#		wget http://dl.atrpms.net/el7-x86_64/atrpms/stable/atrpms-repo-7-7.el7.x86_64.rpm
#		rpm -i atrpms-repo-7-7.el7.x86_64.rpm 
#		rpm --import http://packages.atrpms.net/RPM-GPG-KEY.atrpms
		rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
		rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro 
#		rpm -Uvh ftp://ftp.pbone.net/mirror/li.nux.ro/download/nux/dextop/el7/x86_64/nas-libs-1.9.4-1.el7.nux.x86_64.rpm
		rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
		rpm --import https://mirror.webtatic.com/yum/RPM-GPG-KEY-webtatic-el7
		rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
		rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
	    elif [ "$version" == "8" ]
	    then
		rpm -Uvh https://dl.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/e/epel-release-8-16.el8.noarch.rpm   
		dnf config-manager --set-enabled powertools
		/usr/bin/crb enable
	    elif [ "$version" == "9" ]
	    then				
		rpm -Uvh https://dl.fedoraproject.org/pub/epel/9/Everything/x86_64/Packages/e/epel-release-9-8.el9.noarch.rpm   
		dnf install 'dnf-command(config-manager)'
		dnf config-manager --set-enabled crb
		/usr/bin/crb enable
		dnf config-manager --set-enabled epel
		dnf -y install https://mirrors.rpmfusion.org/free/el/rpmfusion-free-release-9.noarch.rpm
		dnf -y install https://mirrors.rpmfusion.org/nonfree/el/rpmfusion-nonfree-release-9.noarch.rpm
	    else		
		echo "Version $version not supported"
		exit
	    fi
	    start=Yum
	else
	    for file in libodbc1 odbcinst unixodbc unixodbc-dev subversion ncurses-dev uuid-dev libjansson-dev libxml2-dev libxml2-dev libsqlite3-dev subversion alsa-base alsa-utils autopoint autotools-dev comerr-dev dbus debhelper defoma diffstat esound-common fancontrol fontconfig fontconfig-config freetds-common freetds-dev fxload gettext git graphviz gsfonts html2text intltool-debian krb5-multidev libaa1 libaa1-dev libartsc0 libartsc0-dev libasound2 libasound2-dev libasyncns0 libaudio-dev libaudio2 libaudiofile-dev libaudiofile0 libavahi-client-dev libavahi-client3 libavahi-common-data libavahi-common-dev libavahi-common3 libc-client2007e libc-client2007e-dev libcaca-dev libcaca0 libcairo2 libcap-dev libcdt4 libcelt-dev libcelt0-0 libcgraph5 libcorosync4 libcroco3 libct4 libcurl3 libcurl3-gnutls libcurl4-openssl-dev libdatrie1 libdbus-1-3 libdbus-1-dev libdirectfb-1.2-9 libdirectfb-dev libdirectfb-extra libdrm-intel1 libdrm-radeon1 libdrm2 liberror-perl libesd0 libesd0-dev libexpat1-dev libffado-dev libffado2 libflac8 libfont-freetype-perl libfontconfig1 libfontenc1 libfreetype6-dev libgd2-noxpm libgl1-mesa-dev libgl1-mesa-dri libgl1-mesa-glx libglib2.0-0 libglib2.0-data libglib2.0-dev libglibmm-2.4-1c2a libglu1-mesa libglu1-mesa-dev libgmime-2.0-2-dev libgmime-2.0-2a libgraph4 libgsm1 libgsm1-dev libgvc5 libgvpr1 libice-dev libice6 libidn11-dev libiec61883-0 libiksemel-dev libiksemel3 libjack-dev libjack0 libjpeg62 libjpeg62-dev libkrb5-dev liblcms1 libldap2-dev libltdl-dev libltdl7 liblua5.1-0 liblua5.1-0-dev libmail-sendmail-perl libmng1 libncurses5-dev libnewt-dev libnspr4-0d libnss3-1d libodbcinstq1c2 libogg-dev libogg0 libopenais-dev libopenais3 libopenh323-1.18.0 libopenh323-dev libpam0g-dev libpango1.0-0 libpango1.0-common libpathplan4 libperl-dev libperl5.10   libpixman-1-0 libpng12-0 libpng12-dev libpopt-dev libpq-dev libpq5 libpri-dev libpri1.4 libpt-1.10.10 libpt-1.10.10-dev libpt-1.10.10-plugins-alsa libpt-1.10.10-plugins-v4l libpthread-stubs0 libpthread-stubs0-dev libpulse-browse0 libpulse-dev libpulse-mainloop-glib0 libpulse0 libqt3-mt libradiusclient-ng-dev libradiusclient-ng2 libraw1394-11 libreadline-dev libreadline6-dev libresample1 libresample1-dev libsamplerate0 libsamplerate0-dev libsasl2-dev libsdl1.2-dev libsdl1.2debian libsdl1.2debian-alsa libsensors4 libsensors4-dev libslang2-dev libsm-dev libsm6 libsndfile1 libsnmp-base libsnmp-dev libsnmp-perl libsnmp15 libspandsp-dev   libspandsp2 libspeex-dev libspeex1 libspeexdsp-dev libspeexdsp1 libsqlite0 libsqlite0-dev libss7-1 libss7-dev libssh2-1 libssh2-1-dev libssl-dev libsvga1 libsvga1-dev libsybdb5 libsys-hostname-long-perl libsysfs-dev libsysfs2 libthai-data libthai0 libtiff4 libtiff4-dev libtiffxx0c2 libtonezone-dev libtonezone2.0 libtool libts-0.0-0 libunistring0 libvorbis-dev libvorbis0a libvorbisenc2 libvorbisfile3 libvpb-dev libvpb0 libwrap0-dev libx11-dev libx86-1 libxau-dev libxaw7 libxcb-render-util0 libxcb-render0 libxcb1-dev libxcursor1  libxdamage1 libxdmcp-dev libxdot4 libxext-dev libxfixes3 libxfont1 libxft2 libxi6 libxinerama1 libxml++2.6-2 libxml2-dev libxmu6 libxpm4 libxrandr2 libxrender1 libxt-dev libxt6 libxtst6 libxxf86vm1 linux-sound-base lm-sensors mesa-common-dev mlock module-assistant odbcinst odbcinst1debian2 patchutils pkg-config po-debconf quilt rsync shared-mime-info tsconf ttf-dejavu-core ttf-liberation unixodbc unixodbc-dev vpb-driver-source x-ttcidfont-conf x11-common x11proto-core-dev x11proto-input-dev x11proto-kb-dev x11proto-xext-dev xfonts-encodings xfonts-utils xtrans-dev zlib1g-dev libsqlite3-dev libmysqlclient-dev sox libsox-fmt-mp3 tshark libmyodbc libpcap-dev unixodbc unixodbc-dev libsrtp0-dev libtiff4 libtiff4-dev libvorbis-dev libpcap-dev libmyodbc libsox-fmt-mp3 wireshark tshark ffmpeg libtiff-dev libncurses-dev libxml2-dev libsqlite3-dev unixodbc-dev subversion apache2 php5 php-pear php5-dev libvorbis-dev mysql-server php5-mysql sudo libmyodbc libsrtp0-dev sox ffmpeg libsox-fmt-mp3 libtiff-dev libsnappy-dev libssh-dev libjson0-dev mpg123 php5-curl nasm libedit-dev php-mysql php-pear libapache2-mod-php mlocate net-tools php-curl php-mbstring odbc-mariadb
	    do
		apt-get -y install $file
	    done
	    apt-get install build-essential

	    release=`cat /etc/debian_version | cut -d\/ -f 1`
	    if [ "$release" == "jessie" ]
	    then
		git clone git://git.videolan.org/ffmpeg.git
		cd ffmpeg
		./configure
		make
		make install
	    fi

	    start=Dahdi
	    fi
	;;
    Yum)
	yum -y upgrade
	yum -y remove php*
	for package in jwhois ImageMagick sudo mpg123 snappy snappy-devel patch unzip fail2ban postfix ssmtp bc sox gcc gcc-c++ kernel-devel bison openssl openssl-devel perl perl-Net-SSLeay perl-Crypt-SSLeay libtermcap-devel ncurses-devel doxygen curl-devel newt-devel mlocate lynx tar wget nmap bzip2 mod_ssl crontabs vixie-cron speex speex-devel unixODBC unixODBC-devel libtool-ltdl libtool-ltdl-devel mysql-connector-odbc mysql mysql-devel mysql-server php-mysql php-pear php-mbstring php-mcrypt flex screen libtiff-devel kernel-devel libxml2-devel make svn festival wireshark sqlite-devel libogg-devel libvorbis-devel libpcap-devel emacs libuuid-devel uuid libuuid libsrtp libsrtp-devel ffmpeg jansson-devel iptables-devel perl-Text-CSV_XS json-c-devel libssh-devel php ntp ntpsec tcpdump net-tools telnet rrdtool-devel at irqbalance nscd opus opus-devel gsm-devel lsof httpd xmlstarlet mailx cyrus-sasl cyrus-sasl-plain pdftk poppler-utils libedit-devel ghostscript initscripts libtiff-tools ncurses-compat-libs rsync
        do
	    yum -y install $package
	
        done
	chkconfig --level 2345 postfix on
    	yum -y groupinstall "Development Tools"
	yum -y install yum-plugin-replace
	if [ "$version" == "6" ]
	then
	    yum -y replace php-common --replace-with=php55w-common
	    yum -y install php55w-imap php55w-gd
	elif [ "$version" == "7" ]
	then
	    yum -y replace php-common --replace-with=php55w-common
	    yum -y install php55w-imap php55w-gd php55w-ldap php55w-xml php55w-xmlrpc
	    yum -y install  whois x264 sox-plugins-freeworld mariadb mariadb-server libtiff-tools mariadb-server php55 php55-php php55-php-gd php55-php-mbstring php55-php-mysqlnd httpd24-mod_ssl.x86_64 ncurses-devel sqlite-devel libuuid-devel gtk2-devel jansson-devel binutils-devel svn mysql-connector-odbc.x86_64 libtool-ltdl libtool-ltdl-devel


#	    ln -s /opt/rh/php55/root/etc/php.d /etc
#	    ln -s /opt/rh/php55/root/usr/bin/php /usr/bin/php
#	    cp /opt/rh/httpd24/root/etc/httpd/conf.d/php55-php.conf /etc/httpd/conf.d/
#	    cp /opt/rh/httpd24/root/etc/httpd/conf.modules.d/10-php55-php.conf /etc/httpd/conf.modules.d/
#	    cp /opt/rh/httpd24/root/etc/httpd/modules/libphp55-php5.so /etc/httpd/modules/

	    if [ "`uname -i`" == "x86_64" ] 
	    then
		wget http://devel.mirtapbx.com/mirtapbx_support/sox-14.3.2-6.mp3.2.el6.x86_64.rpm
		yum -y install sox-14.3.2-6.mp3.2.el6.x86_64.rpm
	    fi
	elif [ "$version" == "8" ]
	then
	    yum -y install whois
	    yum -y install php php-gd php-mysql php-json php-mysql
	elif [ "$version" == "9" ]
	then
    	    yum -y install http://rpms.remirepo.net/enterprise/remi-release-9.rpm
	    dnf module enable php:remi-8.1 -y
	    yum -y install whois
	    yum -y install php php-gd php-mysql php-mysqlnd php-pear php-json php-mysqlnd php-pecl-zip php-imap php-pecl-grpc php-ldap php-bcmath
	    yum -y upgrade
	fi
        start=GeoIP
	;;
    GeoIP)
	cd /usr/local/src
	if [ "$version" ==  "6" ]
	then
	    kversion=`uname -r`
#	    if  [ "$kversion" < "3" ]
#            then
                wget http://devel.mirtapbx.com/mirtapbx_support/xtables-addons-1.37.tar.xz
                tar xvf xtables-addons-1.37.tar.xz
                cd xtables-addons-1.37
		wget http://devel.mirtapbx.com/mirtapbx_support/mconfig_1.37
		mv mconfig_1.37 mconfig
#            else
#	        wget http://devel.mirtapbx.com/mirtapbx_support/xtables-addons-1.47.1.tar.gz
#		tar xzvf xtables-addons-1.47.1.tar.gz
#		cd xtables-addons-1.47.1
#	    fi
        elif [ "$version" == "7" ]
	then
	        yum -y install gcc gcc-c++ make automake unzip zip xz kernel-devel-`uname -r` wget unzip iptables-devel perl-Text-CSV_XS
	        wget http://devel.mirtapbx.com/mirtapbx_support/xtables-addons-2.10.tar.xz
		tar xvf xtables-addons-2.10.tar.xz
		cd xtables-addons-2.10
		wget http://devel.mirtapbx.com/mirtapbx_support/mconfig_1.37
		mv mconfig_1.37 mconfig
	elif [ "$version" == "8" ]
	then
	        yum -y install gcc gcc-c++ make automake unzip zip xz kernel-devel-`uname -r` wget unzip iptables-devel perl-Text-CSV_XS
	        wget http://devel.mirtapbx.com/mirtapbx_support/xtables-addons-3.9.tar.xz
		tar xvf xtables-addons-3.9.tar.xz
		cd xtables-addons-3.9
		wget http://devel.mirtapbx.com/mirtapbx_support/mconfig_1.37
		mv mconfig_1.37 mconfig
	elif [ "$version" == "9" ]
	then
	        yum -y install gcc gcc-c++ make automake unzip zip xz kernel-devel-`uname -r` wget unzip iptables-devel perl-Text-CSV_XS
	        wget http://devel.mirtapbx.com/mirtapbx_support/xtables-addons-3.24.tar.xz
		tar xvf xtables-addons-3.24.tar.xz
		cd xtables-addons-3.24
		wget http://devel.mirtapbx.com/mirtapbx_support/mconfig_1.37
		mv mconfig_1.37 mconfig	    
	fi
	./configure
	make
	make install
	start=Dahdi
	;;
    Dahdi)
	cd /usr/local/src
	if [ "$version" ==  "6" ]
	then
	    if [ ! -e dahdi-linux-complete-2.11.1+2.11.1.tar.gz ]
	    then
		wget http://downloads.asterisk.org/pub/telephony/dahdi-linux-complete/dahdi-linux-complete-2.11.1+2.11.1.tar.gz
		tar xzvf dahdi-linux-complete-2.11.1+2.11.1.tar.gz
	    fi
	elif [ "$version" ==  "7" ]
	then
	     if [ ! -e dahdi-linux-complete-2.11.1+2.11.1.tar.gz ]
	     then
		 wget http://downloads.asterisk.org/pub/telephony/dahdi-linux-complete/dahdi-linux-complete-2.11.1+2.11.1.tar.gz
		 tar xzvf dahdi-linux-complete-2.11.1+2.11.1.tar.gz		 
	     fi
     	     cd dahdi-linux-complete-2.11.1+2.11.1
	elif [ "$version" ==  "8" ]
	then
	    if [ ! -e dahdi-linux-complete-3.1.0+3.1.0.tar.gz ]
	     then
		 wget https://downloads.asterisk.org/pub/telephony/dahdi-linux-complete/dahdi-linux-complete-3.1.0+3.1.0.tar.gz
		 tar xzvf dahdi-linux-complete-3.1.0+3.1.0.tar.gz	   
	    fi
	    cd dahdi-linux-complete-3.1.0+3.1.0
    	    for i in /usr/src/kernels/* ; do touch $i/include/linux/pci-aspm.h ; done
	elif [ "$version" ==  "9" ]
	then
#	    if [ ! -e dahdi-linux-complete-3.2.0+3.2.0-centosStream9.tar.gz ]
#	     then
#		 wget https://devel.mirtapbx.com/mirtapbx_support/dahdi-linux-complete-3.2.0+3.2.0-centosStream9.tar.gz
#		 tar xzvf dahdi-linux-complete-3.2.0+3.2.0-centosStream9.tar.gz	   
#	    fi
#	    cd dahdi-linux-complete-3.2.0+3.2.0
#   	    for i in /usr/src/kernels/* ; do touch $i/include/linux/pci-aspm.h ; done	    
	    cd /usr/local/src/
	    git clone https://github.com/asterisk/dahdi-linux
	    cd dahdi-linux
        wget http://devel.mirtapbx.com/mirtapbx_support/dahdi_Kbuild
        mv dahdi_Kbuild drivers/dahdi/Kbuild        
	    make
	    make install
	    cd ..
	    cd /usr/local/src
	    git clone https://github.com/asterisk/dahdi-tools
	    cd dahdi-tools
	    autoreconf -i
	    ./configure
	    make
	    make install
	    make config
	    cp /usr/local/src/dahdi-tools/dahdi.init /etc/init.d/dahdi
	    chmod a+x /etc/init.d/dahdi
	    /etc/init.d/dahdi restart
	else
	    if [ ! -e dahdi-linux-complete-3.1.0+3.1.0.tar.gz ]
	     then
		 wget https://downloads.asterisk.org/pub/telephony/dahdi-linux-complete/dahdi-linux-complete-3.2.0+3.2.0.tar.gz
		 tar xzvf dahdi-linux-complete-3.2.0+3.2.0.tar.gz	   
	    fi
	    cd dahdi-linux-complete-3.2.0+3.2.0
    	    for i in /usr/src/kernels/* ; do touch $i/include/linux/pci-aspm.h ; done	    
	fi
	make
	make install
	make config
	cp tools/dahdi.init  /etc/init.d/dahdi
	mv /etc/dahdi/system.conf.sample /etc/dahdi/system.conf
	if [ -e /etc/redhat-release ]
	then
	    chkconfig --level 2345 dahdi on
	fi
	if [ "$version" == "7" ]
	then
	    ln -s /usr/lib/libtonezone.so.2 /usr/lib64/
	elif [ "$version" == "8" ]
        then
	    ln -s /usr/lib/libtonezone.so.2 /usr/lib64/	    
	elif [ "$version" == "9" ]
        then
	    ln -s /usr/lib/libtonezone.so.2 /usr/lib64/	    
	fi
	ldconfig
	/etc/init.d/dahdi restart
	start=Spandsp
	;;
    Spandsp)
	cd /usr/local/src
	if [ ! -e spandsp-0.0.6.tar.gz ]
	then
	    #wget http://www.soft-switch.org/downloads/spandsp/spandsp-0.0.6.tar.gz
	    wget http://devel.mirtapbx.com/mirtapbx_support/spandsp-20160831.tar.gz
	fi
	tar xzvf spandsp-20160831.tar.gz
	cd spandsp-0.0.6
	./configure
	make
	make install
	echo "/usr/local/lib" > /etc/ld.so.conf.d/locallib.conf
	ldconfig
	start=Asterisk
	;;
    Asterisk)
	cd /usr/local/src
	if [ -e /etc/redhat-release ]
	then
	    if [ "$version" ==  "6" ]
	    then
		astversion=13
		yum -y install unixODBC unixODBC-devel		
	    fi
	    if [[ "$version" == "7" ]]
	    then
		astversion=18
		rpm -Uvh http://demo.mirtapbx.com/mirtapbx_support/unixODBC-2.3.4-1.el$version.x86_64.rpm http://demo.mirtapbx.com/mirtapbx_support/unixODBC-devel-2.3.4-1.el$version.x86_64.rpm
	    fi
	    if [[ "$version" == "9" ]]
	    then
		astversion=20
		cd /tmp
		if [ ! -s jansson-2.14.tar.bz2 ]
		then
		    \rm jansson-2.14.tar.bz2*		
		    wget https://raw.githubusercontent.com/asterisk/third-party/master/jansson/2.14/jansson-2.14.tar.bz2
		fi
		if [ ! -s pjproject-2.15.1.tar.bz2 ]
		then
		    \rm pjproject-2.15.1.tar.bz2
		    wget https://raw.githubusercontent.com/asterisk/third-party/master/pjproject/2.15.1/pjproject-2.15.1.tar.bz2
		fi
		cd -
		#wget https://github.com/cisco/libsrtp/archive/refs/heads/main.zip
		wget https://demo.mirtapbx.com/mirtapbx_support/libsrtp-main.zip
		unzip libsrtp-main.zip 
		cd libsrtp-main/
		./configure --enable-openssl ; make shared_library ; make install ; \rm /usr/lib64/libsrtp2.so.1 ; ln -s /usr/local/lib/libsrtp2.so.1 /usr/lib64/libsrtp2.so.1
		cd ..
	    fi
	fi

	if [ ! -e asterisk-$astversion-current.tar.gz ]
	then
            wget http://downloads.asterisk.org/pub/telephony/asterisk/asterisk-$astversion-current.tar.gz
	fi

	wget http://demo.mirtapbx.com/mirtapbx_support/cdr_adaptive_odbc.c.ast$astversion.patch
	wget http://demo.mirtapbx.com/mirtapbx_support/app_queue.c.ast$astversion.patch
	wget http://demo.mirtapbx.com/mirtapbx_support/func_strings.c.ast$astversion.patch
	wget http://demo.mirtapbx.com/mirtapbx_support/pbx_variables.c.ast${astversion}.patch

	tar xzf asterisk-$astversion-current.tar.gz
	cd asterisk-$astversion*/
	./configure --with-pjproject-bundled --with-jansson-bundled
	contrib/scripts/get_mp3_source.sh
	if [ -e ../cdr_adaptive_odbc.c.ast$astversion.patch ]
	then
	    cd cdr
	    patch -p0 < ../../cdr_adaptive_odbc.c.ast$astversion.patch
	    cd ..
        fi
	if [ -e ../app_queue.c.ast$astversion.patch ]
	then
	    cd apps
	    patch -p0 < ../../app_queue.c.ast$astversion.patch
	    cd ..
	fi
	if [ -e ../func_strings.c.ast$astversion.patch ]
	then
	    patch -p0 < ../func_strings.c.ast$astversion.patch
	fi
	if [ -e ../pbx_variables.c.ast${astversion}.patch ]
	then
	    patch -p0 < ../pbx_variables.c.ast${astversion}.patch
	fi
	
	make menuselect.makeopts
	./menuselect/menuselect --enable ODBC_STORAGE menuselect.makeopts
	./menuselect/menuselect --enable format_mp3 menuselect.makeopts
	./menuselect/menuselect --enable app_meetme menuselect.makeopts	
	#./menuselect/menuselect --enable DONT_OPTIMIZE menuselect.makeopts
	./menuselect/menuselect --enable BETTER_BACKTRACES menuselect.makeopts
	./menuselect/menuselect --enable app_voicemail_odbc menuselect.makeopts
	./menuselect/menuselect --disable chan_iax2 menuselect.makeopts
	./menuselect/menuselect --enable chan_sip menuselect.makeopts

	./menuselect/menuselect --enable codec_opus menuselect.makeopts
	./menuselect/menuselect --enable codec_silk menuselect.makeopts

	./menuselect/menuselect --enable CORE-SOUNDS-EN-WAV menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-EN-ULAW menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-EN-ALAW menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-EN-GSM menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-EN-G729 menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-EN-G722 menuselect.makeopts

	./menuselect/menuselect --enable CORE-SOUNDS-EN_AU-WAV menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-EN_AU-ULAW menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-EN_AU-ALAW menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-EN_AU-GSM menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-EN_AU-G729 menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-EN_AU-G722 menuselect.makeopts

	./menuselect/menuselect --enable CORE-SOUNDS-EN_GB-WAV menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-EN_GB-ULAW menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-EN_GB-ALAW menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-EN_GB-GSM menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-EN_GB-G729 menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-EN_GB-G722 menuselect.makeopts

	./menuselect/menuselect --enable CORE-SOUNDS-ES-WAV menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-ES-ULAW menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-ES-ALAW menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-ES-GSM menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-ES-G729 menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-ES-G722 menuselect.makeopts

	./menuselect/menuselect --enable CORE-SOUNDS-FR-WAV menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-FR-ULAW menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-FR-ALAW menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-FR-GSM menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-FR-G729 menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-FR-G722 menuselect.makeopts

	./menuselect/menuselect --enable CORE-SOUNDS-IT-WAV menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-IT-ULAW menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-IT-ALAW menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-IT-GSM menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-IT-G729 menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-IT-G722 menuselect.makeopts

	./menuselect/menuselect --enable CORE-SOUNDS-RU-WAV menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-RU-ULAW menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-RU-ALAW menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-RU-GSM menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-RU-G729 menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-RU-G722 menuselect.makeopts

	./menuselect/menuselect --enable CORE-SOUNDS-JA-WAV menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-JA-ULAW menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-JA-ALAW menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-JA-GSM menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-JA-G729 menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-JA-G722 menuselect.makeopts

	./menuselect/menuselect --enable CORE-SOUNDS-SV-WAV menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-SV-ULAW menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-SV-ALAW menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-SV-GSM menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-SV-G729 menuselect.makeopts
	./menuselect/menuselect --enable CORE-SOUNDS-SV-G722 menuselect.makeopts

	./menuselect/menuselect --enable MOH-OPSOUND-ULAW menuselect.makeopts
	./menuselect/menuselect --enable MOH-OPSOUND-ALAW menuselect.makeopts
	./menuselect/menuselect --enable MOH-OPSOUND-GSM menuselect.makeopts
	./menuselect/menuselect --enable MOH-OPSOUND-G729 menuselect.makeopts
	./menuselect/menuselect --enable MOH-OPSOUND-G722 menuselect.makeopts

	./menuselect/menuselect --enable EXTRA-SOUNDS-EN-WAV menuselect.makeopts
	./menuselect/menuselect --enable EXTRA-SOUNDS-EN-ULAW menuselect.makeopts
	./menuselect/menuselect --enable EXTRA-SOUNDS-EN-ALAW menuselect.makeopts
	./menuselect/menuselect --enable EXTRA-SOUNDS-EN-GSM menuselect.makeopts
	./menuselect/menuselect --enable EXTRA-SOUNDS-EN-G729 menuselect.makeopts
	./menuselect/menuselect --enable EXTRA-SOUNDS-EN-G722 menuselect.makeopts

	./menuselect/menuselect --enable EXTRA-SOUNDS-EN_GB-WAV menuselect.makeopts
	./menuselect/menuselect --enable EXTRA-SOUNDS-EN_GB-ULAW menuselect.makeopts
	./menuselect/menuselect --enable EXTRA-SOUNDS-EN_GB-ALAW menuselect.makeopts
	./menuselect/menuselect --enable EXTRA-SOUNDS-EN_GB-GSM menuselect.makeopts
	./menuselect/menuselect --enable EXTRA-SOUNDS-EN_GB-G729 menuselect.makeopts
	./menuselect/menuselect --enable EXTRA-SOUNDS-EN_GB-G722 menuselect.makeopts

	./menuselect/menuselect --enable EXTRA-SOUNDS-FR-WAV menuselect.makeopts
	./menuselect/menuselect --enable EXTRA-SOUNDS-FR-ULAW menuselect.makeopts
	./menuselect/menuselect --enable EXTRA-SOUNDS-FR-ALAW menuselect.makeopts
	./menuselect/menuselect --enable EXTRA-SOUNDS-FR-GSM menuselect.makeopts
	./menuselect/menuselect --enable EXTRA-SOUNDS-FR-G729 menuselect.makeopts
	./menuselect/menuselect --enable EXTRA-SOUNDS-FR-G722 menuselect.makeopts

	make
	make install
	make samples
	if [ -e /etc/redhat-release ]
	then
            sed s/__ASTERISK_SBIN_DIR__/\\/usr\\/sbin\\//g contrib/init.d/rc.redhat.asterisk > /etc/init.d/asterisk
	    chmod a+x /etc/init.d/asterisk
	    chkconfig --level 2345 asterisk on
	else
            sed s/__ASTERISK_SBIN_DIR__/\\/usr\\/sbin\\//g contrib/init.d/rc.debian.asterisk > /tmp/asterisk
            sed s/__ASTERISK_VARRUN_DIR__/\\/var\\/run\\//g /tmp/asterisk > /tmp/asterisk.2
            sed s/__ASTERISK_ETC_DIR__/\\/etc\\//g /tmp/asterisk.2 > /etc/init.d/asterisk

	    chmod a+x /etc/init.d/asterisk
	    update-rc.d asterisk defaults
	fi
	ldconfig
	/etc/init.d/asterisk start 
        mkdir /var/lib/asterisk/sounds/pbx/
	wget http://demo.mirtapbx.com/mirtapbx_support/modules$astversion.conf
	mv modules$astversion.conf /etc/asterisk/modules.conf
#       cp contrib/systemd/asterisk.service /usr/lib/systemd/system/
	if [ -d "/etc/asterisk" ]
        then
	    start=IonCube
	else
	    echo "Asterisk not installed correctly"
            exit
	fi
	;;
     Bcompiler)
	if [ -e /etc/redhat-release ]
        then	    
            yum -y install automake m4 php-devel php-pear libbz2-dev
	    pecl install bcompiler
	    echo "extension=bcompiler.so" > /etc/php.d/bcompiler.ini
	else
	    pecl install bcompiler
	    echo "extension=bcompiler.so" > /etc/php5/conf.d/bcompiler.ini
	fi
	start=Apache
	;;
    IonCube)
	cd /usr/local/src
#	wget http://downloads3.ioncube.com/loader_downloads/ioncube_loaders_lin_x86-64.tar.bz2
	wget https://downloads.ioncube.com/loader_downloads/ioncube_loaders_lin_x86-64.tar.gz
	tar xzvf ioncube_loaders_lin_x86-64.tar.gz
	if [ -e /etc/redhat-release ]
	then
	    if [ "$version" ==  "6" ]
            then
		cp ioncube/ioncube_loader_lin_5.5.so /usr/lib64/php/modules/
	    elif [ "$version" ==  "7" ]
	    then
		 cp ioncube/ioncube_loader_lin_5.5.so /usr/lib64/php/modules/
	    elif [ "$version" ==  "8" ]		 
            then
		cp ioncube/ioncube_loader_lin_7.2.so /usr/lib64/php/modules/
	    elif [ "$version" ==  "9" ]		 
            then
		cp ioncube/ioncube_loader_lin_8.1.so /usr/lib64/php/modules/
	    fi
		
	fi
	start=Apache
	;;
    Apache)
	if [ -e /etc/redhat-release ]
        then
	    chkconfig --level 2345 httpd on
	    service httpd restart
	else
	    a2enmod ssl
	    update-rc.d apache2 defaults
	    /etc/init.d/apache2 restart
	    ln -s /etc/apache2/sites-available/default-ssl /etc/apache2/sites-enabled
	fi
	start=Files
	;;
    Files)
	if [ -e /etc/redhat-release ]
	then
	    destpath=/var/www/html/pbx
	else
	    destpath=/var/www/pbx
	fi
#	echo "Press ENTER when ready"
#	read

	cd /usr/local/src
	mkdir -p $destpath
	wget http://demo.mirtapbx.com:/mirtapbx_support/mirtapbx_setup.tar.gz
	cp -ra /etc/asterisk /etc/asterisk.orig
	cd $destpath
	tar xzvf /usr/local/src/mirtapbx_setup.tar.gz
	cp $destpath/protected/upgradeMirtaPBX.sh /usr/local/bin
	cp $destpath/protected/odbc.ini /etc
	cp $destpath/protected/odbcinst.ini /etc
	cp $destpath/protected/manager.conf /etc/asterisk
	cp $destpath/protected/cdr.conf /etc/asterisk
	cp $destpath/protected/pjsip.conf /etc/asterisk
	cp $destpath/protected/sorcery.conf /etc/asterisk
	cp $destpath/protected/musiconhold.conf /etc/asterisk
	cp $destpath/protected/extconfig.conf /etc/asterisk
	cp $destpath/protected/sip_notify.conf /etc/asterisk
	cp $destpath/protected/extensions.conf /etc/asterisk
	cp $destpath/protected/cdr_adaptive_odbc.conf /etc/asterisk
	cp $destpath/protected/res_odbc.conf /etc/asterisk
	cp $destpath/protected/voicemail.conf /etc/asterisk
	cp $destpath/protected/asterisk.conf /etc/asterisk
	cp $destpath/protected/features.conf /etc/asterisk
	cp $destpath/protected/logger.conf /etc/asterisk
	cp $destpath/protected/sip.conf /etc/asterisk

	mkdir -p /var/lib/asterisk/astdb
	echo "none /var/lib/asterisk/astdb tmpfs noatime,nodev,nosuid,noexec,mode=0755 0 0" >> /etc/fstab
	mount -a

	if [ "$version" ==  "6" ]
	then
	    cp $destpath/protected/selinux.config /etc/selinux/config
	    cp $destpath/protected/php-5.3.ini /etc/php.ini
	elif [ "$version" ==  "7" ]
	then
	    mkdir -p /var/lib/php/session
	    cp $destpath/protected/selinux.config /etc/selinux/config
	    cp $destpath/protected/php-5.3.ini /etc/php.ini
	    cp $destpath/protected/bashrc /etc
	    chmod a+x /etc/rc.d/rc.local
	elif [ "$version" ==  "8" ]
	then
	     cp $destpath/protected/selinux.config /etc/selinux/config
	     cp $destpath/protected/bashrc /etc
     	elif [ "$version" ==  "9" ]
	then
	     cp $destpath/protected/selinux.config /etc/selinux/config
	     cp $destpath/protected/bashrc /etc
	     cp $destpath/protected/php-8.1.ini /etc/php.ini
	     service php-fpm restart
             timezone=$(readlink /etc/localtime | sed 's/.*zoneinfo\///')
             sed -i "s|^;*date.timezone =.*|date.timezone = $timezone|" /etc/php.ini
	     chmod a+x /etc/rc.d/rc.local
	fi
	cp $destpath/protected/phpMyAdmin.conf /etc/httpd/conf.d
	cp $destpath/protected/sudoers-apache /etc/sudoers.d/apache
	cd /usr/lib/asterisk/modules
	wget http://demo.mirtapbx.com:/mirtapbx_support/codec_g723-ast200-gcc4-glibc-x86_64-pentium4.so
	wget http://demo.mirtapbx.com:/mirtapbx_support/codec_g729-ast200-gcc4-glibc-x86_64-pentium4.so
	echo "<?php header('Location: /pbx'); ?>" > $destpath/../index.php
	/etc/init.d/asterisk restart 
	if [ -e /etc/redhat-release ]
        then
	    if [ "$version" ==  "6" ]
            then
		patch -p0 /etc/httpd/conf/httpd.conf < $destpath/protected/httpd.conf.patch 
	    else
		patch -p0 /etc/httpd/conf/httpd.conf < $destpath/protected/httpd-centos7.conf.patch 
	    fi
	    service httpd restart
	else
	    service apache2 restart
	fi
    	start=Mysql
	;;
     Mysql)
	if [ -e /etc/redhat-release ]
	then
	    destpath=/var/www/html/pbx
	    if [ "$version" == "6" ]
	    then
		cp /var/www/html/pbx/protected/my.cnf /etc/
		chkconfig --level 2345 mysqld on
		/etc/init.d/mysqld start
	    elif [ "$version" == "7" ]
	    then		
		cp /var/www/html/pbx/protected/server.cnf /etc/my.cnf.d/
		chkconfig --level 2345 mariadb on
		service mariadb start
	    elif [ "$version" == "8" ]
	    then
		cd /etc/
		wget http://devel.mirtapbx.com/mirtapbx_support/my.cnf-8
		mv my.cnf-8 my.cnf
		chkconfig --level 2345 mysqld on
		/etc/init.d/mysqld start
	    elif [ "$version" == "9" ]
	    then
		yum -y remove mysql*
		yum -y install https://dev.mysql.com/get/mysql80-community-release-el`rpm -E %{rhel}`-4.noarch.rpm
		cd /etc/pki/rpm-gpg/
		wget https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
		wget https://repo.mysql.com/RPM-GPG-KEY-mysql
		cd -
		mv /var/lib/mysql /var/lib/mysql.old
		mkdir /var/lib/mysql
		chown mysql:mysql /var/lib/mysql
		yum -y install mysql-community-server
		yum -y upgrade
		yum -y install mysql-connector-odbc
#		yum -y install yum-plugin-versionlock
#		yum -y downgrade mysql-connector-odbc-8.0.11
#		yum versionlock mysql-connector-odbc-8.0.11
		cp /var/www/html/pbx/protected/odbcinst8.ini /etc/odbcinst.ini
		cp /var/www/html/pbx/protected/my.cnf-8 /etc/my.cnf
		truncate --size 0 /var/log/mysqld.log
		chown mysql:mysql /var/log/mysqld.log
		service mysqld start
		password=`grep password /var/log/mysqld.log  | cut -d\  -f 13`
		echo "alter user root@localhost identified by '$password' ; uninstall component 'file://component_validate_password' ; alter user root@localhost identified by ''" | mysql -u root  --connect-expired-password -p$password
		chkconfig --level 2345 mysqld on
	    fi
	else
	    destpath=/var/www/pbx
	    #cp /var/www/pbx/protected/my.cnf /etc/mysql
	    up2date-rc.d mysql defaults
	    /etc/init.d/mysql start
	fi
	echo "create database asterisk CHARACTER SET utf8 COLLATE utf8_general_ci;" | mysql -u root
	echo "create database asteriskcdrdb CHARACTER SET utf8 COLLATE utf8_general_ci;" | mysql -u root
	echo "create database voipmonitor CHARACTER SET utf8 COLLATE utf8_general_ci;" | mysql -u root
	if [ "$version" == "9" ]
	then
            echo "create user 'asterisk'@'localhost' identified with mysql_native_password by 'asterisk'" | mysql -u root 
            echo "create user 'voipmonitor'@'localhost' identified with mysql_native_password by 'voipmonitor'" | mysql -u root
            echo "grant all privileges on asterisk.* to 'asterisk'@'localhost'" | mysql -u root 
            echo "grant all privileges on asteriskcdrdb.* to 'asterisk'@'localhost'" | mysql -u root 
            echo "grant all privileges on voipmonitor.* to 'asterisk'@'localhost'" | mysql -u root
            echo "grant all privileges on voipmonitor.* to 'voipmonitor'@'localhost'" | mysql -u root

            echo "create user 'asterisk'@'127.0.0.1' identified with mysql_native_password by 'asterisk'" | mysql -u root 
            echo "create user 'voipmonitor'@'127.0.0.1' identified with mysql_native_password by 'voipmonitor'" | mysql -u root
            echo "grant all privileges on asterisk.* to 'asterisk'@'127.0.0.1'" | mysql -u root 
            echo "grant all privileges on asteriskcdrdb.* to 'asterisk'@'127.0.0.1'" | mysql -u root 
            echo "grant all privileges on voipmonitor.* to 'asterisk'@'127.0.0.1'" | mysql -u root
            echo "grant all privileges on voipmonitor.* to 'voipmonitor'@'127.0.0.1'" | mysql -u root

            echo "create user 'asterisk'@'::1' identified with mysql_native_password by 'asterisk'" | mysql -u root 
            echo "create user 'voipmonitor'@'::1' identified with mysql_native_password by 'voipmonitor'" | mysql -u root
            echo "grant all privileges on asterisk.* to 'asterisk'@'::1'" | mysql -u root 
            echo "grant all privileges on asteriskcdrdb.* to 'asterisk'@'::1'" | mysql -u root 
            echo "grant all privileges on voipmonitor.* to 'asterisk'@'::1'" | mysql -u root
            echo "grant all privileges on voipmonitor.* to 'voipmonitor'@'::1'" | mysql -u root

            echo "update mysql.user set Super_priv='Y' where User='voipmonitor'" | mysql -u root 
	else
	    echo "create user 'asterisk'@'localhost' identified by 'asterisk'" | mysql -u root
	    echo "create user 'voipmonitor'@'localhost' identified by 'voipmonitor'" | mysql -u root
	    echo "grant all privileges on asterisk.* to 'asterisk'@'localhost'" | mysql -u root
	    echo "grant all privileges on asteriskcdrdb.* to 'asterisk'@'localhost'" | mysql -u root
	    echo "grant all privileges on voipmonitor.* to 'asterisk'@'localhost'" | mysql -u root
	    echo "grant all privileges on voipmonitor.* to 'voipmonitor'@'localhost'" | mysql -u root
	fi
	echo "update mysql.user set Super_priv='Y' where User='voipmonitor'" | mysql -u root
	cat $destpath/protected/asterisk.sql | mysql -u root asterisk
	cat $destpath/protected/voipmonitor.sql | mysql -u root voipmonitor
	cat $destpath/protected/asteriskcdrdb.sql | mysql -u root asteriskcdrdb
	cat $destpath/protected/al_autoprovisionlabels.sql | mysql -u root asterisk
	cat $destpath/protected/cd_codecs.sql | mysql -u root asterisk
	cat $destpath/protected/ct_customtypes.sql | mysql -u root asterisk
	cat $destpath/protected/se_settings.sql | mysql -u root asterisk
	cat $destpath/protected/pm_phonemodels.sql | mysql -u root asterisk
	cat $destpath/protected/tz_timezones.sql | mysql -u root asterisk
	cat $destpath/protected/us_users.sql | mysql -u root asterisk 
	cat $destpath/protected/sp_specials.sql | mysql -u root asterisk
#	cat $destpath/protected/cdr_sip_response.sql  | mysql -u root voipmonitor
#	/usr/bin/mysql_secure_installation

#	echo "UPDATE mysql.user SET Password=PASSWORD('passw0rd') WHERE User='root';" | mysql -u root
	echo "DELETE FROM mysql.user WHERE User='';" | mysql -u root
	echo "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');" | mysql -u root
	echo "DROP DATABASE test;" | mysql -u root
	echo "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'" | mysql -u root
	echo "ALTER user root@localhost identified by 'passw0rd' ; flush privileges" | mysql -u root
	
        if [ -e /etc/redhat-release ]
        then
	    if [ "$version" == "6" ]
            then
		service mysqld restart
	    elif [ "$version" == "7" ]
	    then
		service mariadb restart
	    elif [ "$version" == "8" ]
	    then
		service mysqld restart
	    elif [ "$version" == "9" ]
	    then
		
		service mysqld restart
	    fi
	else
	    service mysql restart
	fi
	start=Voipmonitor-static
	;;
     Voipmonitor-static)
	cd /usr/local/src
	wget http://demo.mirtapbx.com/mirtapbx_support/voipmonitor-amd64-31.9-static.tar.gz
	tar xzvf voipmonitor-amd64-31.9-static.tar.gz
	cd voipmonitor-amd64-31.9-static
	\mv /etc/voipmonitor.conf /etc/voipmonitor.conf.old
	./install-script.sh --no-user-input
	\cp /var/www/html/pbx/protected/voipmonitor.conf /etc
        chkconfig --add voipmonitor
        chkconfig voipmonitor on
        mkdir /var/spool/voipmonitor
        service voipmonitor restart
        start=Misc
	;;
     Upgrade-voipmonitor)
	cd /usr/local/src
	\rm -r current-stable*
	\rm -t voipmonitor-*
	wget http://demo.mirtapbx.com/mirtapbx_support/voipmonitor-amd64-31.9-static.tar.gz
        tar xzvf voipmonitor-amd64-31.9-static.tar.gz
        cd voipmonitor-amd64-31.9-static
	./install-script.sh
	start=END
	;;
     Voipmonitor)
	cd /usr/local/src

	if [ -e /etc/redhat-release ]
        then
	    yum -y install libpng-devel fftw-devel lzo-devel glib2-devel unixODBC-devel libogg-devel libvorbis-devel libpcap-devel snappy-devel curl-devel json-c-devel libssh-devel rrdtool-devel libxml2-devel gnutls-devel libgcrypt-devel 
	    yum -y install mariadb-devel lzma-devel
	    rm -r voipmonitor*
	    wget http://devel.mirtapbx.com:/mirtapbx_support/voipmonitor-19.2.1-src.tar.gz
	    tar xzvf voipmonitor-19.2.1-src.tar.gz
	    cd voipmonitor-19.2.1-src
	    wget http://devel.mirtapbx.com:/mirtapbx_support/sql_db.cpp.patch
	    patch -p0 < sql_db.cpp.patch
	    ./configure
	    make
	    make install
	    cp config/init.d/voipmonitor /etc/init.d/
	fi

	chmod a+x /etc/init.d/voipmonitor
	if [ -e /etc/redhat-release ]
	then
	    cp /var/www/html/pbx/protected/voipmonitor.conf /etc
	    chkconfig --add voipmonitor
	    chkconfig voipmonitor on
	else
	    cp /var/www/pbx/protected/voipmonitor.conf /etc
	    up2date-rc.d voipmonitor defaults
	fi
	mkdir /var/spool/voipmonitor
	service voipmonitor restart
	start=Misc
	;;
     Misc)
	if [ -e /etc/redhat-release ]
	then
	    destpath=/var/www/html/pbx
            service iptables stop
            chkconfig --level 2345 iptables off
	    if [ "$version" == "7" ]
	    then
		systemctl disable firewalld
		systemctl stop firewalld
	    elif [ "$version" == "9" ]
	    then
		systemctl disable firewalld
		systemctl stop firewalld
	    fi
	else
	    destpath=/var/www/pbx
	fi
	echo "nohup /usr/local/bin/restartdevstatereceiver.sh &" >> /etc/rc.local
	echo "nohup /usr/local/bin/restartdevstatesender.sh &" >> /etc/rc.local
#	echo "nohup /usr/local/bin/restartpanelserver.sh &" >> /etc/rc.local
	echo "/var/lib/asterisk/agi-bin/applyGeoIPfirewall.sh" >> /etc/rc.local
	echo "/var/lib/asterisk/agi-bin/applyFail2Ban.sh" >> /etc/rc.local
	echo "exit 0 " >> /etc/rc.local
	echo "2 0 * * * root /usr/sbin/asterisk -r -x 'logger rotate'" >> /etc/crontab
	echo "4 0 * * * root find /var/spool/asterisk/monitor/ -type f -mtime +10 -exec rm {} \;" >> /etc/crontab
	echo "4 0 * * * root find /var/log/asterisk/ -type f -mtime +10 -exec rm {} \;" >> /etc/crontab
	echo "1 0 * * * root cd $destpath ; /usr/bin/php $destpath/housekeeping.php &> /tmp/housekeeping.out" >> /etc/crontab
	echo "0 0 * * * root cd $destpath ; /usr/bin/php $destpath/computecosts.php &> /tmp/computecosts.out" >> /etc/crontab
	echo "0 0 * * * root cd $destpath ; /usr/bin/php $destpath/checkupgrade.php &> /tmp/checkupgrade.out" >> /etc/crontab
	echo "* * * * * root cd $destpath ; /usr/bin/php $destpath/checkalarms.php &> /tmp/checkalarms.out" >> /etc/crontab
	echo "* * * * * root cd $destpath ; /usr/bin/php $destpath/checkqueuecallback.php &> /tmp/checkqueuecallback.out" >> /etc/crontab
	echo "* * * * * root cd $destpath ; /usr/bin/php $destpath/checkfaxmailboxes.php &> /tmp/checkfaxmailboxes.out" >> /etc/crontab
	echo "* * * * * root cd $destpath ; /usr/bin/php $destpath/checksendfax.php &> /tmp/checksendfax.out" >> /etc/crontab
	echo "* * * * * root cd $destpath ; /usr/bin/php $destpath/checkcallreservations.php &> /tmp/checkcallreservations.out" >> /etc/crontab
	echo "* * * * * root cd $destpath ; /usr/bin/php $destpath/croncheck.php &> /tmp/croncheck.out" >> /etc/crontab
	echo "* * * * * root cd $destpath ; /usr/bin/php /var/lib/asterisk/agi-bin/checkgeoipf2b.php &> /tmp/geoip.out " >> /etc/crontab
	echo '1 0 * * * root for i in `find /var/spool/voipmonitor/ -type f -name \*.pcap`; do /var/lib/asterisk/agi-bin/uploadvoipmonitor.php $i; rm -f $i ; done' >> /etc/crontab
	chkconfig --level 2345 ntpd on

	yum -y install snapd ; systemctl enable --now snapd.socket ; ln -s /var/lib/snapd/snap /snap
	snap install core; snap refresh core ; snap install --classic certbot ; ln -s /snap/bin/certbot /usr/bin/certbot
	
	yum -y install python3-certbot-apache

	/usr/local/bin/upgradeGeoIP.sh

	start=Config
	;;
	Config)
	start=END
	;;
     Nagios)
        for file in nrpe nagios-plugins-load nagios-plugins-disk nagios-plugins-perl nagios-plugins-procs sipsak yum-security perl-Nagios-Plugin perl-LWP-UserAgent-Determined.noarch nagios-plugins-ping sysstat nagios-plugins-ntp 
        do
	   yum -y install $file
        done
	yum -y install perl-Switch
	if [ "$version" == "6" ]
	then
	    rpm -i --nodeps https://demo.mirtapbx.com/mirtapbx_support/nagios-plugins-check_sip-1.3-4.el6.x86_64.rpm
	    cp /var/www/html/pbx/protected/nrpe.cfg /etc/nagios
	    cp /var/www/html/pbx/protected/sudoers-nagios /etc/sudoers.d/nagios
	elif [ "$version" == "7" ]
	then
	    rpm -i --nodeps  https://demo.mirtapbx.com/mirtapbx_support/nagios-plugins-check_sip-1.3-4.el7.centos.x86_64.rpm
	    cp /var/www/html/pbx/protected/nrpe.el7.cfg /etc/nagios/nrpe.cfg
	    cp /var/www/html/pbx/protected/sudoers-nrpe /etc/sudoers.d/nrpe
	elif [ "$version" == "9" ]
	then
	    rpm -i --nodeps  https://demo.mirtapbx.com/mirtapbx_support/nagios-plugins-check_sip-1.3-7.el9.x86_64.rpm
	    touch /var/run/nrpe.pid
	    chown nrpe:nrpe /var/run/nrpe.pid
	    cp /var/www/html/pbx/protected/nrpe.el7.cfg /etc/nagios/nrpe.cfg
	    cp /var/www/html/pbx/protected/sudoers-nrpe /etc/sudoers.d/nrpe
	fi
	cp /var/www/html/pbx/protected/nagios/* /usr/local/bin
	chmod 644 /etc/yum.repos.d/*
	chkconfig --level 2345 nrpe on
	echo "ready?"
	read
	echo "create user 'nagios'@'localhost' identified by 'wXq3u8RrPX7ue8xj'" | mysql -u root -ppassw0rd
	echo "create user 'nagios'@'**********' identified by 'wXq3u8RrPX7ue8xj'" | mysql -u root -ppassw0rd
	echo "create user 'nagios'@'**********' identified by 'wXq3u8RrPX7ue8xj'" | mysql -u root -ppassw0rd
	echo "GRANT REPLICATION CLIENT ON * . * TO  'nagios'@'localhost'" | mysql -u root -ppassw0rd
	echo "GRANT REPLICATION CLIENT ON * . * TO  'nagios'@'**********'" | mysql -u root -ppassw0rd
	echo "GRANT REPLICATION CLIENT ON * . * TO  'nagios'@'**********'" | mysql -u root -ppassw0rd
	echo "flush privileges" | mysql -u root -ppassw0rd
	exit
	;;
     Replication)
	echo "ready?"
	read
	echo "create user 'replication'@'**********' identified by 'HGNJ4C4YtAt3WtTY'" | mysql -u root -ppassw0rd
	echo "create user 'replication'@'**********' identified by 'HGNJ4C4YtAt3WtTY'" | mysql -u root -ppassw0rd
	echo "grant replication slave on *.* to 'replication'@'************'" | mysql -u root -ppassw0rd
	echo "grant replication slave on *.* to 'replication'@'************'" | mysql -u root -ppassw0rd
	echo "flush privileges" | mysql -u root -ppassw0rd
	exit
	;;
     Kernel)
	yum -y install tar
	yum -y install yum-plugin-versionlock
	mkdir /root/.ssh
	echo "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAxSvbcJCK6DnI052MOC2hcstRq9qg3zH9jnLk6PYnoWaOA038QIlanz8t+6OMgRgxV+b6Qx/gJhgQCq+bSUlmmRbfcKZemcTyv79TnubRZ36+UPoyF+5GmDDjcrOoaLomzJhW5HOS/nQwMugylTyFVbCCaX3hsmqpSeymg9S5INdU8UnETc9/ILxXW2fvbJkf5svR4bJVzr3/v2ckiR2ciUtlBsp8pUbDvZume0gD+8fbDy+3UAEZE3Ip9/O/Jp+EkCj27B+QKOADwPFx7PbGA0oa6ebBr9i7Ht62ljtPWEmcKRimDRb6+vbCFCCALmEHp0DFCLfKV+9uStkbSG75tw== root@mirtapbx" >> /root/.ssh/authorized_keys
	if [ -e /etc/redhat-release ]
	then
	    if [ "$version" == "9" ]
	    then
		update-crypto-policies --set LEGACY
		yum -y upgrade
#		wget http://demo.mirtapbx.com/mirtapbx_support/kernel-5.14.0-362.tar.gz
#		tar xzvf kernel-5.14.0-362.tar.gz
#		yum -y install kernel*rpm	   
#		grubby --set-default /boot/vmlinuz-5.14.0-362.18.1.el9_3.x86_64
#		yum versionlock kernel-5.14.0-362.18.1.el9_3.x86_64 kernel-devel-5.14.0-362.18.1.el9_3.x86_64 kernel-modules-5.14.0-362.18.1.el9_3.x86_64 kernel-core-5.14.0-362.18.1.el9_3.x86_64 kernel-modules-core-5.14.0-362.18.1.el9_3.x86_64
	    else	    
		cd /etc/yum.repos.d/
		yum -y install wget
		yum -y install kernel kernel-firmware kernel-devel
	    fi
	else
	    apt-get install linux-headers-`uname -r`
	fi
	echo "Now you need to reboot ... maybe try the kernel with echo \"savedefault --default=0 --once\" | grub --batch "
	exit
	;;    
     MySQL57)
        mysql -u root -ppassw0rd --execute="SET GLOBAL innodb_fast_shutdown=0"
        mysqladmin -u root -ppassw0rd shutdown
        yum -y install https://dev.mysql.com/get/mysql57-community-release-el6-11.noarch.rpm
        yum -y upgrade
        mysqld --skip-grant-tables &
        mysql_upgrade -u root -ppassw0rd
        killall mysqld
        service mysqld restart
        exit
        ;;
      MySQL80)
        echo "create database asterisk CHARACTER SET utf8 COLLATE utf8_general_ci;" | mysql -u root -ppassw0rd
        echo "create database asteriskcdrdb CHARACTER SET utf8 COLLATE utf8_general_ci;" | mysql -u root -ppassw0rd
        echo "create database voipmonitor CHARACTER SET utf8 COLLATE utf8_general_ci;" | mysql -u root -ppassw0rd
        echo "create user 'asterisk'@'localhost' identified with mysql_native_password by 'asterisk'" | mysql -u root -ppassw0rd
        echo "create user 'voipmonitor'@'localhost' identified with mysql_native_password by 'voipmonitor'" | mysql -u root -ppassw0rd
        echo "grant all privileges on asterisk.* to 'asterisk'@'localhost'" | mysql -u root -ppassw0rd
        echo "grant all privileges on asteriskcdrdb.* to 'asterisk'@'localhost'" | mysql -u root -ppassw0rd
        echo "grant all privileges on voipmonitor.* to 'asterisk'@'localhost'" | mysql -u root -ppassw0rd
        echo "grant all privileges on voipmonitor.* to 'voipmonitor'@'localhost'" | mysql -u root -ppassw0rd
        echo "update mysql.user set Super_priv='Y' where User='voipmonitor'" | mysql -u root -ppassw0rd

        echo "create user 'nagios'@'localhost' identified with mysql_native_password by 'wXq3u8RrPX7ue8xj'" | mysql -u root -ppassw0rd
        echo "create user 'nagios'@'*****' identified with mysql_native_password by 'wXq3u8RrPX7ue8xj'" | mysql -u root -ppassw0rd
        echo "create user 'nagios'@'*****' identified with mysql_native_password by 'wXq3u8RrPX7ue8xj'" | mysql -u root -ppassw0rd
        echo "GRANT REPLICATION CLIENT ON * . * TO  'nagios'@'localhost'" | mysql -u root -ppassw0rd
        echo "GRANT REPLICATION CLIENT ON * . * TO  'nagios'@'*****'" | mysql -u root -ppassw0rd
        echo "GRANT REPLICATION CLIENT ON * . * TO  'nagios'@'*****'" | mysql -u root -ppassw0rd
        echo "flush privileges" | mysql -u root -ppassw0rd

        echo "create user 'replication'@'*****' identified with mysql_native_password by 'HGNJ4C4YtAt3WtTY'" | mysql -u root -ppassw0rd
        echo "create user 'replication'@'*****' identified with mysql_native_password by 'HGNJ4C4YtAt3WtTY'" | mysql -u root -ppassw0rd
        echo "grant replication slave on *.* to 'replication'@'*****'" | mysql -u root -ppassw0rd
        echo "grant replication slave on *.* to 'replication'@'*****'" | mysql -u root -ppassw0rd
        echo "flush privileges" | mysql -u root -ppassw0rd
        exit
        ;;
	fastUpgradeMysql80)
        echo "use voipmonitor;truncate me_media;use asterisk;truncate ee_extensionevents; truncate sl_serverlogs; truncate sessions2; use asteriskcdrdb; truncate cel" | mysql -u root -ppassw0rd
	mysqldump -u root -ppassw0rd --max-allowed-packet=1024M --force asterisk > /root/asterisk.sql
	mysqldump -u root -ppassw0rd --max-allowed-packet=1024M --force asteriskcdrdb > /root/asteriskcdrdb.sql
	mysqldump -u root -ppassw0rd --max-allowed-packet=1024M --force voipmonitor > /root/voipmonitor.sql
	service mariadb stop
	service mysqld stop
	yum -y install https://dev.mysql.com/get/mysql80-community-release-el`rpm -E %{rhel}`-7.noarch.rpm
	cd /etc/pki/rpm-gpg/ 
	wget https://repo.mysql.com/RPM-GPG-KEY-mysql-2023
	wget https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
	wget https://repo.mysql.com/RPM-GPG-KEY-mysql
	cd -
	mv /var/lib/mysql /var/lib/mysql.old
	mkdir /var/lib/mysql
	chown mysql:mysql /var/lib/mysql
	yum -y upgrade
	yum -y install yum-plugin-versionlock
	yum -y downgrade mysql-connector-odbc-8.0.11
	yum versionlock mysql-connector-odbc-8.0.11
	\cp /var/www/html/pbx/protected/my.cnf-8 /etc/my.cnf
	truncate --size 0 /var/log/mysqld.log
	service mysqld start
	password=`grep password /var/log/mysqld.log  | cut -d\  -f 13`
	echo "alter user root@localhost identified by '$password' ; uninstall component 'file://component_validate_password' ; alter user root@localhost identified by 'passw0rd'" | mysql -u root  --connect-expired-password -p$password
	echo "create database asterisk CHARACTER SET utf8 COLLATE utf8_general_ci;" | mysql -u root -ppassw0rd
        echo "create database asteriskcdrdb CHARACTER SET utf8 COLLATE utf8_general_ci;" | mysql -u root -ppassw0rd
        echo "create database voipmonitor CHARACTER SET utf8 COLLATE utf8_general_ci;" | mysql -u root -ppassw0rd
        echo "create user 'asterisk'@'localhost' identified with mysql_native_password by 'asterisk'" | mysql -u root -ppassw0rd
        echo "create user 'voipmonitor'@'localhost' identified with mysql_native_password by 'voipmonitor'" | mysql -u root -ppassw0rd
        echo "grant all privileges on asterisk.* to 'asterisk'@'localhost'" | mysql -u root -ppassw0rd
        echo "grant all privileges on asteriskcdrdb.* to 'asterisk'@'localhost'" | mysql -u root -ppassw0rd
        echo "grant all privileges on voipmonitor.* to 'asterisk'@'localhost'" | mysql -u root -ppassw0rd
        echo "grant all privileges on voipmonitor.* to 'voipmonitor'@'localhost'" | mysql -u root -ppassw0rd
        echo "update mysql.user set Super_priv='Y' where User='voipmonitor'" | mysql -u root -ppassw0rd
	
	cat /root/asterisk.sql | mysql -u root -ppassw0rd --max-allowed-packet=1024M --force asterisk
	cat /root/asteriskcdrdb.sql | mysql -u root -ppassw0rd --max-allowed-packet=1024M --force asteriskcdrdb
	cat /root/voipmonitor.sql | mysql -u root -ppassw0rd --max-allowed-packet=1024M --force voipmonitor
	exit
	;;
     esac
done
