#!/bin/bash
#
# --- ROCK-COPYRIGHT-NOTE-BEGIN ---
# 
# 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/scripts/Create-PkgUpdPatch
# ROCK Linux is Copyright (C) 1998 - 2003 Clifford Wolf
# 
# 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.
# 
# --- ROCK-COPYRIGHT-NOTE-END ---

if [ "$1" != "${1#-}" ] ; then
	cat << EOT >&2

$0:
Script for automatically creating package-update patches

Input format (e.g.): automake-1.5, bind-9.2.0rc3, binutils-2.11.90.0.31,
                     gettext-0.10.40, gnome-applets-1.4.0.4

Output format: unified diff for the .desc files

EOT
	exit 1
fi

if [ "$*" ] ; then
	echo "$*" | exec $0
	exit $?
fi

tr ', \t' '\n\n\n' |
tr -s '\n' | tr 'A-Z' 'a-z' |
while read x ; do
	pkg="" ; pkgdir=""
	[ -z "$x" ] && continue

	echo "[ $x ]" >&2
	for y in package/*/* ; do
		z=${y##*/}
		if [[ "$x" == "$z"[-_]* ]] ; then
			pkg="$z" ; pkgdir="$y"
		fi
	done
	if [ "$pkg" ] ; then
		oldver="`egrep "^\[(V|VER|VERSION)\] " $pkgdir/$pkg.desc |
				tr '\t' ' ' | tr -s ' ' | cut -f2 -d' '`"
		newver=${x/$pkg?/} ; tmpfile=`mktemp` ; tmpfile2=`mktemp`
		echo "Upgrade Patch for $pkg ($pkgdir): $oldver -> $newver"
		sed "s/${oldver//./\\.}/$newver/g" < $pkgdir/$pkg.desc | \
		sed "s/^\[\(D\|DOWN\|DOWNLOAD\)\] [0-9]\+/[\1] 0/" > $tmpfile
		diff -u ./$pkgdir/$pkg.desc $tmpfile | tee $tmpfile2
		[ -s $tmpfile2 ] || echo "Patch for '$x' is empty!" >&2
		rm -f $tmpfile $tmpfile2
	else
		echo "Can't find package for '$x'!" >&2
		echo "Can't find package for '$x'!"
	fi
	echo
done