#!/bin/sh

lock() {
    # Credit: https://github.com/petvas/i3lock-blur
    # TODO Handle several screens
    TMPBG=/tmp/screen_locked.png
    LOCK=~/.i3/lock.png
    RES=$(xrandr | grep 'current' | sed -E 's/.*current\s([0-9]+)\sx\s([0-9]+).*/\1x\2/')

    #ffmpeg -f x11grab -video_size $RES -y -i $DISPLAY -i $LOCK -filter_complex \
    #        "boxblur=5:1,overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2" \
    #        -vframes 1 $TMPBG -loglevel quiet
    ffmpeg -f x11grab -video_size $RES -y -i $DISPLAY -filter_complex \
            "boxblur=5:1" -vframes 1 $TMPBG -loglevel quiet
    i3lock -i $TMPBG
    rm $TMPBG
}

case "$1" in
    lock)
        lock
        ;;
    logout)
        i3-msg exit
        ;;
    suspend)
        lock && sudo pm-suspend
        ;;
    lidclose)
        lock && sudo pm-suspend
        ;;
    hibernate)
        lock && sudo pm-hibernate
        ;;
    reboot)
        systemctl reboot
        ;;
    shutdown)
        systemctl poweroff
        ;;
    *)
        echo "Usage: $0 {lock|logout|suspend|hibernate|reboot|shutdown}"
        exit 2
esac

exit 0
