#!/usr/bin/env python3

"Stage1: extract the main change and call stage2."

import argparse
import os
import sys

from depends_on.common import extract_depends_on, init_sensitive_strings, log


def main(args):
    "Main function."

    init_sensitive_strings()

    # parse the command line
    # -e or --extra-dir to add a directory to the list of directories to scan
    # -h or --help to display the help
    # 1 argument: the url of the change
    extra_dirs = []
    argparser = argparse.ArgumentParser()
    argparser.add_argument(
        "-e", "--extra-dir", action="append", dest="extra_dirs", default=[]
    )
    argparser.add_argument("url", nargs=1)
    parsed_args = argparser.parse_args(args[1:])
    url = parsed_args.url[0]
    extra_dirs = parsed_args.extra_dirs

    _, data = extract_depends_on(url, False, extra_dirs)
    top_dir = data["top_dir"]

    log(f"+ chdir {top_dir}")
    os.chdir(top_dir)

    stage2 = os.path.join(os.path.dirname(__file__), "depends_on_stage2")
    log(f"+ {stage2}")
    os.execl(stage2, "depends_on_stage2", "false")


if __name__ == "__main__":
    sys.exit(main(sys.argv))

# depends_on_stage1 ends here
