import os
import subprocess


def install_rad_ci(ctx):
    runcmd_prepend_to_path = globals()["runcmd_prepend_to_path"]
    srcdir = globals()["srcdir"]

    default_target = os.path.join(srcdir, "target")
    target = os.environ.get("CARGO_TARGET_DIR", default_target)
    bindir = os.path.join(target, "debug")
    assert os.path.exists(os.path.join(bindir, "rad-ci"))
    runcmd_prepend_to_path(ctx, dirname=bindir)


def install_vm_image(ctx, filename=None):
    assert "/" not in filename

    image = os.environ.get("IMAGE")
    if image is None:
        raise Exception("set IMAGE in environment to path to Ambient image to use")
    if not os.path.exists(image):
        raise Exception("$IMAGE MUST point to an existing file")

    os.symlink(image, filename)
