#! /usr/bin/env python3

#
#  Copyright 2016 - 2026 gary@drinkingtea.net
#
#  This Source Code Form is subject to the terms of the Mozilla Public
#  License, v. 2.0. If a copy of the MPL was not distributed with this
#  file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

import shutil
import subprocess
import sys


def run(args: list[str]):
    if subprocess.run(args).returncode != 0:
        sys.exit(1)


studio_path = sys.argv[1]
project_dir = sys.argv[2]
project_name = sys.argv[3]
project_bin = f'build/gba-release/bin/{project_name}.bin'
project_gba = f'{project_name}.gba'
project_manifest = f'{project_name.lower()}-manifest.json'

shutil.copyfile(project_bin, project_gba)
run([
        studio_path,
        'cmd',
        project_dir,
        'net.drinkingtea.studio',
        'pack',
        '-rom-bin', project_gba,
        '-manifest', project_manifest])
run(['gbafix', project_gba])
