#! /usr/bin/env bash

operation=${1:-$(gum choose rmlint download git)}

case $operation in
rmlint)
    rmlint -g --types="duplicates" \
        --config=sh:handler=clone \
        /srv/pool/
    ;;
download)
    ENTRY=$(zenity --entry --width=250 \
        --title "Insert a link" \
        --text="Verify the following link is correct" \
        --add-entry="Link:" \
        --entry-text "$(xclip -o -sel clip)")
    if [ -n "$ENTRY" ]; then
        cmd="download -u jawz"

        if ! zenity --question --text "Use archive database?" \
            --ok-label="Yes" --cancel-label="Cancel"; then
            cmd+=" -a"
        fi
        if ! zenity --question --text "Skip downloaded files?" \
            --ok-label="Yes" --cancel-label="Cancel"; then
            cmd+=" -s"
        fi

        cmd+=" -i \"$ENTRY\" "
        ghostty -e "$cmd"
    else
        zenity --error --width=250 \
            --text "Please verify and try again"
    fi
    ;;
git)
    git_dir=$HOME/Development/Git
    while IFS= read -r repo; do
        if ! [ -d "$repo/.git" ]; then
            continue
        fi
        cd "$repo" || exit
        gum style --foreground 2 "Updating $(basename "$repo")"
        git fsck --full
        git pull
    done < <(fd . "$git_dir" -td --absolute-path -d 1)
    ;;
esac
