#!/bin/sh

DIST_SITE='https://dist.aitken.com'
DIST_DIR='vim-colorschemes'
DEST_DIR="$HOME/.vim/colors"
COLORSCHEME_FILES="
    nord.vim \
    solarized.vim \
    tokyonight.vim \
"


ask_yes_no() {
    prompt="$1"
    default="$2"            # Y/N/blank

    # Format the prompt based on the default choice
    case "$default" in
        [Yy]*) hint=" [Y/n]: " ;;
        [Nn]*) hint=" [y/N]: " ;;
        *)     hint=" (y/n): " ;;
    esac

    while true; do
        printf "%s%s" "$prompt" "$hint"
        read -r response

        if [ -z "$response" ]; then
            case "$default" in
                [Yy]*) return 0 ;;
                [Nn]*) return 1 ;;
            esac
        fi

        case "$response" in
            [Yy]*) return 0 ;;
            [Nn]*) return 1 ;;
            *) echo "Please answer 'y' or 'n'." ;;
        esac
    done
}

if ! ask_yes_no "Copy vim color schemes to [$DEST_DIR]?" "N"; then
    echo "Aborted."
    exit 0
fi

mkdir -p $DEST_DIR || { echo "Error: Could not create $DESTDIR."; exit 1; }

for file in $COLORSCHEME_FILES; do
    url="${DIST_SITE}/${DIST_DIR}/${file}"

    http_status=$(command curl -s --show-error --fail --output-dir $DEST_DIR -o $file $url -w "%{http_code}" || true)
    if [ "$http_status" -ne 200 ]; then
        echo "Download of [$url] failed." >&2
        echo "  HTTP eror code: $http_status" >&2
    fi
done

exit 0


