#!/data/data/com.termux/files/usr/bin/bash

while true; do
    clear
    echo "=========================="
    echo "     IS.GD SHORTENER"
    echo "=========================="
    echo

    read -p "Enter long URL: " LONG_URL

    if [ -z "$LONG_URL" ]; then
        echo "URL cannot be empty."
        sleep 2
        continue
    fi

    RESPONSE=$(curl -sG \
        --data-urlencode "url=$LONG_URL" \
        --data "format=json" \
        "https://is.gd/create.php")

    SHORT_URL=$(echo "$RESPONSE" | grep -o '"shorturl":"[^"]*"' | cut -d'"' -f4)

    ERROR_MSG=$(echo "$RESPONSE" | grep -o '"errormessage":"[^"]*"' | cut -d'"' -f4)

    echo
    echo "=========================="

    if [ -n "$SHORT_URL" ]; then
        echo "Short URL:"
        echo "$SHORT_URL"
    else
        echo "Error:"
        echo "${ERROR_MSG:-Unknown error}"
    fi

    echo "=========================="
    echo
    read -p "Generate another link? (y/n): " CHOICE

    case "$CHOICE" in
        y|Y) ;;
        *) break ;;
    esac
done