#!/bin/bash

rootpapesdir="$HOME/pix/Wallpaper/4chget"
while [[ "$option" != "w" && "$option" != "wg" ]]; do
    option=$(echo -e "wg\nw" | tofi --prompt-text "Is it wallpaper/general (wg) or anime wallpaper (w)?")
done

[[ $option == "w" ]] && parentdir="$rootpapesdir/w" && 	nel="nel" || parentdir="$rootpapesdir/wg"

while [[ -d "$parentdir/$dir" ]]; do
    dir=$(tofi --prompt-text "Enter folder name: " --require-match=false </dev/null)
done
endir="$parentdir/$dir"
mkdir -p "$endir"

threadopt=$(echo -e "Yes\nNo" | tofi --prompt-text "Do you have the thread link in your clipboard?")

[[ "$threadopt" == "Yes" ]] && thread=$(wl-paste | grep -oP '(?<=thread\/)\d+') || thread=$(tofi --prompt-text "Enter thread no.: (#######) " --require-match false </dev/null)

wget -P "$endir" -nd -r -l 1 -H -D i.4cdn.org,is2.4chan.org -A png,jpg,jpeg -R '*s.*' "https://boards.4chan$nel.org/$option/thread/$thread"

viewopt=$(echo -e "Yes\nNo" | tofi --prompt-text "Do you wish to view the downloaded papes now?")
[[ $viewopt == Yes ]] && imv -fr $endir