#! /bin/bash
#
# description: script to generate the md5sum from DEBs/RPMs download folder
# author: Ben Bois
# last update: 2010.05.01
#

if [ -z $1 ]
then
	echo ""
	echo "usage: $0 type"
	echo ""
	echo "type can be:"
	echo "        $0 DEB"
	echo "    or"
	echo "        $0 RPM"
	echo ""
	echo ""
	exit 0
fi

if [ ! -d "$1" ]
then
	echo ""
	echo "$1 must be a valid sub-folder"
	echo ""
	echo ""
	exit 0
fi

echo ""
echo "starting the job..."
echo ""

ARRAY=(`ls -1AB $1/`)
LEN=${#ARRAY[*]}

cd $1
i=0
while [ $i -lt $LEN ]; do
	SUBFOLDER=${ARRAY[$i]}
	if [ -d $SUBFOLDER ]
	then 
		RENFOLDER=`sed 's/_download//g'<<<$SUBFOLDER`
		echo "renaming '$SUBFOLDER' to '$RENFOLDER'..."
		mv $SUBFOLDER $RENFOLDER
		cd $RENFOLDER
		FILE=(`ls -1AB `)
		echo "file '$FILE' is md5suming..."
		md5sum $FILE > $FILE.md5
		cd ..
	fi
	let i++
done

echo ""
echo "job done!"
echo ""
exit 0


