#!/bin/bash
set -e

echo "Creating directories..."
mkdir -p /storage/scripts/pidesktop

echo "Downloading files..."
curl -s "https://opengist.stefka.eu/jiriks74/element14-libreelec/raw/HEAD/powerkey.py" -o /storage/scripts/pidesktop/powerkey.py
curl -s "https://opengist.stefka.eu/jiriks74/element14-libreelec/raw/HEAD/pidesktop-powerkey.service" -o /storage/.config/system.d/pidesktop-powerkey.service
echo "Files downloaded successfully."

echo "Loading the systemd service..."
systemctl daemon-reload
systemctl enable pidesktop-powerkey
echo "Service loaded. Starting the service..."
echo "Watch the case now. The light should blink once/twice to indicate that the the script is working."
sleep 5
systemctl start pidesktop-powerkey

echo ""
echo "Setup finished."
echo "The power button should work now."
echo ""
echo "Disclaimer: Reboot might not be possible anymore. The Pi-Desktop cuts power after 30 seconds of no response from the Pi."
echo "            If the Pi takes more than 30 seconds to reboot the Pi-Desktop will cut it's power and you'll have to turn it on again."
