#!/bin/bash
########################################################################
# Gidenyol Bilisim - yesilsaha.net
# Yeni Video Sunucusuna Gecis Scripti v1.2
# Bu script LOKAL KASALARDA calistirilir
########################################################################

OK='\033[0;32m'
ER='\033[0;31m'
WA='\033[1;33m'
NC='\033[0m'

NEW_SERVER="95.217.120.124"
NEW_PORT="22"
NEW_USER="root"
NEW_PASS="auqiWxkQCe2Mm4"

clear
echo "################################################################"
echo "#    Yesilsaha.Net - Yeni Video Sunucusuna Gecis v1.2         #"
echo "################################################################"
echo ""

# ---------------------------------------------------------------
# 1. ayarlar.conf dosyasini bul
# ---------------------------------------------------------------
CONF_DIR=""
if [ -f /gidenyol/ayarlar.conf ]; then
    CONF_DIR="/gidenyol"
elif [ -f /metmar/ayarlar.conf ]; then
    CONF_DIR="/metmar"
fi

if [ -z "$CONF_DIR" ]; then
    printf "${ER}[HATA] ayarlar.conf bulunamadi!${NC}\n"
    echo "Beklenen: /gidenyol/ayarlar.conf veya /metmar/ayarlar.conf"
    exit 1
fi

source "$CONF_DIR/ayarlar.conf"
printf "${OK}[OK] Config: $CONF_DIR/ayarlar.conf${NC}\n"
printf "${OK}[OK] Halisaha Adi: $HALISAHA_ADI${NC}\n"

# ---------------------------------------------------------------
# 2. donustur.sh ve sil.sh dosyalarini ayri ayri bul
#    (config dizininden farkli yerde olabilir)
# ---------------------------------------------------------------
DONUSTUR=""
for p in /gidenyol/donustur.sh /metmar/donustur.sh; do
    [ -f "$p" ] && DONUSTUR="$p" && break
done

SIL=""
for p in /gidenyol/sil.sh /metmar/sil.sh; do
    [ -f "$p" ] && SIL="$p" && break
done

printf "${OK}[OK] donustur.sh: ${DONUSTUR:-BULUNAMADI}${NC}\n"
printf "${OK}[OK] sil.sh     : ${SIL:-BULUNAMADI}${NC}\n"

# ---------------------------------------------------------------
# 3. OS tespiti
# ---------------------------------------------------------------
if [ -f /etc/almalinux-release ]; then
    PKG_MGR="dnf"
    printf "${OK}[OK] OS: AlmaLinux${NC}\n"
else
    PKG_MGR="yum"
    printf "${OK}[OK] OS: CentOS${NC}\n"
fi
echo ""

# ---------------------------------------------------------------
# 4. sshpass kurulumu
# ---------------------------------------------------------------
if ! command -v sshpass &>/dev/null; then
    printf "${WA}[..] sshpass kuruluyor...${NC}\n"
    $PKG_MGR install -y epel-release &>/dev/null
    $PKG_MGR install -y sshpass &>/dev/null
    if ! command -v sshpass &>/dev/null; then
        printf "${ER}[HATA] sshpass kurulamadi! Manuel kurun: $PKG_MGR install -y sshpass${NC}\n"
        exit 1
    fi
fi
printf "${OK}[OK] sshpass hazir${NC}\n"

# ---------------------------------------------------------------
# 5. SSH anahtari olustur (yoksa)
# ---------------------------------------------------------------
if [ ! -f /root/.ssh/id_rsa ]; then
    mkdir -p /root/.ssh
    chmod 700 /root/.ssh
    ssh-keygen -q -t rsa -N "" -f /root/.ssh/id_rsa
    printf "${OK}[OK] SSH anahtari olusturuldu${NC}\n"
else
    printf "${OK}[OK] SSH anahtari mevcut${NC}\n"
fi

# ---------------------------------------------------------------
# 6. Yeni sunucuya SSH anahtarini kopyala
# ---------------------------------------------------------------
printf "${WA}[..] SSH anahtari yeni sunucuya kopyalaniyor...${NC}\n"
sshpass -p "$NEW_PASS" ssh-copy-id \
    -o StrictHostKeyChecking=no \
    -o UserKnownHostsFile=/dev/null \
    -p "$NEW_PORT" \
    "${NEW_USER}@${NEW_SERVER}" 2>/dev/null

if [ $? -ne 0 ]; then
    printf "${ER}[HATA] SSH anahtari kopyalanamadi!${NC}\n"
    exit 1
fi
printf "${OK}[OK] SSH anahtari yeni sunucuya kopyalandi${NC}\n"
echo ""

