#!/bin/bash
# Deploy site to wildcard.streamers.channel via SFTP
# Usage: ./deploy.sh [path-to-export-dir]
#
# The export directory should contain:
#   index.html, about.html, etc.
#   assets/css/styles.css
#   assets/images/...
#   assets/videos/...
#   assets/js/...

set -e

SFTP_HOST="whp01.cloud-hosting.io"
SFTP_USER="shadowdao"
REMOTE_DIR="wildcard.streamers.channel"
EXPORT_DIR="${1:-.}"

echo "🚀 Deploying site to ${REMOTE_DIR}..."
echo "   Host: ${SFTP_USER}@${SFTP_HOST}"
echo "   Source: ${EXPORT_DIR}"
echo ""

# Check if export directory exists
if [ ! -d "$EXPORT_DIR" ]; then
    echo "❌ Export directory not found: ${EXPORT_DIR}"
    echo "   Export your site from the Site Builder first (Deploy > Export ZIP)"
    exit 1
fi

# Count files
HTML_COUNT=$(find "$EXPORT_DIR" -maxdepth 1 -name "*.html" | wc -l)
ASSET_COUNT=$(find "$EXPORT_DIR/assets" -type f 2>/dev/null | wc -l)
echo "   HTML files: ${HTML_COUNT}"
echo "   Asset files: ${ASSET_COUNT}"
echo ""

# Build SFTP batch commands
BATCH_FILE=$(mktemp)
cat > "$BATCH_FILE" <<EOF
cd ${REMOTE_DIR}
-mkdir assets
-mkdir assets/css
-mkdir assets/js
-mkdir assets/images
-mkdir assets/videos
EOF

# Add HTML files
for f in "$EXPORT_DIR"/*.html; do
    [ -f "$f" ] && echo "put \"$f\" \"$(basename "$f")\"" >> "$BATCH_FILE"
done

# Add asset files
if [ -d "$EXPORT_DIR/assets" ]; then
    find "$EXPORT_DIR/assets" -type f | while read -r f; do
        rel="${f#$EXPORT_DIR/}"
        echo "put \"$f\" \"$rel\"" >> "$BATCH_FILE"
    done
fi

echo "bye" >> "$BATCH_FILE"

echo "📦 SFTP batch file created. Uploading..."
echo ""

# Execute SFTP
sftp -b "$BATCH_FILE" "${SFTP_USER}@${SFTP_HOST}"

# Cleanup
rm -f "$BATCH_FILE"

echo ""
echo "✅ Deployment complete!"
echo "   Visit: https://abc.streamers.channel/"
echo ""
