#!/bin/bash

copynote=`mktemp`
copynotepatch=`mktemp`
oldfile=`mktemp`
newfile=`mktemp`

cat << EOT > $copynote

This copyright note is auto-generated by ./scripts/Create-CopyPatch.
Please add additional copyright information _after_ the line containing
the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
the ./scripts/Create-CopyPatch script. Do not edit this copyright text!

ROCK Linux: rock-src/@@FILENAME@@
ROCK Linux is Copyright (C) 1998 - `date +%Y` Clifford Wolf

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; either version 2 of the License, or
(at your option) any later version. A copy of the GNU General Public
License can be found at Documentation/COPYING.

Many people helped and are helping developing ROCK Linux. Please
have a look at http://www.rocklinux.org/ and the Documentation/TEAM
file for details.

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) ..."
echo -n > copy.patch

[ $# = 0 ] && set Documentation/. architecture/. misc/. \
                  package/. scripts/. target/.

bash scripts/xfind.sh $* -type f ! -name "*~" \
	! -name Create-CopyPatch | sed 's,/\./,/,g' | \
while read filename
do
	grep -q -- '--- NO-ROCK-COPYRIGHT-NOTE ---' $filename && continue

	tag="`grep -- '--- ROCK-COPYRIGHT-NOTE-BEGIN ---' \
	           $filename | sed 's,---.*,,' | head -1`"
	cat $filename > $oldfile

	if [ -z "$tag" -a '(' \
		"$filename" != "${filename%/*.init}" -o \
		"$filename" != "${filename%/*.sh}" ')' ] &&
	   grep -n '^#!' $filename | grep -q ':1:'
	then
		sed '1 a\
#
# --- ROCK-COPYRIGHT-NOTE-BEGIN ---\
# --- ROCK-COPYRIGHT-NOTE-END ---' < $filename > $oldfile
		tag="# "
	fi

	if [ -z "$tag" -a '(' \
		"" != "$( expr $filename : \
			'package/.*/\(.*\)/\1\.conf' )" -o \
		"$filename" != "${filename%/*.diff}" -o \
		"$filename" != "${filename%/*.patch}" -o \
		"$filename" != "${filename%/*.patch.*}" -o \
		"$filename" != "${filename%/parse-config*}" -o \
		"$filename" != "${filename%/*config*.in}" -o \
		"$filename" != "${filename%/config*.hlp}" ')' ]
	then
		sed '1 i\
# --- ROCK-COPYRIGHT-NOTE-BEGIN ---\
# --- ROCK-COPYRIGHT-NOTE-END ---\
' < $filename > $oldfile
		tag="# "
	fi

	if [ -z "$tag" -a '(' \
		"$filename" != "${filename%/*.cache}" -o \
		"$filename" != "${filename%/*.desc}" ')' ]
	then
		sed '1 i\
[COPY] --- ROCK-COPYRIGHT-NOTE-BEGIN ---\
[COPY] --- ROCK-COPYRIGHT-NOTE-END ---
' < $filename > $oldfile
		tag="[COPY] "
	fi

	if [ "$tag" ] ; then
	    {
		grep -B 100000 -- '--- ROCK-COPYRIGHT-NOTE-BEGIN ---' $oldfile
		cat $copynote | sed -e "s,@@FILENAME@@,$filename,; s,^,$tag,"
		grep -A 100000 -- '--- ROCK-COPYRIGHT-NOTE-END ---' $oldfile
	    } > $newfile
	    if ! cmp -s $oldfile $newfile ; then
		echo "Creating patch for $filename."
		diff -u ./$filename $newfile |
			sed -e "2 s,$newfile,./$filename," >> copy.patch
	    fi
	else
		echo "WARNING: No Copyright tags in $filename found!"
	fi
done

rm -f $copynote $copynotepatch $newfile