#!/bin/bash
SUSPEND_TIMEOUT=300

# There are occasions when you want to lock the screen after timeout but don't shutdown
# Make this function return 1 if you don't want to shutdown
function veto() {
    if [[ "$(cmus-remote -Q | grep '^status ')" == "status playing" ]]; then
        return 1
    elif [[ -f "/tmp/i3-lock.no-suspend" ]]; then
        return 1
    else
        return 0
    fi
}
waitForSuspend() {
    while [ True ]; do
        sleep "$SUSPEND_TIMEOUT"
        if veto; then
            systemctl suspend
        fi
    done
}
resolution=$(xrandr | grep -E ' *[0-9]*x[0-9]* *[0-9][0-9]\.[0-9][0-9]\*\+' | awk '{print $1}')
waitForSuspend &
waitPID="$!"
trap "kill $waitPID" EXIT
image="$HOME/.cache/i3lock/lock_${resolution}"
[[ ! -d "$HOME/.cache/i3lock" ]] && mkdir "$HOME/.cache/i3lock"
if [[ ! -f "$image" ]]; then
    convert "$HOME/.config/i3/lock.png" -resize "${resolution}^" -gravity Center -crop "${resolution}+0+0" "$image"
fi
i3lock -i "$image" -f -n
