#!/usr/bin/env python
#
# Print the options last passed to scons
#
import argparse
import configparser
import os
import sys

parser = argparse.ArgumentParser(description="Tell us how scons was invoked")
parser.add_argument(
    "configFile", type=str, nargs="?", default=None, help="The file with the information we need"
)
parser.add_argument("--cc", type=str, default="gcc", help="Use this compiler if build.cfg is unavailable")
parser.add_argument(
    "--opt", type=int, default=0, help="Use this optimisation level if build.cfg is unavailable"
)
parser.add_argument("--quiet", "-q", action="store_true", help="Don't generate any output")

args = parser.parse_args()
dirName = "."
if args.configFile and os.path.isdir(args.configFile):
    dirName, args.configFile = args.configFile, None

if not args.configFile:
    args.configFile = os.path.join(dirName, ".sconf_temp", "build.cfg")

cc = args.cc
opt = args.opt

if os.path.exists(args.configFile):
    config = configparser.ConfigParser()
    try:
        config.read(args.configFile)

        cc = config.get("Build", "cc")
        opt = config.get("Build", "opt")
    except Exception as e:
        if not args.quiet:
            print(f"File {args.configFile}: error {e}", file=sys.stderr)

else:
    if not args.quiet:
        print(f"File {args.configFile} doesn't exist", file=sys.stderr)

print(f"cc={cc} opt={opt}")
