#!/bin/bash
#< Script to check for empty subdirectories within a specified directory

BASENAME="/usr/bin/basename"
ECHO="/bin/echo"
FIND="/usr/bin/find"
GREP="/usr/bin/grep"
WC="/usr/bin/wc"

THISPROG=$( ${BASENAME} $0 )

function print_error {
   ${ECHO} "Error: $@" >&2
}

function print_usage {
   {
      ${ECHO} "Usage: ${THISPROG} <directory>"
   } >&2
}

function perform_find {
   ${FIND} "${DIRECTORY}" -type d -maxdepth 1 -print | while read DIR; do
      FILECOUNT=$( ${FIND} "${DIR}" -maxdepth 1 -print | ${GREP} -v "^${DIR}$" | ${WC} -l )
      if [ "${FILECOUNT}" -eq "0" ]; then
         ${ECHO} "${DIR} is empty"
      fi
   done
}

if [ "$#" -eq "0" ]; then
   DIRECTORY="."
elif [ "$#" -eq "1" ]; then
   if [ ! -d "$1" ]; then
      print_error "Invalid directory specified"
      exit 1
   fi
   DIRECTORY="$1"
else
   print_usage
   exit 1
fi

perform_find

exit 0