#!/bin/bash

# Global Variables

SYSTEMD_UNIT_FILES="/etc/systemd/system"

# Debian default
GDM_CUSTOM_FILE='/etc/gdm3/custom.conf'

# Check if this is running on debian which has renamed their custom.conf file to daemon.conf
if [[ -f /etc/gdm3/daemon.conf ]]; then
    GDM_CUSTOM_FILE='/etc/gdm3/daemon.conf'
fi


#Check for the existence of the /var/tmp/station folder, if it's not there, create it
STATION_DIR="/var/tmp/station"
test -d "$STATION_DIR" || mkdir -p "$STATION_DIR"

#if the status file doesn't exist, create it and set the status to open
STATUS_FILE="/var/tmp/station/status"
test -f "$STATUS_FILE" || echo "unset" >"$STATUS_FILE"

STATUS=$(cat "$STATUS_FILE")

# Fuctions

close() {
    # Logs the user out; locks the user's password; disabled autologin; enables banner message; reboots
    USER=$1

    if [ $STATUS = "closed" ]; then
        echo "Station already closed"
        return 0
    else

        #If user is logged in, log them out
        if checkUser $USER; then
            loginctl terminate-user $USER
        fi
        usermod -L $USER &&
            sed --in-place=.bak 's/^AutomaticLoginEnable = true/AutomaticLoginEnable = false/g' $GDM_CUSTOM_FILE &&
            sed --in-place=.bak 's/^banner-message-enable=false/banner-message-enable=true/g' /etc/gdm3/greeter.dconf-defaults &&
            echo "closed" >$STATUS_FILE
        systemctl reboot
        return 0
    fi
}

open() {
    # Unlocks the users password; enables autologin; disables banner message; reboots
    USER=$1

    if [ $STATUS = "open" ]; then
        echo "Station already open"
        return 0

    else

        usermod -U $USER &&
            udevadm trigger && #assure monitors will wakeup
            sed --in-place=.bak 's/^AutomaticLoginEnable = false/AutomaticLoginEnable = true/g' $GDM_CUSTOM_FILE &&
            sed --in-place=.bak 's/^banner-message-enable=true/banner-message-enable=false/g' /etc/gdm3/greeter.dconf-defaults &&
            echo "open" >$STATUS_FILE
        systemctl reboot
        return 0

    fi
}

function checkUser() {

    for u in $(who | awk '{print $1}' | sort | uniq); do
        if [ "$u" == "$1" ]; then
            return 0
        fi
    done
    return 1

}

function setup() {
    #Determine if necessary files are present
    READY_FLAG=0

    [[ -f $SYSTEMD_UNIT_FILES/station-close@.service ]] || READY_FLAG=1
    [[ -f $SYSTEMD_UNIT_FILES/station-close@.timer ]] || READY_FLAG=1
    [[ -f $SYSTEMD_UNIT_FILES/station-open@.service ]] || READY_FLAG=1
    [[ -f $SYSTEMD_UNIT_FILES/station-open@.timer ]] || READY_FLAG=1
    [[ -f $SYSTEMD_UNIT_FILES/station-notify@.service ]] || READY_FLAG=1
    [[ -f $SYSTEMD_UNIT_FILES/station-notify@.timer ]] || READY_FLAG=1
    [[ -f /usr/local/bin/station-notify.py ]] || READY_FLAG=1

    if [[ $READY_FLAG -eq 0 ]]; then
        echo "ready to go"

        systemctl enable --now station-close@$1.timer
        systemctl enable --now station-open@$1.timer
        systemctl enable --now station-notify@$1.timer

        echo "All station services enabled"

    else
        echo "missing neccessary file"
        exit 1
    fi

}

function teardown() {

    systemctl disable --now station-close@$1.timer
    systemctl disable --now station-open@$1.timer
    systemctl disable --now station-notify@$1.timer

    echo "All station services disabled"

}

#Determine which function the user passed in to the command

case "$1" in
"") ;;
close)
    "$@"
    exit
    ;;
open)
    "$@"
    exit
    ;;
setup)
    "$@"
    exit
    ;;
teardown)
    "$@"
    exit
    ;;
*)
    echo "Unknown function: $1"
    exit 2
    ;;
esac
