#!/usr/bin/env python3

"""deploy butler to a host."""

import os.path

import cmd


def deploy(host, directory):
    """deploy butler to the host and make the server run in directory."""
    butler_directory = os.path.join(directory, 'butler')
    print('cloning')
    cloned = cmd.clone(host, directory)
    if cloned:
        print('clone was performed')
    else:
        print('pull was performed')
    print('building')
    cmd.build(host, butler_directory)
    print('generating')
    cmd.gen(host, butler_directory)
    print('checking if server is running')
    if cloned:
        print('restarting server since clone performed')
        cmd.restart_server(host, butler_directory)
    elif cmd.is_server_running(host):
        print('server is already running')
    else:
        print('starting server')
        cmd.start_server(host, butler_directory)


if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('host', help='host the server is being deployed to')
    parser.add_argument('directory',
                        help='directory to clone into')
    args = parser.parse_args()
    deploy(args.host, args.directory)
