#!/bin/bash

echo "This update process trust the github master branch of Galène..."

apt update && apt -y upgrade
DATE=`date "+%Y%m%d"`


echo "Backuping ~galene directory"
cp -rp ~galene galene.dirbckp.${DATE}

echo "Updating the Galène source..."
cd /opt/galene-src
git pull

echo "Building the Galène Go program"
CGO_ENABLED=0 go build -ldflags='-s -w'

echo "Moving the new binary and useful directories..."
cp -rfp /opt/galene-src/{groups,galene,data,static} ~galene/

echo "Re-creating the isem workgroup"
if [ ! -d /home/galene/groups/myteam/ ]; then
    mkdir /home/galene/groups/myteam
fi

echo "Checking permissions..."
chown -R galene:galene ~galene
setfacl -R -m u:galene:rx /etc/letsencrypt/
#setfacl -R -m u:galene:rx /etc/letsencrypt/{live,archive}
#setfacl -R -m u:galene:rx /etc/letsencrypt/{archive,live}/galene.example.tld
for file in `ls /etc/letsencrypt/live/galene.example.tld/`
do
    setfacl -m u:galene:r /etc/letsencrypt/live/galene.example.tld/$file
done

echo "Restarting Galène service"
service galene restart
