[source]
same_as = "../drivers"

[build]
template = "custom"
script = """
BINS=(
    fbcond
    inputd
    nvmed
    pcid
    vesad
    lived
)

virt_bins()
{
    BINS+=(virtio-blkd virtio-gpud)
}

x86_common_bins()
{
    BINS+=(ahcid ided ps2d vesad)
    virt_bins
}

aarch64_bins()
{
    case "${BOARD}" in
        raspi3b | raspi3bp)
            BINS+=(bcm2835-sdhcid)
        ;;
        *)
        #qemu-virt
            virt_bins
        ;;
    esac
}

case "${TARGET}" in
    i686-unknown-redox)
        x86_common_bins
        ;;
    x86_64-unknown-redox)
        x86_common_bins
        BINS+=(acpid)
        ;;
    aarch64-unknown-redox)
        aarch64_bins
        ;;
    *)
        ;;
esac

mkdir -pv "${COOKBOOK_STAGE}/bin" "${COOKBOOK_STAGE}/lib/drivers"
export CARGO_PROFILE_RELEASE_OPT_LEVEL=s
export CARGO_PROFILE_RELEASE_PANIC=abort
"${COOKBOOK_CARGO}" build --release \
    --manifest-path "${COOKBOOK_SOURCE}/Cargo.toml" \
    $(for bin in "${BINS[@]}"; do echo "-p" "${bin}"; done)
for bin in "${BINS[@]}"
do
    case "${bin}" in
      pcid | fbcond | inputd | vesad | lived | ps2d | acpid)
        cp -v "target/${TARGET}/release/${bin}" "${COOKBOOK_STAGE}/bin"
        ;;
      *)
        cp -v "target/${TARGET}/release/${bin}" "${COOKBOOK_STAGE}/lib/drivers"
        ;;
    esac
done

mkdir -pv "${COOKBOOK_STAGE}/etc/pcid"
cp -v "${COOKBOOK_SOURCE}/initfs.toml" "${COOKBOOK_STAGE}/etc/pcid/initfs.toml"
"""
