import shutil
import sys
from pathlib import Path
from platform import system
from subprocess import CalledProcessError, run

project_dir = Path(".")
assets_dir = project_dir / "assets"
main_py = project_dir / "main.py"
icon_file = assets_dir / ("duck.ico" if system() == "Windows" else "duck.png")

for dir_to_remove in ["dist", "build"]:
    dir_path = project_dir / dir_to_remove
    if dir_path.exists():
        shutil.rmtree(dir_path)

pyinstaller_cmd = [
    "pyinstaller", "--onefile", "--windowed",
    f"--add-data={assets_dir}{';' if system() == 'Windows' else ':'}{assets_dir}",
    f"--name=DuckTrack", f"--icon={icon_file}", str(main_py)
]

try:
    run(pyinstaller_cmd, check=True)
except CalledProcessError as e:
    print("An error occurred while running PyInstaller:", e)
    sys.exit(1)