import urllib.request
import json
import zipfile
import os
import ssl

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

url = "https://api.github.com/repos/ggerganov/llama.cpp/releases/latest"
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
try:
    with urllib.request.urlopen(req, context=ctx) as response:
        data = json.loads(response.read().decode())

    download_url = None
    for asset in data['assets']:
        if "bin-win-cuda-cu12.2.0-x64.zip" in asset['name'] or ("bin-win-cu" in asset['name'] and asset['name'].endswith(".zip") and "x64" in asset['name']):
            download_url = asset['browser_download_url']
            break
            
    if download_url:
        print(f"Downloading {download_url}...")
        zip_path = "llama.zip"
        with urllib.request.urlopen(download_url, context=ctx) as resp, open(zip_path, 'wb') as out_file:
            out_file.write(resp.read())
        print("Extracting to 'llama_bin'...")
        with zipfile.ZipFile(zip_path, 'r') as zip_ref:
            zip_ref.extractall("llama_bin")
        print("Done extracting.")
        os.remove(zip_path)
    else:
        print("Could not find the target zip. Available assets:")
        for asset in data['assets']:
            print(" -", asset['name'])
except Exception as e:
    print(f"Error: {e}")
