#!/bin/bash
# LabManager: systemd units & scripts installer v2.0

GIT_URL=https://gitlab.di.unipmn.it
GIT_BASE_DIR=ulisse/labmanager-systemd/raw
GIT_BRANCH=master

BIN_FILES=( "desktop-wallpaper" "esamesync-esame.sh" "firefox-home" "get-config" "iptables-rules" "kdm-autologin" "kioskmode" "mk-homedir" "nfs" "usb" "virtualbox" )

SYSTEMD_FILES=( "esame-desktop-wallpaper.service" "esame-firefox-home.service" "esame-get-config-boot.service" "esame-get-config.service" "esame-iptables-rules.service" "esame-kdm-autologin.service" "esame-kioskmode.service" "esame-mk-homedir.service" "esame-nfs.service" "esame-usb.service" "esame-virtualbox.service" "esamesync-esame.service" "esame-get-config.timer" "esame-configurator.target" "esame.target" "esamekiosk.target" )

# DON'T TOUCH UNDER THIS LINE! #################################################

function downloadFiles {
    local base_url="$1"
    shift
    local files=( "$@" )

    i=1
    for f in "${files[@]}"
    do
        echo -e "\n$i/${#files[@]} - $base_url/$f"
        curl -# --fail -O "$base_url/$f"
        i=$(expr $i + 1)
    done
}

echo -e "\nLabManager: systemd units & scripts installer"

if [ -z "$1" ]; then
    echo -e "\nNo branch/tag specified, using branch 'master'."
    echo -e "Please call '$0 <branch/tag>' to use a different source.\n"
else
    GIT_BRANCH="$1"
    echo -e "\nUsing branch/tag '$GIT_BRANCH'.\n"
fi

mkdir -p /usr/libexec/labmanager

# script per i servizi systemd
echo -e "\e[1mDownloading systemd script files...\e[0m"
cd /usr/libexec/labmanager
echo "Creating symlink bta-no-usbpen -> /bin/sleep..."
ln -s -f /bin/sleep bta-no-usbpen
echo
downloadFiles "$GIT_URL/$GIT_BASE_DIR/$GIT_BRANCH/bin" ${BIN_FILES[@]}
chmod +x *
echo
echo

# servizi systemd
echo -e "\e[1mDownloading systemd unit files...\e[0m"
cd /etc/systemd/system
downloadFiles "$GIT_URL/$GIT_BASE_DIR/$GIT_BRANCH/systemd" ${SYSTEMD_FILES[@]}
echo
echo

# following links should be not needed, specifying services as "Wants" directives should suffice

# create symlinks for target esame and esamekiosk
#mkdir esame.target.wants
#cd esame.target.wants
#ln -s /usr/lib/systemd/system/accounts-daemon.service .
#ln -s /usr/lib/systemd/system/rtkit-daemon.service .
#ln -s /usr/lib/systemd/system/switcheroo-control.service .
#ln -s /usr/lib/systemd/system/systemd-update-utmp-runlevel.service .
#ln -s /usr/lib/systemd/system/udisks2.service .
#mkdir esamekiosk.target.wants
#cd esamekiosk.target.wants
#ln -s /usr/lib/systemd/system/accounts-daemon.service .
#ln -s /usr/lib/systemd/system/rtkit-daemon.service .
#ln -s /usr/lib/systemd/system/switcheroo-control.service .
#ln -s /usr/lib/systemd/system/systemd-update-utmp-runlevel.service .
#ln -s /usr/lib/systemd/system/udisks2.service .

echo -e "\e[1mReload systemd...\e[0m"
systemctl daemon-reload
echo
echo

echo -e "\e[1mEnable systemd services...\e[0m"
systemctl enable esame-*.service
systemctl enable esamesync-esame.service
systemctl enable esame-get-config.timer
echo
echo

echo -e "\e[1mDisable unwanted systemd services...\e[0m"
systemctl disable esame-usb.service
echo
echo

echo -e "\e[1mChange default target to esame-configurator.target...\e[0m"
systemctl set-default esame-configurator.target
echo
echo

echo -e "\e[1mAll done!\e[0m"
