#!/bin/bash - 
#===============================================================================
#
#          FILE: update_jitsi.sh
# 
#         USAGE: ./update_jitsi.sh 
# 
#   DESCRIPTION: 
# 
#       OPTIONS: ---
#  REQUIREMENTS: ---
#          BUGS: ---
#         NOTES: ---
#        AUTHOR: YOUR NAME (), 
#  ORGANIZATION: 
#       CREATED: 07/13/2021 16:50
#      REVISION:  ---
#===============================================================================

set -o nounset                              # Treat unset variables as an error

echo "Update System and Jitsi Meet"
apt update -qq
apt upgrade -qq -y

echo "+ Copying background image"
cp /home/rene/top-view-photo-of-people-near-wooden-table-3183150_small.jpg /usr/share/jitsi-meet/images/welcome-background-cust.png

echo "+ Modify interface_config.js"
sed -i -e "s/APP_NAME.*$/APP_NAME: 'Mewimeet',/" /usr/share/jitsi-meet/interface_config.js
sed -i -e "s/jitsi.org/mewimeet.de/g" /usr/share/jitsi-meet/interface_config.js
sed -i -e "s/_CONTENT: false/_CONTENT: true/g" /usr/share/jitsi-meet/interface_config.js

echo "+ Modify welcomePageAdditionalContent.html"
cat << EOF > /usr/share/jitsi-meet/static/welcomePageAdditionalContent.html
<template id = "welcome-page-additional-content-template">
<div style="text-align: center; font-size: 16px; margin: 20px 0; margin-top: 10px; z-index: 900; position: relative; color: #fff;">
Mewimeet |
	<a href="https://mewimeet.de/jitsi-meet-anleitung" style="color: #42b2e2; text-decoration: underline;">Anleitung</a> |
	<a href="https://test.webrtc.org" style="color: #42b2e2; text-decoration: underline;">Browser Test</a> |
	<a href="https://mewimeet.de/datenschutz" style="color: #42b2e2; text-decoration: underline;">Datenschutz</a>|
	<a href=" https://mewimeet.de/impressum" style="color: #42b2e2; text-decoration: underline;">Impressum</a>
<p>Aktuelle Nutzung: <span id="x-conferences">X</span> Konferenzen mit <span id="x-participants">X</span> Teilnehmer | <span id="x-cpu">X.X</span>% CPU und <span id="x-memory">X.X</span>% RAM</p>
<script>
var totelram = 1987; // Arbeitsspeicher in MB
fetch('https://mewimeet.de/stats.json').then(function (response) {return response.json();}).then(function (data){document.getElementById('x-conferences').innerHTML = data.conferences;document.getElementById('x-participants').innerHTML = data.participants;}).catch(function (err){console.warn('Something went wrong.', err);});
fetch('https://mewimeet.de/cpu.txt').then(response => response.text()).then(data => { document.getElementById('x-cpu').innerHTML = parseFloat(data).toFixed(1)});
fetch('https://mewimeet.de/memory.txt').then(response => response.text()).then(data => {var memory =100/totelram*data; document.getElementById('x-memory').innerHTML = memory.toFixed(1)});
</script>
</div>
</template>
EOF

echo "+ Modify plugin.head.html"
cat <<EOF >/usr/share/jitsi-meet/plugin.head.html
<style>
.welcome .header{
background-image: url(../images/welcome-background-cust.png);
position: absolute;
bottom: 0;
top: 0;
left: 0;
right: 0;
height: auto;
}

.welcome .header .header-container{
margin: 15% 32px 0 32px;
}

.profile-edit-field:last-child{
display: none;
}

//.welcome-cards-container{
//      display: none;
//}

.welcome-page-content{
margin-top:40%;
}

.welcome-tabs{
display: none;
}

.meetings-list{
display: none;
}

.welcome .page .page-content{
margin: 15% 32px 0 32px;
}
</style>
EOF

echo "+ Restarting Services"
for SERVICE in prosody jicofo jitsi-videobridge2 nginx; do
	systemctl restart "${SERVICE}"
done

