#!/bin/bash

# shellcheck source=/dev/null
[ -f "/etc/default/packagename" ] && . /etc/default/foxde-autologin

# Some defaults
[ -z "$AUTOLOGIN_HOME" ] && AUTOLOGIN_HOME="/home/autologin-fuchs"
[ -z "$AUTOLOGIN_GID" ] && AUTOLOGIN_GID="4200"
[ -z "$AUTOLOGIN_UID" ] && AUTOLOGIN_UID="4200"


#wait for the home directory to be created
while [ ! -d "$AUTOLOGIN_HOME" ] ; do
	sleep 5
done

#create the file so we can watch it
if [ ! -e "$AUTOLOGIN_HOME/.halt" ] ; then
	touch "$AUTOLOGIN_HOME/.halt"
	chown "$AUTOLOGIN_UID:$AUTOLOGIN_GID" "$AUTOLOGIN_HOME/.halt"
fi

# Prevent a shutdown in the first 5 seconds
sleep 5

# wait for the file to be written to, and if yes power off the machine, do nothing on failure
if inotifywait -e close_write "$AUTOLOGIN_HOME/.halt" ; then
	poweroff
fi

