#!/usr/bin/env bash

echo 
echo "*** install Thirdlane Communications Manager ***"
echo 

echo Check Asterisk Version...
/usr/sbin/asterisk -rx "core show version"
echo

touch /etc/asterisk/sip_peers.include

mkdir -p /etc/asterisk/provisioning/
touch /etc/asterisk/provisioning/models.txt
mkdir -p /etc/asterisk/user_provisioning/
chown asterisk:asterisk /etc/asterisk/ -R

mkdir -p /usr/local/share/perl5/
mkdir -p /var/log/thirdlane/

time \
  yum -y install turnserver rtpengine thirdlane-api-core thirdlane-uniserver thirdlane-api-server-ng thirdlane-crm-connector thirdlane-kamailio-config --enablerepo=epel
#time \
#yum -y install rtpengine-kernel rtpengine-dkms kernel-devel-$(uname -r) --enablerepo=epel
#  yum -y install thirdlane-ha-cluster-scripts turnserver thirdlane-prosody rtpengine thirdlane-cacher --enablerepo=epel
# thirdlane-jitsi-videobridge

# temporary hack
sed -i -e 's/^modparam("dialog", "dlg_flag", FLT_DLG).*$/#modparam("dialog", "dlg_flag", FLT_DLG)/' /etc/kamailio/include/features/presence.cfg

time \
  yum -y install thirdlane-prosody --enablerepo=epel --exclude=prosody 

# fix kamailio config
sed -i 's/^#!define LOG_LEVEL L_DBG.*$/#!define LOG_LEVEL L_INFO/' /etc/kamailio/local.cfg 

systemctl stop prosody turnserver rtpengine cacher pusher

# fix turnserver config
turn_pass=`openssl rand -hex 32`
echo -e "\n\nno-cli\ncli-password=$turn_pass\n" >> /etc/turnserver/turnserver.conf
echo pidfile=\"/var/run/turnserver/turnserver.pid\" >> /etc/turnserver/turnserver.conf
sed -i 's/^listening-device=.*/#&/g' /etc/turnserver/turnserver.conf 
chmod -x /usr/lib/systemd/system/turnserver.service
echo "d /run/turnserver   710 turnserver turnserver" > /etc/tmpfiles.d/turnserver.conf 

# fix rtpengine
mkdir -p /var/lib/ngcp-rtpengine/

# fix prosody
pkill luajit
xmpp_pass=`openssl rand -hex 32`
cat >>/etc/prosody/components.cfg.lua <<-EOF

Component "sms-api.default.local"
component_secret = "$xmpp_pass"

EOF

# save my.cnf
cp -f /etc/my.cnf /etc/my.cnf.sav

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

#exit

yum -y install pbxm-perl-lib awscli2 google-cloud-cli rclone

# pbxm debug -  'error' => 0, 'warn' => 1, 'info' => 2, 'debug' => 3, 'trace' => 4, 'default' => 3 
/usr/local/sbin/debug_adm --action=enable --object=debug --level=debug

yum -y install pbxmw-${WBMTYPE}
#cat /etc/webmin/asterisk/install.log

yum -y install thirdlane-tnt-watcher thirdlane-tnt-crm thirdlane-tnt-backup
systemctl daemon-reload

yum -y install thirdlane-phone-templates-${WBMTYPE} thirdlane-phone-firmware thirdlane-connect-${WBMTYPE} thirdlane-connect-downloads

# restore my.cnf
cp -f /etc/my.cnf.sav /etc/my.cnf

# api-server-ng fix
#sed -i "s/^VAL=.*redhat-release.*$/VAL=10/" /etc/sysconfig/api-server-ng.sh

yum -y install thirdlane-pbxprotect

#systemctl restart nginx
#systemctl restart webmin
#systemctl restart syslog-ng

mkdir -p /usr/libexec/webmin/asterisk/

cat >/usr/libexec/webmin/asterisk/wizard_finished.sh <<-EOF
#!/bin/bash
log=/tmp/daemons.log
echo \$(date +'%F %T.%N') run wizard_restart_daemons.sh
echo \$(date +'%F %T.%N') run wizard_restart_daemons.sh >>\$log

#echo /usr/libexec/webmin/asterisk/wizard_restart_daemons.sh | at now

job="* * * * * /usr/libexec/webmin/asterisk/wizard_restart_daemons.sh; /usr/bin/crontab -l | /bin/grep -iv wizard_restart_daemons | /usr/bin/crontab -"
tmp=/tmp/crontab
/usr/bin/crontab -l >\$tmp && echo "\${job}" >>\$tmp && cat \$tmp | /usr/bin/crontab -
EOF

chmod +x /usr/libexec/webmin/asterisk/wizard_finished.sh

cat >/usr/libexec/webmin/asterisk/wizard_restart_daemons.sh <<-EOF
#!/bin/bash
log=/tmp/daemons.log

list='prosody asterisk kamailio memcached2 cacher pusher api-server-ng crm-connector rtpengine turnserver uniserver jvb license api munin munin-node munin-cgi-graph tarantool@pbxm-watcher tarantool@crm-connector'

for x in \$list; do
  echo \$(date +'%F %T.%N') systemctl restart \$x
  echo \$(date +'%F %T.%N') systemctl restart \$x >>\$log 
  /usr/bin/systemctl restart \$x 
done
#/usr/bin/sed -i  /etc/odbc.ini -e 's/^Driver = MariaDB/Driver = MySQL/g'
EOF

# fix /etc/odbcinst.ini
grep MariaDB /etc/odbcinst.ini >/dev/null || cat >> /etc/odbcinst.ini <<-EOF

[MariaDB]
Description     = ODBC for MariaDB
Driver          = /usr/lib/libmaodbc.so
Driver64        = /usr/lib64/libmaodbc.so
FileUsage       = 1
EOF

chmod +x /usr/libexec/webmin/asterisk/wizard_restart_daemons.sh

# fix nginx 
usermod -a -G asterisk nginx

if [ -e /var/thirdlane_load/xmpp_split_config.pl ]; then
echo Fix prosody configs...
cd /
/bin/perl /var/thirdlane_load/xmpp_split_config.pl
systemctl stop  prosody
systemctl start prosody
fi

echo Configure provisioning config...
genpass="openssl rand -hex 12"
cat > /etc/asterisk/provisioning.txt <<-EOF
proxy_server_backup=
reg_server=
enable_http=1
ntp_server=
enable_ftp=0
provisioning_mode=local
proxy_server=
reg_server_backup=
directory=/home/PlcmSpIp
firmware_server=
enable_tftp=0
server=domain.tld
default_protocol=http
enable_https=0
http_auth=`$genpass`:`$genpass`
ftp_auth=PlcmSpIp:`$genpass`
EOF
