#!/bin/bash

# Copyright (c) 2005,2006  Utrecht University (The Netherlands),
# ETH Zurich (Switzerland),
# INRIA Sophia-Antipolis (France),
# Max-Planck-Institute Saarbruecken (Germany),
# and Tel-Aviv University (Israel).  All rights reserved.
# Copyright (c) 2012 GeometryFactory Sarl (France)
#
# This file is part of CGAL (www.cgal.org)
#
# $URL$
# $Id$
# SPDX-License-Identifier: LGPL-3.0-or-later
#
# Author(s)     : Joachim Reichel <joachim.reichel@gmx.de>
#                 Laurent Rineau



# This script can be used to check all files in an internal or external release
# for proper license notices. It reports all files without a proper notice.

# The script uses another script, licensecheck, that uses a set of regular
# expressions to detect various known license notices.

# In CGAL, we allow only (L)GPLv3+ license, or licenses that are more
# permissives.

# Additionally, there might be provisions in the
# top-level LICENSE file that are not taken into account by this script.

# Note that there might be license errors that are not detected by this script.
# For example, we do not check that files under LGPL and QPL are not mixed in
# one library.

set -e

DIR=stats
PREFIX1=$DIR/X
PREFIX2=$DIR/Y
EXTENSIONS="C cpp h xpm gif pcx bmp jpeg png txt html vcproj sln dsp dsw cin cout cmd nef cgal dll lib tex makefile readme"
CHECK_PATTERN="\.(`echo $EXTENSIONS | sed -e 's/ /\\|/g'`)"

if [ -e $DIR ]; then
        echo error: \'$DIR\' exists, this script needs \'$DIR\' for its own purposes
        exit 1
fi

mkdir $DIR
rm -f $PREFIX1* $PREFIX2*

if [ ! -f INSTALL.md ]; then
        echo This script should be run from the top-level directory of an internal or external release.
        exit 1
fi

licensecheck -r * -c $CHECK_PATTERN | grep -Ev ': L?GPL \(v3 or later\)' | \
        grep -v "^$DIR/\|^include/CGAL/CORE/\|^src/CGAL_Core/\|^src/CGAL_ImageIO/\|^config/support/\|test/\|^Packages/\|^developer_scripts/\|^winutils/\|^cmake/platforms" | sort >$PREFIX1 || true

echo Note that files in the following directories are ignored:
echo include/CGAL/CORE,
echo src/CGAL_Core, src/CGAL_ImageIO, config/support,
echo test, Packages, developer_scripts, doc, winutils

echo
echo Results including examples and demo:
echo

for EXT in $EXTENSIONS; do
        grep -i "\(\.\|/\)$EXT:" $PREFIX1 >$PREFIX1.$EXT || true
        echo "$EXT `cat $PREFIX1.$EXT | wc -l`"
done

PATTERN=`echo $EXTENSIONS | sed -e 's/ /\\:\\\\|/g'`
grep -v -i "\(\.\|/\)\($PATTERN:\)" $PREFIX1 >$PREFIX1.misc || true
echo "misc `cat $PREFIX1.misc | wc -l`"

echo "total `cat $PREFIX1 | wc -l `"

echo
echo Results excluding examples and demo:
echo

grep -v "^examples/\|^demo/" $PREFIX1 >$PREFIX2 || true

for EXT in $EXTENSIONS; do
        grep -i "\(\.\|/\)$EXT:" $PREFIX2 >$PREFIX2.$EXT || true
        echo "$EXT `cat $PREFIX2.$EXT | wc -l`"
done

PATTERN=`echo $EXTENSIONS | sed -e 's/ /\\:\\\\|/g'`
grep -v -i "\(\.\|/\)\($PATTERN:\)" $PREFIX2 >$PREFIX2.misc || true
echo "misc `cat $PREFIX2.misc | wc -l`"

echo "total `cat $PREFIX2 | wc -l `"

echo
echo Detailed results excluding examples and demo:

for EXT in $EXTENSIONS; do
        if [ `cat $PREFIX2.$EXT | wc -l` -gt 0 ]; then
                echo
                echo $EXT files
                echo
                cat $PREFIX2.$EXT
        fi
done

echo
echo misc files
echo
cat $PREFIX2.misc

rm -f $PREFIX1* $PREFIX2*
rmdir $DIR
