#!/bin/sh
#
# grab_checksum
# (c) 2004-2019 Steve McIntyre <steve@einval.com>
#
# GPL v2
#
# Parse Packages and Sources files out of a mirror and pre-process
# them into a single list ready for mkisofs to check later

set -e

CHECKSUM=$1
MIRROR=$2
ARCHES="$3"
CODENAME=$4
DI_CODENAME=$5
OUT=$6

case $CHECKSUM in
    md5|sha256)
	# OK
        ;;
    *)
	echo "$0: Unsupported checksum specified: $CHECKSUM"
	echo "Abort"
	exit 1
	;;
esac

export CHECKSUM

for ARCH in $ARCHES
do
    LOCATIONS="$MIRROR/dists/$CODENAME/ $MIRROR/dists/$DI_CODENAME/"
    if [ "$BACKPORTS"x != ""x ] ; then
	LOCATIONS="$LOCATIONS $MIRROR/dists/$CODENAME-backports/"
    fi
    if [ "$UNRELEASED" = 1 ] ; then
	LOCATIONS="$LOCATIONS $MIRROR/dists/unreleased/"
    fi
    echo "Looking in $LOCATIONS"

    for LOCATION in $LOCATIONS; do
        if [ ! -d $LOCATION ]; then
            echo "Error: $LOCATION is not a directory"
            exit 1
        fi
    done

    case $ARCH in
        source)
            FILES=`find $LOCATIONS -follow -name Sources.gz -o -name Sources.xz`
            echo "Using $CHECKSUM sums from Sources files:"
            echo $FILES
            $BASEDIR/tools/catz $FILES | \
		MIRROR=$MIRROR CHECKSUM=$CHECKSUM perl -e '
                chomp;
                my %files;
                my $dir;
                my $filename;
                my $mirror = $ENV{"MIRROR"};
		my $checksum = $ENV{"CHECKSUM"};
                while (<>) {
                    if (m/^ ([[:xdigit:]]{32}) (\d+) (\S+)/sg) {
                        $files{$3}{"md5"} = $1;
                        $files{$3}{"size"} = $2;
                    }
                    if (m/^ ([[:xdigit:]]{64}) (\d+) (\S+)/sg) {
                        $files{$3}{"sha256"} = $1;
                        $files{$3}{"size"} = $2;
                    }
                    if (m/^Directory: (\S+)/sg) {
                        $dir = $1;
                    }
                    if (m/^$/) {
                        for $filename (keys %files) {
                            printf("%s  %12s  %s/%s/%s\n",
                                $files{$filename}{$checksum},
                                $files{$filename}{"size"},
                                $mirror, $dir, $filename);
                        }
                        undef %files;
                    }
                }' | sort | uniq >> $OUT
            ;;
        *)
            FILES=`find $LOCATIONS -follow -name Packages.gz \
                   -o -name Packages.xz | grep binary-$ARCH`
            echo "Using $CHECKSUM sums from Packages files:"
            echo $FILES
            NUM_FILES=`echo $FILES| wc -w`
            if [ $NUM_FILES -eq 1 ] ; then
                echo "No files found for arch $ARCH. Abort!"
                exit 1
            fi
            $BASEDIR/tools/catz $FILES | \
		MIRROR=$MIRROR CHECKSUM=$CHECKSUM perl -e '
                chomp;
                my $mirror = $ENV{"MIRROR"};
		my $checksum = $ENV{"CHECKSUM"};
                my $filename;
                my $size;
                my $sum;
                while (<>) {
                    if (m/^Filename: (\S+)/sg) {
                        $filename = $1;
                    }
                    if (m/^Size: (\S+)/sg) {
                        $size = $1;
                    }
		    if ($checksum eq "md5" and (m/^MD5sum: (\S+)/sg)) {
                        $sum = $1;
                    } elsif ($checksum eq "sha256" and (m/^SHA256: (\S+)/sg)) {
                        $sum = $1;
                    }
                    if (m/^$/) {
                        printf("%s  %12s  %s/%s\n", $sum, $size, $mirror, $filename);
			$sum = "";
                    }
                }' | sort | uniq >> $OUT
            # Use the new D-I images. Do NOT use the "current"
            # link; it causes problems with overlaid files...
            for VER in $MIRROR/dists/$DI_CODENAME/main/installer-$ARCH/*
            do
                if [ -d $VER ] && [ ! -L $VER ] ; then
		    if [ $CHECKSUM = md5 ]; then
			FILE=$VER/images/MD5SUMS
		    else
			FILE=$VER/images/SHA256SUMS
		    fi
                    echo "Using $CHECKSUM sums from d-i: $FILE"
                    LOC=dists/$DI_CODENAME/main/installer-$ARCH/`basename $VER`/images
                    for ENTRY in `cat $FILE | sed 's/  /:/g'`
                    do
                        PATH=`echo $ENTRY | /bin/sed "s?^.*:\./?$MIRROR/$LOC/?g"`
                        CSUM=`echo $ENTRY | /bin/sed 's/:.*$//g'`
                        SIZE=`/usr/bin/stat -c %s $PATH`
                        printf '%s  %12.12s  %s\n' $CSUM $SIZE $PATH
                    done | sort | uniq >> $OUT
                 fi
            done
            ;;             
    esac
done

exit 0

