#!/bin/bash

USER_NAME=$SUDO_USER
ACTUAL_HOME="$HOME"
ROOOT_HOME="/root"

#test of empty string
if [ -z "$USER_NAME" ]
then
    echo "-------------------------------------------------------------------------"
    echo "|         Executing script as [root]"
    echo "-------------------------------------------------------------------------"
else
    echo "-------------------------------------------------------------------------"
    echo "|         Executing script as [$USER_NAME]"
    echo "-------------------------------------------------------------------------"
fi

if [ $ACTUAL_HOME == $ROOOT_HOME ]
then
   echo "Please execute this script as normal user, NOT ROOT, try again..."
   exit 1
fi

#----------------------------------
# Get parameters from command line
#----------------------------------
# Installation type:
#   Values: 1 => Full installation
#           2 => Update only
INSTALLATION_TYPE=$1
# Installation type:
#   Values: string with repository name (branche or tag)
REPOSITORY_TO_CLONE=$2
# Installation of node modules type:
#   Values: 1 => FORCE install of node modules
#           2 => Dont force, download the node_modules.zip
NODE_MODULES=$3
RTSP=$4

#test of empty string
if [[ ( -z "$INSTALLATION_TYPE" ) || ( -z "$REPOSITORY_TO_CLONE" ) || ( -z "$NODE_MODULES" ) ]]
then
    echo "-------------------------------------------------------------------------"
    echo "|         Please enter the parameters of installation like below:"
    echo "-------------------------------------------------------------------------"
    echo "1° Parameter: installation type"
    echo "  1 - Full installation"
    echo "  2 - Update only"
    echo "2° Parameter: repository to download from"
    echo "  Ex1.: VIS-147"
    echo "  Ex1.: MV.v3.13.1b"
    echo "3° Parameter: force install of node modules"
    echo "  1 - FORCE installation of node modules"
    echo "  2 - Dont force"
    echo "-------------------------------------------------------------------------"
    echo "Example of a full call for this install script:"
    echo "$0 1 MV.v3.13.1b 2 --> Full installation, from the repository 'MV.v3.13.1b' with 'dont force' installation of node modules"
    exit 1
fi



if [ $INSTALLATION_TYPE == "2" ]
then
    if [ ! -d "$HOME/Multivis" ];
    then
        echo "-------------------------------------------------------------------------"
        echo "|        You selected the UPDATE ONLY type,"
        echo "|        but the directory Multivis doesn't existis."
        echo "|        switching installation type to FULL INSTALLATION..."
        echo "-------------------------------------------------------------------------"
        $INSTALLATION_TYPE="1"
    fi
fi





if [ $INSTALLATION_TYPE == "1" ]
then
    echo "#########################################################################"
    echo "#####################                         ###########################"
    echo "#####################   INSTALLATION STARTED   ###########################"
    echo "#####################                         ###########################"
    echo "#########################################################################"
    pm2 kill
    #Test if a video file for installing exists
    TEST_FILE="$HOME/Videos/installing_system.mp4"
    if [ -f "$TEST_FILE" ]
    then
        sudo rm $HOME/Videos/installing_system.mp4
    fi
    wget -c https://www.multiviscloud.com/linux/data/installing_system.mp4 -O $HOME/Videos/installing_system.mp4
    ( sleep 1 ; mplayer $HOME/Videos/installing_system.mp4 -fs -ontop -loop 0 -quiet ) &
fi
if [ $INSTALLATION_TYPE == "2" ]
then
    echo "#########################################################################"
    echo "#####################                         ###########################"
    echo "#####################   UPDATE STARTED        ###########################"
    echo "#####################                         ###########################"
    echo "#########################################################################"

    #Test if a video file for installing exists
    TEST_FILE="$HOME/Videos/updating_system.mp4"
    if [ -f "$TEST_FILE" ]
    then
        sudo rm $HOME/Videos/updating_system.mp4
    fi
    wget -c https://www.multiviscloud.com/linux/data/updating_system.mp4 -O $HOME/Videos/updating_system.mp4
    ( sleep 1 ; mplayer $HOME/Videos/updating_system.mp4 -fs -ontop -loop 0 -quiet ) &
fi


echo "#########################################################################"
echo "#####################                         ###########################"
echo "#####################   CREATING FOLDER FOR   ###########################"
if [ $INSTALLATION_TYPE == "1" ]
then
    echo "#####################      INSTALLATION        ###########################"
