#!/bin/sh
# Specifically handling LID events
# Can take the form:
# button/lid LID close
# button/lid LID open
# We are only interested in value $3.

# Do not turn off machine if external screens are connected.

case "$3" in

close)
    should_sleep=true
    screens=$(find /sys/devices -type f -wholename '*drm/*card*-DP-*/status')
    while IFS= read -r screen; do
        if [ "$(cat "$screen")" = connected ]; then
            should_sleep=false
        fi
    done <<EOF
$screens
EOF

    if [ "$should_sleep" = true ]; then
        suspend-to-ram
        logger "LID closed, no external screens, suspending..."
        zzz
    else
        logger "LID closed, external screens, not suspending."
    fi
    ;;

open) logger "LID opened" ;;

*) logger "ACPI action undefined (LID): $*" ;;

esac