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

# hostname of main server
BM_DOMAIN=vr.binaural.me

# 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 JVB server. 
function getHostname() {
    ipaddr=
    while [ "$ipaddr" = "" ]; do
        for i in {1..20} ; do
            hostname=jvb${i}.${BM_DOMAIN}
            ipaddr=`host -ta ${hostname}`
            ipaddr=${ipaddr##* }
            if [ "$ipaddr" = "`curl 169.254.169.254/latest/meta-data/public-ipv4`" ]; then break; fi
#            if [ "$ipaddr" = "13.230.218.86" ]; then break; fi
            ipaddr=
        done
        if [ "$ipaddr" = "" ]; then sleep 3s; fi
    done
}
echo apt update 
sudo apt-get -y update
#rm /var/lib/dpkg/lock-frontend
#rm /var/lib/dpkg/lock

#install java8
echo install jdk 8 
sudo apt-get -y install openjdk-8-jdk-headless
#sleep 1
#rm /var/lib/dpkg/lock-frontend
#rm /var/lib/dpkg/lock

#install emacs and jitsi-meet
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
#rm /var/lib/dpkg/lock-frontend
#rm /var/lib/dpkg/lock

#sudo apt-get -y install emacs
sudo debconf-set-selections <<< "jitsi-videobridge2 jitsi-videobridge/jvb-hostname text ${BM_DOMAIN}"
sudo apt-get -y install jitsi-videobridge2=2.1-570-gb802be83-1
sudo apt-get -y install nginx
sudo apt-get -y install unzip
#sudo apt-get -y install wget

# aws のデフォルト名を使うのは無理、SSL証明書が入手できない。Let's encryptに頼る。
#　JVB1~20でインスタンスのグローバルIPをDNSに登録する → CertBotが走る
#get hostname
getHostname
echo 
echo hostname = $hostname
echo 

# download or get cert.
if [ "`curl -LI https://binaural.me/public_packages/uploader/vr/${hostname}_fullchain.pem -o /dev/null -w '%{http_code}\n' -s`" = "200" ]; then
    echo download cert
    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
    echo get new cert
    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


# get jvb server's settings
cd ~
curl --output vr_jvb_etc.tgz https://binaural.me/public_packages/uploader/vr/vr_jvb_etc.tgz
tar -zxf vr_jvb_etc.tgz
sudo cp -r ./jvb_etc/* /etc/

#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/nginx/sites-available/default.conf
SED_INPLACE /etc/jitsi/videobridge/jvb.conf
SED_INPLACE /etc/jitsi/videobridge/sip-communicator.properties
sudo rm /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/default.conf /etc/nginx/sites-enabled/default

# 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

# restart
sudo /etc/init.d/nginx restart
sudo /etc/init.d/jitsi-videobridge2 stop
sleep 3s
sudo /etc/init.d/jitsi-videobridge2 start
