#!python
"""LICENSE
Copyright 2017 Hermann Krumrey <hermann@krumreyh.com>

This file is part of ci-scripts.

ci-scripts is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ci-scripts 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with ci-scripts.  If not, see <http://www.gnu.org/licenses/>.
LICENSE"""


import os
import argparse
from ci_scripts.common import process_call


def main():
    """
    Generates jacoco test coverage reports and syncs it to progstats
    :return: None
    """

    parser = argparse.ArgumentParser()
    parser.add_argument("submodule_name", default="app",
                        help="The name of the android gradle submodule")
    args = parser.parse_args()
    submodule_name = args.submodule_name
    project_name = os.environ["CI_PROJECT_NAME"]
    progstats_data = os.environ["PROGSTATS_DATA_PATH"]

    process_call(["./gradlew", "test", "jacocoTestReport"])

    source = os.path.join(
        submodule_name, "build/reports/jacoco/jacocoTestReport/html"
    ) + "/"
    dest = os.path.join(
        progstats_data, "coverage", project_name + "-" + submodule_name
    )

    process_call(["rsync", "-a", source, dest])


if __name__ == "__main__":
    main()
