#!/usr/bin/env python3

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

import os
import sys

from depends_on.common import extract_depends_on


def main(args):
    "Main function."

    if len(args) != 2 or args[1] in ("-h", "--help"):
        print(f"Usage: {args[0]} <CHANGE URL>", file=sys.stderr)
        return 0 if len(args) > 1 and args[1] in ("-h", "--help") else 1

    main_dir = os.getcwd()

    _, top_dir = extract_depends_on(args[1], False)

    print(f"+ chdir {top_dir}", file=sys.stderr)
    os.chdir(top_dir)

    stage2 = os.path.join(os.path.dirname(__file__), "depends_on_stage2")
    print(f"+ {stage2}", file=sys.stderr)
    os.execl(stage2, "depends_on_stage2", "false")


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

# stage1.py ends here
