#!/usr/bin/env python3

"""
A wrapper to convert `sigstore-conformance` CLI protocol invocations to match `sigstore-python`.
"""

import os
import sys

SUBCMD_REPLACEMENTS = {
    "sign-bundle": "sign",
    "verify-bundle": "verify",
}

ARG_REPLACEMENTS = {
    "--certificate-identity": "--cert-identity",
    "--certificate-oidc-issuer": "--cert-oidc-issuer",
}

# Trim the script name.
fixed_args = sys.argv[1:]

# Substitute incompatible subcommands.
subcmd = fixed_args[0]
if subcmd in SUBCMD_REPLACEMENTS:
    fixed_args[0] = SUBCMD_REPLACEMENTS[subcmd]

# Replace incompatible flags.
fixed_args = [
    ARG_REPLACEMENTS[arg] if arg in ARG_REPLACEMENTS else arg for arg in fixed_args
]

# Fix-up the subcommand: the conformance suite uses `verify`, but
# `sigstore` requires `verify identity` for identity based verifications.
subcommand, *fixed_args = fixed_args
if subcommand == "sign":
    fixed_args = ["sigstore", "sign", *fixed_args]
elif subcommand == "verify":
    fixed_args = ["sigstore", "verify", "identity", *fixed_args]
else:
    raise ValueError(f"unsupported subcommand: {subcommand}")

os.execvp("sigstore", fixed_args)
