#!/bin/bash

export LANG=C
export LC_ALL=C

if [ "$1"x = ""x ] ; then
    echo "$0: tell me which dir to target!"
    echo "abort"
    exit 1
fi

CHECK=`ls -al $1/*/iso-* 2>/dev/null`
if [ "$CHECK"x = ""x ] ; then
    echo "$0: could not find stuff to work on in $1"
    echo "abort"
    exit 1
fi

echo "$0: Merging checksum files"

for DIR in $1/*/*-*; do
    cd $DIR
    # Check that we have checksums in this dir, and which ones
    CHECK=`ls -1 *SUMS.* 2>/dev/null | sed 's/SUMS\..*$/SUMS/' | sort -u`
    if [ "$CHECK"x != ""x ] ; then
	for SUM in $CHECK; do
            cat $SUM.* | sort -k2 | uniq > $SUM
	    rm $SUM.*
	done
        echo "  $DIR OK"
    else
        echo "  IGNORING $DIR: no checksum files"
    fi
done
