"""
OPNSpeedGuard
By KIP

:3
"""

import argparse
import os
import pathlib

def run():
    from . import cli_config
    parser = argparse.ArgumentParser(prog="opnspeedguard",
                                     description="Automatically switches an OPNSense's WireGuard connection to a fast peer, for use in road warrior setups. (c) KIP")
    parser.add_argument("--genconfig", action="store_true",
                        help="Generate a template config.json file to be used with -config")
    parser.add_argument("--config", type=str,
                        help="Path to REQUIRED config json file that controls auto connect behavior. If not specified, the program will try to use config.json in the working directory.")
    parser.add_argument("--api", type=str,
                        help="Path to REQUIRED api_key.txt file generated by OPNSense. If not specified, the program will try to use api_key.txt in the working directory.")

    args = parser.parse_args()

    print("== PRE-RUN INFO ==")
    # Gen config
    if args.genconfig:
        if args.config is not None:
            genconfig_location = args.config
            # GEN CONFIG
            print(f"Generating template config file to {genconfig_location}...")
        else:
            genconfig_location = "config.json"
            print(f"Generating template config file to {os.getcwd()}...")

        from .config import genconfig
        genconfig(genconfig_location)
        exit(2)

    # config
    if args.config is None:
        print("--config is not specified. Fallback to config.json in working directory.")
        if not os.path.exists("config.json"):
            print(
                f"[ERR] Cannot find fallback config.json. Either set --config, or place config.json at {os.getcwd()}.")
            print(
                "You can generate a template config file using opnspeedguard --genconfig --config=output_file_location")
            exit(-1)
        else:
            cli_config.config_path = "config.json"
    else:
        if not os.path.exists(args.config):
            print(f"[ERR] Cannot find config file located at {args.config}. Check your arguments.")
            exit(-1)
        else:
            cli_config.config_path = args.config

    # api key
    if args.api is None:
        print("--api is not specified. Fallback to api_key.txt in working directory.")
        if not os.path.exists("api_key.txt"):
            print(f"[ERR] Cannot find fallback api_key.txt. Either set --api, or place api_key.txt at {os.getcwd()}")
            print(
                "This file can be retrieved from the OPNSense web panel, via System > Access > Users > Ticket icon (Create and Download API keys...).")
            exit(-1)
        else:
            cli_config.api_path = "api_key.txt"
    else:
        if not os.path.exists(args.api):
            print(f"[ERR] Cannot find api key file located at {args.api}. Check your arguments.")
            exit(-1)
        else:
            cli_config.api_path = args.api

    print("====")
    from . import auto_connect
    auto_connect.auto_connect()

if __name__ == "__main__":
    run()