# ---------------------------------------------------------------
# 7. donustur.sh guncelle
# ---------------------------------------------------------------
if [ -n "$DONUSTUR" ]; then
    cp "$DONUSTUR" "${DONUSTUR}.bak.$(date +%Y%m%d_%H%M%S)"
    # Tum olasi eski sunucu adreslerini degistir
    sed -i "s/root@srv1\.net\.org\.tr/root@${NEW_SERVER}/g"    "$DONUSTUR"
    sed -i "s/root@cdn2\.net\.org\.tr/root@${NEW_SERVER}/g"    "$DONUSTUR"
    sed -i "s/root@srv[0-9]*\.net\.org\.tr/root@${NEW_SERVER}/g" "$DONUSTUR"
    # Config path duzelt - nerede olursa olsun gercek yolu yaz
    sed -i "s|source /metmar/ayarlar\.conf|source ${CONF_DIR}/ayarlar.conf|g"   "$DONUSTUR"
    sed -i "s|source /gidenyol/ayarlar\.conf|source ${CONF_DIR}/ayarlar.conf|g" "$DONUSTUR"
    printf "${OK}[OK] donustur.sh guncellendi: $DONUSTUR${NC}\n"
    printf "${OK}    Hedef: root@${NEW_SERVER}${NC}\n"
else
    printf "${ER}[!!] donustur.sh bulunamadi! (/gidenyol veya /metmar altinda)${NC}\n"
fi

# ---------------------------------------------------------------
# 8. sil.sh config path duzelt
# ---------------------------------------------------------------
if [ -n "$SIL" ]; then
    sed -i "s|source /metmar/ayarlar\.conf|source ${CONF_DIR}/ayarlar.conf|g"   "$SIL"
    sed -i "s|source /gidenyol/ayarlar\.conf|source ${CONF_DIR}/ayarlar.conf|g" "$SIL"
    printf "${OK}[OK] sil.sh guncellendi: $SIL${NC}\n"
fi

echo ""

# ---------------------------------------------------------------
# 9. Yeni sunucuda klasor yapisi olustur (-n: stdin okuma)
# ---------------------------------------------------------------
printf "${WA}[..] Yeni sunucuda klasorler olusturuluyor ($HALISAHA_ADI)...${NC}\n"

ssh -n \
    -o StrictHostKeyChecking=no \
    -o UserKnownHostsFile=/dev/null \
    -p "$NEW_PORT" \
    "${NEW_USER}@${NEW_SERVER}" \
    "mkdir -p \
       /usr/local/nginx/html/mp4/${HALISAHA_ADI}/saha1/kamera1 \
       /usr/local/nginx/html/mp4/${HALISAHA_ADI}/saha1/kamera2 \
       /usr/local/nginx/html/mp4/${HALISAHA_ADI}/saha2/kamera3 \
       /usr/local/nginx/html/mp4/${HALISAHA_ADI}/saha2/kamera4 \
       /usr/local/nginx/html/mp4/${HALISAHA_ADI}/saha3/kamera5 \
       /usr/local/nginx/html/mp4/${HALISAHA_ADI}/saha3/kamera6 && \
     chmod -R 755 /usr/local/nginx/html/mp4/ && echo 'OK'" 2>/dev/null

if [ $? -eq 0 ]; then
    printf "${OK}[OK] Klasorler hazir: /usr/local/nginx/html/mp4/$HALISAHA_ADI/${NC}\n"
else
    printf "${WA}[!!] Klasorler rsync sirasinda olusturulacak${NC}\n"
fi

echo ""

# ---------------------------------------------------------------
# 10. Baglanti testi (-n: stdin okuma)
# ---------------------------------------------------------------
printf "${WA}[..] Baglanti testi...${NC}\n"
CONN_TEST=$(ssh -n \
    -o StrictHostKeyChecking=no \
    -o UserKnownHostsFile=/dev/null \
    -o ConnectTimeout=10 \
    -p "$NEW_PORT" \
    "${NEW_USER}@${NEW_SERVER}" \
    "echo OK" 2>/dev/null)

if [ "$CONN_TEST" = "OK" ]; then
    printf "${OK}[OK] SSH baglantisi basarili!${NC}\n"
else
    printf "${ER}[HATA] Baglanti testi basarisiz!${NC}\n"
fi

# ---------------------------------------------------------------
# 11. Cron yeniden baslat
# ---------------------------------------------------------------
systemctl restart crond.service &>/dev/null
printf "${OK}[OK] Cron yeniden baslatildi${NC}\n"

echo ""
echo "################################################################"
printf "${OK}  GUNCELLEME TAMAMLANDI!${NC}\n"
echo ""
printf "${OK}  Halisaha  : $HALISAHA_ADI${NC}\n"
printf "${OK}  donustur  : $DONUSTUR${NC}\n"
printf "${OK}  Yeni CDN  : root@${NEW_SERVER}${NC}\n"
echo ""
printf "${OK}  Video URL : https://${NEW_SERVER}/mp4/${HALISAHA_ADI}/...${NC}\n"
echo "################################################################"
echo ""
