#!/bin/bash

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/lib/exec/labmanager

# script per i servizi systemd
echo "Downloading systemd script files..."
cd /usr/lib/exec/labmanager
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-systemd/raw/master/bin/bta-no-usbpen
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-systemd/raw/master/bin/get-config
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-systemd/raw/master/bin/kdm-autologin
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-systemd/raw/master/bin/mk-homedir
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-systemd/raw/master/bin/firefox-home
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-systemd/raw/master/bin/iptables-rules
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-systemd/raw/master/bin/kioskmode
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-systemd/raw/master/bin/nfs
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-systemd/raw/master/bin/usb
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-systemd/raw/master/bin/virtualbox
chmod +x *
echo "OK."

echo ""

# servizi systemd
echo "Downloading systemd service files..."
cd /etc/systemd/system
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-systemd/raw/master/systemd/esame-apply-config.service
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-systemd/raw/master/systemd/esame-apply-config.timer
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-systemd/raw/master/systemd/esame-firefox-home.service
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-systemd/raw/master/systemd/esame-get-config.service
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-systemd/raw/master/systemd/esame-iptables-rules.service
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-systemd/raw/master/systemd/esame-kdm-autologin.service
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-systemd/raw/master/systemd/esame-kioskmode.service
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-systemd/raw/master/systemd/esame-mk-homedir.service
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-systemd/raw/master/systemd/esame-nfs.service
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-systemd/raw/master/systemd/esame.target
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-systemd/raw/master/systemd/esame-usb.service
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-systemd/raw/master/systemd/esame-virtualbox.service
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

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

echo ""

# sfondi
echo "Downloading backgrounds..."
mkdir -p /opt/backgrounds/lab-linux/2017
cd /opt/backgrounds/lab-linux/2017
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-extras/raw/master/backgrounds/lab-linux/2017/KEEPCALM-esame.png
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-extras/raw/master/backgrounds/lab-linux/2017/KEEPCALM-moodle.png
curl -O https://gitlab.di.unipmn.it/ulisse/labmanager-extras/raw/master/backgrounds/lab-linux/2017/KEEPCALM-show.png
echo "OK."

echo ""
echo "All done!"
