#!/usr/bin/env python

import os
import sys
import subprocess
import argparse
from slg_dev_ops import install_nginx


def get_arguments():
    parser = argparse.ArgumentParser()

    parser.add_argument(
        '-o', '--os', default="ubuntu",
        help="The target OS, (ubuntu or debian)")

    parser.add_argument(
        '-c', '--codename', default="bionic",
        help="The target codename (ubuntu: trusty, xenial, artful, bionic || debian: jessie, stretch)")

    bools = parser.add_argument_group()

    bools.add_argument('-s', '--suppress_start, action="store_false",
                       help="If tick added, will not start nginx on completion")

    args = parser.parse_args()

    return args


def test_args(args):
    os = args.os
    codename = args.codename
    if os == 'ubuntu' or args.os == 'debian':
        pass
    else:
        return 'This script only accepts "ubuntu" or "debian" as OS'

    if os == 'ubuntu' and codename in [
            'trusty', 'xenial', 'artful', 'bionic']:
        pass
    elif os == 'debian' and codename in ['jessie', 'stretch']:
        pass
    else:
        return 'Please input an acceptable codename: run slg_dev_ops -h to see acceptable inputs per OS'


args = get_arguments()
not_ready = test_args(args)
if not_ready:
    print(not_ready)
else:

    # root check; this created a process that locked root access for some reason. We'll just rely on the bash commands sudo instead of a python "with" command.
    # if os.geteuid() == 0:
    install_nginx(os_=args.os, codename=args.codename,
                  start=args.suppress_start)
    # else:
    #     print("We're not root.")
    #     subprocess.call(['sudo', *sys.argv])
    #     sys.exit()