fi
if [ $INSTALLATION_TYPE == "2" ]
then
    echo "#####################        UPDATE           ###########################"
fi
echo "#########################################################################"
sudo mkdir $HOME/Multivis_temp
sudo mkdir $HOME/Multivis_temp/auto_start_script
sudo mkdir $HOME/Multivis_temp/multivis_update_files
sudo mkdir $HOME/Multivis_temp/node_modules_downloaded
sudo mkdir $HOME/Multivis_temp/node_modules_public
sudo mkdir $HOME/Multivis_temp/node_modules_sabijs
sudo mkdir $HOME/Multivis_temp/repository


echo "#########################################################################"
echo "#####################                         ###########################"
echo "#####################   STOPING MVIS          ###########################"
echo "#####################                         ###########################"
echo "#########################################################################"
export DISPLAY=:0
wmctrl -c 'MULTIVISDISPLAY'
sleep 1
wmctrl -c 'SABISERVER'
sleep 1
wmctrl -c 'MULTIVISSERVER'
sleep 1
wmctrl -c 'MultiVis: Display'


echo "#########################################################################"
echo "#####################                         ###########################"
echo "##################### BACKUP OF CONFIG FILES  ###########################"
echo "#####################                         ###########################"
echo "#########################################################################"
sudo cp -rf $HOME/Multivis/data $HOME/Multivis_temp
sudo cp -f $HOME/Documents/SAGE2_Media/config/default-cfg.json $HOME/Multivis_temp
sudo cp -f $HOME/Documents/SAGE2_Media/config/defaultWin-cfg.json $HOME/Multivis_temp



if [ $INSTALLATION_TYPE == "1" ]
then
    echo ""
    echo "#########################################################################"
    echo "#####################                         ###########################"
    echo "#####################DELETING MVIS ROOT FOLDER###########################"
    echo "#####################                         ###########################"
    echo "#########################################################################"
    sudo rm -f -r -d $HOME/Multivis
fi
if [ $INSTALLATION_TYPE == "2" ]
then
    echo ""
    echo "#########################################################################"
    echo "#####################                         ###########################"
    echo "#####################        BACKUP AND       ###########################"
    echo "#####################CLEAN OF MVIS ROOT FOLDER###########################"
    echo "#####################                         ###########################"
    echo "#########################################################################"

    if [ $NODE_MODULES == "2" ]
    then
        sudo cp -rf $HOME/Multivis/node_modules $HOME/Multivis_temp/node_modules_public
        sudo cp -rf $HOME/Multivis/sabi.js/node_modules $HOME/Multivis_temp/node_modules_sabijs
    fi

    sudo rm -f -r -d $HOME/Multivis
fi

echo "#########################################################################"
echo "#####################                         ###########################"
echo "#####################   CLEANING MVIS DOCS    ###########################"
echo "#####################                         ###########################"
echo "#########################################################################"
sudo rm -f -r -d $HOME/Documents/SAGE2_Media



#
# Create the folder to install MultiVis data
#
sudo groupadd multivis
sudo usermod -a -G multivis $USER_NAME
sudo mkdir /var/multivis
sudo chgrp multivis /var/multivis
sudo chmod 775 /var/multivis/
sudo chown -R $USER_NAME /var/multivis/
sudo touch /var/multivis/username
sudo echo "$USER_NAME" > /var/multivis/username


#Apps not installed in previously installations
sudo apt install -y jq

