#!/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" )

OLD_FILES=(
"/usr/libexec/labmanager"
"/etc/systemd/system/esame.target.wants"
"/etc/systemd/system/esamekiosk.target.wants"
"/etc/systemd/system/esame-configurator.target.wants"
"/etc/systemd/system/graphical.target.wants/esame-get-config.timer"
"/etc/systemd/system/esame-desktop-wallpaper.service"
"/etc/systemd/system/esame-firefox-home.service"
"/etc/systemd/system/esame-get-config-boot.service"
"/etc/systemd/system/esame-get-config.service"
"/etc/systemd/system/esame-iptables-rules.service"
"/etc/systemd/system/esame-kdm-autologin.service"
"/etc/systemd/system/esame-kioskmode.service"
"/etc/systemd/system/esame-mk-homedir.service"
"/etc/systemd/system/esame-nfs.service"
"/etc/systemd/system/esame-usb.service"
"/etc/systemd/system/esame-virtualbox.service"
"/etc/systemd/system/esamesync-esame.service"
"/etc/systemd/system/esame-get-config.timer"
"/etc/systemd/system/esame-configurator.target"
"/etc/systemd/system/esame.target"
"/etc/systemd/system/esamekiosk.target"
)

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

function removeFiles {
    local files=( "$@" )

    for f in "${files[@]}"
    do
        rm -rf $f
        echo "Removed file: $f"
    done
}

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"
        if [[ $? != 0 ]]
        then
            echo "curl exited with error code != 0, installation aborted."
            exit 1
        fi
        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

# v1.0 has a different set of files and install procedure, print warning and abort installation
if [[ "$GIT_BRANCH" == "v1.0" ]]
then
    echo -e "\e[1mWARNING!\e[0m"
    echo "Branch 'v1.0' has a different installation procedure, please get"
    echo -e "the correct installation script here:\n"
    echo -e "$GIT_URL/$GIT_BASE_DIR/v1.0\n"
    exit 1
fi

# remove obsolete files
echo -e "\e[1mRemoving obsolete files...\e[0m"
removeFiles ${OLD_FILES[@]}
echo
echo

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 not be 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 .
cd ..
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 .
cd ..

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
systemctl start 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"
