#
# 
#Copyright Holder: Jansen Sena <jansen@comunidadesol.org>
#
#License:
#
#   This package is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This package is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this package; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
#   02111-1307, USA.
#
# ##################
#  System utilities
# ##################
#
MD5SUM="/usr/bin/md5sum"
LS="/bin/ls"
ECHO="/bin/echo"
ALL_UTILS="MD5SUM LS ECHO"
#
# #############
#  DEFINITIONS
# #############
#
MD5FILE_SUFFIX="md5sum"
SYNTAX_MSG="Usage: mkmd5sum4me.sh <dir> [<dir> [<dir>...]]"
#
# ###################################
#  Checking the number of parameters
# ###################################
#
if [ $# -eq 0 ] ; then 
    $ECHO $SYNTAX_MSG
    exit 2
fi
#
# ######################################################### 
#  Checking if all the utilities aer present in the system
# #########################################################
#
for util in $ALL_UTILS ; do 
    eval util_aux=\$$util
    if [ ! -x $util_aux ] ; then
	$ECHO "The $util_aux file is not present in your system or it doesn't have execution permission."
	$ECHO "Aborting."
	exit 1
    fi
done
#
# ########### 
#  FUNCTIONS
# ###########
#
#
# This function check if a directory exists and if there is execute permission on it.
# Syntax: CHECK_DIRECTORY <DIRECTORY PATH> <SHORT DIRECTORY TYPE DESCRIPTION>
#
function CHECK_DIRECTORY() {
    DIRECTORY=$1
    TYPE=$2
    if [ ! -d $DIRECTORY ] ; then
       $ECHO "The $DIRECTORY $TYPE directory does not exist or it is not a directory."
       $ECHO "Aborting."
       exit 1
    elif [ ! -x $DIRECTORY ] ; then
          $ECHO "The $DIRECTORY $TYPE directory does not have execute permission."
          $ECHO "Aborting."
          exit 1
    fi
}
#
# ######################
#  Checking directories
# ######################
#
for dir in $* ; do 
    CHECK_DIRECTORY $dir ""
done
#
# ########################################################
#  Calculting the md5 for all the files in each directory
# ########################################################
#
for dir in $* ; do 
    cd $dir
    FILES=`ls`
    #
    # To be solved: it will not work with there is some directory including white spaces.
    #
    $ECHO -n "Calculating MD5 sum for $dir directory..."
    for file in $FILES ; do 
	$MD5SUM $file > $file.$MD5FILE_SUFFIX 2>/dev/null
	if [ $? -ne 0 ] ; then 
	    $ECHO "There was some problem during the md5 calculation for $file in $dir directory."
	    $ECHO "Ignoring."
	fi
    done    
    $ECHO "done."
done
