#!/bin/sh
# recursively delete empty directories

rmdir_empty() {
  # make default . explicit
  if [ -z "$1" ]; then
    set -- .
  fi

  # do the first level directories first
  for maxdepth_opt in "-maxdepth 1" ""; do
    for d in "$@"; do

      # directory might have been removed in the first level round
      if [ -z "$max_depth_opt" -a ! -e "$d" ]; then
        continue
      fi

      find "$d" -depth $maxdepth_opt \
        -path "." -prune -or \
        -type d -empty -exec rmdir -v {} \;
    done
  done
}

rmdir_empty "$@"
