#!/usr/bin/python3
# -*- coding: utf-8 -*-

import argparse

from vmmgr import config, vmmgr

def register_app(app, host, login, password):
    # Used by package install.sh script
    vmmgr.register_app(app, host, login, password)

def unregister_app(app):
    # Used by package uninstall.sh script
    vmmgr.unregister_app(app)

def rebuild_issue():
    # Used by inittab on VM startup
    vmmgr.rebuild_issue()

parser = argparse.ArgumentParser(description='VM application manager')
parser.set_defaults(action=None)
subparsers = parser.add_subparsers()

parser_register_app = subparsers.add_parser('register-app')
parser_register_app.set_defaults(action=register_app)
parser_register_app.add_argument('app', help='Application name')
parser_register_app.add_argument('host', help='Application subdomain')
parser_register_app.add_argument('login', nargs='?', help='Admin login')
parser_register_app.add_argument('password', nargs='?', help='Admin password')

parser_unregister_app = subparsers.add_parser('unregister-app')
parser_unregister_app.set_defaults(action=unregister_app)
parser_unregister_app.add_argument('app', help='Application name')

parser_rebuild_issue = subparsers.add_parser('rebuild-issue')
parser_rebuild_issue.set_defaults(action=rebuild_issue)

args = parser.parse_args()
if args.action is register_app:
    register_app(args.app, args.host, args.login, args.password)
elif args.action is unregister_app:
    unregister_app(args.app)
elif args.action is rebuild_issue:
    rebuild_issue()
else:
    parser.print_usage()
