#!/bin/bash
#

PRODUCT_NAME=appliance
PRODUCT=$PRODUCT_NAME
NYQUIST_VERSION=1.0.0
BOGEN_ROOT=/opt/bogen
MAIN_TAR_LOC=/tmp
MAIN_TAR_NAME=$PRODUCT-$NYQUIST_VERSION
PRODUCT_ROOT=$BOGEN_ROOT/$PRODUCT_NAME
INSTALL_SCRIPT=install_$PRODUCT_NAME
NYQUIST_SCRIPTS=$BOGEN_ROOT/$PRODUCT_NAME/scripts
HTML_ROOT=$PRODUCT_ROOT/root/var/www/html
BACKUPS_DIR=/var/opt/$PRODUCT/backups
INSTALL_LOG=$PRODUCT_ROOT/Install-log-`date +%F-%R`.txt
#
date >> $INSTALL_LOG
echo "$PRODUCT_NAME product installation started..." | tee -a $INSTALL_LOG
#mkdir -p --mode=755 /etc/asterisk
#mkdir -p --mode=750 /etc/asterisk/startup.d
mkdir -p --mode=770 $BACKUPS_DIR
chgrp www-data $BACKUPS_DIR
chmod 770 $BACKUPS_DIR

#
echo "Installing required packages..." | tee -a $INSTALL_LOG
apt-get update >> $INSTALL_LOG  2>&1
apt-get -y install debconf-utils >> $INSTALL_LOG 2>&1
debconf-set-selections <<< 'tftpd-hpa tftpd-hpa/directory string /srv/tftp'
debconf-set-selections <<< 'tftpd-hpa tftpd-hpa/address string :69'
debconf-set-selections <<< 'tftpd-hpa tftpd-hpa/options string --secure'
$NYQUIST_SCRIPTS/install-packages install
echo -e "Package installation done.\n" | tee -a $INSTALL_LOG


#
# Decompress main tar file
#
tar -x -P -C / -f $MAIN_TAR_LOC/$MAIN_TAR_NAME.tar
gzip -f $MAIN_TAR_LOC/$MAIN_TAR_NAME.tar

: <<'END'

#
# Install product files
#
cd $BOGEN_ROOT
tar -x -P -C / -f $NYQUIST_CORE/$PRODUCT-$NYQUIST_VERSION-files.tar
gzip -f $NYQUIST_CORE/$PRODUCT-$NYQUIST_VERSION-files.tar

WEB_USERNAME="www-data"

SUDOER_STRING="www-data ALL = NOPASSWD: /etc/init.d/ntp, /usr/bin/sntp, /usr/local/bin/remove_calls, /usr/local/bin/schedule_events, /usr/local/bin/create_call, /usr/local/bin/clock_set, /usr/local/bin/create-syscontacts, /usr/local/bin/create-mac-phoneprov, /usr/local/bin/update-all-snom-phones, /usr/local/bin/reboot-all-snom-phones, /usr/local/bin/update-digium-phone-conf, /usr/local/bin/update-phones, /usr/local/bin/update-parking-lot, /bin/date, /etc/timezone, /etc/ntp.conf, /var/db/ntp-kod, /bin/sed, /etc/dhcp/dhclient.conf, /etc/localtime, /bin/ln, /bin/mv, /sbin/dhclient, /bin/rm, /usr/local/bin/update-dahdi-conf, /usr/local/bin/update-outside-lines, /usr/sbin/lsdahdi, /usr/sbin/dahdi_genconf, /usr/sbin/dahdi_cfg, /usr/local/bin/run_dahdi_genconf, /usr/local/bin/create-yealink-cfg, /usr/local/bin/sync-yealink-phones, /usr/local/bin/update-all-yealink-phones, /usr/local/bin/update-siptrunks-conf, /usr/local/bin/update-siptrunks-status, /usr/local/bin/run_update_outsideline_status, /usr/local/bin/update-yealink-idlescreen, /usr/local/bin/get_todays_schedule, /usr/local/bin/create-remote-phonebook, /usr/local/bin/create-backup, /usr/local/bin/update-backup-files-list, /usr/local/bin/system-backup, /usr/local/bin/run_system_backup, /usr/local/bin/update-server-ipaddr, /usr/local/bin/import-file, /usr/local/bin/get-calls-info, /usr/local/bin/check-ast-logs, /usr/local/bin/update-device-status, /usr/local/bin/getlog, /usr/local/bin/licinfo, /usr/local/bin/run_server_cmd, /usr/local/bin/reboot-all-devices, /usr/local/bin/update-paging-exclusion, /usr/local/bin/run_sysupdate, /usr/local/bin/system-update"
echo -n "Updating WEB UI permissions..." | tee -a $INSTALL_LOG
if grep -q "$WEB_USERNAME" /etc/sudoers; then
	sed -i "/\b\($WEB_USERNAME\)\b/d" /etc/sudoers
	echo $SUDOER_STRING >> /etc/sudoers
