#!/bin/sh

if [[ "$1" == "--help" || "$1" == "" ]] ; then \
  echo "Usage:" && \
  echo && \
  echo "winetreediff DIRNAME [ -ac ] [ -c ] [ -n ]" && \
  echo && \
  echo "Compares the wine source trees in the current directory and DIRNAME." && \
  echo && \
  echo "  -ac: ignore the './configure' file" && \
  echo "   -c: colorize the output (requires 'colordiff' in PATH)" && \
  echo "   -n: don't diff, just list new files." && \
  echo && echo ; \
else \
(
noslash=$( echo "$1" | sed s/\\/$// )
noslashquot=$( echo "$noslash" | sed s/\\//\\\\\\//g | sed s/\\./\\\\\\./g )
delme0=$( mktemp )
delme1=$delme0'_delme1.tmp.diff'
delme2=$delme0'_delme2.tmp.diff'

ac=$( if [[ "$2" == "-ac" || "$3" == "-ac" || "$4" == "-ac" ]] ; then echo '-x configure'; fi )

# echo "noslash=$noslash noslashquot=$noslashquot delme0=$delme0 delme1=$delme1 delme2=$delme2 ac=$ac"
rm -f "$delme1" "$delme2"
touch "$delme1" && touch "$delme2" && \
diff -ur --minimal --exclude-from=/home/greg/bin/winetreediff_excl $ac $noslash . | \
grep -v '^Only\ in\ .*wine$' | \
grep -v '^Only\ in\ .*dlls/rpcrt4/tests\: stringinput_server$' | \
grep -v '^Only\ in\ .*include\: config.h$' | \
grep -v '^Only\ in\ .*programs/avitools\: aviinfo$' | \
grep -v '^Only\ in\ .*programs/avitools\: aviplay$' | \
grep -v '^Only\ in\ .*programs/avitools\: icinfo$' | \
grep -v '^Only\ in\ .*programs/clock\: clock$' | \
grep -v '^Only\ in\ .*programs/cmdlgtst\: cmdlgtst$' | \
grep -v '^Only\ in\ .*programs/control\: control$' | \
grep -v '^Only\ in\ .*programs/expand\: expand$' | \
grep -v '^Only\ in\ .*programs/notepad\: notepad$' | \
grep -v '^Only\ in\ .*programs/osversioncheck\: osversioncheck$' | \
grep -v '^Only\ in\ .*programs/progman\: progman$' | \
grep -v '^Only\ in\ .*programs/regapi\: regapi$' | \
grep -v '^Only\ in\ .*programs/regedit\: regedit$' | \
grep -v '^Only\ in\ .*programs/regsvr32\: regsvr32$' | \
grep -v '^Only\ in\ .*programs/regtest\: regtest$' | \
grep -v '^Only\ in\ .*programs/rpcss\: rpcss$' | \
grep -v '^Only\ in\ .*programs/rundll32\: rundll32$' | \
grep -v '^Only\ in\ .*programs/start\: start$' | \
grep -v '^Only\ in\ .*programs/uninstaller\: uninstaller$' | \
grep -v '^Only\ in\ .*programs/view\: view$' | \
grep -v '^Only\ in\ .*programs/wcmd\: wcmd$' | \
grep -v '^Only\ in\ .*programs\: wineapploader$' | \
grep -v '^Only\ in\ .*programs/wineboot\: wineboot$' | \
grep -v '^Only\ in\ .*programs/winecfg\: winecfg$' | \
grep -v '^Only\ in\ .*programs/wineconsole\: wineconsole$' | \
grep -v '^Only\ in\ .*programs/winedbg\: winedbg$' | \
grep -v '^Only\ in\ .*programs/winefile\: winefile$' | \
grep -v '^Only\ in\ .*programs\: winelauncher$' | \
grep -v '^Only\ in\ .*programs/winemine\: winemine$' | \
grep -v '^Only\ in\ .*programs/winevdm\: winevdm$' | \
grep -v '^Only\ in\ .*programs/winepath\: winepath$' | \
grep -v '^Only\ in\ .*programs/winhelp\: hlp2sgml$' | \
grep -v '^Only\ in\ .*programs/winhelp\: winhelp$' | \
grep -v '^Only\ in\ .*programs/winver\: winver$' | \
grep -v '^Only\ in\ .*server\: wineserver$' | \
grep -v '^Only\ in\ .*stringinput_server\: stringinput_server$' | \
grep -v '^Only\ in\ .*tools\: bin2res$' | \
grep -v '^Only\ in\ .*tools\: fnt2bdf$' | \
grep -v '^Only\ in\ .*tools\: makedep$' | \
grep -v '^Only\ in\ .*tools\: mingwrap$' | \
grep -v '^Only\ in\ .*tools/widl\: widl$' | \
grep -v '^Only\ in\ .*tools/winebuild\: winebuild$' | \
grep -v '^Only\ in\ .*tools/winebuild\: winebuild.man$' | \
grep -v '^Only\ in\ .*tools/winedump\: winedump$' | \
grep -v '^Only\ in\ .*tools\: winegcc$' | \
grep -v '^Only\ in\ .*tools\: winewrap$' | \
grep -v '^Only\ in\ .*tools/wmc\: wmc$' | \
grep -v '^Only\ in\ .*tools/wrc\: wrc$' | \
if [[ "$2" == "-n" || "$3" == "-n" || "$4" == "-n" ]] ; then \
    grep '^Only\ in\ .*\:' ; \
else \
    ( tee "$delme1" | grep -v '^Only\ in\ .*\:' ) ; \
fi

if [[ "$2" != "-n" && "$3" != "-n" && "$4" != "-n" ]] ; then \
    cat "$delme1" | \
    grep '^Only\ in\ .*\:' | \
    sed s/^Only\ in\ // | \
    sed s/\:\ /\\// | \
    ( tee "$delme2" | grep "^\.\/" ) | \
    sed "s/^\.\//diff\ -u\ \/dev\/null\ \.\//" | \
    sed "s/$/;/" | \
    bash -C - ; \
fi

if [[ "$2" != "-n" && "$3" != "-n" && "$4" != "-n" ]] ; then \
    cat "$delme2" | \
    grep -v "^\.\/" | \
    sed "s/^$noslashquot\//diff\ -u\ \.\//" | \
    sed "s/$/\ \/dev\/null;/" | \
    ( cd $noslash && bash -C - ) ; \
fi

rm -f "$delme1" "$delme2"
) | \
if [[ "$2" == "-c" || "$3" == "-c" || "$4" == "-c" ]] ; then \
    colordiff ; \
else \
    tee /dev/null ; \
fi ; \
fi
