import argparse
import subprocess
import sys

from dev_tools.build.freeze.build import freeze_build_app
from dev_tools.build.package.build import package_build
from dev_tools.build.source.archive import package_source_archive
from dev_tools.utility.path import ROOT


def main():
    parser = argparse.ArgumentParser("Clarabot Nano - app builder utility")
    parser.add_argument("--development", action="store_true", default=False)
    parser.add_argument("--skip_package", action="store_true", default=False)
    parser.add_argument("--prefer_archive_package", action="store_true", default=False)
    parser.add_argument("--skip_signing", action="store_true", default=False)
    parser.add_argument("--skip_src_archive", action="store_true", default=False)
    parser.add_argument("--skip_pip_audit", action="store_true", default=False)
    args = parser.parse_args()

    if args.skip_pip_audit:
        print("WARNING: skipping audit step!")
    else:
        print("Audit step...")
        try:
            subprocess.check_call([sys.executable, "-m", "pip_audit"], cwd=str(ROOT))
        except subprocess.CalledProcessError:
            if input("Ignore the audit issues? (yes/no)").lower() != "yes":
                raise

    print("Freeze step...")
    frozen_dir, frozen_version = freeze_build_app(args.development, args.skip_signing)

    if args.skip_package:
        print("WARNING: skipping package step!")
    else:
        print("Package step...")
        package_build(args.development, frozen_dir, frozen_version, args.prefer_archive_package, args.skip_signing)

    if args.development or args.skip_src_archive:
        print("WARNING: skipping source archive creation!")
    else:
        package_source_archive(frozen_version)


if __name__ == "__main__":
    main()