else
	echo $SUDOER_STRING >> $INSTALL_LOG
	echo $SUDOER_STRING >> /etc/sudoers
fi
echo "done" | tee -a $INSTALL_LOG


echo -n "Installing PJSIP..." | tee -a $INSTALL_LOG
cd $NYQUIST_CORE
tar -x -P -f $NYQUIST_CORE/$PRODUCT-pjsip.tar
gzip -f $NYQUIST_CORE/$PRODUCT-pjsip.tar
cd $PJPROJECT_DIR
make install >> $INSTALL_LOG 2>&1
ldconfig >> $INSTALL_LOG 2>&1
echo "done" | tee -a $INSTALL_LOG


echo -n "Installing $PRODUCT source..." | tee -a $INSTALL_LOG
cd $NYQUIST_CORE
tar -x -P -f $NYQUIST_CORE/$PRODUCT-$NYQUIST_VERSION-src.tar
gzip -f $NYQUIST_CORE/$PRODUCT-$NYQUIST_VERSION-src.tar
#cd $PJPROJECT_DIR
#make install >> $INSTALL_LOG 2>&1
#ldconfig >> $INSTALL_LOG 2>&1
echo "done" | tee -a $INSTALL_LOG

#
# Upate SNMP configuration
#
#sed -i "s/^#rocommunity public.*localhost/rocommunity public localhost/" /etc/snmp/snmpd.conf >> $INSTALL_LOG 2>&1
#service snmpd restart >> $INSTALL_LOG 2>&1

chown root:www-data /srv/tftp
chmod 775 /srv/tftp
echo "done" | tee -a $INSTALL_LOG


#
# Web specific installation...
#
#
# Apache
#
echo "==> Installing Apache" | tee -a $INSTALL_LOG
apt-get -y install apache2
echo "Apache Install done" | tee -a $INSTALL_LOG
#
# PHP
#
echo "==> Installing PHP" | tee -a $INSTALL_LOG
apt-get -y install php5 php-pear php5-mysql
echo "PHP Install done" | tee -a $INSTALL_LOG
#
# Update php.ini
#
#sed -i "s/upload_max_filesize = .*/upload_max_filesize = 2G/" /etc/php5/apache2/php.ini
#sed -i "s/post_max_size = .*/post_max_size = 4G/" /etc/php5/apache2/php.ini
#sed -i "s/session.gc_maxlifetime = .*/session.gc_maxlifetime = 86400/" /etc/php5/apache2/php.ini
service apache2 restart