if [ $INSTALLATION_TYPE == "1" ]
then
    echo ""
    echo "-------------------------------------------------------------------------"
    echo "|        Updating the system"
    echo "-------------------------------------------------------------------------"
    sudo apt-get -y update
    sudo apt-get -y upgrade

    echo "##########################################################################"

    echo "-------------------------------------------------------------------------"
    echo "|        Instaling DEV apps"
    echo "-------------------------------------------------------------------------"
    sudo apt-get install -y build-essential
    sudo apt-get install -y cmake
    sudo apt-get install -y libvlc-dev
    sudo apt-get install -y openssh-server
    sudo apt-get install -y git curl libcap2-bin
    sudo apt-get install -y net-tools
    sudo apt-get install -y libgconf-2-4
    sudo apt-get install -y ntp
    sudo apt-get install -y ntpdate
    sudo apt-get install -y nethogs
    sudo apt-get install -y intel-gpu-tools
    sudo apt-get install -y cec-utils
	sudo apt-get install -y livemedia-utils
	sudo apt-get install -y docker.io
	sudo apt install -y vim
	sudo apt install -y x11vnc

    sudo service ntp stop

    sudo echo '#Configure the ntp.conf, time of the system' > /etc/ntp.conf
    sudo echo '# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help' >> /etc/ntp.conf
    sudo echo '' >> /etc/ntp.conf
    sudo echo 'driftfile /var/lib/ntp/ntp.drift' >> /etc/ntp.conf
    sudo echo '' >> /etc/ntp.conf
    sudo echo '# Leap seconds definition provided by tzdata' >> /etc/ntp.conf
    sudo echo 'leapfile /usr/share/zoneinfo/leap-seconds.list' >> /etc/ntp.conf
    sudo echo '' >> /etc/ntp.conf
    sudo echo '# Enable this if you want statistics to be logged.' >> /etc/ntp.conf
    sudo echo '#statsdir /var/log/ntpstats/' >> /etc/ntp.conf
    sudo echo '' >> /etc/ntp.conf
    sudo echo 'statistics loopstats peerstats clockstats' >> /etc/ntp.conf
    sudo echo 'filegen loopstats file loopstats type day enable' >> /etc/ntp.conf
    sudo echo 'filegen peerstats file peerstats type day enable' >> /etc/ntp.conf
    sudo echo 'filegen clockstats file clockstats type day enable' >> /etc/ntp.conf
    sudo echo '' >> /etc/ntp.conf
    sudo echo '# Specify one or more NTP servers.' >> /etc/ntp.conf
    sudo echo '' >> /etc/ntp.conf
    sudo echo '# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board' >> /etc/ntp.conf
    sudo echo '# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for' >> /etc/ntp.conf
    sudo echo '# more information.' >> /etc/ntp.conf
    sudo echo 'pool s500mldc01.magazineluiza.intranet iburst' >> /etc/ntp.conf
    sudo echo '' >> /etc/ntp.conf
    sudo echo "# Use Ubuntu's ntp server as a fallback." >> /etc/ntp.conf
    sudo echo '#pool ntp.ubuntu.com' >> /etc/ntp.conf
    sudo echo '' >> /etc/ntp.conf
    sudo echo '# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for' >> /etc/ntp.conf
    sudo echo '# details. The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>' >> /etc/ntp.conf
    sudo echo '# might also be helpful.' >> /etc/ntp.conf
    sudo echo '#' >> /etc/ntp.conf
    sudo echo '# Note that "restrict" applies to both servers and clients, so a configuration' >> /etc/ntp.conf
    sudo echo '# that might be intended to block requests from certain clients could also end' >> /etc/ntp.conf
    sudo echo '# up blocking replies from your own upstream servers.' >> /etc/ntp.conf
    sudo echo '' >> /etc/ntp.conf
    sudo echo "# By default, exchange time with everybody, but don't allow configuration." >> /etc/ntp.conf
    sudo echo 'restrict -4 default kod notrap nomodify nopeer noquery limited' >> /etc/ntp.conf
    sudo echo 'restrict -6 default kod notrap nomodify nopeer noquery limited' >> /etc/ntp.conf
    sudo echo '' >> /etc/ntp.conf
    sudo echo '# Local users may interrogate the ntp server more closely.' >> /etc/ntp.conf
    sudo echo 'restrict 127.0.0.1' >> /etc/ntp.conf
    sudo echo 'restrict ::1' >> /etc/ntp.conf
    sudo echo '' >> /etc/ntp.conf
    sudo echo '# Needed for adding pool entries' >> /etc/ntp.conf
    sudo echo 'restrict source notrap nomodify noquery' >> /etc/ntp.conf
    sudo echo '' >> /etc/ntp.conf
    sudo echo '# Clients from this (example!) subnet have unlimited access, but only if' >> /etc/ntp.conf
    sudo echo '# cryptographically authenticated.' >> /etc/ntp.conf
    sudo echo '#restrict 192.168.123.0 mask 255.255.255.0 notrust' >> /etc/ntp.conf
    sudo echo '' >> /etc/ntp.conf
    sudo echo '# If you want to provide time to your local subnet, change the next line.' >> /etc/ntp.conf
    sudo echo '# (Again, the address is an example only.)' >> /etc/ntp.conf
    sudo echo '#broadcast 192.168.123.255' >> /etc/ntp.conf
    sudo echo '' >> /etc/ntp.conf
    sudo echo '# If you want to listen to time broadcasts on your local subnet, de-comment the' >> /etc/ntp.conf
    sudo echo '# next lines. Please do this only if you trust everybody on the network!' >> /etc/ntp.conf
    sudo echo '#disable auth' >> /etc/ntp.conf
    sudo echo '#broadcastclient' >> /etc/ntp.conf
    sudo echo '' >> /etc/ntp.conf
    sudo echo '#Changes recquired to use pps synchonisation as explained in documentation:' >> /etc/ntp.conf
    sudo echo '#http://www.ntp.org/ntpfaq/NTP-s-config-adv.htm#AEN3918' >> /etc/ntp.conf
    sudo echo '' >> /etc/ntp.conf
    sudo echo '#server 127.127.8.1 mode 135 prefer # Meinberg GPS167 with PPS' >> /etc/ntp.conf
    sudo echo '#fudge 127.127.8.1 time1 0.0042 # relative to PPS for my hardware' >> /etc/ntp.conf
    sudo echo '' >> /etc/ntp.conf
    sudo echo '#server 127.127.22.1 # ATOM(PPS)' >> /etc/ntp.conf
    sudo echo '#fudge 127.127.22.1 flag3 1 # enable PPS API' >> /etc/ntp.conf

    sudo service ntp start
    sudo service ntp restart

    echo "##########################################################################"

    echo "-------------------------------------------------------------------------"
    echo "|        Instaling Google chromium"
    echo "-------------------------------------------------------------------------"

    #Instaltion of google chrome
    sudo mkdir -p /etc/apt/sources.list.d
    wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
    sudo sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list'
    sudo apt-get update
    sudo apt-get install -y google-chrome-stable

    #Installation of chromium
    #sudo apt-get install -y chromium-browser

    echo "##########################################################################"

    echo "-------------------------------------------------------------------------"
    echo "|        Instaling Nodejs"
    echo "-------------------------------------------------------------------------"
    curl -sL https://deb.nodesource.com/setup_8.x | sudo bash -
    sudo apt-get install -y nodejs
    sudo apt-get install -y npm

    echo "##########################################################################"

    echo "-------------------------------------------------------------------------"
    echo "|        Instaling Tools"
    echo "-------------------------------------------------------------------------"
    echo "|        -> ffmpeg"
    echo "-------------------------------------------------------------------------"
    echo "----------> ffmpeg: downloading repository-------------------------------"
    sudo add-apt-repository ppa:jonathonf/ffmpeg-4 -y
    echo "----------> ffmpeg: updating repository----------------------------------"
    sudo apt-get update
    echo "----------> ffmpeg: instaling ffmpeg-------------------------------------"
    sudo apt-get install -y ffmpeg
    echo "-------------------------------------------------------------------------"
    echo "|        -> exiftool"
    echo "-------------------------------------------------------------------------"
    sudo apt-get install -y libimage-exiftool-perl
    echo "-------------------------------------------------------------------------"
    echo "|        -> GS"
    echo "-------------------------------------------------------------------------"
    sudo apt-get install -y ghostscript
    echo "-------------------------------------------------------------------------"
    echo "|        -> Security tool"
    echo "-------------------------------------------------------------------------"
    sudo apt-get install -y libnss3-tools
    echo "-------------------------------------------------------------------------"
    echo "|        -> imagemagick"
    echo "-------------------------------------------------------------------------"
    sudo apt-get install -y imagemagick
    echo "-------------------------------------------------------------------------"
    echo "|        -> wmctrl"
    echo "-------------------------------------------------------------------------"
    sudo apt-get install -y wmctrl
    echo "-------------------------------------------------------------------------"
    echo "|        -> xdotool"
    echo "-------------------------------------------------------------------------"
    sudo apt-get install -y xdotool
    echo "-------------------------------------------------------------------------"
    echo "|        -> lxterminal"
    echo "-------------------------------------------------------------------------"
    sudo apt-get install -y lxterminal
    echo "-------------------------------------------------------------------------"
    echo "|        -> unzip"
    echo "-------------------------------------------------------------------------"
    sudo apt-get install -y unzip

    echo "##########################################################################"
