#! /bin/bash
#####################################################################################################
# SecureCopyFiles with KDialog Progressbar - v0.1
#
# Copyright (C) 2009 Markus Schulze
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
##########################################################################################################################################

# the folder you've selected (%u)
FOLDER=${1}
# user and path of the remote pc (second parameter)
TO=${2}
# foldername (to create it remote)
FOLDERNAME=${FOLDER##*/}
# count files
CNT=`ls -l $FOLDER | grep ^- | awk '{print $9}' | wc -l`

dcopRef=`kdialog --title "Kopiervorgang" --progressbar "Kopiervorgang gestartet! Der Ordner wird jetzt kopiert. Je nach Größe kann sowas dauern..." $CNT`
dcop $dcopRef showCancelButton true
sleep 1

# get DEST and USER from  $TO
DEST=`echo $TO | cut -d : -f 1`
USER=`echo $DEST | cut -d @ -f 1`

ssh $DEST "mkdir -p /home/$USER/HomeShare/$FOLDERNAME"

find $FOLDER -type f -print0 | while read -d $'\0' file
do
	if [ `dcop $dcopRef wasCancelled` == "false" ]
	then
		dcop $dcopRef setLabel "Kopiere $file ==>  $TO"
		dcop $dcopRef showCancelButton true
		inc=$((`dcop $dcopRef progress` + 1))
		sleep 0.5
                scp "$file" $TO/$FOLDERNAME
		dcop $dcopRef setProgress $inc
		dcop $dcopRef setLabel "$file erfolgreich kopiert"
		dcop $dcopRef showCancelButton true
		sleep 0.75
	else
		kdialog --title "Abbruch" --sorry "Vorgang abgebrochen!"
		break
	fi	
done

dcop $dcopRef close
