#!/bin/bash
#
#
# md5sum-mint 0.1 created by shane <linuxmint.com>
# This program 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 program 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 program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  
# USA
# dependencies
#	bash
#       zenity

################################################
#		TRANSLATIONS
#-----------------------------------------------
#	Default = English
title="MD5 Checksum                  "
title1="MD5 Checksum - File selection "
title2="MD5 Checksum - Error          "
text1="File selection"
text2="Calculating the checksum...   "
text3="Calculation abroted"
text4="MD5 Checksum:"
text5="File with the checksum:"
text6="<b>Select the actions you wish to perform</b>"
error1="You don't have read permission"
error2="You don't have write permission"
error3="No action selected. Exiting."
action0="Action"
action1="Display the calculated md5sum"
action2="Save the md5sum to a file"
case $LANG in
	######## Czech by churchyard ########
	cs* )
		title="Kontrolní součet MD5                "
		title1="Kontrolní součet MD5 - Vybrat soubor"
		title2="Kontrolní součet MD5 - Chyba         "
		text1="Vybrat soubor"
		text2="Probíhá výpočet...   "
		text3="Výpočet přerušen"
		text4="Kontrolní součet MD5:"
		text5="Soubor obsahující kontrolní součet MD5:"
		text6="<b>Zvolte akce, které chcete vykonat</b>"
		error1="Nemáte právo k čtení"
		error2="Nemáte právo k zápisu"
		error3="Žádná akce nebyla zvolena. Končím aplikaci."
		action0="Akce"
		action1="Zobrazit kontrolní součet MD5"
		action2="Uložit MD5 do souboru"
esac
#-----------------------------------------------

################################################

#Check if running under X
xcheck=`tty | cut -d '/' -f3`
if [ $xcheck != "pts" ]
then
	echo "Error: md5sum-mint must be run under X"
	exit 1
fi

#If no file is selected
if [ -z "$1" ]
then
	cd $HOME
	input=$(zenity --file-selection --title="$title1")
else
#If a file is selected
	if [ -f "$1" ]
	then
		input="$1"
	else
#If a directory is selected
		cd "$1"
		input=$(zenity --file-selection --title="$title1")
	fi
fi

#If still no file is selected (operation cancelled)
if [ -z "$input" ]
then
	exit 1
fi

#Check read permissions for input file
if [ ! -r "$input" ]
then
	zenity --error --title="$title" --text "<b>$input:</b>

$error1"
	exit 2
fi

#Get location and filename from input
wdir=`dirname "$input"`
file=`basename "$input"`

#cd into directory
cd "$wdir"

#debug
#zenity --info --text "Debug:
#dir is $wdir
#file is $file"

#Actions selection dialog
action=$(zenity --list --title="$title" --text "$text6" --checklist --column "" --column "$action0" TRUE "$action1" FALSE "$action2" --separator=":")

#debug
#zenity --info --text "Debug:
#$action ."

#If no action selected
if [ -z "$action" ]
then
	zenity --error --title="$title2" --text "$error3"
	exit 3
fi

#Check if saving to file is selected
if [ "$action" != "$action1" ]
then

#Check write permission to directory
	if [ ! -w "$wdir" ]
	then
		zenity --error --title="$title2" --text "<b>$wdir:</b>

$error2"
		exit 4
	fi
fi

#debug
#zenity --info --text "Debug:
#$action"

#Calculate md5sum, progress dialog and output to file
md5sum "$file" | tee >(cut -d ' ' -f1 > /tmp/sum) | (zenity --progress --title="$title" --pulsate --auto-kill --auto-close --text "$text2") 

#Find the zenity progress dialog
running=`ps aux | grep "$text2" | sed '/grep/ d'`

#Loop while progress dialog is running
until [ -z "$running" ]
do
	sleep 1
	running=`ps aux | grep "$text2" | sed '/grep/ d'`
done

#Check if md5sum is still running after zenity dialog has closed

sum=`cat /tmp/sum`
rm /tmp/sum

#debug
#zenity --info --text "Debug:
#$sum"

#Completed successfully! Now for the final actions!

#action for display and save
if [ "$action" = "$action1:$action2" ]
then
	rm "$file".md5sum
	echo $sum > "$file".md5sum
	zenity --info --title="$title" --text "<b>$file:</b>

$text4
<b>$sum</b>

$text5
<b>$file.md5sum</b>"
	exit
fi

#action for display only
if [ "$action" = "$action1" ]
then
	zenity --info --title="$title" --text "<b>$file:</b>

$text4
<b>$sum</b>"
	exit
fi

#action for save only
if [ "$action" = "$action2" ]
then
	rm "$file".md5sum
	echo $sum > "$file".md5sum
	zenity --info --title="$title" --text "<b>$file:</b>

$text5
<b>$file.md5sum</b>"
	exit
fi
killall md5sum
exit