fi


if [ $INSTALLATION_TYPE == "1" ]
then
    echo "-------------------------------------------------------------------------"
    echo "|     Instaling Multivis from $REPOSITORY_TO_CLONE repository"
    echo "-------------------------------------------------------------------------"
    sudo mkdir $HOME/Multivis
    cd $HOME
    sudo git clone --branch $REPOSITORY_TO_CLONE http://gitlab.comtex.com.br/products/control.git Multivis
fi
if [ $INSTALLATION_TYPE == "2" ]
then
    echo "-------------------------------------------------------------------------"
    echo "|     Updating Multivis from $REPOSITORY_TO_CLONE repository"
    echo "-------------------------------------------------------------------------"
    sudo git clone --branch $REPOSITORY_TO_CLONE http://gitlab.comtex.com.br/products/control.git $HOME/Multivis_temp/repository
    sudo cp -rf $HOME/Multivis_temp/repository $HOME/Multivis
fi

echo "##########################################################################"

echo "-------------------------------------------------------------------------"
echo "|     Creating Certifications"
echo "-------------------------------------------------------------------------"
cd $HOME/Multivis/keys
sudo ./GO-linux

sudo npm config set jobs 1
sudo npm config set registry http://registry.npmjs.org/

echo "##########################################################################"

echo "-------------------------------------------------------------------------"
echo "|     Install lib forever for production"
echo "-------------------------------------------------------------------------"
sudo npm install forever -g
#sudo npm uninstall forever -g

