#!/bin/bash
# --- T2-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# 
# T2 SDE: scripts/Create-CopyPatch
# Copyright (C) 2004 - 2008 The T2 SDE Project
# Copyright (C) 1998 - 2003 ROCK Linux Project
# 
# More information can be found in the files COPYING and README.
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- T2-COPYRIGHT-NOTE-END ---

copynote=`mktemp`
copynotepatch=`mktemp`
rocknote=`mktemp`
oldfile=`mktemp`
newfile=`mktemp`
tmpfile=`mktemp`

cat << EOT > $copynote
This copyright note is auto-generated by ./scripts/Create-CopyPatch.

T2 SDE: @@FILENAME@@
@@COPYRIGHT@@

More information can be found in the files COPYING and README.

EOT

cp $copynote $copynotepatch

cat << EOT >> $copynote
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License. A copy of the
GNU General Public License can be found in the file COPYING.
EOT

cat << EOT >> $copynotepatch
This patch file is dual-licensed. It is available under the license the
patched project is licensed under, as long as it is an OpenSource license
as defined at http://www.opensource.org/ (e.g. BSD, X11) or under the terms
of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
EOT

echo "Creating copy.patch (this may take a while) ..." >&2

usesvn=
if [ "$1" = "-svn" ]; then
	usesvn=1; shift
fi

