#!/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 ---

. scripts/functions
override=0
files=''

while [ "$1" ]; do
    case "$1" in
	-override)   override=1 ; shift ;;
	-repository) files="$files `echo package/$2/*/*.desc`" ; shift ; shift ;;
	-*)
	    echo "Usage:"
	    echo "./scripts/Create-CkSumPatch [options] <package-name(s)> <target-file(s)> \\"
	    echo "                            <desc-file(s)> -repository <repository>"
	    echo 
	    echo " Compute checksums for (source) files mentioned in .desc files and output"
	    echo " a patch which can be applied to the ROCK sources to update the checksums."
	    echo " Only checksums for files that have the checksum set to 0 will be computed."	    	
	    echo " Process single packages, a single repository, single .desc files or files"
	    echo " from a target directory (e.g. target/bootdisk/download.txt)."
	    echo
	    echo "Options:"
	    echo "	-override	create new checksums even if old ones are not equal 0;"
	    echo "			checksums set to X are not changed anyhow"			  	
	    echo "	-help		show this help text"
	    echo
	    echo "Example: create a cksum patch and apply it"
	    echo " ./scripts/Create-CkSumPatch gcc -repository stf target/bootdisk/download.txt \\"
	    echo " package/base/dietlibc/dietlibc.desc | patch -p0" 	
	    echo
	    exit
	    ;;
	target/*)    files="$files $1" ; shift ;;
	*.desc)      files="$files $1" ; shift ;;
	*)           files="$files `echo package/*/$1/$1.desc`" ; shift ;;
    esac
done

# cksum_file path-to-desc-or-target-file
cksum_file() {
    case "$1" in
	target/*)
	    has_D='cat'
	    sedscript='s,^[0-9]*  *$file,$newcksum $file,'
	    ;;
	*.desc)
	    has_D='fgrep "[D]" | sed "s/[[]D[^ ]*//"' 
	    sedscript='s,\[D\]  *[0-9]*  *$file,[D] $newcksum $file,'
	    ;;
	*)
	    echo "!!! File type not recognized" >&2
	    return -1
    esac

    if [ ! -f "$1" ]; then
	echo "!!! File not found: $1" >&2
	return -1
    fi

    cp $1 /tmp/$$
    eval "egrep -v '^#' $1 | $has_D" | while read cksum file url flags; do
	[ "$cksum" = 'X' ] && continue
	[ "$cksum" != '0' -a "$override" = '0' ] && continue

	gzfile=`source_file cksum $file url $flags`
	bzfile="`echo "$gzfile" | sed 's,\.\(t\?\)\(gz\|Z\)$,.\1bz2,'`"
	if [ ! -f "$bzfile" ]; then
	    echo "!!! File not present: $bzfile" >&2
	    continue
	fi

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

	if [ "$cksum" != 0 -a "$cksum" != "$newcksum" ]; then
	    echo "!!! Checksum of $file changed (was $cksum)." >&2
	fi

	eval "sed \"$sedscript\" -i /tmp/$$"
    done

    diff -u $1 /tmp/$$
    rm -f /tmp/$$
}

echo "Creating checksum patch ..." >&2

for f in $files; do
    cksum_file $f
done