#!/bin/bash
# esame systemd services and targets installer v2.0

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

function askYesNo {
    QUESTION=$1
    DEFAULT=$2
    if [ "$DEFAULT" = true ]; then
        OPTIONS="[Y/n]"
        DEFAULT="y"
    else
        OPTIONS="[y/N]"
        DEFAULT="n"
    fi
    read -p "$QUESTION $OPTIONS " -n 1 -s -r INPUT
    INPUT=${INPUT:-${DEFAULT}}
    echo ${INPUT}
    if [[ "$INPUT" =~ ^[yY]$ ]]; then
        ANSWER=true
    else
        ANSWER=false
    fi
}

mkdir -p /usr/libexec/labmanager

# script per i servizi systemd
echo "Downloading systemd script files..."
cd /usr/libexec/labmanager
curl -O $GIT_URL/$GIT_BASE_DIR/bin/bta-no-usbpen
curl -O $GIT_URL/$GIT_BASE_DIR/bin/get-config
curl -O $GIT_URL/$GIT_BASE_DIR/bin/kdm-autologin
curl -O $GIT_URL/$GIT_BASE_DIR/bin/mk-homedi
curl -O $GIT_URL/$GIT_BASE_DIR/bin/firefox-home
curl -O $GIT_URL/$GIT_BASE_DIR/bin/iptables-rules
curl -O $GIT_URL/$GIT_BASE_DIR/bin/kioskmode
curl -O $GIT_URL/$GIT_BASE_DIR/bin/nfs
curl -O $GIT_URL/$GIT_BASE_DIR/bin/usb
curl -O $GIT_URL/$GIT_BASE_DIR/bin/virtualbox

chmod +x *

# script esamesync-esame
cd /usr/local/sbin
curl -O $GIT_URL/$GIT_BASE_DIR/bin/esamesync-esame.sh
chmod +x esamesync-esame.sh

echo "OK."

echo ""

# servizi systemd
echo "Downloading systemd service files..."
cd /etc/systemd/system
curl -O $GIT_URL/$GIT_BASE_DIR/systemd/esame-get-config-boot.service
curl -O $GIT_URL/$GIT_BASE_DIR/systemd/esame-get-config.service
curl -O $GIT_URL/$GIT_BASE_DIR/systemd/esame-get-config.timer
curl -O $GIT_URL/$GIT_BASE_DIR/systemd/esame-firefox-home.service
curl -O $GIT_URL/$GIT_BASE_DIR/systemd/esame-get-config.service
curl -O $GIT_URL/$GIT_BASE_DIR/systemd/esame-iptables-rules.service
curl -O $GIT_URL/$GIT_BASE_DIR/systemd/esame-kdm-autologin.service
curl -O $GIT_URL/$GIT_BASE_DIR/systemd/esame-kioskmode.service
curl -O $GIT_URL/$GIT_BASE_DIR/systemd/esame-mk-homedir.service
curl -O $GIT_URL/$GIT_BASE_DIR/systemd/esame-nfs.service
curl -O $GIT_URL/$GIT_BASE_DIR/systemd/esame-usb.service
curl -O $GIT_URL/$GIT_BASE_DIR/systemd/esame-virtualbox.service
curl -O $GIT_URL/$GIT_BASE_DIR/systemd/esame.target
curl -O $GIT_URL/$GIT_BASE_DIR/systemd/esame-configurator.target
curl -O $GIT_URL/$GIT_BASE_DIR/systemd/esamesync-esame.target
curl -O $GIT_URL/$GIT_BASE_DIR/systemd/esamekiosk.target

echo "OK."

echo ""

# disable old esame service?
askYesNo "Disable old systemd esame.service?" false
DOIT=$ANSWER
if [ "$DOIT" = true ]; then
    systemctl disable esame.service
fi

# 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 -n "Reload systemd"
systemctl daemon-reload

echo -n "Enable systemd services..."
systemctl enable esame-*.service
systemctl enable esamesync-esame.service
systemctl enable esame-get-config.timer
echo " OK."

echo ""

echo ""
echo "All done!"
