#!/bin/bash
#
# Copyright (c) 2021, AT&T Intellectual Property.
# All rights reserved.
#
# SPDX-License-Identifier: LGPL-2.1-only
#

usage () {
    echo "Usage: $(basename $0) target source" >&2
    echo "target: something like 'origin/master'" >&2
    echo "source: something like 'bugfix/foo' or 'feature/bar'" >&2
}

[ "$#" -ne 2 ] && { usage; exit 1; }

TARGET="$1" # most likely "origin/master"
SOURCE="$2" # most likely "bugfix/foo" or "feature/bar"

GIT_SOURCE_HASH=$(git rev-list -n1 "$SOURCE")
GIT_TARGET_HASH=$(git rev-list -n1 "$TARGET")
GIT_MERGE_BASE=$(git merge-base $GIT_SOURCE_HASH $GIT_TARGET_HASH)


# Look for commits without "VRVDR" or "VYINT"
# in the last line of the log message.
#
missing=""
for commit in `git log --oneline "$GIT_MERGE_BASE..$GIT_SOURCE_HASH" --pretty=format:"%h"`; do
    if [ "`git show $commit --quiet | tail -n 1 | egrep 'VRVDR|VYINT'`" == "" ]; then
        missing+="`git show --pretty=oneline --abbrev-commit --quiet $commit`\n"
    fi
done

if [ -n "$missing" ]; then
    echo -e "* Please add the VRVDR or VYINT jira ID to these commits:\n$missing"
fi


# Check file permissions.
#
check_files() {

    # All dirs are 755.
    #
    find . -type d \! -perm 755

    # Source code is 644.
    #
    find src include lib protobuf .github -type f \! -perm 644

    # Everything in the TLD is 644.
    #
    find . -maxdepth 1 -type f \! -perm 644

    # Executables are 755.
    #
    find scripts tools -type f \! -perm 755

    # Tests are 644 except for dataplane_test.sh which is 755.
    #
    find tests -type f \! -perm 644 | grep -v tests/whole_dp/dataplane_test.sh
    find tests -name dataplane_test.sh \! -perm 755

    # Debian executables must be 755
    #
    find `find debian -type f -exec file {} \; | grep executable | cut -d: -f1` \! -perm 755

    # Debian non-executables must be 644
    #
    find `find debian -type f -exec file {} \; | grep -v executable | cut -d: -f1` \! -perm 644
}

f=$(check_files)

if [ -n "$f" ]; then
    echo -e "* Check the permissions of these files:\n$f\n"
fi


# List of files to ignore.
IGN_FILES="debian\/.*\n.*\.conf$\n.*\.md$\nCODEOWNERS\n^.git.*\n.*\.gitignore\n^\.clang-.*\n\.editorconfig\niwyu.mapping\nLICENSE\ndataplane\.section-ordering\nmeson_options.txt\nsrc\/sff8436.h\nsrc\/sff8472.h"

# All files in the diff, except deleted files.
all_files=`git diff --diff-filter=d --name-only --format=format:'' "$GIT_MERGE_BASE..$GIT_SOURCE_HASH"`

# Strip out the ignored files.
check_files=`sed -e "$(sed 's:.*:s/&//ig:' <<< $(echo -e $IGN_FILES))" <<< $all_files`

# Bail out if there's nothing to check.
if [ -z "$check_files" ]; then
    exit 0
fi


# Ensure all files contain a copyright message with the current year.
#
YEAR=`date +%Y`
copyright=`egrep -d skip -L "Copyright \(c) .*$YEAR,{,1} AT&T Intellectual Property" $check_files`
if [ -n "$copyright" ]; then
    echo -e "* Add or update the copyright message in these files:\n$copyright\n"
fi


# Ensure all files contain an SPDX license.
#
spdx=`grep -L "SPDX-License-Identifier: " $check_files`
if [ -n "$spdx" ]; then
    echo -e "* Missing SPDX-License-Identifier in these files:\n$spdx\n"
fi


# Idempotent headers - .h files must end "#endif /* $FILENAME */"
#
h_files=`echo $check_files | grep -Po "[^[:blank:]]*\.h( |$)"`
idem=`for h in $h_files; do FN=\`basename $h | sed s/\.h$/_h/ | tr [a-z] [A-Z]\`; tail -n 1 $h | grep -L "^#endif.*$FN" > /tmp/x && echo $h; done`

if [ -n "$idem" ]; then
    echo -e "* Check the idempotency of these files:\n$idem\n"
fi


# Always successful.
#
exit 0
