#!/usr/bin/env python

from __future__ import print_function

import os
import sys


dirs = [
    'examples',
    'spotify_tensorflow',
    'tests'
]


LICENSE = """
# -*- coding: utf-8 -*-
#
# Copyright 2017-2019 Spotify AB.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
""".strip()


def stderr(x):
    print(x, file=sys.stderr)


def stdout(x):
    print(x)


py_files = []
for dir in dirs:
    dir = os.path.join(os.getcwd(), dir)
    for (dirpath, _, filenames) in os.walk(dir):
        for filename in filenames:
            if filename.endswith(".py"):
                py_files.append(os.sep.join([dirpath, filename]))

violations = set()
for file in py_files:
    # print open(file).read()
    if not open(file).read().startswith(LICENSE):
        violations.add(file)


stderr("")
stderr(LICENSE)
stderr("")
stderr("")
stderr("Checked {} python files".format(len(py_files)))
if len(violations) > 0:
    stderr("Found violations in: {} file(s):".format(len(violations)))
    for violation in violations:
        stdout(violation)
    sys.exit(1)
else:
    stderr("All good!")
