#!/usr/bin/env python3
"""Absolute path of the SELECTED soundtrack candidate, or nothing.

Generating a track does not select it, and assemble only mixes the selected
one — so the selection is what the delivery copies, not merely the first
candidate that happens to exist.
"""
import json
import os
import sys

project = sys.argv[1]
path = os.path.join(project, "artifacts", "soundtrack.json")
if not os.path.isfile(path):
    raise SystemExit(0)
try:
    artifact = json.load(open(path, encoding="utf-8"))
except json.JSONDecodeError:
    raise SystemExit(0)
selected = artifact.get("selected")
for candidate in artifact.get("candidates") or []:
    if selected and candidate.get("backendId") != selected:
        continue
    rel = candidate.get("path")
    if rel:
        print(os.path.join(project, rel))
        break