#
# Laravel
#
#echo "Remove Debian test index page" | tee -a $INSTALL_LOG
#rm -f /var/www/html/index.html
#echo "Enable copy hidden files" | tee -a $INSTALL_LOG
#shopt -s dotglob
#echo "==> Copy Laravel website to /var/www/html/laravel/" | tee -a $INSTALL_LOG
#mkdir -p --mode=755 /var/www/html
#if [ -d /var/www/html/laravel ];
#then
#   cp -rf $NYQUIST_CORE/root/var/www/html/laravel/*  /var/www/html/laravel/
#else
#   cp -rf $NYQUIST_CORE/root/var/www/html/laravel/  /var/www/html/laravel/
#fi
#chmod -R 777 /var/www/html/laravel/storage
#chmod -R 777 /var/www/html/laravel/bootstrap/cache
#echo "Laravel website copy done" | tee -a $INSTALL_LOG
#
#echo -n "==> Add File Symbolic Links ..." | tee -a $INSTALL_LOG
#if [ ! -e /var/www/html/laravel/public/audio/monitor ]; then
#  ln -s /var/spool/asterisk/monitor  /var/www/html/laravel/public/audio
#fi
#if [ ! -e /var/www/html/laravel/public/audio/announcements ]; then
#  ln -s  /var/lib/asterisk/sounds/user/announcements   /var/www/html/laravel/public/audio
#fi
#if [ ! -e /var/www/html/laravel/public/audio/tones ]; then
#  ln -s  /var/lib/asterisk/sounds/user/tones   /var/www/html/laravel/public/audio
#fi
#if [ ! -e /var/www/html/laravel/public/audio/sounds ]; then
#  ln -s  /var/lib/asterisk/sounds   /var/www/html/laravel/public/audio
#fi
#if [ ! -e /var/www/html/laravel/public/audio/audio-dist ]; then
#  ln -s  /var/opt/nyquist/audio-dist   /var/www/html/laravel/public/audio
#fi
echo "done" | tee -a $INSTALL_LOG
#
# SSL Config
#
#echo "Create SSL directory" | tee -a $INSTALL_LOG
#mkdir -p /etc/apache2/ssl
#echo "Apache SSL Certificate Configuration for Web Interface..." | tee -a $INSTALL_LOG
#cat << EOF
#Please choose one of the following SSL configuration options:
#
#Create - Create a self-signed SSL certificate/key for this server.
#         A self-signed SSL certificate/key will be created for this server.
#         NOTE: You can use this option to create a temporary key for testing,
#         then later install a certificate issued by certificate authority.
#
#Enter  - Enter the Certificate and Private Key data for this server.
#         Notes:
#         Certificate data starts with "-----BEGIN CERTIFICATE-----".
#         Private Key data starts with "-----BEGIN PRIVATE KEY-----".
#
#Import - Import Certificate and Private Key files by specifing filenames.
#         Notes:
#         Certificate filenames usually end with .crt (e.g. server.crt)
#         Certificate files contain "-----BEGIN CERTIFICATE-----" on first line.
#         Private Key filenames usually end with .key (e.g. server.key)
#         Private Key files contain "-----BEGIN PRIVATE KEY-----" on first line.
#
#Skip   - Skip SSL configuration for this server (already done or will do later)
#
#EOF
#
# Prompt user for SSL option
#
#while true; do
#   read -p "Select option: (C)reate, (E)nter, (I)mport or (S)kip? " answer
#   echo "SSL prompt, answer= $answer" >> $INSTALL_LOG
#   case $answer in
#      [Cc]* ) openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout /etc/apache2/ssl/apache.key  -out /etc/apache2/ssl/apache.crt ;
#              cat /etc/apache2/ssl/apache.key  /etc/apache2/ssl/apache.crt > /etc/apache2/ssl/apache.pem
#              break;;
#      [Ee]* ) echo "Enter Certificate data, use ^D on newline to finish:";
#              cat > /etc/apache2/ssl/apache.crt;
#              echo "Enter Private Key data, use ^D on newline to finish:";
#              cat > /etc/apache2/ssl/apache.key;
#              cat /etc/apache2/ssl/apache.key  /etc/apache2/ssl/apache.crt > /etc/apache2/ssl/apache.pem
#              break;;
#      [Ii]* ) read -p "Enter Certificate (.crt) filename: " answer;
#              cp $answer /etc/apache2/ssl/apache.crt
#              read -p "Enter Private Key (.key) filename: " answer;
#              cp $answer /etc/apache2/ssl/apache.key;
#              cat /etc/apache2/ssl/apache.key  /etc/apache2/ssl/apache.crt > /etc/apache2/ssl/apache.pem
#              break;;
#      [Ss]* ) echo "Skipped" ; break;;
#          * ) echo "Please enter a valid choice: (C)reate, (E)nter, (I)mport or (S)kip";;
#   esac
#done
#
# Webhost Config
#
#echo "Copy Apache Conf" | tee -a $INSTALL_LOG
#cp -rf $NYQUIST_CORE/root/etc/apache2/apache2.conf  /etc/apache2
#echo "Copy Web Host file" | tee -a $INSTALL_LOG
#cp -rf $NYQUIST_CORE/root/etc/apache2/sites-available/000-default.conf  /etc/apache2/sites-available
#echo "Load Web Host Conf" | tee -a $INSTALL_LOG
#cd /etc/apache2/sites-available/
#a2dissite 000-default.conf
#a2ensite 000-default.conf
#echo "Load ReWrite Module and SSL Module" | tee -a $INSTALL_LOG
#cd /etc/apache2/mods-available/
#a2enmod rewrite.load
#a2enmod ssl.load
#service apache2 restart
echo "Web Host Config Done" | tee -a $INSTALL_LOG
#
# Event Scheduling
#


#mkdir -p /var/www/html/laravel/public/exports
#chown root:www-data /var/www/html/laravel/public/exports
#chmod 777  /var/www/html/laravel/public/exports

#mkdir -p /var/www/html/laravel/public/updates
#chown root:www-data /var/www/html/laravel/public/updates
#chmod 777  /var/www/html/laravel/public/updates

