#!/bin/bash
OK='\033[0;32m'
ER='\033[0;31m'
WA='\033[1;33m'
NC='\033[0m'

clear
echo "============================================================"
echo "  TESHIS + MANUEL RSYNC - Lokal Kasada Calistir"
echo "============================================================"
echo ""

# Config bul
CONF_DIR=""
[ -f /gidenyol/ayarlar.conf ] && CONF_DIR="/gidenyol"
[ -f /metmar/ayarlar.conf ]   && CONF_DIR="/metmar"

if [ -z "$CONF_DIR" ]; then
    printf "${ER}[!!] ayarlar.conf bulunamadi${NC}\n"; exit 1
fi
source "$CONF_DIR/ayarlar.conf"
printf "${OK}[OK] Halisaha: $HALISAHA_ADI${NC}\n"

# donustur.sh icindeki hedef sunucu
echo ""
echo "--- donustur.sh hedef sunucu ---"
grep -o 'root@[^:]*' "$CONF_DIR/donustur.sh" | head -3
echo ""

# SSH key var mi?
echo "--- SSH Anahtari ---"
if [ -f /root/.ssh/id_rsa ]; then
    printf "${OK}[OK] /root/.ssh/id_rsa mevcut${NC}\n"
else
    printf "${ER}[!!] SSH anahtari YOK! guncelle.sh calistirin.${NC}\n"
fi

# Yeni sunucuya SSH baglantisi calis
echo ""
echo "--- Yeni Sunucu (95.217.120.124) SSH Testi ---"
ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 \
    root@95.217.120.124 "echo '[OK] SSH baglandı'" 2>&1

# hazir dizinlerde MP4 var mi?
echo ""
echo "--- hazir/ dizinlerindeki MP4 dosyalari ---"
find /usr/local/nginx/html/mp4/$HALISAHA_ADI -name "*.mp4" 2>/dev/null
SAYI=$(find /usr/local/nginx/html/mp4/$HALISAHA_ADI -name "*.mp4" 2>/dev/null | wc -l)
printf "${WA}Toplam: $SAYI adet MP4${NC}\n"

# FLV kayit durumu
echo ""
echo "--- Aktif FLV kayitlari ---"
find /usr/local/nginx/html/mp4/$HALISAHA_ADI -name "*.flv" 2>/dev/null
FLV=$(find /usr/local/nginx/html/mp4/$HALISAHA_ADI -name "*.flv" 2>/dev/null | wc -l)
printf "${WA}Aktif kayit: $FLV adet FLV${NC}\n"

# Nginx calisyor mu?
echo ""
echo "--- Nginx Durumu ---"
systemctl is-active nginx && printf "${OK}[OK] Nginx calisiyor${NC}\n" || printf "${ER}[!!] Nginx durdu!${NC}\n"

# Cron calisyor mu?
echo ""
echo "--- Cron durumu ---"
systemctl is-active crond && printf "${OK}[OK] Crond calisiyor${NC}\n" || printf "${ER}[!!] Crond durdu!${NC}\n"
echo "Mevcut crontab:"
crontab -l 2>/dev/null | head -5

echo ""
echo "============================================================"
echo "  MANUEL RSYNC BASLATILIYOR..."
echo "============================================================"
bash "$CONF_DIR/donustur.sh"
echo ""
printf "${OK}Manuel rsync tamamlandi!${NC}\n"
echo "============================================================"
