#!/bin/sh

echo "Calculating deltas between the dumps extracted in this folder"
echo "Before executing this script, each dump must have been imported to KGTK in a folder"
echo "For example, 20170102 is the folder where I would find the edge_file.tsv associated with the dump 2017-01-02"

folder=.
mkdir deltas
line_n=0
for entry in "$folder"/*
do
  #echo "Processing: $entry"
  folder_new_name=$(basename "$entry")
  if [ $line_n -lt 1 ] 
  then
        folder_old_name=$folder_new_name
        echo $folder_old_name
  fi
  if [ $line_n -gt 0 ]
  then
        # Check if old and new files exist
				#echo "OLD $folder_old_name/edgefile_sorted.tsv"
				#echo "NEW $folder_new_name/edgefile_sorted.tsv"
        if  [[ -f "$folder_old_name"/edgefile_sorted.tsv ]] && [[ -f "$folder_new_name"/edgefile_sorted.tsv ]];
        then
            delta_name=delta"$folder_old_name"_"$folder_new_name"
            echo "Both old and new edge files exist. Checking if the delta $delta_name exists ..."
            delta_folder=deltas/"$delta_name"
            if ! [ -d "$delta_folder" ]; 
            then
						    echo "Creating folder $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/
            else
                echo "$delta_folder already exists. Not calculating delta again"
            fi
            folder_old_name=$folder_new_name
      fi
  fi
  line_n=`expr $line_n + 1`
done
