import urllib
import os
import subprocess
import sys

# URLs for the MSI and MST files
msi_url = "https://example.com/setup.msi"   # <-- replace with your actual MSI URL
mst_url = "https://example.com/custom.mst"  # <-- replace with your actual MST URL

# Local paths to save downloaded files
msi_path = r"C:\Temp\setup.msi"
mst_path = r"C:\Temp\custom.mst"

# Create folder if it doesn't exist
if not os.path.exists(os.path.dirname(msi_path)):
    os.makedirs(os.path.dirname(msi_path))

# Download MSI
try:
    print("Downloading MSI...")
    urllib.urlretrieve(msi_url, msi_path)
    print("MSI downloaded successfully.")
except Exception as e:
    print("Failed to download MSI:", e)
    sys.exit(1)

# Download MST
try:
    print("Downloading MST...")
    urllib.urlretrieve(mst_url, mst_path)
    print("MST downloaded successfully.")
except Exception as e:
    print("Failed to download MST:", e)
    sys.exit(1)

# Install MSI with MST
cmd = [
    "msiexec.exe",
    "/i", msi_path,
    "TRANSFORMS={}".format(mst_path),
    "/qn",
    "/norestart"
]

try:
    print("Installing MSI with MST...")
    result = subprocess.call(cmd)
    if result == 0:
        print("Installation completed successfully.")
    else:
        print("Installation failed with exit code:", result)
except Exception as e:
    print("Error during installation:", e)
    sys.exit(1)
