import requests
import os
import sys
import subprocess
import time
# ============== UPDATE SETTINGS ==============
CURRENT_VERSION = "0.0.0"
BASE_URL = "https://raw.githubusercontent.com/OmerFarukYerebakan/update/HEAD/"
VERSION_URL = BASE_URL + "version.txt?t=" + str(int(time.time()))
APP_URL     = BASE_URL + "app.py?t=" + str(int(time.time()))
APP_NAME  = os.path.basename(__file__)
TEMP_NAME = "app_new.py"
# =============================================
def get_remote_version():
    r = requests.get(VERSION_URL, timeout=5)
    r.raise_for_status()
    return r.text.strip()
def download_new_version():
    print("🔄 Downloading New Version...")
    r = requests.get(APP_URL, timeout=10)
    r.raise_for_status()
    with open(TEMP_NAME, "wb") as f:
        f.write(r.content)
def replace_and_restart():
    print("♻ Applying Update...")
    time.sleep(1)
    os.replace(TEMP_NAME, APP_NAME)
    print("🚀 Restarting...")
    subprocess.Popen([sys.executable, APP_NAME])
    sys.exit()
def check_update():
    try:
        remote_version = get_remote_version()
        print(f"Local version: {CURRENT_VERSION}")
        print(f"Remote version : {remote_version}")
        if remote_version != CURRENT_VERSION:
            download_new_version()
            replace_and_restart()
        else:
            print("✅ No Update Available.")
    except Exception as e:
        print("⚠ Update Failed:", e)
# ============== MAIN ==============
if __name__ == "__main__":
    check_update()
    while True:
        print("Program Running...")
        time.sleep(5)
