#!/bin/bash
#
# Verify that all source files contain a valid copyright.
# Could be expanded to include files like README, scripts, etc.
#
# usage:  checkCopyright {path}
#
# Will check all files that normally contain code at and below path.  
# Will skip soft links.  Default path is CWD.
#

validString1="Copyright (c) [0-9][0-9][0-9][0-9] Sandia Corporation"
validString2="This software is distributed under the BSD License"
validString3="This is third-party software that is distributed with"

if [ $# == 1 ] ; then
  topdir=$1
else
  topdir=.
fi

export codeFiles=`find $topdir -regex ".*\.[phc][yxp]*"`

declare -i numBad=0
declare -i numTotal=0

echo "The following files need copyright messages:"
echo "============================================"

for fname in $codeFiles ; do
  if [ -f $fname ] ; then
    needsCopyRight=1

    #
    # Check if both validString1 and validString2 are in the file
    #
    grep --regexp="$validString1" $fname > /dev/null
    fail=$?
    if [ $fail == 0 ] ; then
      grep "$validString2" $fname > /dev/null
      fail=$?
      if [ $fail == 0 ] ; then
        needsCopyRight=0
      fi
    fi
  
    #
    # If both validString1 and validString2 are not in the file,
    # then check if validString3 is in the file.
    #
    if [ $needsCopyRight == 1 ] ; then
       grep --regexp="$validString3" $fname > /dev/null
       fail=$?
       if [ $fail == 0 ] ; then
          needsCopyRight=0
       fi
    fi
       
    if [ 1 -lt 0 ] ; then
    #
    # Check if both validString4 and validString5 are in the file
    #
    grep --regexp="$validString4" $fname > /dev/null
    fail=$?
    if [ $fail == 0 ] ; then
      grep "$validString5" $fname > /dev/null
      fail=$?
      if [ $fail == 0 ] ; then
        needsCopyRight=0
      fi
    fi
    fi
  
    if [ $needsCopyRight == 1 ] ; then
      echo $fname
      numBad=$numBad+1
    fi
    numTotal=$numTotal+1
  fi
done

echo ""
echo "Total number of files missing copyright: $numBad"
echo "Total number of files checked:           $numTotal"







