#!/usr/bin/env python3

import os
import pkg_resources
import random
import semver
import sys
import subprocess

try:
    from os2borgerpc.client.updater import (  # noqa: F401
        get_newest_client_version,
        update_client,
        update_client_test,
    )
    from os2borgerpc.client.jobmanager import update_and_run  # noqa: E402
except ImportError:
    # If any of the imports fail, reinstall the client
    try:
        subprocess.check_call(
            [
                sys.executable,
                "-m",
                "pip",
                "install",
                "--force-reinstall",
                "os2borgerpc-client",
            ]
        )
        sys.exit(0)
    except subprocess.CalledProcessError:
        print("Restoring client failed\n", file=sys.stderr)

# Higher values: Check for updates less often
UPDATE_FREQUENCY = 200

if not os.geteuid() == 0:
    sys.exit("\n Only root can run this program. \n")

# We are root!

# Keep this in sync with package name in setup.py
# This is deliberatly duplicated here from jobmanager.py to not be
# dependent on jobmanager.py at this point
OS2BORGERPC_CLIENT_VERSION = pkg_resources.get_distribution(
    "os2borgerpc_client"
).version

# Perform an update-check with a random interval.
if random.randint(1, UPDATE_FREQUENCY) == 1:
    print("Checking for new update to client.")
    newest_client_version = get_newest_client_version()
    print(
        f"Newest client version: {newest_client_version}, "
        f"Installed version: {OS2BORGERPC_CLIENT_VERSION}"
    )
    # update_client_test()
    if semver.compare(newest_client_version, OS2BORGERPC_CLIENT_VERSION) == 1:
        print("Updating client, please re-run jobmanager.")
        update_client()

update_and_run()
