#!/bin/sh

echo "Starting script for calculating deltas. This part calculates the difference"
echo "Reading dumps to process from dumps_to_calculate_delta.txt"
line_n=0
mkdir deltas
while read -r line ; do
    # Initialize folder, unpack and sort. $line has the full path
		folder_new_name=$(basename $line)
    folder_new_name="${folder_new_name%%.*}"
    echo "Name of folder: $folder_new_name"
#		echo "Saving sorted edge file..."
#		tar -zcvf "$folder_new_name".tar.gz "$folder_new_name"/edgefile_sorted.tsv
		# Assumption: kgtk_compressed_dumps exists
#		mv "$folder_new_name".tar.gz kgtk_compressed_dumps
		#if it is the first line, then save it as old. If not replace old with new when done
		if [ $line_n -lt 1 ]
		then
			folder_old_name=$folder_new_name
			echo $folder_old_name
		fi
		if [ $line_n -gt 0 ]
    then
			delta_name=delta"$folder_old_name"_"$folder_new_name"
			delta_folder=deltas/"$delta_name"
			echo "Creating folder for $delta_name in $delta_folder"
			mkdir $delta_folder
			echo "Calculating deltas between $folder_old_name and $folder_new_name"
			python3 kgtk-delta-wikidata/calculateDelta/calculate_deltas.py -o $folder_old_name/edgefile_sorted.tsv -n $folder_new_name/edgefile_sorted.tsv -d $delta_folder
			tar -zcvf "$delta_name".tar.gz $delta_folder
			mv "$delta_name".tar.gz deltas_compressed/
			folder_old_name=$folder_new_name
    fi
		line_n=`expr $line_n + 1`
done < dumps_to_calculate_delta.txt
#dumps_to_process.txt
