#!/bin/sh

ARG1=$1
ARG2=$2
if [ -z "$ARG1" ] || [ -z "$ARG2" ]; then
	echo one of the arguments is empty
	exit 1
fi


compare() {
	# replace replaces storage operation from "Changed" to "Added"
	# normalize replaces performs replace and sorts keys in lexicographic order
	# next we normalize every json file
	# and finally compare them as a whole
	jq --argfile x "$1" --argfile y "$2" \
      -n 'def replace: map(if (.state == "Changed") then (.state="Added") else . end);
          def normalize: .storage = (.storage | replace | sort_by(.key));
          ($x | map(normalize)) as $x
        | ($y | map(normalize)) as $y
        | $x | range(length) | . as $i | select($x[$i] != $y[$i]) | $x[$i].block | halt_error(1)'
}

if [ -f "$ARG1" ] && [ -f "$ARG2" ]; then
	compare "$ARG1" "$ARG2"
	if [ $? -ne 0 ]; then
		echo failed
		exit 1
	fi

    exit 0
fi

if [ ! -d "$ARG1" ] || [ ! -d "$ARG2" ]; then
	echo both arguments must have the same type and exist
	exit 1
fi

FIRST=$3
if [ -z "$FIRST" ]; then
    FIRST=1
fi

LAST=$4
if [ -z "$LAST" ]; then
    LAST=40 # 40_00000 block
fi

# directories contain 100k blocks
for i in `seq $FIRST $LAST`; do
	dir=BlockStorage_${i}00000
	echo Processing directory $dir

	# files are grouped by 1k blocks
	for j in `seq $(((i-1)*100 + 1)) $((i*100))`; do
		file=dump-block-${j}000.json
		compare "$ARG1/$dir/$file" "$ARG2/$dir/$file"
		if [ $? -ne 0 ]; then
			echo failed on file $dir/$file
			exit 1
		fi
	done
done