#!/bin/sh

watch () {
    mpv "$1"
    clear
    read -p 'Did you finish watching? [Y/n]: ' yn
    while true; do
        if [ -z "$yn" ] || [ "$yn" = "Y" ] || [ "$yn" = "y" ]; then
            name=$(printf '%s\n' "$1" | sed 's:./::g')
            printf '%s\n' "$name" >> "$2"
            break
        elif [ "$yn" = "N" ] || [ "$yn" = "n" ]; then
            break
        fi
        read -p 'Try again? [Y/n]: ' yn
    done
}

# check () {
#
# }

# config
[ ! -d "$HOME/.config/watcher" ] && mkdir -p "$HOME/.config/watcher/"
[ ! -f "$HOME/.config/watcher/config" ] && touch "$HOME/.config/watcher/config"
directory=$(cat $HOME/.config/watcher/config | grep 'directory' | cut -d= -f 2)
exclude=$(printf '! -name %s ' $(cat $HOME/.config/watcher/config | grep 'exclude' | cut -d= -f 2 | sed 's/;/\n/g'))

# watchlist
[ ! -f '$HOME/.cache/watchlist' ] && touch "$HOME/.cache/watchlist"
[ -s "$HOME/.cache/watchlist" ] && watchlist=$(printf '! -name %s ' $(cat $HOME/.cache/watchlist)) || watchlist=""

cd $directory

while true; do
    current_dir=$(basename $(pwd))
    choice=$(find -maxdepth 1 -mindepth 1 $exclude $watchlist | sort | fzf -i --prompt "Current directory: $current_dir ")
    [ -z "$choice" ] && clear && exit
    [ -d "$choice" ] && cd "$choice"
    [ -f "$choice" ] && watch "$choice" "$HOME/.cache/watchlist" && watchlist=$(printf '! -name %s ' $(cat $HOME/.cache/watchlist))
done