# add index.php files to block browser access to public directories
#cp /var/www/html/laravel/public/audio/index.php /var/www/html/laravel/public/exports/index.php
#cp /var/www/html/laravel/public/audio/index.php /var/lib/asterisk/sounds/index.php
#cp /var/www/html/laravel/public/audio/index.php /var/lib/asterisk/sounds/en/index.php
#cp /var/www/html/laravel/public/audio/index.php /var/lib/asterisk/sounds/user/index.php
#cp /var/www/html/laravel/public/audio/index.php /var/lib/asterisk/sounds/user/announcements/index.php
#cp /var/www/html/laravel/public/audio/index.php /var/lib/asterisk/sounds/user/announcements/gsm/index.php
#cp /var/www/html/laravel/public/audio/index.php /var/lib/asterisk/sounds/user/tones/index.php
#cp /var/www/html/laravel/public/audio/index.php /var/spool/asterisk/monitor/index.php
#cp /var/www/html/laravel/public/audio/index.php /var/opt/nyquist/audio-dist/index.php


#
# Setup Asterisk to auto-start on boot or after crash
#	
#install $NYQUIST_SCRIPTS/safe_asterisk /usr/sbin
#install $NYQUIST_SCRIPTS/safe_asterisk /etc/init.d
#update-rc.d safe_asterisk defaults
#
# Check for cron.allow and cron.deny
#
#if [ -e /etc/cron.allow ]; then
#   if ! grep -q "$WEB_USERNAME" /etc/cron.allow; then
#      echo ""
#      echo "WARNING: user www-data is not in /etc/cron.allow"
#      echo "Automatic backups will not be performed."
#      echo ""
#      while true; do
#      read -p "Would you like to add www-data to /etc/cron.allow now? Y/N:" answer
#         case $answer in
#            [Yy]* ) echo "www-data" >> /etc/cron.allow ; break;;
#            [Nn]* ) echo "Skipped" ; break;;
#                * ) echo "Please answer yes or no.";;
#         esac
#      done
#   fi
#fi
#if [ -e /etc/cron.deny ]; then
#   if grep -q "$WEB_USERNAME" /etc/cron.deny; then
#      echo ""
#      echo "WARNING: user www-data was found in /etc/cron.deny"
#      echo "Automatic backups will not be performed."
#      echo "If automatic backups are desired, please remove www-data from /etc/cron.deny"
#   fi
#fi

# Fix bug where tftpd does not start on reboot
#sed -i 's/0.0.0.0:69/:69/' /etc/default/tftpd-hpa

# Make sure tftpd is started
#service tftpd-hpa start

# Install log rotation settings
#install --mode=644 $NYQUIST_SCRIPTS/asterisk /etc/logrotate.d
#

#Disable services
SERVICE=avahi-daemon.service
if [ "systemctl is-enabled $SERVICE" != "disabled" ] 
then
	echo "$SERVICE is enabled, disabling now" | tee -a $INSTALL_LOG
	systemctl disable $SERVICE
else
	echo "$SERVICE is already disabled" | tee -a $INSTALL_LOG
fi

SERVICE=bonescript-autorun.service
if [ "systemctl is-enabled $SERVICE" != "disabled" ] 
then
	echo "$SERVICE is enabled, disabling now" | tee -a $INSTALL_LOG
	systemctl disable $SERVICE
else
	echo "$SERVICE is already disabled" | tee -a $INSTALL_LOG
fi

SERVICE=bonescript.socket
if [ "systemctl is-enabled $SERVICE" != "disabled" ] 
then
	echo "$SERVICE is enabled, disabling now" | tee -a $INSTALL_LOG
	systemctl disable $SERVICE
else
	echo "$SERVICE is already disabled" | tee -a $INSTALL_LOG
fi

SERVICE=bonescript.service
if [ "systemctl is-enabled $SERVICE" != "disabled" ] 
then
	echo "$SERVICE is enabled, disabling now"| tee -a $INSTALL_LOG
	systemctl disable $SERVICE
else
	echo "$SERVICE is already disabled" | tee -a $INSTALL_LOG
fi

SERVICE=cloud9.service
if [ "systemctl is-enabled $SERVICE" != "disabled" ] 
then
	echo "$SERVICE is enabled, disabling now" | tee -a $INSTALL_LOG
	systemctl disable $SERVICE
else
	echo "$SERVICE is already disabled" | tee -a $INSTALL_LOG
fi

echo ""
echo "Installation Done" | tee -a $INSTALL_LOG
echo ""
echo -n "Creating backup..."
/usr/local/bin/update-backup-files-list
/usr/local/bin/create-backup Installation

END

echo "done"
date >> $INSTALL_LOG


