#!/bin/bash

# Pfad zur Ausgabedatei (anpassen an Webroot)
TARGET_FILE="/var/www/jussef_vod/ping.txt"

# --- Initialisierung ---
START_TIME=$(date +%s)
UPDATE_COUNT=0

# Funktion zur Berechnung der Laufzeit
get_uptime() {
    local now=$(date +%s)
    local diff=$((now - START_TIME))
    local days=$((diff / 86400))
    local hours=$(( (diff % 86400) / 3600 ))
    local mins=$(( (diff % 3600) / 60 ))
    local secs=$((diff % 60))

    printf "%dd %02dh %02dm %02ds" $days $hours $mins $secs
}

# Bildschirm leeren für saubere Ausgabe
clear

while true; do
    CURRENT_TIMESTAMP=$(date +%s)

    # Zeitstempel in Datei schreiben
    echo "$CURRENT_TIMESTAMP" > "$TARGET_FILE"
    ((UPDATE_COUNT++))

    # Ausgabe in der Konsole (\r sorgt dafür, dass die Zeile überschrieben wird)
    echo -ne "\r\033[K" # Zeile löschen
    echo -ne "Status: [ RUNNING ] | "
    echo -ne "Timestamp: $CURRENT_TIMESTAMP | "
    echo -ne "Updates: $UPDATE_COUNT | "
    echo -ne "Laufzeit: $(get_uptime) | "
    echo -ne "Start: $(date -d @$START_TIME '+%H:%M:%S')"

    sleep 1
done