echo "##########################################################################"

echo "-------------------------------------------------------------------------"
echo "|     Install lib pm2 for production"
echo "-------------------------------------------------------------------------"

sudo npm install pm2@3.2.9 -g
sudo pm2 install pm2-logrotate@2.2.0
sudo pm2 set pm2-logrotate:compress true
sudo pm2 set pm2-logrotate:max_size 100M
sudo pm2 set pm2-logrotate:retain 7
#sudo pm2 set pm2-logrotate:compress true
#sudo pm2 set pm2-logrotate:rotateInterval '0 0 * * *'
#sudo pm2 set pm2-logrotate:workerInterval 30

sudo chown -R $USER_NAME:$USER_NAME $HOME/.pm2

echo "##########################################################################"

if [ $NODE_MODULES == "1" ]
then
    echo "-------------------------------------------------------------------------"
    echo "|   FORCE install of node modules"
    echo "|"
    echo "|   Instaling nodejs dependences for Multivis"
    echo "-------------------------------------------------------------------------"
    cd $HOME/Multivis
    sudo npm install

    echo "##########################################################################"

    echo "-------------------------------------------------------------------------"
    echo "|   Instaling nodejs dependences for Sabi.js server"
    echo "-------------------------------------------------------------------------"
    cd $HOME/Multivis/sabi.js
    sudo npm install
fi

if [ $NODE_MODULES == "2" ]
then

    if [ $INSTALLATION_TYPE == "1" ]
    then
        echo "-------------------------------------------------------------------------"
        echo "|   Downloading nodejs dependences for Multivis"
        echo "-------------------------------------------------------------------------"
        sudo wget -c https://www.multiviscloud.com/linux/data/node_modules.zip -O $HOME/Multivis_temp/node_modules_downloaded/node_modules.zip
        sudo unzip -o $HOME/Multivis_temp/node_modules_downloaded/node_modules.zip -d $HOME/Multivis


		echo "-------------------------------------------------------------------------"
		echo "|Downloading NODE-DEMUX"
		echo "-------------------------------------------------------------------------"
		sudo wget -c https://www.multiviscloud.com/linux/data/node_demux.zip -O $HOME/Multivis_temp/node_modules_downloaded/node_demux.zip
		sudo unzip -o $HOME/Multivis_temp/node_modules_downloaded/node_demux.zip -d $HOME/Multivis/node_modules

    fi

    if [ $INSTALLATION_TYPE == "2" ]
    then
        echo "#########################################################################"
        echo "#####################                         ###########################"
        echo "##################### RESTORE OF NODE MODULES ###########################"
        echo "#####################                         ###########################"
        echo "#########################################################################"
        sudo cp -rf $HOME/Multivis_temp/node_modules_public/node_modules $HOME/Multivis
        sudo cp -rf $HOME/Multivis_temp/node_modules_sabijs/node_modules $HOME/Multivis/sabi.js
    fi
fi





