#! /usr/bin/python3

import os
import sys
import argparse
import yunohost


def _parse_cli_args():
    """Parse additional arguments for the cli"""
    parser = argparse.ArgumentParser(add_help=False)
    parser.add_argument(
        "--output-as",
        choices=["json", "plain", "none"],
        default=None,
        help="Output result in another format",
    )
    parser.add_argument(
        "--debug",
        action="store_true",
        default=False,
        help="Log and print debug messages",
    )
    parser.add_argument(
        "--quiet", action="store_true", default=False, help="Don't produce any output"
    )
    parser.add_argument(
        "--timeout",
        type=int,
        default=None,
        help="Number of seconds before this command will timeout because it can't acquire the lock (meaning that another command is currently running), by default there is no timeout and the command will wait until it can get the lock",
    )
    # deprecated arguments
    parser.add_argument(
        "--plain", action="store_true", default=False, help=argparse.SUPPRESS
    )
    parser.add_argument(
        "--json", action="store_true", default=False, help=argparse.SUPPRESS
    )

    opts, args = parser.parse_known_args()

    # output compatibility
    if opts.plain:
        opts.output_as = "plain"
    elif opts.json:
        opts.output_as = "json"

    return (parser, opts, args)


# Stupid PATH management because sometimes (e.g. some cron job) PATH is only /usr/bin:/bin ...
default_path = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
if os.environ["PATH"] != default_path:
    os.environ["PATH"] = default_path + ":" + os.environ["PATH"]

# Main action ----------------------------------------------------------

if __name__ == "__main__":
    if os.geteuid() != 0:
        sys.stderr.write(
            "\033[1;31mError:\033[0m yunohost command must be "
            "run as root or with sudo.\n"
        )
        sys.exit(1)

    parser, opts, args = _parse_cli_args()

    # Execute the action
    yunohost.cli(
        debug=opts.debug,
        quiet=opts.quiet,
        output_as=opts.output_as,
        timeout=opts.timeout,
        args=args,
        parser=parser,
    )
