#! /bin/bash 
# Last edited on 2017-07-28 20:04:28 by jstolfi

# Compares {DIR1} and {DIR2}.  Writes files
# ".only-1-dirs.dir" and ".only-2-dirs.dir" with 
# whole subdirectories that exist only in one of the two
# directories.

dir1="$1"; shift;
dir2="$1"; shift;

n=1;
for d in ${dir1} ${dir2} ; do 
  dfile=".all-dirs-${n}.dir"
  if [[ ! ( -d ${d} ) ]]; then
    echo "** ${d} does not exist or is not a directory" 1>&2; exit 1
  fi
  # List all directories into ${dfile} with "/" at end:
  ( cd $d && find ./ -type d -print ) \
    | sed -e 's:$:/:' -e 's:/[.]/:/:g' -e 's:^[.]/::g' \
    | egrep -e '[/]' \
    | sort | uniq \
    > ${dfile}
  n=$(( $n + 1 ))
done

# Compare the two directory lists:
dfile1=".all-dirs-1.dir"
dfile2=".all-dirs-2.dir"
for n in 1 2 ; do
  m=$(( 3 - $n ))
  tfile=".only-subs-${n}.dir" # Directories only in ${dir1}, with final "/".
  ofile=".only-dirs-${n}.dir" # Directories only in ${dir1} minus subs.
  
  bool ${n}-${m} ${dfile1} ${dfile2} \
    > ${tfile}
  echo "BOGUS DIR" >> ${tfile} # To get around the old fgrep bug.
  cat ${tfile} \
    | sed -e 's:/$::g' \
    | sort | uniq \
    | fgrep -v -f ${tfile} \
    > ${ofile}
done
