#!/bin/bash
# AWSに直接コピペすると文字化けするので注意

# hostname of main server
BM_DOMAIN=vr.binaural.me
hostname=${BM_DOMAIN}

# function wait for another apt process. 
function wait_for_dpkg_lock {
  # check for a lock on dpkg (another installation is running)
  lsof /var/lib/dpkg/lock > /dev/null
  dpkg_is_locked="$?"
  if [ "$dpkg_is_locked" == "0" ]; then
    echo "Waiting for another installation to finish"
    sleep 5
    wait_for_dpkg_lock
  fi
  rm /var/lib/dpkg/lock-frontend
  rm /var/lib/dpkg/lock
}
echo wait_for_dpkg_lock
wait_for_dpkg_lock

# function to get hostname of main server. 
function getHostname() {
    ipaddr=
    while [ "$ipaddr" = "" ]; do
        ipaddr=`host -ta ${hostname}`
        ipaddr=${ipaddr##* }
        if [ "$ipaddr" = "`curl 169.254.169.254/latest/meta-data/public-ipv4`" ]; then break; fi
        ipaddr=
        sleep 3s;
    done
}

echo apt update
sudo apt-get -y update

#install java8
wait_for_dpkg_lock
sudo apt-get -y install openjdk-8-jdk-headless

#install jitsi-meet
wait_for_dpkg_lock
curl https://download.jitsi.org/jitsi-key.gpg.key | sudo apt-key add -
sudo sh -c "echo 'deb https://download.jitsi.org stable/' > /etc/apt/sources.list.d/jitsi-stable.list"
sudo apt-get -y update
#sudo apt-get -y install emacs
sudo debconf-set-selections <<< "jitsi-meet jitsi-videobridge/jvb-hostname text ${hostname}"
sudo debconf-set-selections <<< "jitsi-meet-web-config jitsi-meet/cert-choice select Generate a new self-signed certificate (You will later get a chance to obtain a Let's encrypt certificate)"

wait_for_dpkg_lock
#   Stable 6433     07-Oct-2021
#   jitsi-videobridge2_2.1-570-gb802be83-1_all.deb
sudo apt-get -y install jitsi-videobridge2=2.1-570-gb802be83-1
sudo apt-get -y install jicofo=1.0-813-1
sudo apt-get -y install jitsi-meet-web=1.0.5415-1
sudo apt-get -y install jitsi-meet-web-config=1.0.5415-1
sudo apt-get -y install jitsi-meet-prosody=1.0.5415-1
sudo apt-get -y install jitsi-meet-turnserver=1.0.5415-1
sudo apt-get -y install jitsi-meet=2.0.6433-1

#get BinauralMeet
sudo mkdir /usr/share/BinauralMeet
sudo chmod 777 /usr/share/BinauralMeet
cd /usr/share/BinauralMeet
curl --output bm.tgz https://binaural.me/public_packages/bm.tgz
tar zxf bm.tgz
mv ./build/* .
rm config.js
curl --output config.js https://binaural.me/public_packages/uploader/vr/vr_config.js

#get roomInfo
cd /usr/share/BinauralMeet
mkdir roomInfo
cd roomInfo
curl --output roomInfo.tgz https://binaural.me/public_packages/roomInfo.tgz
tar zxf roomInfo.tgz
mv ./build/* .
rm config.js
curl --output config.js https://binaural.me/public_packages/uploader/vr/vr_config.js

# get main server's settings
cd ~
curl --output vr_main_etc.tgz https://binaural.me/public_packages/uploader/vr/vr_main_etc.tgz
tar -zxf vr_main_etc.tgz
sudo cp -a /etc ./etc_org
sudo cp -r ./etc/* /etc/
sudo rm /etc/prosody/conf.d/${hostname}.cfg.lua
sudo ln -s /etc/prosody/conf.avail/${hostname}.cfg.lua /etc/prosody/conf.d/
sudo rm /etc/nginx/sites-enabled/${hostname}.conf
sudo ln -s /etc/nginx/sites-available/before_cert.${hostname}.conf /etc/nginx/sites-enabled/${hostname}.conf

# get cert download or get
getHostname
if [ "`curl -LI https://binaural.me/public_packages/uploader/vr/${hostname}_fullchain.pem -o /dev/null -w '%{http_code}\n' -s`" = "200" ]; then
    sudo mkdir -p /etc/letsencrypt/live/${hostname}
    sudo curl --output /etc/letsencrypt/live/${hostname}/fullchain.pem https://binaural.me/public_packages/uploader/vr/${hostname}_fullchain.pem
    sudo curl --output /etc/letsencrypt/live/${hostname}/privkey.pem https://binaural.me/public_packages/uploader/vr/${hostname}_privkey.pem
else
    # get cert by certbot
    sudo snap install core; sudo snap refresh core
    sudo apt-get remove certbot
    sudo snap install --classic certbot
    sudo ln -s /snap/bin/certbot /usr/bin/certbot
    sudo certbot --nginx --non-interactive --agree-tos --email hasevr@gs.haselab.net --domains $hostname

    #upload cert 
    sudo curl -X POST -H "Content-Type: multipart/form-data" -F "do_upload=true" -F "userfile=@/etc/letsencrypt/live/${hostname}/fullchain.pem;filename=${hostname}_fullchain.pem" https://binaural.me/public_packages/uploader/vr/index.php 
    sudo curl -X POST -H "Content-Type: multipart/form-data" -F "do_upload=true" -F "userfile=@/etc/letsencrypt/live/${hostname}/privkey.pem;filename=${hostname}_privkey.pem" https://binaural.me/public_packages/uploader/vr/index.php 
fi

#change nginx conf to real one.
sudo rm /etc/nginx/sites-enabled/${hostname}.conf
sudo ln -s /etc/nginx/sites-available/${hostname}.conf /etc/nginx/sites-enabled/${hostname}.conf
#set jvb password to prosody
prosodyctl register jvb auth.vr.binaural.me 9W4YdU5t


# update video bridge
cd /usr/share/jitsi-videobridge
sudo chmod 777 .
sudo rm -rf *
wget https://binaural.me/public_packages/jitsi-videobridge-2.1-SNAPSHOT-archive.zip
unzip jitsi-videobridge-2.1-SNAPSHOT-archive.zip
rm jitsi-videobridge-2.1-SNAPSHOT-archive.zip
cd jitsi-videobridge-2.1-SNAPSHOT
cp -r * ..
cd ..
rm -rf jitsi-videobridge-2.1-SNAPSHOT

wget https://binaural.me/public_packages/jvb_scripts.tgz
tar -zxf jvb_scripts.tgz
rm jvb_scripts.tgz
cp -r jvb_scripts/* .
rm -rf jvb_scripts
chmod 777 *.sh

#replace paramters in config files
SED="sed -e s/BM_THIS_HOST_PRIVATE_ADDRESS/`curl 169.254.169.254/latest/meta-data/local-ipv4`/g -e s/BM_THIS_HOST_PUBLIC_ADDRESS/`curl 169.254.169.254/latest/meta-data/public-ipv4`/g -e s/BM_THIS_HOST_NAME/$hostname/g"
function SED_INPLACE() {
    sudo cat $1|$SED>tmp.txt
    sudo cp tmp.txt $1
}
SED_INPLACE /etc/jitsi/videobridge/jvb.conf
SED_INPLACE /etc/jitsi/videobridge/sip-communicator.properties

#restart
sudo /etc/init.d/nginx stop
sudo /etc/init.d/prosody stop
sudo /etc/init.d/jicofo stop
sudo /etc/init.d/jitsi-videobridge2 stop
sudo /etc/init.d/nginx start
sudo /etc/init.d/prosody start
sudo /etc/init.d/jicofo start

#Start local jvb also
sudo /etc/init.d/jitsi-videobridge2 start

#Install and start bmRelayServer
#install deno and pm2
sudo apt-get -y install git
sudo apt-get -y install unzip
curl -fsSL https://deno.land/x/install/install.sh | sh
wait_for_dpkg_lock
sudo apt-get -y install nodejs npm
sudo npm install pm2 -g
#install bmRelayServer
cd
git clone https://github.com/BinauralMeet/bmRelayServer
cd bmRelayServer
rm config.json
echo {>>config.json
echo    \"port\": 8443,>>config.json
echo    \"tls\": true>>config.json
echo }>>config.json
ln -s /etc/letsencrypt/live/${hostname}/fullchain.pem host.crt
ln -s /etc/letsencrypt/live/${hostname}/privkey.pem host.key
pm2 start bmRelayServer.ts --interpreter="/root/.deno/bin/deno" --interpreter-args="run --allow-net --allow-read"

#install JPE
wait_for_dpkg_lock
sudo apt-get -y install docker-compose
sudo apt-get -y install maven
wait_for_dpkg_lock
cd /usr/share
git clone https://github.com/BinauralMeet/Jitsi-Prometheus-Exporter.git
cd Jitsi-Prometheus-Exporter
mvn package -DskipTests
/usr/bin/java -server -d64 -DCONFIG_HOME=./conf -jar ./target/jpe-0.0.5-SNAPSHOT.jar&

curl --output jpe_conf.tgz https://binaural.me/public_packages/uploader/vr/vr_main_jpe_conf.tgz
tar zxf jpe_conf.tgz
cp ./jpe_conf/app-override.properties ./conf/
cp ./jpe_conf/grafana.ini ./monitoring/grafana/

cd monitoring
find . -type d -exec chmod 777 \{\} \;
docker-compose up
