import pyaxml
import click


@click.command()
@click.argument("input_file")
@click.argument("output_file")
def copy_arsc(input_file, output_file):
    with open(input_file, "rb") as f:
        arsc = pyaxml.ARSC.from_axml(f.read())

    pkg = arsc.get_packages()[0]

    try:
        res_id, value_data = arsc.get_id_public(pkg, "xml", "network_security_config")
        path = pyaxml.StringBlocks(proto=arsc.proto.stringblocks).decode_str(value_data)
        print(f"Found: id={res_id:#010x}, path={path}")
    except KeyError:
        print("network_security_config not found in xml, creating it.")
        res_id = arsc.add_id_public(
            pkg,
            "xml",
            "network_security_config",
            "res/xml/network_security_config.xml",
        )
        arsc.compute()
        print(f"Created: id={res_id:#010x}")

    with open(output_file, "wb") as fo:
        fo.write(arsc.pack())

    print(f"Written to {output_file}")


if __name__ == "__main__":
    copy_arsc()
