#!/usr/bin/bash

# This script is supposed to be run as root, trigered by systemd service (shuttleworker.service). It will grab the first logged-in users and run the shuttleworker under him.
# If nobody is logged it will wait until somebody does log-in.
# This is slightly modified script originating from https://github.com/Shamanon/ShuttlePRO

trap "kill 0" EXIT

MYUSER=($(users))
sleep 1

while ! [ "$MYUSER" ]; do
	sleep 10
	MYUSER=($(users))
done

pkill /usr/local/bin/shuttleworker
su $MYUSER -c "DISPLAY=:0 /usr/local/bin/shuttleworker"
