#!/bin/sh

if [ $# -eq 0 -o "$1" = "-h" -o "$1" = "-help" -o "$1" = "--help" ]
then
    echo "usage: gpl-check [-h|-help|--help|-2|-3|-u|-n] file1 [file2 ...]"
    echo "   -h|-help|--help       print this help"
    echo "   -2                    print file names found with GPLv2+"
    echo "   -3                    print file names found with GPLv3+"
    echo "   -u                    print file names found with a GPL of unknown version"
    echo "   -n                    print file names with no GPL found"
    echo "   file1 [file2 ...]     list of files to check"
else 
    GPLV2=0
    GPLV3=0
    GPLVUNK=0
    NOGPL=0

    for i
    do 
	case $i in
	    -2) GPLV2=1;;
	    -3) GPLV3=1;;
	    -23) GPLV2=1;GPLV3=1;;
	    -u) GPLVUNK=1;;
	    -n) NOGPL=1;;
	    -nu|-un) GPLVUNK=1;NOGPL=1;;
	    *)  if [ $GPLV2 -eq 0 -a $GPLV3 -eq 0 -a $GPLVUNK -eq 0 -a $NOGPL -eq 0 ]
		then 
                    GPLV2=1 
		    GPLV3=1 
		    GPLVUNK=1 
		    NOGPL=1 
                fi 
		if grep -l 'either version 2, or' $i > /dev/null
		then 
		    if [ $GPLV2 -eq 1 ]
		    then echo "$i GPLv2+"
		    fi
		elif grep -l 'either version 3' $i > /dev/null
		then 
		    if [ $GPLV3 -eq 1 ]
		    then echo "$i GPLv3+"
		    fi
		elif grep -l 'GNU General Public License' $i > /dev/null
		then 
		    if [ $GPLVUNK -eq 1 ]
		    then echo "$i GPLv unknown"
		    fi
		else 
		    if [ $NOGPL -eq 1 ]
		    then echo "$i no GPL"
		    fi
		fi;;
	esac
    done
fi