#!/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 "Error: 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" \
        | sed 's/,/\n/g' \
        | grep '"shorturl"' \
        | cut -d':' -f2- \
        | sed 's/^"//' \
        | sed 's/"$//' \
        | sed 's/^[[:space:]]*//')

    ERROR_MSG=$(echo "$RESPONSE" \
        | sed 's/,/\n/g' \
        | grep '"errormessage"' \
        | cut -d':' -f2- \
        | sed 's/^"//' \
        | sed 's/"$//' \
        | sed 's/^[[:space:]]*//')

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

    if [ -n "$SHORT_URL" ]; then
        echo "Short URL:"
        echo "$SHORT_URL"
    else
        echo "Failed to create short URL."
        [ -n "$ERROR_MSG" ] && echo "Error: $ERROR_MSG"
    fi

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

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

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