#!/bin/bash
CONFIG_DIR="$HOME/.config/mbscreenlocker"
CONFIG_FILE="$CONFIG_DIR/mbscreenlocker.conf"

# Make config directory if not exist
mkdir -p $CONFIG_DIR

# If config file not exist create one with defaults
if [ ! -f $CONFIG_FILE ]; then
cat <<EOF > ${CONFIG_FILE}
# Effect to use: dim, blur, dimblur or pixel
effect=dim
# ScreenLocker program: betterlockscreen or i3lock
screenlocker=i3lock
EOF
fi

# read config variables from file
source <(grep = $CONFIG_FILE)

case $LANG in
    pl*)
    TITLE="Tworzenie obrazków dla blokady ekranu"
    TEXT="\nCierpliwości...\nto może chwilkę potrwać.."
    TITLE2="Gotowe"
    TEXT2="\n<b>super+l</b> aby zablokować ekran"
    ;;
    *)
    TITLE="Caching images for faster screen locking"
    TEXT="\nPlease wait...\nthis might take few seconds..."
    TITLE2="Done"
    TEXT2="All required changes have been applied.\n Use <b>super + l</b> to lock screen."
    ;;
esac

lock() {
    if [[ $screenlocker == "betterlockscreen" ]]; then
        betterlockscreen -l $effect
    else
        i3lock -B 2 -k --time-color="337d00ff" --date-color="337d00ff"
    fi
}

cache() {

source <(grep file ~/.config/nitrogen/bg-saved.cfg)
notify-send.sh -u critical -i emblem-photos -R /tmp/mbscreencache "$TITLE" "$TEXT"
betterlockscreen -u "$file"
notify-send.sh -u normal -i emblem-photos -R /tmp/mbscreencache "$TITLE2" "$TEXT2"
}

setimg() {
    img=$(yad --file --file-filter="Image Files (*.jpg *.jpeg *.png *.tif)| *.jpg *.JPG *.jpeg *.JPEG *.png *.PNG")
    if [ -f "$img" ]; then
    notify-send.sh -u critical -i emblem-photos -R /tmp/mbscreencache "$TITLE" "$TEXT"
    betterlockscreen -u "$img"
    notify-send.sh -u normal -i emblem-photos -R /tmp/mbscreencache "$TITLE2" "$TEXT2"
    fi
}

usage() {
    grep "^#:" $0 | while read DOC; do printf '%s\n' "${DOC###:}"; done
    exit
}



case "$1" in
    -c|cache) cache;;
    -h|--help) usage;;
    -s|setimg) setimg;;  
     *) lock;;
esac

exit 0