#! /bin/sh
#
# updatecopyright - updates copyright in files
#
# Copyright (C) 2005 - 2018 Eggheads Development Team
#
# This file 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.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# As a special exception to the GNU General Public License, if you
# distribute this file as part of a program that contains a
# configuration script generated by Autoconf, you may include it under
# the same distribution terms that you use for the rest of that program.

# Brackets as a cheap way to not find self
PATTERN="copyright.*eggheads developmen[t]"

find_copyright_files() {
	grep -lri -- "$PATTERN" *
}

update_copyright() {
	LINE=`grep -i -- "$PATTERN" $1`
	# Format 1
	grep -Ei 'copyright.*[0-9]{4} ?- ?[0-9]{4}.*eggheads' $i 2>&1 >/dev/null
	if [ $? -eq 0 ]; then
		echo "Format 1"
		sed -e 's/\(copyright[^\n0-9]*[0-9][0-9][0-9][0-9] *- *\)[0-9][0-9][0-9][0-9]\( eggheads\)/\1'"$YEAR"'\2/ig' $i > ${i}_
		chmod --reference=$i ${i}_
		mv ${i}_ $i
	fi
	# Format 2
	grep -Ei 'copyright[^-]*[0-9]{4}[^-]*eggheads' $i 2>&1 >/dev/null
	if [ $? -eq 0 ]; then
		echo "Format 2"
		sed -e 's/\(copyright[^\s0-9-]*[0-9][0-9][0-9][0-9]\)\( eggheads\)/\1 - '"$YEAR"'\2/ig' $i > ${i}_
		chmod --reference=$i ${i}_
		mv ${i}_ $i
	fi
	# Cats and Dogs
	sed -i '/Eggdrop v%s (C) 1997 Robey Pointer/c\               "Eggdrop v%s (C) 1997 Robey Pointer (C) 2010-'$YEAR' Eggheads",' src/main.c
}

if test ! -f src/main.c; then
	echo "You are not in the Eggdrop root directory."
	exit 1
fi

if test "x$1" = "x"; then
	echo "Usage: $0 <year>"
	exit 1
fi

YEAR=$1

if test $YEAR -lt 2015 -o $YEAR -gt 2030; then
	echo "Invalid year. Usage: $0 <year>"
	exit 1
fi

count=0
echo "Updating documentation copyright..."
sed -i "s/^copyright = .*/copyright = u'$YEAR, Eggheads'/" doc/sphinx_source/conf.py
./misc/generatedocs
for i in $(find_copyright_files); do
	count=$(($count + 1))
	echo "${i}... "
	update_copyright $i $YEAR
	echo "done."
done

echo "Found/fixed ${count} files."
