#!/usr/bin/env bash
set -euo pipefail

APP_DIR="/root/radyo-projesi"
ZIP_PATH="/root/radyo-projesi.zip"
UPDATE_DIR="/root/radyo-projesi-upload"

echo "AkkurtCast kurulumu basliyor..."

if [ "$(id -u)" -ne 0 ]; then
  echo "Bu script root kullanicisi ile calismali."
  exit 1
fi

apt-get update
apt-get install -y ca-certificates curl gnupg unzip

if ! command -v docker >/dev/null 2>&1; then
  install -m 0755 -d /etc/apt/keyrings
  curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  chmod a+r /etc/apt/keyrings/docker.gpg
  . /etc/os-release
  echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu ${VERSION_CODENAME} stable" > /etc/apt/sources.list.d/docker.list
  apt-get update
  apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
fi

if ! docker compose version >/dev/null 2>&1 && ! command -v docker-compose >/dev/null 2>&1; then
  apt-get install -y docker-compose
fi

if [ ! -f "$ZIP_PATH" ]; then
  echo "$ZIP_PATH bulunamadi. Once zip dosyasini sunucuya yukleyin."
  exit 1
fi

rm -rf "$UPDATE_DIR"
mkdir -p "$UPDATE_DIR"
unzip -o "$ZIP_PATH" -d "$UPDATE_DIR" >/dev/null

mkdir -p "$APP_DIR"
cd "$APP_DIR"

BACKUP_DIR="/root/radyo-projesi-backup-$(date +%Y%m%d-%H%M%S)"
mkdir -p "$BACKUP_DIR"
for item in web-panel liquidsoap haber docker-compose.yml deploy-server.sh DEPLOY-SUNUCU.md install.sh upload-and-install.ps1 ayarlar.json; do
  if [ -e "$item" ]; then
    cp -a "$item" "$BACKUP_DIR/"
  fi
done

copy_item() {
  local item="$1"
  if [ -e "$UPDATE_DIR/$item" ]; then
    rm -rf "$APP_DIR/$item"
    cp -a "$UPDATE_DIR/$item" "$APP_DIR/$item"
  fi
}

copy_item web-panel
copy_item liquidsoap
copy_item haber
copy_item docker-compose.yml
copy_item deploy-server.sh
copy_item DEPLOY-SUNUCU.md
copy_item install.sh
copy_item upload-and-install.ps1

if [ -e "$UPDATE_DIR/ayarlar.json" ] && [ ! -e "$APP_DIR/ayarlar.json" ]; then
  cp -a "$UPDATE_DIR/ayarlar.json" "$APP_DIR/ayarlar.json"
fi

mkdir -p muzik jingle/saat jingle/normal state recordings haber/bultenler
touch .env
chmod +x deploy-server.sh install.sh

if docker compose version >/dev/null 2>&1; then
  COMPOSE="docker compose"
else
  COMPOSE="docker-compose"
fi

compose_up() {
  if $COMPOSE up -d; then
    return 0
  fi

  echo "Compose ilk denemede basarisiz oldu; web-panel ve radio-engine container'lari temizlenip tekrar deneniyor..."
  for svc in radio-engine web-panel; do
    ids=$(docker ps -aq --filter "label=com.docker.compose.service=$svc" || true)
    if [ -n "$ids" ]; then
      docker rm -f $ids >/dev/null 2>&1 || true
    fi
  done
  docker rm -f radio-engine radio-web-panel >/dev/null 2>&1 || true
  $COMPOSE up -d
}

python3 -m py_compile web-panel/app.py
compose_up

sleep 8
if $COMPOSE ps web-panel >/dev/null 2>&1; then
  $COMPOSE exec -T web-panel python -c "import app; ayarlar=app.ayarlari_oku(); app.liquidsoap_yaz(ayarlar); print('Liquidsoap yenilendi.')" || true
fi

if $COMPOSE ps radio-engine >/dev/null 2>&1; then
  $COMPOSE exec -T radio-engine liquidsoap --check /etc/liquidsoap/main.liq || true
  $COMPOSE restart web-panel radio-engine || true
fi

echo
echo "Kurulum/guncelleme tamamlandi."
echo "Yedek: $BACKUP_DIR"
echo "Panel: http://SUNUCU_IP:5050"
echo "Yayin: http://SUNUCU_IP:8090/radyo"
$COMPOSE ps
