#!/bin/sh

line_n=0
delta_folder=deltas
if [ ! -d $delta_folder ]; then
	mkdir $delta_folder
fi
while read -r line ; do
    # Initialize folder, unpack and sort. $line has the full path
    folder_new_name=$line
    #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
    fi
    if [ $line_n -gt 0 ]
    then
      delta_name=delta"$folder_old_name"_"$folder_new_name"
      delta_folder=deltas/"$delta_name"
			old=$folder_old_name/edgefile_sorted.tsv
			new=$folder_new_name/edgefile_sorted.tsv
			if [ -f  $old ]; then
        echo "OLD File: $old"
        if [ -f  $new ]; then
					echo "NEW File: $new"
					if [ ! -d $delta_folder ]; then
      			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 $old -n $new -d $delta_folder
      			tar -zcvf "$delta_name".tar.gz $delta_folder
      			mv "$delta_name".tar.gz deltas_compressed/
					fi
				fi
			fi
      folder_old_name=$folder_new_name
    fi
    line_n=`expr $line_n + 1`
done < dumps_to_calculate_delta.txt
