__author__ = '@tomereyz'

import argparse
import os
from DockerFile import DockerFile
from ChallengeBuild import ChallengeBuild


def main(port, arch, docker_name, aslr, directory=None, no_cache=False,
         build_run=False):
    if not directory:
        directory = os.getcwd()

    with ChallengeBuild(directory=directory, arch=arch, aslr=aslr) as challenge_handle:
        if build_run:
            if not docker_name:
                raise Exception("missing docker name")
        with DockerFile(sources=challenge_handle.sources, docker_name=docker_name, no_cache=no_cache,
                        build_run=build_run, port=port, arch=arch, aslr=aslr) as docker_handle:
            print docker_handle.docker_name
            interactive = raw_input('execute `rm -f ~/.ssh/known_hosts` y/n?')
            if interactive == 'y':
                print 'rm -f ~/.ssh/known_hosts'
                os.system('rm -f ~/.ssh/known_hosts')


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='ZenySec Exploitation Exercises')
    parser.add_argument('-dir', '--directory', help='main directory', required=False, dest='directory')

    parser.add_argument('-d', '--docker-name', help='docker name', required=True, dest='docker_name')
    parser.add_argument('-ca', '--no-cache', help='use cache when building docker', required=False, dest='no_cache',
                        action='store_true')
    parser.add_argument('-r', '--build-run', help='build and run docker', required=False, dest='build_run',
                        action='store_true')
    parser.add_argument('-p', '--port', help='running docker port', required=True, dest='port')

    parser.add_argument('-a', '--architecture', help='i386/arm32v7', required=True, dest='arch')

    parser.add_argument('-as', '--aslr', help='yes/no', required=True, dest='aslr')
    main(**vars(parser.parse_args()))
