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

import argparse
import getpass
import sys
sys.path.append('/srv/vm')

from mgr import VMMgr

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

parser_update_login = subparsers.add_parser('update-login', help='Updates application login')
parser_update_login.set_defaults(action='update-login')
parser_update_login.add_argument('app', help='Application name')
parser_update_login.add_argument('login', help='Administrative login')
parser_update_login.add_argument('password', help='Administrative password')

parser_show_tiles = subparsers.add_parser('show-tiles', help='Shows application tiles in Portal')
parser_show_tiles.set_defaults(action='show-tiles')
parser_show_tiles.add_argument('app', help='Application name')

parser_hide_tiles = subparsers.add_parser('hide-tiles', help='Hides application tiles in Portal')
parser_hide_tiles.set_defaults(action='hide-tiles')
parser_hide_tiles.add_argument('app', help='Application name')

parser_start_app = subparsers.add_parser('start-app', help='Start application including it\'s dependencies')
parser_start_app.set_defaults(action='start-app')
parser_start_app.add_argument('app', help='Application name')

parser_stop_app = subparsers.add_parser('stop-app', help='Stops application including it\'s dependencies if they are not used by another running application')
parser_stop_app.set_defaults(action='stop-app')
parser_stop_app.add_argument('app', help='Application name')

parser_enable_autostart = subparsers.add_parser('enable-autostart', help='Enables application autostart')
parser_enable_autostart.set_defaults(action='enable-autostart')
parser_enable_autostart.add_argument('app', help='Application name')

parser_disable_autostart = subparsers.add_parser('disable-autostart', help='Disables application autostart')
parser_disable_autostart.set_defaults(action='disable-autostart')
parser_disable_autostart.add_argument('app', help='Application name')

parser_rebuild_issue = subparsers.add_parser('rebuild-issue', help='Rebuilds /etc/issue using current settings - used on VM startup')
parser_rebuild_issue.set_defaults(action='rebuild-issue')

parser_register_proxy = subparsers.add_parser('register-proxy', help='Rebuilds nginx proxy target for an application container')
parser_register_proxy.set_defaults(action='register-proxy')
parser_register_proxy.add_argument('app', help='Application name')

parser_unregister_proxy = subparsers.add_parser('unregister-proxy', help='Removes nginx proxy target for an application container')
parser_unregister_proxy.set_defaults(action='unregister-proxy')
parser_unregister_proxy.add_argument('app', help='Application name')

parser_update_host = subparsers.add_parser('update-host', help='Rebuilds domain structure of VM with new host name and new HTTPS port')
parser_update_host.set_defaults(action='update-host')
parser_update_host.add_argument('domain', help='Domain name')
parser_update_host.add_argument('port', help='HTTPS port')

parser_update_common = subparsers.add_parser('update-common', help='Updates common configuration properties used by multiple applications')
parser_update_common.set_defaults(action='update-common')
parser_update_common.add_argument('--email', help='Administrative e-mail address')
parser_update_common.add_argument('--gmaps-api-key', help='Google Maps API key')

parser_update_password = subparsers.add_parser('update-password', help='Updates password for HDD encryption and WSGI administration interface')
parser_update_password.set_defaults(action='update-password')

parser_create_selfsigned = subparsers.add_parser('create-selfsigned', help='Creates and installs selfsigned certificate for currently set domain')
parser_create_selfsigned.set_defaults(action='create-selfsigned')

parser_request_cert = subparsers.add_parser('request-cert', help='Requests and installs Let\'s Encrypt certificate for currently set domain')
parser_request_cert.set_defaults(action='request-cert')

parser_install_cert = subparsers.add_parser('install-cert', help='Installs user supplied certificate')
parser_install_cert.set_defaults(action='install-cert')
parser_install_cert.add_argument('certificate', help='Certificate file')
parser_install_cert.add_argument('key', help='Key file')

args = parser.parse_args()
mgr = VMMgr()
if args.action == 'update-login':
    mgr.update_login(args.app, args.login, args.password)
elif args.action == 'show-tiles':
    mgr.show_tiles(args.app)
elif args.action == 'hide-tiles':
    mgr.hide_tiles(args.app)
elif args.action == 'start-app':
    mgr.start_app(args.app)
elif args.action == 'stop-app':
    mgr.stop_app(args.app)
elif args.action == 'enable-autostart':
    mgr.enable_autostart(args.app)
elif args.action == 'disable-autostart':
    mgr.disable_autostart(args.app)
elif args.action == 'rebuild-issue':
    mgr.rebuild_issue()
elif args.action == 'register-proxy':
    mgr.register_proxy(args.app)
elif args.action == 'unregister-proxy':
    mgr.unregister_proxy(args.app)
elif args.action == 'update-host':
    mgr.update_host(args.domain, args.port)
elif args.action == 'update-common':
    mgr.update_common(args.email, args.gmaps_api_key)
elif args.action == 'update-password':
    oldpassword = getpass.getpass('Old password: ')
    newpassword = getpass.getpass('New password: ')
    mgr.update_password(oldpassword, newpassword)
elif args.action == 'create-selfsigned':
    mgr.create_selfsigned()
elif args.action == 'request-cert':
    mgr.request_cert()
elif args.action == 'install-cert':
    mgr.install_cert(args.certificate, args.key)
