#!/data/data/com.termux/files/usr/bin/bash
#
# One-time setup: install termux-services and register sms-api + sshd
# Run this ON THE PHONE in Termux.
#

set -e

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m'

echo -e "${CYAN}╔═══════════════════════════════════════════════════╗${NC}"
echo -e "${CYAN}║${NC}  ${BOLD}SMS Server — Service Setup${NC}                       ${CYAN}║${NC}"
echo -e "${CYAN}╚═══════════════════════════════════════════════════╝${NC}"
echo ""

# 1. Install termux-services (provides runit supervisor)
echo -e "${YELLOW}[1/5]${NC} Installing termux-services..."
pkg install -y termux-services 2>/dev/null || {
  echo -e "${RED}Failed to install termux-services${NC}"
  exit 1
}
echo -e "${GREEN}  OK${NC}"

# 2. Source sv helper (adds sv command to PATH)
echo -e "${YELLOW}[2/5]${NC} Loading service helpers..."
source "$PREFIX/etc/profile.d/start-services.sh" 2>/dev/null || true
echo -e "${GREEN}  OK${NC}"

# 3. Make run scripts executable
echo -e "${YELLOW}[3/5]${NC} Setting permissions..."
SVC_DIR="$HOME/sms-server/termux-sms/services"
chmod +x "$SVC_DIR/sms-api/run"
chmod +x "$SVC_DIR/sms-api/log/run"
chmod +x "$SVC_DIR/sshd/run"
echo -e "${GREEN}  OK${NC}"

# 4. Symlink services into runit service directory
echo -e "${YELLOW}[4/5]${NC} Registering services with runit..."
mkdir -p "$PREFIX/var/service"

# Remove old symlinks if they exist
rm -f "$PREFIX/var/service/sms-api" 2>/dev/null
rm -f "$PREFIX/var/service/sshd-custom" 2>/dev/null

ln -s "$SVC_DIR/sms-api" "$PREFIX/var/service/sms-api"
ln -s "$SVC_DIR/sshd" "$PREFIX/var/service/sshd-custom"
echo -e "${GREEN}  OK${NC}"

# 5. Kill old watchdog and standalone processes
echo -e "${YELLOW}[5/5]${NC} Cleaning up old processes..."
pkill -f "sms-watchdog.sh" 2>/dev/null || true
pkill -f "termux-sms-api-server.py" 2>/dev/null || true
# Don't kill sshd — runit will take it over
echo -e "${GREEN}  OK${NC}"

# Acquire wake lock
termux-wake-lock 2>/dev/null

echo ""
echo -e "${CYAN}═══════════════════════════════════════════════════${NC}"
echo -e "${GREEN}${BOLD}  Setup complete!${NC}"
echo -e "${CYAN}═══════════════════════════════════════════════════${NC}"
echo ""
echo -e "${BOLD}Services registered:${NC}"
echo -e "  ${GREEN}sms-api${NC}      — Flask SMS API server (port 5001)"
echo -e "  ${GREEN}sshd-custom${NC}  — SSH daemon (port 8022)"
echo ""
echo -e "${BOLD}Management commands:${NC}"
echo -e "  ${CYAN}sv status sms-api${NC}       — Check status"
echo -e "  ${CYAN}sv restart sms-api${NC}      — Restart"
echo -e "  ${CYAN}sv down sms-api${NC}         — Stop"
echo -e "  ${CYAN}sv up sms-api${NC}           — Start"
echo -e "  ${CYAN}cat ~/logs/sms-api.log${NC}  — View logs"
echo ""
echo -e "${BOLD}runit will automatically restart services if they crash.${NC}"
echo ""

# Wait a moment for runit to pick up the new services
sleep 3

# Show status
echo -e "${BOLD}Current status:${NC}"
sv status sms-api 2>/dev/null || echo "  sms-api: starting..."
sv status sshd-custom 2>/dev/null || echo "  sshd-custom: starting..."

echo ""
echo -e "${BOLD}Health check:${NC}"
sleep 2
curl -s http://127.0.0.1:5001/health && echo "" || echo -e "${YELLOW}  Server still starting — wait a few seconds and retry${NC}"
