#!/bin/bash

. /etc/disco/client.cfg

SCRIPTROOT=$(dirname $(readlink -f $0))
if [ "$NOOP" != "" ]; then
    DISCOROOT=/var/disco/testfs/noop
else
    DISCOROOT=/var/disco/testfs/real
fi

cd $DISCOROOT

diff -r ./rootfs ./scratchfs |\
    grep -v "^Only in ./rootfs" |\
    sed s/"^Only in \.\/scratchfs\(.*\): \(.*\)"/"info: File: created \1\/\2 : (CONTENT)"/g |\
    sed s/"\/\/"/"\/"/g |\
    grep -v "File: created /.unionfs" |\
    sed s/"^Binary files .\/rootfs\(.*\) and .\/scratchfs.*"/"info: File: modified \1 : (OLDMD5SUM) => (NEWMD5SUM)"/g |\
    sed s/"^diff -r .\/rootfs\(.*\) .\/scratchfs\(.*\)"/"info: File: modified \1 :"/g > /tmp/$$.discofsdiff

find ./scratchfs/.unionfs -iname "*_HIDDEN~" |\
    sed s/"^.\/scratchfs\/.unionfs\(.*\)_HIDDEN~"/"info: File: deleted \1"/g >> /tmp/$$.discofsdiff

# Find the permissions/timestamp diffs from rsync
# FIXME: This should be the root of all diffs, not tacked on at the end.
rsync -ani ./scratchfs/* ./rootfs/ |\
    grep -v "_HIDDEN~\$" |\
    grep -v .unionfs |\
    sed s/"\.\/scratchfs"/""/g |\
    sed s/"^\(.*\)\$"/"info: \1"/g

# Swap out the (CONTENT) and (MD5SUM) hashes for actual content and md5s
# FIXME: Stop calling a python script for this.
cat /tmp/$$.discofsdiff | NOOP=$NOOP python ${SCRIPTROOT}/disco-fs-fixup.py > /tmp/$$.newfile
mv /tmp/$$.newfile /tmp/$$.discofsdiff


cat /tmp/$$.discofsdiff |\
    sed s/"^info:\(.*\)"/"${COLOR_CYAN}info:\1${COLOR_NORMAL}"/g |\
    sed s/"^warning: \(.*\)"/"${COLOR_YELLOW}warning: \1${COLOR_NORMAL}"/g |\
    sed s/"^error: \(.*\)"/"${COLOR_RED}error: \1${COLOR_RED}"/g
