#!/bin/sh

tmpfile="$(mktemp)"

while :; do
    case "$1" in
        "--verbose"|"--debug"|"-v")
            if [ -z "$debug" ]; then
                [ "$debug" = "true" ] && echo "Enabling debug output"
                debug="true"
            else
                echo "debug has already been enabled!"
            fi
            ;;

        "--offline"|"-o")
            if [ -z "$offline" ]; then
                [ "$debug" = "true" ] && echo "Enabling offline mode"
                offline="true"
            else
                echo "offline has already been enabled!"
            fi
            ;;

        "--nekodir")
            if [ -z "$nekodir" ]; then
                nekodir="$2"
                offline="true"
                [ "$debug" = "true" ] && echo "Using offline dir $nekodir"
            else
                echo "You can only specify the neko dir argument once!"
            fi
            shift
            ;;

        "--nsfw"|"nsfw"|"-n"|"n")
            if [ -z "$imgtype" ]; then
                [ "$debug" = "true" ] && echo "Getting a nsfw image"
                imgtype="nsfw"
            else
                echo "$imgtype images were already chosen! Please only specify either sfw or nsfw!"
            fi
            ;;

        "--sfw"|"sfw"|"-s"|"s")
            if [ -z "$imgtype" ]; then
                [ "$debug" = "true" ] && echo "Getting a sfw image"
                imgtype="sfw"
            else
                echo "$imgtype images were already chosen! Please only specify either sfw or nsfw!"
            fi
            ;;

        "--w3m"|"w3m"|"--img"|"img"|"-i"|"i")
            [ "$debug" = "true" ] && echo "Using w3m image backend for neofetch"
            use_w3m="true"
            ;;

        "--height"|"-h")
            if [ -z "$use_height" ]; then
                use_height="$2"
                [ "$debug" = "true" ] && echo "Using height $use_height"
            else
                echo "You can only specify the height argument once!"
            fi
            shift
            ;;

        *)
            if [ -z "$1" ]; then
                break
            fi
            echo "Could not interpret parameter '$1'."
            ;;
    esac
    shift
done
offlineloc="${nekodir:-/usr/share/nekofetch/nekos}"
if [ "$offline" = "true" ]; then
    if [ ! -d "$offlineloc" ]; then
        echo "The specified offline images location ($offlineloc) does not exist or is not a directory!"
        exit 1
    fi
fi
[ "$debug" = "true" ] && [ "$offline" = "true" ] && echo "The offline images location is $offlineloc"

if [ ! "$offline" = "true" ]; then
    if [ "$imgtype" = "nsfw" ]; then
        imgurl="https://nekos.life/api/v2/img/cum_jpg"
    else
        imgurl="https://nekos.life/api/v2/img/neko"
    fi
fi

if [ -n "$use_height" ]; then
    height="$use_height"
else
    height="$(($(stty size | awk '{print $1}') - 5))"
fi

if [ "$offline" = "true" ]; then
    pickdir="$offlineloc/$imgtype"
    cp "$(find "$pickdir" -type f | shuf -n 1)" "$tmpfile.jpg"
else
    url=$(curl -fsSL "$imgurl" | jq -r ".url")
    [ "$debug" = "true" ] && echo "Using image at url $url"
    curl -fsSLo "$tmpfile.jpg" "$url"
fi

if [ "$TERM" = "xterm-kitty" ]; then
    command -v convert > /dev/null 2>&1 && neofetch --kitty "$tmpfile.jpg" || kitty_imagemagick_warn=true
    if [ "$kitty_imagemagick_warn" = "true" ]; then
        jp2a --height="$height" "$tmpfile.jpg" > "$tmpfile"
        neofetch --source "$tmpfile"
    fi

elif [ "$LC_TERMINAL" = "iTerm2" ]; then
    neofetch --iterm2 "$tmpfile.jpg"

elif [ "$use_w3m" = "true" ]; then
    neofetch --w3m "$tmpfile.jpg"

else
    jp2a --height="$height" "$tmpfile.jpg" > "$tmpfile"
    neofetch --source "$tmpfile"
fi

rm "$tmpfile" "$tmpfile.jpg"

if [ "$kitty_imagemagick_warn" = "true" ]; then
    echo "WARN: imagemagick is required for kitty image backend"
fi
