#!/bin/sh
#
# Installs bat color themes and optionally a bat config file.
# Run as the target user (not root) so files land in the correct $HOME.
#
# To overwrite an existing bat config, set OVERWRITE_BAT_CONFIG=1:
#   OVERWRITE_BAT_CONFIG=1 sh install-bat-themes.sh
#   make bat-themes-fresh   (uses the same env var)

PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin
export PATH
set -e
trap '[ $? -eq 0 ] || printf "ERROR: %s did not complete successfully.\n" "$0" >&2' EXIT
trap 'exit 1' INT HUP TERM

DIST_SITE='https://dist.aitken.com'
DIST_DIR='local/bat-themes'
DEST_DIR="$HOME/.config/bat/themes"
BAT_THEMES="
    NordSoftComments.tmTheme
    tokyonight_day.tmTheme
    tokyonight_moon.tmTheme
    tokyonight_night.tmTheme
    tokyonight_storm.tmTheme
"


ask_yes_no() {
    _prompt="$1"
    _default="$2"
    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 ;;
            *) printf '%s\n' "Please answer 'y' or 'n'." ;;
        esac
    done
}

# When run via "curl url | sh", stdin is the pipe and ask_yes_no would
# get EOF immediately.  Skip the prompt and proceed in non-interactive mode.
if [ -t 0 ]; then
    if ! ask_yes_no "Install bat themes to [$DEST_DIR]?" "N"; then
        printf '%s\n' "Aborted."
        exit 0
    fi
fi

mkdir -p "$DEST_DIR" || { printf 'Error: could not create %s\n' "$DEST_DIR" >&2; exit 1; }

# Install bat config -- skip if already present unless OVERWRITE_BAT_CONFIG is set
_config="$HOME/.config/bat/config"
if [ -f "$_config" ] && [ -z "$OVERWRITE_BAT_CONFIG" ]; then
    printf 'bat config already exists at %s -- skipping (set OVERWRITE_BAT_CONFIG=1 to overwrite)\n' "$_config"
else
    http_status=$(command curl -s --show-error --fail \
        -o "$_config" "${DIST_SITE}/${DIST_DIR}/config" -w "%{http_code}" || true)
    if [ "$http_status" -ne 200 ]; then
        printf 'Download of bat config failed (HTTP %s)\n' "$http_status" >&2
    fi
fi

# Install themes
for file in $BAT_THEMES; do
    url="${DIST_SITE}/${DIST_DIR}/${file}"
    http_status=$(command curl -s --show-error --fail \
        -o "${DEST_DIR}/${file}" "$url" -w "%{http_code}" || true)
    if [ "$http_status" -ne 200 ]; then
        printf 'Download of [%s] failed (HTTP %s)\n' "$url" "$http_status" >&2
    fi
done

# Rebuild bat's theme cache (Ubuntu installs the binary as 'batcat')
if command -v bat > /dev/null 2>&1; then
    _bat=bat
elif command -v batcat > /dev/null 2>&1; then
    _bat=batcat
else
    _bat=''
fi

if [ -n "$_bat" ]; then
    "$_bat" cache --build
else
    printf 'Warning: bat not found; run "bat cache --build" after installing bat.\n'
fi

exit 0
