#!/usr/bin/env python
# This file is part of Cockpit.
#
# Copyright (C) 2013 Red Hat, Inc.
#
# Cockpit is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Cockpit is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Cockpit; If not, see <http://www.gnu.org/licenses/>.

import argparse
import os
import sys
import subprocess

from common import testinfra
from common import vmmanage

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Install Cockpit on a virtual machine')
    parser.add_argument('-v', '--verbose', action='store_true', help='Display verbose progress details')
    parser.add_argument('-s', '--sit', action='store_true', help='Sit and wait if install script fails')
    parser.add_argument('-q', '--quick', action='store_true', help='Build faster')
    parser.add_argument('-b', '--build-image', action='store', help='Build in this image')
    parser.add_argument('-B', '--build-only', action='store_true', help='Only build and download results')
    parser.add_argument('-I', '--install-only', action='store_true', help='Only upload and install')
    parser.add_argument('-c', '--containers', action='store_true', help='Install container images')
    parser.add_argument('--address', help='Address of already running machine')
    parser.add_argument('image', nargs='?', default=testinfra.DEFAULT_IMAGE, help='The image to use')
    args = parser.parse_args()

    install_image = not args.build_only and args.image
    build_image = not args.install_only and (args.build_image or args.image)
    try:
        vmmanage.build_and_install(install_image, build_image, args={"verbose": args.verbose, "sit": args.sit, "quick": args.quick, "build_image": build_image,
            "build_only": args.build_only, "install_only": args.install_only, "containers": args.containers, "address": args.address})
    except Exception as e:
        print e
        sys.exit(1)
