#!/usr/bin/env python3
"""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 sys
import shutil
from ci_scripts.common import process_call


def main():
    """
    Generates git statistics using git_stats and gitstats and copies them
    to a progstats data directory

    Requires the following environment variables to be set:

      PROGSTATS_DATA_PATH: The path to the progstats data path
      CI_PROJECT_NAME: The project name, automatically set by Gitlab CI

    Also, the gitstats and git_stats programs need to be installed.

    :return: None
    """
    destination = os.environ["PROGSTATS_DATA_PATH"]
    project = os.environ["CI_PROJECT_NAME"]
    gitstats_dest = os.path.join(destination, "gitstats", project)
    git_stats_dest = os.path.join(destination, "git_stats", project)

    gitstats_temp_dest = "gitstats_temp_dest"
    git_stats_temp_dest = "git_stats_temp_dest"

    for path in [gitstats_temp_dest, git_stats_temp_dest]:
        if not os.path.isdir(path):
            os.makedirs(path)

    process_call(["gitstats", ".", gitstats_temp_dest])
    process_call(["git_stats", "generate", "-o", git_stats_temp_dest])

    rsync_cmd = ["rsync", "-av", "--delete-after"]
    process_call(rsync_cmd + [gitstats_temp_dest + "/", gitstats_dest])
    process_call(rsync_cmd + [git_stats_temp_dest + "/", git_stats_dest])

    for path in [gitstats_temp_dest, git_stats_temp_dest]:
        shutil.rmtree(path)


if __name__ == "__main__":
    try:
        main()
    except Exception as e:
        print(e)
        sys.exit(1)
