#!/usr/bin/bash


function printhelp {
cat <<- EOF
i3lockmore by Sven Greiner <sven@sammyshp.de>

This is a wrapper for i3lock that adds additional functionality:

    --pixelate [scalefactor]
        Uses the pixelated screen content as the background of the lockscreen.

        The optional value "scalefactor" controls the size of the pixels. A value
        of 1 results in large pixels while a value of 100 has no effect.
        (It scales down the background to "scalefactor" percent of the original
        screen content).

    --grayscale
        Convert the background into grayscale. Requires --pixelate.

    --resize-image /path/to/image
        simply use a big image and resize it to the current screen size

    --dpms timeout
        Uses DPMS to turn the screen off after "timeout" seconds of inactivity.

        CAUTION: This sets --nofork (see "man i3lock") to restore the previous
        value of the DPMS timeout after the screen was unlocked.

All other arguments are passed to i3lock.
EOF
}


USE_PIXELATE=false
PIXELATE_SCALEFACTOR=4
USE_DPMS=false
USE_IMAGE=false
DPMS_TIMEOUT=5
declare -a PARAMS
DPMS_OLD=
BACKGROUND=
GRAYSCALE=


while [[ $# -gt 0 ]]; do
    case "$1" in
        --pixelate)
            USE_PIXELATE=true
            if [[ $# -gt 1 && "$2" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
                PIXELATE_SCALEFACTOR="$2"
                shift
            fi
            ;;

        --grayscale)
            GRAYSCALE="-colorspace gray"
            shift
            ;;

        --resize-image)
            USE_IMAGE=true
            IMAGE_FILE="$2"
            shift
            ;;

        --dpms)
            if [[ $# -gt 1 && "$2" =~ ^[0-9]+$ ]]; then
                USE_DPMS=true
                DPMS_TIMEOUT="$2"
                shift
                PARAMS+=( "-n" )
            else
                echo "Usage: --dpms timeout"
                exit 1
            fi
            ;;

        --morehelp)
            printhelp
            exit
            ;;

        *)
            # Pass argument to i3lock
            PARAMS+=( "$1" )
            ;;
    esac

    shift # Consume argument
done


function finish {
    if [[ "$USE_PIXELATE" == true ]] || [[ "$USE_IMAGE" == true ]]; then
        if [[ "$BACKGROUND" =~ ^/tmp/i3lockmore-background\.[a-zA-Z0-9]+$ ]]; then
            rm "$BACKGROUND"
        fi;
    fi

    if [[ "$USE_DPMS" == true ]]; then
        xset dpms $DPMS_OLD
    fi
}
trap finish EXIT


if [[ "$USE_PIXELATE" == true ]]; then
    BACKGROUND=$(mktemp /tmp/i3lockmore-background.XXXXX)
    import -window root jpeg:"$BACKGROUND"
    SIZE=$(identify -format "%[fx:w]x%[fx:h]" "$BACKGROUND")
    convert jpeg:"$BACKGROUND" -scale $PIXELATE_SCALEFACTOR% $GRAYSCALE -sample $SIZE\! -quality 11 png24:"$BACKGROUND"

    PARAMS+=( -i "$BACKGROUND" )
fi


if [[ "$USE_IMAGE" == true ]]; then
    BACKGROUND=$(mktemp /tmp/i3lockmore-background.XXXXX)
    SCREEN_SIZE=($(xrandr |grep \* |awk '{print $1}'))
    COUNT=0
    convert "$IMAGE_FILE" -resize ${SCREEN_SIZE[0]} png24:"$BACKGROUND"
    while [ $COUNT -le $(( ${#SCREEN_SIZE[@]} -1 )) ]
    do
        if [ ${SCREEN_SIZE[$COUNT]} != ${SCREEN_SIZE[0]} ]
        then
            cat <<- EOF
ERROR: Dualscreen setup with different resolutions.
       Use the same resolution on both screen to use this feature.
       Starting i3lock without changing resolution...
EOF
            PARAMS+=( -i $IMAGE_FILE)
        else
            PARAMS+=( -i "$BACKGROUND" )
        fi
        COUNT=$(($COUNT + 1 ))
    done
fi


if [[ "$USE_DPMS" == true ]]; then
    DPMS_OLD=$(xset q | grep Standby | sed -n 's/\s*Standby: \([0-9]*\)\s*Suspend: \([0-9]*\)\s*Off: \([0-9]*\).*/\1 \2 \3/p')
    xset dpms 0 0 $DPMS_TIMEOUT
fi


i3lock "${PARAMS[@]}"
