#!/usr/bin/env python3
import sys
import subprocess
import zipfile
import shutil

from pathlib import Path

def main():
    pl_path = None
    home = Path.home()

    # Check all (most) possible prism launcher locations
    possible_dirs = [ r"%APPDATA%/PrismLauncher", r"%HOMEPATH%\scoop\persist\prismlauncher", f"{home}/Library/Application Support/PrismLauncher", f"{home}/.local/share/PrismLauncher", f"{home}/.var/app/org.prismlauncher.PrismLauncher/data/PrismLauncher" ]
    for path in possible_dirs:
        if Path(path).is_dir():
            pl_path = path

    # Prism launcher doesn't create necessary folders till it's launched once
    if not pl_path:
        sys.exit("ERROR: PrismLauncher not found, install / launch and try again")

    # Same story here
    mods_path = Path(pl_path + "/instances/1.21.11/minecraft/mods")
    if not mods_path.is_dir():
        sys.exit("ERROR: Add fabric 1.21.11 and launch instance once")

    if not shutil.which("curl"):
        print("Please install curl:")
        print(" - sudo pacman -Syu curl")
        print(" - nix-shell -p curl")

    print("Downloading mods")
    subprocess.run("curl -q -o mods.zip https://dl.kan.sh/mods.zip", shell=True)

    print("Extracting mods")
    with zipfile.ZipFile("mods.zip") as f:
        f.extractall(mods_path)

    mods = Path("mods.zip")
    mods.unlink()

    print("Done!!")


if __name__ == "__main__":
    main()
