#!/bin/bash
CONFIG_DIR="$HOME/.config/mbwallpaper"
CONFIG_FILE="$CONFIG_DIR/mbwallpaper.conf"
WALLPAPERS_LIST="$CONFIG_DIR/wplist"

# 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}
# Directory with wallpapers
wallpaper_dir=/usr/share/backgrounds/
# Rotate time in seconds
interval=10
EOF
fi

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

setter=nitrogen
# decide which setter and command use to set wallpaper
case "$setter" in
    nitrogen) command="nitrogen --set-zoom-fill --save";;
    feh) command="feh --bg-fill" ;;
    pcmanfm) command="pcmanfm -w" ;;
    *) echo "Unknown wallpaper setter. Please check your config file $CONFIG_FILE";exit 1;;
esac

#check if wallpaper dir was changed, if so delete wallpaper list
if [ ! -f "$CONFIG_DIR/.lastdir" ]; then
    echo "$wallpaper_dir" > "$CONFIG_DIR/.lastdir"
else
lastdir=$(<$CONFIG_DIR/.lastdir)
[[ "$wallpaper_dir" != "$lastdir" ]] && rm "$WALLPAPERS_LIST" && echo "$wallpaper_dir" > "$CONFIG_DIR/.lastdir"
fi

checkwplist() {
    # If list does not exist or is empty
    if [[ ! -f $WALLPAPERS_LIST || $(wc -l <$WALLPAPERS_LIST) -lt 1 ]]; then
    # Prepare list with wallpapers (only PNG and JPG)
    ls -1 "$wallpaper_dir"/*{.jpg,.JPG,.jpeg,.JPEG,.png,.PNG,.avif,.AVIF,.webp,.WEBP} > "$WALLPAPERS_LIST"
    fi
}

one() {
    checkwplist
    line=$(shuf -n 1 $WALLPAPERS_LIST)
        #delete it from list
        grep -v "$line" "$WALLPAPERS_LIST" > "$CONFIG_DIR/tmpfile"; mv "$CONFIG_DIR/tmpfile" "$WALLPAPERS_LIST"
        #run command to set wallpaper
        ${command} ${line}
        exit 0
}

choose() {
    case $LANG in
    pl*)
    TITLE="Wybierz tapetę"
    TEXT="\n<b>Strzałki</b> / <b>kółko myszy</b> poprzednia/następna.\n<b>Enter</b> by ustawić tapetę.\n<b>Esc</b> by wyjść."
    ;;
    *)
    TITLE="Select Wallpaper"
    TEXT="\n<b>Arrows</b> / <b>mousewheel</b> prev/next.\n<b>Enter</b> to set wallpaper.\n<b>Esc</b> to quit."
    ;;
esac
    
    
    SCREENSIZE=$(wmctrl -d |grep "*" | awk -F' ' '{print $4}')
    W="${SCREENSIZE%x*}"
    H="${SCREENSIZE#*x}"
    FW=$((W/4))
    FH=$((H/4))
    X=$((FW*3/2))
    Y=$((H-FH-40))
    notify-send.sh -t 12000 -i ~/.config/mabox/wpicon.png "$TITLE" "$TEXT"
    feh -B "#4D4D4D" -N -x --scale-down -E ${FH} -y ${FW} -^ "Enter=select, Arrows=next/prev Esc=quit" -g ${FW}x${FH}+${X}+${Y} ${wallpaper_dir} -A "${command} '%f'"
}

slideshow() {


while true
do
    checkwplist
    # if wallpapers list have more than 0 lines
    while [ $(wc -l <$WALLPAPERS_LIST) -gt 0 ]
    do
        # get random line
        line=$(shuf -n 1 $WALLPAPERS_LIST)
        #delete it from list
        grep -v "$line" "$WALLPAPERS_LIST" > "$CONFIG_DIR/tmpfile"; mv "$CONFIG_DIR/tmpfile" "$WALLPAPERS_LIST"
        #run command to set wallpaper
        ${command} ${line}
        sleep ${interval}
    done
done
}
changedir(){
	wdir=${1/$HOME/\~}
	#notify-send.sh "dirrrrrrr" "$wdir"
	sd "wallpaper_dir=.*$" "wallpaper_dir=${wdir}" ${CONFIG_FILE}
}

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

case "$1" in
    -o|one) one;;
    -c|choose) choose;;
    -h|--help) usage;;
    -s) slideshow;;
    -d|changedir) changedir "$2";;
esac

exit 0
