import pyaxml
import click


@click.command()
@click.argument("input_file")
@click.argument("output_file")
@click.argument("activity_name")
@click.argument("new_activity_name")
def copy_axmlfile(input_file, output_file, activity_name, new_activity_name):
    filename = input_file
    with open(filename, "rb") as f:
        # Read AXML and get XML object
        axml, _ = pyaxml.AXML.from_axml(f.read())
        xml = axml.to_xml()
        android_name = "{http://schemas.android.com/apk/res/android}name"
        for activity in xml.findall(
            f"./application/activity/[@{android_name}='{activity_name}']"
        ):
            activity.attrib[android_name] = new_activity_name
        # Reencode XML to AXML
        axml_object = pyaxml.axml.AXML()
        axml_object.from_xml(xml)
    # Rewrite the file
    with open(output_file, "wb") as f:
        f.write(axml_object.pack())


if __name__ == "__main__":
    copy_axmlfile()
