#!/bin/bash
########################################################################
# Gidenyol Bilisim - yesilsaha.net
# ESKİ HALE AL - Record interval orijinal degerlerine doner
# Kullanim: bash eski_hale_al.sh
########################################################################

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

NGINX_CONF="/usr/local/nginx/conf/nginx.conf"

clear
echo "################################################################"
echo "#   ESKİ HALE AL - Record interval orijinaline donuyor        #"
echo "################################################################"
echo ""

if [ ! -f "$NGINX_CONF" ]; then
    printf "\033[0;31m[HATA] $NGINX_CONF bulunamadi!\033[0m\n"
    exit 1
fi

# Oncelikle yedekten geri yuklemeyi dene
if [ -f "${NGINX_CONF}.orijinal" ]; then
    cp "${NGINX_CONF}.orijinal" "$NGINX_CONF"
    printf "${OK}[OK] Orijinal yedekten geri yuklendi${NC}\n"
else
    # Yedek yoksa manuel olarak degerlerini eski haline getir
    printf "${WA}[..] Yedek bulunamadi, degerler manuel olarak geri aliniyor...${NC}\n"

    # Once hepsini 120s olan yerleri sirayla orijinal degerlerine dondur
    # nginx.conf'taki siralama: kamera1, kamera2, kamera3, kamera4, kamera5, kamera6
    # Orijinal degerler: 3490 3500 3510 3520 3530 3550

    python3 - << 'PYEOF'
import re

conf_path = "/usr/local/nginx/conf/nginx.conf"
with open(conf_path, "r") as f:
    content = f.read()

# Orijinal degerler siraya gore
original_values = [3490, 3500, 3510, 3520, 3530, 3550]

# Her record_interval 120s satirini sirayla orijinal degerle degistir
count = 0
def replace_nth(match):
    global count
    if count < len(original_values):
        val = original_values[count]
        count += 1
        return f"record_interval {val}s;"
    return match.group(0)

new_content = re.sub(r'record_interval \d+s;', replace_nth, content)

with open(conf_path, "w") as f:
    f.write(new_content)

print(f"[OK] {count} kamera icin degerler geri alindi")
PYEOF

    if [ $? -ne 0 ]; then
        # Python yoksa sed ile tek tek degistir
        # Ilk eslesen 120s -> 3490s, ikinci -> 3500s, vs.
        sed -i '0,/record_interval 120s;/s/record_interval 120s;/record_interval 3490s;/' "$NGINX_CONF"
        sed -i '0,/record_interval 120s;/s/record_interval 120s;/record_interval 3500s;/' "$NGINX_CONF"
        sed -i '0,/record_interval 120s;/s/record_interval 120s;/record_interval 3510s;/' "$NGINX_CONF"
        sed -i '0,/record_interval 120s;/s/record_interval 120s;/record_interval 3520s;/' "$NGINX_CONF"
        sed -i '0,/record_interval 120s;/s/record_interval 120s;/record_interval 3530s;/' "$NGINX_CONF"
        sed -i '0,/record_interval 120s;/s/record_interval 120s;/record_interval 3550s;/' "$NGINX_CONF"
    fi
fi

# Kontrol - kac tane kamera var ve degerler ne
printf "${OK}Mevcut record_interval degerleri:${NC}\n"
grep "record_interval" "$NGINX_CONF" | sed 's/^[[:space:]]*/  /'

# donustur.sh'deki kucuk dosya silme satirini geri ac
for DPATH in /metmar/donustur.sh /gidenyol/donustur.sh; do
    if [ -f "$DPATH" ]; then
        sed -i 's|^#TEST_DISABLED# \(find /usr/local/nginx/html/mp4/.*-delete\)|\1|g' "$DPATH"
        printf "${OK}[OK] $DPATH kucuk dosya silme tekrar aktif${NC}\n"
    fi
done

# Nginx yeniden baslat
systemctl restart nginx 2>/dev/null || service nginx restart 2>/dev/null
printf "${OK}[OK] Nginx yeniden baslatildi${NC}\n"

echo ""
echo "################################################################"
printf "${OK}  TAMAMLANDI! Orijinal surelerine donuldu.${NC}\n"
echo "################################################################"
echo ""
