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

# This file is part of Cockpit.
#
# Copyright (C) 2016 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 glob
import os
import subprocess
import sys
import shutil
import tempfile

sys.dont_write_bytecode = True
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

from common import testinfra

def main():
    parser = argparse.ArgumentParser(description='Run cockpit build and unit tests in Koji')
    parser.add_argument('-v', '--verbose', action='store_true', help='Verbose output (unsupported)')
    opts = parser.parse_args()

    # Splits 23 out of fedora-23
    system = testinfra.DEFAULT_IMAGE.split("-")[-1]

    # Converts fedora-23 to f23
    try:
        number = int(system)
        system = testinfra.DEFAULT_IMAGE[0] + system
    except ValueError:
        pass

    make_srpm = os.path.join(testinfra.TEST_DIR, "..", "tools", "make-srpm")
    srpm = subprocess.check_output([make_srpm], shell=True).strip()

    # Do a build
    cmd = ["koji", "build", "--wait", "--scratch", system, srpm]
    subprocess.check_call(cmd)

if __name__ == '__main__':
    sys.exit(main())
