#!/bin/sh

set -e

echo "Setting up passwordless suspend..."
echo "$USER ALL=(ALL) NOPASSWD: /usr/bin/systemctl suspend" | sudo tee /etc/sudoers.d/suspend > /dev/null
sudo chmod 440 /etc/sudoers.d/suspend
sudo visudo -cf /etc/sudoers.d/suspend

echo "Creating desktop entry..."
mkdir -p ~/.local/share/applications
cat > ~/.local/share/applications/suspend.desktop << 'EOF'
[Desktop Entry]
Name=Suspend
Exec=sudo systemctl suspend
Comment=
Terminal=false
PrefersNonDefaultGPU=false
Icon=cinnamon-panel-launcher
Type=Application
EOF
chmod +x ~/.local/share/applications/suspend.desktop

if [ -d "$HOME/Desktop" ]; then
    cp ~/.local/share/applications/suspend.desktop ~/Desktop/
    chmod +x ~/Desktop/suspend.desktop
    echo "Shortcut also added to Desktop."
fi

echo "Done"