echo "-------------------------------------------------------------------------"
echo "|Adjusting files for Linux structure"
echo "-------------------------------------------------------------------------"
echo "|   File:"
echo "|       sabi.json"
echo "|   Location:"
echo "|       Multivis/sabi.js/config/"
echo "-------------------------------------------------------------------------"
echo '{"global":{"security":"true","server_port":"10000","tcp_port":"11000"},"main":{"header":"<h1>MultiVis Control Interface<\/h1>","footer":"<h4>MultiVis Control Interface<\/h4>","image":"images/MultiVis.svg","image_style":"width=\"90%\" style=\"max-width:420px;align:center;margin: 0 auto;\"","pages":["MultiVis","Admin"]},"actions":{"sage2-on":{"script":"scripts\\sage2_on.bat"},"sage2-off":{"script":"scripts\\sage2_off.bat"},"sage2-on-electron":{"script":"scripts/electron_on.sh"},"sage2-off-electron":{"script":"scripts/electron_off.sh"},"restart-multivis":{"script":"scripts/restart_multivis.sh"},"restart-server":{"script":"scripts/restart_server.sh"},"edit-config":{"editor":"'$HOME'/Documents/SAGE2_Media/config/default-cfg.json"},"edit-form":{"openurl":"form.html?action=edit-config"},"edit-assist":{"openurl":"assistedConfig.html"},"updateSage":{"sendData":"forMA","dataID":"inputMA","sendPurpose":"performGitUpdate"},"notepad-on":{"command":"start notepad"},"notepad-off":{"command":"Taskkill /IM notepad.exe /F"},"firefox-on":{"command":"start firefox"},"firefox-off":{"command":"Taskkill /IM firefox.exe /F"}},"MultiVis":{"title":"MultiVis","navbar":"true","image":"images/MultiVis.svg","groups":["lauchMvis","serverCtrl"],"lauchMvis":{"title":"Launch MultiVis","description":"Start and stop MultiVis. You can also set a meeting access code to protect access to your meeting.","image":"images/display.png","actions":[{"title":"Start","action":"sage2-on-electron"},{"title":"Restart","action":"restart-multivis"},{"title":"Stop","action":"sage2-off-electron"}]},"serverCtrl":{"title":"Server control","description":"Control the server.","image":"images/server.png","actions":[{"title":"Restart","action":"restart-server"}]}},"Admin":{"title":"Admin","navbar":"true","groups":["group2"],"group2":{"title":"Configure MultiVis","image":"images/settings.png","description":"Allows to edit your wall configuration. Simple edits can be done using a form in <em>basic<\/em> mode. You can also edit the file in <em>expert<\/em> mode. The launcher password can be changed in the form of multivis:sabi:xxxx.","actions":[{"title":"Wall configuration - <em>assisted<\/em>","action":"edit-assist"},{"title":"Wall configuration - <em>basic<\/em>","action":"edit-form"},{"title":"Wall configuration -  <em>expert<\/em>","action":"edit-config"}]}}}' > $HOME/Multivis/sabi.js/config/sabi.json

echo "-------------------------------------------------------------------------"
echo "|Downloading update files"
echo "-------------------------------------------------------------------------"
sudo wget -c https://www.multiviscloud.com/linux/data/multivis.comtex.zip -O $HOME/Multivis_temp/multivis_update_files/multivis.comtex.zip
sudo unzip -o $HOME/Multivis_temp/multivis_update_files/multivis.comtex.zip -d $HOME/Multivis

sudo chmod +x $HOME/Multivis/add_apps.sh
sudo chmod +x $HOME/Multivis/run_server.sh
sudo chmod +x $HOME/Multivis/run_sabi_server.sh
sudo chmod +x $HOME/Multivis/hidewindow.sh
sudo chmod +x $HOME/Multivis/sabi.js/scripts/electron_on.sh
sudo chmod +x $HOME/Multivis/sabi.js/scripts/electron_off.sh
sudo chmod +x $HOME/Multivis/sabi.js/scripts/restart_server.sh

sudo mkdir $HOME/Documents/SAGE2_Media
sudo mkdir $HOME/Documents/SAGE2_Media/config
sudo cp $HOME/Multivis/config/default-cfg.json $HOME/Documents/SAGE2_Media/config
sudo chmod 777 $HOME/Multivis/config/default-cfg.json
sudo cp $HOME/Multivis/config/defaultWin-cfg.json $HOME/Documents/SAGE2_Media/config
sudo chmod 777 $HOME/Multivis/config/defaultWin-cfg.json

