#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2014  Aurélien Gâteau <agateau@kde.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import argparse
import os
import shutil
import subprocess
import sys

import yaml

DESCRIPTION = """
Generate Graphviz dot files for all frameworks.
"""

fnull = open("/dev/null", "w")

def generate_dot(fw_dir, build_dir):
    fw_name = os.path.basename(fw_dir)
    ret = subprocess.call(["cmake", fw_dir, "--graphviz={}.dot".format(fw_name)],
        stdout=fnull,
        cwd=build_dir)
    if ret != 0:
        sys.stdout.write("Generating the dot file for {} failed.\n".format(fw_name))
        sys.exit(ret)

def main():
    parser = argparse.ArgumentParser(description=DESCRIPTION)

    parser.add_argument("fw_base_dir",
        help="Base dir containing all frameworks")
    parser.add_argument("dot_base_dir",
        help="Destination dir where dot files will be generated")

    args = parser.parse_args()
    fw_base_dir = os.path.abspath(args.fw_base_dir)
    dot_base_dir = os.path.abspath(args.dot_base_dir)

    lst = os.listdir(fw_base_dir)
    for idx, fw_name in enumerate(lst):
        fw_dir = os.path.join(fw_base_dir, fw_name)
        yaml_path = os.path.join(fw_dir, fw_name + ".yaml")
        if not os.path.exists(yaml_path):
            continue

        progress = 100 * (idx + 1) / len(lst)
        print("{}% {}".format(progress, fw_name))

        with open(yaml_path) as f:
            fw_info = yaml.load(f)
            tier = fw_info["tier"]

        build_dir = os.path.join(dot_base_dir, "tier" + str(tier), fw_name)
        if not os.path.exists(build_dir):
            os.makedirs(build_dir)
        generate_dot(fw_dir, build_dir)
        shutil.copy(yaml_path, build_dir)

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