#! /bin/bash

oldpath="$1"
newpath="$2"


if [ "${oldpath}" = "" -o "${newpath}" = "" ]; then
    echo usage: $0  old_dir  new_dir
    exit
fi


killall -9 mgdiff &> /dev/null
echo
diffcount=0


for oldfile in "${oldpath}"/*; do

    newfile="${newpath}/${oldfile##*/}"
    
    if [ "${oldfile##*.}" = "o" -o "${oldfile##*.}" = "lst" ]; then
        echo "${oldfile} not compared, uninteresting"
    elif [ ! -f "${oldfile}" -o ! -f ${newfile} ]; then
        echo "${oldfile} or ${newfile} is not a regular file"
    else
        if ! diff -abB "${oldfile}" "${newfile}" &> /dev/null; then
            echo "${oldfile}" and "${newfile}" are different
            sleep $[diffcount*diffcount/2]
            mgdiff "${oldfile}" "${newfile}" &
            diffcount=$[diffcount+1]
        fi
    fi
done


echo
if   [ "${diffcount}" = "0" ]; then
    echo "no differences found"
elif [ "${diffcount}" = "1" ]; then
    echo "1 file is different"
else
    echo ${diffcount} "files are different"
fi
echo