if [ $INSTALLATION_TYPE == "1" ]
then
    echo "-------------------------------------------------------------------------"
    echo "|Registering the autostart script"
    echo "-------------------------------------------------------------------------"
    sudo touch $HOME/Multivis_temp/auto_start_script/multivis.desktop
    sudo echo "[Desktop Entry]" > $HOME/Multivis_temp/auto_start_script/multivis.desktop
    sudo echo "Name=Multivis" >> $HOME/Multivis_temp/auto_start_script/multivis.desktop
    sudo echo "Exec=lxterminal --title=SABISERVER --command="$HOME"/Multivis/run_sabi_server.sh" >> $HOME/Multivis_temp/auto_start_script/multivis.desktop
    sudo echo "Icon="$HOME"/Multivis/public/images/S2-logo.png" >> $HOME/Multivis_temp/auto_start_script/multivis.desktop
    sudo echo "Type=Application" >> $HOME/Multivis_temp/auto_start_script/multivis.desktop
    sudo echo "Terminal=false" >> $HOME/Multivis_temp/auto_start_script/multivis.desktop
    sudo cp -f $HOME/Multivis_temp/auto_start_script/multivis.desktop /etc/xdg/autostart
    sudo cp -f $HOME/Multivis_temp/auto_start_script/multivis.desktop $HOME/Desktop

	#echo "-------------------------------------------------------------------------"
    #echo "|Registering the autostart run_rtsp_server script"
    #echo "-------------------------------------------------------------------------"
    #sudo touch $HOME/Multivis_temp/auto_start_script/multivis_rtsp.desktop
    #sudo echo "[Desktop Entry]" > $HOME/Multivis_temp/auto_start_script/multivis_rtsp.desktop
    #sudo echo "Name=MultivisRTSP" >> $HOME/Multivis_temp/auto_start_script/multivis_rtsp.desktop
    #sudo echo "Exec=lxterminal --title=MultivisRTSP --command="$HOME"/node-multivis-rtsp/run_server_rtsp.sh" >> $HOME/Multivis_temp/auto_start_script/multivis_rtsp.desktop
    #sudo echo "Icon="$HOME"/Multivis/public/images/S2-logo.png" >> $HOME/Multivis_temp/auto_start_script/multivis_rtsp.desktop
    #sudo echo "Type=Application" >> $HOME/Multivis_temp/auto_start_script/multivis_rtsp.desktop
    #sudo echo "Terminal=false" >> $HOME/Multivis_temp/auto_start_script/multivis_rtsp.desktop
    #sudo cp -f $HOME/Multivis_temp/auto_start_script/multivis_rtsp.desktop /etc/xdg/autostart

	sudo rm /etc/xdg/autostart/multivis_rtsp.desktop
fi

echo "#########################################################################"
echo "#####################                         ###########################"
echo "##################### RESTORE OF CONFIG FILES ###########################"
echo "#####################                         ###########################"
echo "#########################################################################"
sudo cp -rf $HOME/Multivis_temp/data $HOME/Multivis
sudo cp -f $HOME/Multivis_temp/default-cfg.json $HOME/Documents/SAGE2_Media/config
sudo cp -f $HOME/Multivis_temp/defaultWin-cfg.json $HOME/Documents/SAGE2_Media/config


#Clear the installation files
echo "#########################################################################"
echo "#####################                         ###########################"
echo "#####################  CLEAR OF INSTALLATION   ###########################"
echo "#####################         FILES           ###########################"
echo "#########################################################################"
sudo rm -f $HOME/install.v2.sh
sudo rm -f -r -d $HOME/Multivis_temp


cd $HOME/Multivis
sudo ./add_apps.sh



#Configure the folders for THIS user
sudo chown -R $USER_NAME:$USER_NAME $HOME/Multivis
sudo chmod -R 777 $HOME/Multivis
sudo chown -R $USER_NAME:$USER_NAME $HOME/Documents
sudo chmod -R 777 $HOME/Documents

#Configure the kiosk mode
#sudo mv -f $HOME/Multivis/panel $HOME/.config/lxpanel/Lubuntu/panels
#sudo chown $USER_NAME:$USER_NAME $HOME/.config/lxpanel/Lubuntu/panels/panel
#sudo chmod 664 $HOME/.config/lxpanel/Lubuntu/panels/panel
sudo lxpanelctl exit

