#!/bin/bash
# ================================================================
# ngrok-start.sh – Jalankan ngrok untuk Music Player
# Lokasi: config/ngrok-start.sh
# ================================================================
# Cara pakai:
#   cd /Applications/XAMPP/xamppfiles/htdocs/kalkulator/music-player/config
#   chmod +x ngrok-start.sh   (sekali saja)
#   ./ngrok-start.sh
# ================================================================

NGROK_BIN="$HOME/bin/ngrok"
XAMPP_PORT=80
APP_PATH="/kalkulator/music-player"

echo ""
echo "╔══════════════════════════════════════════╗"
echo "║     🎵 Neomorph Player – ngrok Launcher   ║"
echo "╚══════════════════════════════════════════╝"
echo ""

# Cek ngrok ada
if [ ! -f "$NGROK_BIN" ]; then
    echo "❌ ngrok tidak ditemukan di $NGROK_BIN"
    echo "   Install dengan:"
    echo "   mkdir -p ~/bin && curl -s https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-darwin-amd64.tgz | tar xz -C ~/bin/"
    exit 1
fi

# Cek XAMPP Apache berjalan
if ! curl -s --max-time 2 "http://localhost:$XAMPP_PORT" > /dev/null 2>&1; then
    echo "⚠️  Apache XAMPP tampaknya belum berjalan di port $XAMPP_PORT"
    echo "   Pastikan XAMPP Apache sudah di-Start terlebih dahulu."
    echo ""
fi

# Cek authtoken
if ! "$NGROK_BIN" config check > /dev/null 2>&1; then
    echo "🔑 Anda belum punya authtoken ngrok."
    echo ""
    echo "   1. Daftar gratis di : https://ngrok.com"
    echo "   2. Copy token dari  : https://dashboard.ngrok.com/get-started/your-authtoken"
    echo "   3. Jalankan perintah:"
    echo ""
    echo "      ~/bin/ngrok config add-authtoken TOKEN_ANDA"
    echo ""
    echo "   Kemudian jalankan ulang script ini."
    exit 1
fi

echo "🚀 Memulai tunnel ngrok ke port $XAMPP_PORT..."
echo "   Tekan Ctrl+C untuk menghentikan"
echo ""
echo "📎 URL publik akan muncul di bawah setelah tunnel aktif:"
echo "   Format  : https://xxxx-xxxx.ngrok-free.app"
echo "   Player  : https://xxxx-xxxx.ngrok-free.app${APP_PATH}/"
echo "   Admin   : https://xxxx-xxxx.ngrok-free.app${APP_PATH}/login.html"
echo "   phpMyAdmin : https://xxxx-xxxx.ngrok-free.app/phpmyadmin"
echo ""

"$NGROK_BIN" http $XAMPP_PORT
