#!/usr/bin/env python

import argparse
import os
import sys

from system_status_server.infodaemon import InfoDaemon, InfoServer

parser = argparse.ArgumentParser()
start_group = parser.add_mutually_exclusive_group(required = True)
start_group.add_argument("command", help = "command", choices = ['start', 'stop', 'restart', 'status'], nargs='?')
start_group.add_argument("-c", "--console", help = "run in the console, not forking", action='store_true')
parser.add_argument("--config", help = "config file", type = argparse.FileType('r'))
parser.add_argument("--host", help = "listening host", type = str, default = '0.0.0.0')
parser.add_argument("--port", help = "listening port", type = int, default = 35280)

args = parser.parse_args()

config = dict(listen = dict())

if args.config:
    try:
        with open(config_file) as file:
            self.config.update(json.loads(file.read()))
    except ValueError as e:
        print(e)
        sys.exit(1)
else:
    config['listen'].update(dict(host = args.host, port = args.port))

if args.console:
    server = InfoServer(config)
    server.serve()
else:
    daemon = InfoDaemon('/tmp/system-status-server.pid', config, '/tmp/system-status-server.log')
    command = getattr(daemon, args.command)
    print(command())
