#!/bin/bash
#########################################################
#							#
# This are NScripts v3.4				#
#							#
# Licensed under the GNU GENERAL PUBLIC LICENSE 3	#
#							#
# Copyright 2007 - 2008 Christopher Bratusek		#
#							#
#########################################################

echo $NAUTILUS_SCRIPT_SELECTED_URIS > ~/.gnome2/temp_chkmd5_list

for file in $(cat ~/.gnome2/temp_chkmd5_list); do

origfile=$(echo $file | sed -e 's/file:\/\///g' -e 's/\%20/\ /g' -e 's/\.md5//g')

if [[ $(echo $origfile) == "*.md5" ]]; then

file_name=$(echo $file | sed -e 's/file:\/\///g' -e 's/\%20/\ /g')

export LANG=C; md5sum -c "$file_name" > /tmp/checkfile.txt 2>&1 | zenity --progress --title "Check md5" --text "Checking: $file" --pulsate --auto-close

P_OK="$origfile is OK!"
P_FAIL="$origfile is corrupt!"

Passed=`cat /tmp/checkfile.txt | fgrep -o -e "OK" | wc -l`
Failed=`cat /tmp/checkfile.txt | fgrep -o -e "FAILED" | wc -l`
   if [[ $Failed == 0 ]]; then
       StatusMessage="$P_OK"
   else
       StatusMessage="$P_FAIL"
   fi
echo -en "\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n" >> /tmp/checkmd5.txt
echo "$StatusMessage" >> /tmp/checkmd5.txt

elif 	[[ -a $origfile.md5 ]]; then 
	$file=$file.md5
	file_name=$(echo $file | sed -e 's/file:\/\///g' -e 's/\%20/\ /g')

	export LANG=C; md5sum -c "$file_name" > /tmp/checkfile.txt 2>&1 | zenity --progress --title "Check md5" --text "Checking: $file" --pulsate --auto-close

	P_OK="$origfile is OK!"
	P_FAIL="$origfile is corrupt!"

	Passed=`cat /tmp/checkfile.txt | fgrep -o -e "OK" | wc -l`
	Failed=`cat /tmp/checkfile.txt | fgrep -o -e "FAILED" | wc -l`
	   if [[ $Failed == 0 ]]; then
	       StatusMessage="$P_OK"
	   else
	       StatusMessage="$P_FAIL"
	   fi
	echo -en "\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n" >> /tmp/checkmd5.txt
	echo "$StatusMessage" >> /tmp/checkmd5.txt
	

else	echo -en "\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n" >> /tmp/checkmd5.txt
	echo "No MD5-Sum for $origfile" >> /tmp/checkmd5.txt
fi

rm -f /tmp/checkfile.txt

done

zenity --text-info --title "Result" --width=640 --height=480 --filename=/tmp/checkmd5.txt

rm -f ~/.gnome2/temp_chkmd5_list
rm -f /tmp/checkmd5.txt
