#!/usr/bin/env bash

# Set the variable for bash behavior
shopt -s nullglob
shopt -s dotglob

# Die if dir name provided on command line
[[ $# -eq 0 ]] && { echo "" ; echo "   Usage: $0 dir-name"; echo "" ; exit 1; }

# Check for empty files using arrays
chk_files=(${1}/*)

echo ""
(( ${#chk_files[*]} )) && echo "   Files found in '$1' directory." || echo "   Directory '$1' is empty."
echo ""

# Unset the variable  for bash behavior
shopt -u nullglob
shopt -u dotglob


# ---------------------------------------------

# Check if directory is empty
#
is_empty_directory () {

   # Set the variable for bash behavior
   shopt -s nullglob
   shopt -s dotglob

   # Die if no dir name was provided on command line
   if [[ $# -eq 0 ]]; then

      echo ""
      echo "   [ Error ]: Wrong call of function 'is_empty_directory ()'.

               Usage: is_empty_directory <dir-name>"
      echo ""
      return 1
   fi

   chk_empty_dir=(${1}/*)

   # Unset the variable  for bash behavior
   shopt -u nullglob
   shopt -u dotglob

   if (( ${#chk_empty_dir[*]} )) ; then

      # Files found in $1 directory (send false)
      return 1
   else

      # Directory $1 is empty. (send true)
      return 0
   fi

}

echo ""
if $(is_empty_directory "${1}") ; then
   echo "   Directory '$1' is empty."
else
   echo "   Files found in '$1' directory."
fi
echo ""

exit 0
