#!/bin/bash
# SPDX-License-Identifier: EUPL-1.2

IFS=$'\n'

files_without_header=()

newly_added_files=($(git diff --name-only --diff-filter=A --cached))
#newly_added_files=($(git ls-files))
if [ -n "${newly_added_files[0]}" ]
then
    # Check for Copyright statement
    for newly_added_file in ${newly_added_files}; do
        files_without_header+=($(grep -L -e "This file is part of meli" -e "This file is part of melib" -e "SPDX-License-Identifier" "${newly_added_file}"))
    done

    if [ -n "${files_without_header[0]}" ]
    then
        echo "License preamble not found in the following newly added files:"
        for file in "${files_without_header[@]}"
        do
            :
            echo "   - ${file}";
        done
        exit 1;
    else
        exit 0;
    fi

    ##Debug:
    #files_without_header+=($(git diff --name-only -S"debug!"))
    #echo ${files_without_header}

    #if [ -n "${files_without_header}" ]
    #then
    #    (echo "debug! macro added in the following files:";
    #        echo "";
    #    for file in "${files_without_header[@]}"
    #    do
    #        :
    #        tput bold
    #        echo "   - ${file}";
    #        echo "";
    #        tput sgr0;
    #        FORCE_COLOR=true git -c color.status=always diff --color=always -S"debug!" "${file}";
    #      done) | less -R
    #    exit 1;
    #else
    #    exit 0;
    #fi
fi