#echo "#########################################################################"
#echo "#####################                         ###########################"
#echo "#####################  INSTALL RTSP LIVE555 	###########################"
#echo "#####################                         ###########################"
#echo "#########################################################################"
#sudo wget http://multiviscloud.com/linux/live555/live555-latest.tar.gz - sudo wget http://multiviscloud.com/linux/live555/live.2018.11.26.tar.gz
#sudo cp live.2018.11.26.tar.gz $HOME/Downloads/
#sudo cp live555-latest.tar.gz $HOME/Downloads/
#sudo chmod 777 -R $HOME/Downloads/
#cd $HOME/Downloads/
#sudo tar -xzvf live.2018.11.26.tar.gz
#sudo tar -xzvf live555-latest.tar.gz
#cd $HOME/Downloads/live
#sudo ./genMakefiles linux
#sudo make

#sudo mkdir $HOME/node-multivis-rtsp
#cd $HOME/node-multivis-rtsp
#sudo wget -c http://multiviscloud.com/linux/node-multivis-rtsp.zip -O $HOME/node-multivis-rtsp/node-multivis-rtsp.zip
#sudo unzip -o $HOME/node-multivis-rtsp/node-multivis-rtsp.zip -d $HOME/node-multivis-rtsp
#sudo chmod 777 -R $HOME/node-multivis-rtsp
#sudo chown comtex:comtex -R $HOME/node-multivis-rtsp

if [ $RTSP == "rtsp" ]
then

	echo "#########################################################################"
	echo "#####################                         ###########################"
	echo "##############   INSTALLATION KURENTO MEDIA SERVER     ##################"
	echo "#####################                         ###########################"
	echo "#########################################################################"

	sudo docker run -d --name kms -p 8888:8888 kurento/kurento-media-server:xenial-latest

	sudo rm /etc/systemd/system/kurento.service

	sudo echo '[Unit]' >> /etc/systemd/system/kurento.service
	sudo echo 'Description=Kurento Media Serve' >> /etc/systemd/system/kurento.service
	sudo echo 'Requires=docker.service' >> /etc/systemd/system/kurento.service
	sudo echo 'After=docker.service' >> /etc/systemd/system/kurento.service
	sudo echo '' >> /etc/systemd/system/kurento.service
	sudo echo '[Service]' >> /etc/systemd/system/kurento.service
	sudo echo '#Restart=always' >> /etc/systemd/system/kurento.service
	sudo echo 'ExecStart=/usr/bin/docker start -a kms' >> /etc/systemd/system/kurento.service
	sudo echo 'ExecStop=/usr/bin/docker stop -t 2 kms' >> /etc/systemd/system/kurento.service
	sudo echo '[Install]' >> /etc/systemd/system/kurento.service
	sudo echo 'WantedBy=default.target' >> /etc/systemd/system/kurento.service

	sudo chmod 655 /etc/systemd/system/kurento.service

	sudo systemctl enable kurento
	sudo service kurento start

fi


echo "#########################################################################"
echo "#####################                         ###########################"
echo "#####################     CONFIG FIREWALL     ###########################"
echo "#####################                         ###########################"
echo "#########################################################################"

###
# Set up UFW for Multi Vis appliances

###
sudo set -e

# Disable firewall
sudo ufw --force disable

# Set default rules
sudo ufw default allow outgoing
sudo ufw default deny incoming

# Set rules
sudo ufw allow 22       #SSH
sudo ufw allow http
sudo ufw allow https
sudo ufw allow 3000     #Certificates
sudo ufw allow 8080     #Meeting config
sudo ufw allow 9090     #MVControl UI
sudo ufw allow 9292     #MVControl UI
sudo ufw allow 10000    #Admin Interface

# Enable firewall
sudo ufw --force enable
#exit 0

echo "#########################################################################"
echo "#####################                         ###########################"
echo "#####################   INSTALLATION ENDED     ###########################"
echo "#####################                         ###########################"
echo "#########################################################################"

# The structure below is a background command executer
#( sleep 3 ; echo "6 seconds" ) &

#( sleep 1 ; lxterminal --title=SABISERVER --command=$HOME/Multivis/run_sabi_server.sh ) &
echo "#########################################################################"
echo "#####################                         ###########################"
echo "#####################  THE SYSTEM WILL REBOOT ###########################"
echo "#####################                         ###########################"
echo "#########################################################################"
reboot
#Exit the terminal where this script is running
#kill -9 $PPID
