#!/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-CkSumPatch
# 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 -a "$1" = -misc ] ; then
	set x -repository misc ; shift
fi

if [ $# = 1 -a "$1" = -all ] ; then
	shift $# ; cmpfield=2
else
	if [ "$1" = "-repository" -o "$1" = "-target" ]
	then cmpfield=2 ; shift  ;  else cmpfield=3 ; fi

	if [ "$#" -eq 0 -o "${1#-}" != "$1" ] ; then
		echo "Usage: $0 <package-names>"
		echo "or     $0 -repository <respository-names>"
		echo "or     $0 -target <target-names>"
		echo "or     $0 -misc"
		echo "or     $0 -all"
		exit 1
	fi
fi

echo "Creating cksum.patch ..."
echo -n > cksum.patch

./scripts/Download -list-cksums | grep '^00* ' | \
while read cksum gzfile ; do

   bzfile="`echo "$gzfile" | sed 's,\.\(t\?\)gz$,.\1bz2,'`"

   if [ -f "$bzfile" ]
   then
	checkthis=1
	y="`echo $bzfile | cut -f$cmpfield -d/`"
	for x ; do
		[ $checkthis = 1 ] && checkthis=0
		[ "$y" = $x ] && checkthis=2
	done
	if [ $checkthis = 0 ] ; then
		: echo "Skip: $bzfile" ; continue
	fi

	if [[ "$bzfile" = *.bz2 ]] || [[ "$bzfile" = *.tbz2 ]] ; then
		echo -n "$bzfile (bzip2): "
		cksum="`bunzip2 < $bzfile | cksum | cut -f1 -d' '`"
	else
		echo -n "$gzfile (raw): "
		cksum="`cksum "$gzfile" | cut -f1 -d' '`"
	fi
	echo $cksum

	rep="`echo $bzfile | cut -f2 -d/`"

	if [ -d package/$rep ] ; then
		xfile="`echo $gzfile | cut -f4- -d/`"
		pkgdir="package/`echo $gzfile | cut -f2,3 -d/`"
		descfile="$pkgdir/`echo $gzfile | cut -f3 -d/`.desc"

		if [ -f $descfile ] ; then
			sedscript="s,\[D\]  *00*  *$xfile,[D] $cksum $xfile,"
			sed "$sedscript" < $descfile > /tmp/$$
			if cmp -s ./$descfile /tmp/$$ ; then
				echo "!!! SED script didn't change anything:"
				echo "!!! $sedscript"
			else
				{ echo "diff -u0 ./$descfile /tmp/$$"
				diff -u0 ./$descfile /tmp/$$ ; } >> cksum.patch
			fi
			rm -f /tmp/$$
		else
			echo "!!! No Desc File for $gzfile"
			echo "!!! (My guess was $descfile)"
		fi
	elif [ -d target/$rep -o "$rep" = misc ] ; then
		if [ -d target/$rep ] ; then
			descfile="target/$rep/download.txt"
		else
			descfile="scripts/miscdown.txt"
		fi
		xfile="`echo $gzfile | cut -f3- -d/`"

		if [ -f $descfile ] ; then
			sedscript="s,^00*  *$xfile,$cksum $xfile,"
			sed "$sedscript" < $descfile > /tmp/$$
			if cmp -s ./$descfile /tmp/$$ ; then
				echo "!!! SED script didn't change anything:"
				echo "!!! $sedscript"
			else
				{ echo "diff -u0 ./$descfile /tmp/$$"
				diff -u0 ./$descfile /tmp/$$ ; } >> cksum.patch
			fi
			rm -f /tmp/$$
		else
			echo "!!! No Desc File for $gzfile"
			echo "!!! (My guess was $descfile)"
		fi
	else
		echo "!!! No Desc File for $gzfile"
		echo "!!! (I didn't have any possible guesses)"
	fi
   fi
done
