# Shell script to show the differences between two consecutive captures of crunchyroll lists

# Path to crdl.pl (same dir as this script)
path=$(dirname "$0")
#echo ${path}

if [[ ("$#" == 1 && $1 == "diff") ]] ; then

    echo "Re-doing diff"

    timestamp=`cat timestamp.txt`
    #echo ${timestamp}

else

    # Capture flat list and read associated timestamp
    ${path}/crdl.pl browse -json
    timestamp=`cat timestamp.txt`
    
if [[ ! -s cr_data_browse_${timestamp}.txt ]]
then
    echo
    echo "  Retrieval failed, update tokens in config file and try again."
    echo
    exit 1
fi

    # Capture watchlist with the same timestamp and sort alphabetically, while we're at it
    ${path}/crdl.pl watchlist -json -timestamp ${timestamp}
    sort < cr_data_watchlist_${timestamp}.txt > cr_data_watchlist_${timestamp}.sorted.txt
    
    # Update symlinks
    # Before calling this script, symlink 'latest' needs to point at last week's list.txt file
    mv latest previous
    ln -s cr_data_browse_${timestamp}.txt latest
    #echo ${timestamp}

fi

# Create unified diff, extract just the header and changed lines
diff -duw previous latest | grep '^[-+]' > cr_data_browse_${timestamp}.diff.txt

# Create side-by-side diff
diff -yw previous latest > cr_data_browse_${timestamp}.diff.sidebyside.txt

# Extract removed, added, and changed entries; trim to just the name and separator
#grep '<\t' cr_data_browse_${timestamp}.diff.sidebyside.txt | sed 's/ - .*$//' | sed 's/^/-/' > cr_data_browse_${timestamp}.diff.removed.txt
#grep '>\t' cr_data_browse_${timestamp}.diff.sidebyside.txt | sed 's/ - .*$//' | sed 's/^.*>\t/+/' > cr_data_browse_${timestamp}.diff.added.txt
#grep '|\t' cr_data_browse_${timestamp}.diff.sidebyside.txt | sed 's/^\(.*\) *|\t*/-\1\n+/' | sed 's/ - .*$//' > cr_data_browse_${timestamp}.diff.changed.txt
# Extract removed, added, and changed entries
grep '<\t' cr_data_browse_${timestamp}.diff.sidebyside.txt | sed 's/^/-/' > cr_data_browse_${timestamp}.diff.removed.txt
grep '>\t' cr_data_browse_${timestamp}.diff.sidebyside.txt | sed 's/^.*>\t/+/' > cr_data_browse_${timestamp}.diff.added.txt
grep '|\t' cr_data_browse_${timestamp}.diff.sidebyside.txt | sed 's/^\(.*\) *|\t*/-\1\n+/' > cr_data_browse_${timestamp}.diff.changed.txt

# Grep those names in the unified diff to get complete lines
head -n 2 cr_data_browse_${timestamp}.diff.txt > cr_data_browse_${timestamp}.diff.clean.txt
echo >> cr_data_browse_${timestamp}.diff.clean.txt

echo "Removed:" >> cr_data_browse_${timestamp}.diff.clean.txt
while read d; do grep -- "$d" cr_data_browse_${timestamp}.diff.txt; done < cr_data_browse_${timestamp}.diff.removed.txt >> cr_data_browse_${timestamp}.diff.clean.txt
echo >> cr_data_browse_${timestamp}.diff.clean.txt

echo "Added:" >> cr_data_browse_${timestamp}.diff.clean.txt
while read d; do grep -- "$d" cr_data_browse_${timestamp}.diff.txt; done < cr_data_browse_${timestamp}.diff.added.txt >> cr_data_browse_${timestamp}.diff.clean.txt
echo >> cr_data_browse_${timestamp}.diff.clean.txt

echo "Changed:" >> cr_data_browse_${timestamp}.diff.clean.txt
while read d; do grep -- "$d" cr_data_browse_${timestamp}.diff.txt; done < cr_data_browse_${timestamp}.diff.changed.txt >> cr_data_browse_${timestamp}.diff.clean.txt
echo >> cr_data_browse_${timestamp}.diff.clean.txt

# Clean up
rm cr_data_browse_${timestamp}.diff.sidebyside.txt
rm cr_data_browse_${timestamp}.diff.removed.txt
rm cr_data_browse_${timestamp}.diff.added.txt
rm cr_data_browse_${timestamp}.diff.changed.txt

