#!/usr/bin/python3.6

import argparse
import os.path
from subprocess import run, DEVNULL
import sys
import urllib.request


def main() -> None:
    argparser = argparse.ArgumentParser(description="Retrace Server plugin checker")
    argparser.add_argument("PLUGIN", type=str, help="Plugin name")
    argparser.add_argument("VERSION", type=str, help="Release version")
    argparser.add_argument("ARCHITECTURE", type=str, help="CPU architecture")
    argparser.add_argument("--plugin-dir", default="/usr/share/retrace-server/plugins",
                           help="Path to plugins.")
    argparser.add_argument("--only-valid", action="store_true",
                           help="Print only valid mirrors")
    argparser.add_argument("--only-invalid", action="store_true",
                           help="Print only invalid mirrors")
    argparser.add_argument("--first-valid", action="store_true",
                           help="Print first valid mirror per repository.")

    args = argparser.parse_args()

    # Prepend path and import plugin
    sys.path.insert(0, args.plugin_dir)

    try:
        plugin = __import__(args.PLUGIN)
    except Exception as ex:
        print("Plugin could not be loaded. Use --plugin-dir if in another location.")
        print(f"Encountered error: {ex}")
        sys.exit(1)
    except KeyboardInterrupt:
        print("Interrupted")
        sys.exit(0)


    verbose = not(args.first_valid or args.only_valid or args.only_invalid)

    for i, r in enumerate(plugin.repos):
        if verbose:
            print("Repository {0} - testing".format(i))

        repo_ok = False
        for p in r:
            path_ok = False
            mirror_path = p.replace("$VER", args.VERSION).replace("$ARCH", args.ARCHITECTURE)
            if p.startswith("http:") or p.startswith("https:") or p.startswith("ftp:"):
                try:
                    with urllib.request.urlopen(mirror_path) as _response:
                        # Don't do anything. We only want to know whether the URL
                        # works.
                        pass
                    path_ok = True
                except Exception:
                    pass
            elif p.startswith("rsync:"):
                rsync = run(["rsync", "--list-only", mirror_path], stdout=DEVNULL, stderr=DEVNULL, check=False)
                path_ok = not rsync.returncode
            else:
                path_ok = os.path.exists(mirror_path)

            repo_ok = repo_ok or path_ok
            if verbose:
                print("\t[ {0} ] {1}".format(" OK " if path_ok else "FAIL", mirror_path))
            else:
                if args.first_valid and path_ok:
                    print(mirror_path)
                    break
                if args.only_valid and path_ok:
                    print(mirror_path)
                elif args.only_invalid and not path_ok:
                    print(mirror_path)

        if verbose:
            print("Repository {1} - {0}".format("OK" if repo_ok else "FAIL", i))

if __name__ == "__main__":
    main()