if [ $# = 0 ]; then
    set architecture/. misc/. package/. scripts/. target/.
else
    # check if file or package name was given
    files=""
    for i; do
	if [ -f $i -o -d $i ]; then
	    files="$files ${i#./}"
	elif [ -d package/*/$i ]; then
	    for each in `echo package/*/$i`; do
		[[ $each = *~ ]] && continue
		files="$files $each/."
	    done
	else
	    echo Cannot find \'$i\', ignoring. 1>&2
	fi
    done
    set -- $files 
    [ $# = 0 ] && exit
fi

bash scripts/xfind.sh $* -type f ! -name "*~" \
	! -name Create-CopyPatch | sed 's,/\./,/,g' |
while read filename ; do
	# determine the comment mode first
	mode=none
	case "$filename" in
		*.cache) mode=skip	;;
		*/Makefile|*.sh|*.pl|*.in|*.hlp|*.conf) mode=sh ;;
		*.cron|*.postinstall|*.init) mode=sh ;;
		*.h|*.c|*.cc|*.cpp|*.lex|*.y|*.spec|*.tcx|*.tmpl|*.tcc) mode=c ;;
		*.lua) mode=lua ;;
		*.desc) mode=asci ;;
		*scripts/[A-Z][a-z-]*|*/parse-config*) mode=sh ;;
		*patch|*diff|*patch.*) mode=sh ;;
		*m4) mode=m4 ;;
	esac

	#echo "Mode type: $mode"

	grep -q -- '--- NO-\(T2\|ROCK\)-COPYRIGHT-NOTE ---' "$filename" &&
		continue

	case "$mode" in
		skip)	continue ;;
		none)
			if head -n 1 "$filename" | grep -q '^#!'; then
				mode=sh
			else
				case "`sed -n 's/^ *\([^ ]\+\) .*COPYRIGHT-NOTE-BEGIN .*/\1/p' "$filename"`" in
					"#") mode=sh ;;
					"*") mode=c ;;
				esac
			fi

			if [ "$mode" == none ]; then
				echo "Unknown type for $filename" 1>&2
				continue
			fi
	esac

	# make a copy in the case we have no matching conditional below
	sed 's,ROCK-COPYRIGHT-NOTE,T2-COPYRIGHT-NOTE,g' "$filename" > $oldfile

	tag=$( sed -n -e '/^\(.*\)--- \(T2\|ROCK\)-COPYRIGHT-NOTE-BEGIN ---.*/{s//\1/;p;q;}' "$filename" )
	thisyear=`date +%Y`

	if [ -n "$tag" ]; then
		# has a note, catch copyrights
		oldcopyright=`sed -e '/--- T2-COPYRIGHT-NOTE-BEGIN ---/,/--- T2-COPYRIGHT-NOTE-END ---/!d' \
		 	-e '/.*\(Copyright (C) .*\)/!d;s//\1/;' \
			-e 's/Clifford Wolf/ROCK Linux Project/' \
			$oldfile`
	else
		oldcopyright=
	fi

	if echo "$oldcopyright" | grep -q 'T2 SDE'; then
		# if has T2 copyright, renew if necesary
		since=$( echo "$oldcopyright" | sed -n -e 's,.* (C) \([^ ]*\) .*T2 SDE.*,\1,p' )

		if [ $since -lt $thisyear ]; then
			copyright=`echo "$oldcopyright" | sed -e \
			"s,.*T2 SDE.*,Copyright (C) $since - $thisyear The T2 SDE Project,"`
		else
			copyright="$oldcopyright"
		fi
	else
		# else, add one...
		# but, since when the file exist?
		if [ "$usesvn" = 1 ]; then
			since=`TZ=UTC svn log -q "$filename" 2> /dev/null |
				grep '^r' | tail -n 1 | cut -d'|' -f3 | cut -c2-5`
			
			# but until 2003 we were ROCK Linux, so
			if [ ${since:-2004} -lt 2004 ]; then
				oldcopyright="Copyright (C) $since - 2003 ROCK Linux Project"
				since=2004
			fi
		else
			since=$thisyear
		fi

		# prepare a copyright tag
		if [ ${since:-1} -lt $thisyear ]; then
			copyright="Copyright (C) ${since:-ESVNTO} - $thisyear The T2 SDE Project"
		else
			copyright="Copyright (C) $thisyear The T2 SDE Project"
		fi
		copyright="$copyright${oldcopyright:+\\n$oldcopyright}"
	fi

	action='i'
	pretag= posttag=
	if [ -z "$tag" ]; then
		# doesn't have a note
		if head -n 1 "$filename" | grep -q '^#!'; then
			action='a'
		fi
		case "$mode" in
			sh)
				tag="# "
				;;
			asci)
				tag="[COPY] "
				;;
			c)
				pretag='/*' posttag=' */'
                		tag=' * '
				;;
			m4)
				tag="dnl "
				;;
			lua)
				tag="-- "
				;;
		esac

		# insert one
		sed -i "1 $action\\
${pretag:+$pretag\\
}$tag--- T2-COPYRIGHT-NOTE-BEGIN ---\\
$tag--- T2-COPYRIGHT-NOTE-END ---\\
${posttag:+$posttag\\
}" $oldfile
	fi

	mangled_filename=`echo "$filename" | \
		sed 's,package/\([^/]*\)/\(.*\),package/.../\2,'`

	#echo BEFORE
	#cat $oldfile

	if [ "$tag" ] ; then
	    # implant T2 copy note
	    {
		grep -B 100000 -- '--- T2-COPYRIGHT-NOTE-BEGIN ---' $oldfile
		{
		if [ "$filename" != "${filename%/*.diff}" -o \
		     "$filename" != "${filename%/*.patch}" -o \
		     "$filename" != "${filename%/*.patch.*}" ] ; then
			sed -e "s,@@FILENAME@@,$mangled_filename,; \
				s,@@COPYRIGHT@@,${copyright//
/\n},;"  $copynotepatch
		else
			sed -e "s,@@FILENAME@@,$mangled_filename,; \
				s,@@COPYRIGHT@@,${copyright//
/\n},;"  $copynote
		fi
		# we need a separated sed call because $rockcopyright adds a new line
		} | sed -e "s,^,$tag,"

		grep -A 100000 -- '--- T2-COPYRIGHT-NOTE-END ---' $oldfile
	    } > $newfile

	    # create the difference
	    if ! cmp -s $oldfile $newfile ; then
		echo "Creating patch for $filename." >&2
		diff -u "./$filename" $newfile |
			sed -e "2 s,$newfile,./$filename,"
	    fi
	else
		echo "WARNING: No Copyright tags in $filename found!" >&2
	fi
done

rm -f $copynote $copynotepatch $newfile

