#!/bin/sh
# copy-fast  - right click option to copy a file to a partition
#=================================================================#
# Localisation: don570 (@ murga-linux.com/puppy) '2012            #
# Released under GNU GPL (http://www.gnu.org/licenses/)           #
#=================================================================#
# version 1.0 june 1 2013  by don570
# version 1.1 june 7 2013  - add folders to list
# version 1.2 june 8 - corrected quotes around LINE
# version 1.3 june 9 - filename in window
# version 1.4 june 10 -  width request="300"  for combo
# version 1.5 june 11 -  width VARIED BETWEEN 300 AND 150
# version 1.6 june 12 -  folders are copied with rsync -a  for mp3 players
# version 1.7 june 13 -  windows "2"   -  EXIT variable changed
# version 1.8 june 15 -  widen buttons for foreign languages
# version 1.9 june 16 -  removed desktop file from package  - folder copy has log message
# version 2.0 july 20 - Corrected Carolina distro bug with yaf-splash
# version 2.1 july 23  - added Spanish translation
# version 2.3  aug 2014 - added windows share file - changed no files found
# version 2.4  dec 2014 - added /mnt/network share

export TEXTDOMAIN=copy-fast
export OUTPUT_CHARSET=UTF-8
for P in gtkdialog4 gtkdialog3 gtkdialog; do
  GTKDIALOG=$(which $P) && break
done

export VERSION="2.4"
export FILEPATH="$1"

mkdir -p /tmp/copy-fast/copy_temp_$$
mkdir -p /root/.config/copy-fast
mkdir -p /tmp/rsync-results
export TEMPDIR=/tmp/copy-fast/copy_temp_$$
export TEMPFOLDER=/root/.config/copy-fast

rm -f  /tmp/rsync.log
echo -e "$(gettext 'Current date:') $(date +%c)\n$(gettext 'Also recorded in /tmp/rsync-results.')\n$(gettext 'Please wait patiently for completion.')\n" > /tmp/rsync.log


TEXT=$(gettext 'A list of copied files is available at /tmp/rsync-results')

# create a list of drives that are mounted
rm -f $TEMPDIR/list # start with no drive list
df -h > $TEMPDIR/df.tmp
cp  $TEMPDIR/df.tmp $TEMPDIR/df.tmp2

# find share folder
sed -n '/SharedDocs/p' $TEMPDIR/df.tmp > $TEMPDIR/df.tmp3

SHAREFOLDER=$(cat $TEMPDIR/df.tmp3  | awk '{ print$6 }' | sed 's/\/root\/[a-Z]*\///;s/\/mnt-point//' )
DESTINATION=$(cat $TEMPDIR/df.tmp3  | awk '{ print$6 }')

if [ -z "$SHAREFOLDER" ] ; then   # needed for old versions of df
SHAREFOLDER=$(cat $TEMPDIR/df.tmp3  | awk '{ print$5 }' | sed 's/\/root\/[a-Z]*\///;s/\/mnt-point//' )
SHAREFOLDER=$(echo $SHAREFOLDER) # eliminate line
DESTINATION=$(cat $TEMPDIR/df.tmp3  | awk '{ print$5 }')
DESTINATION=$(echo $DESTINATION) # eliminate line
fi

#check for two connections to fileserver -->  2 lines in variable
LINECOUNT=$(echo "$SHAREFOLDER" | wc -l )
if [ $LINECOUNT -eq 2 ] ;then  # get rid of second line
echo "$SHAREFOLDER" >  $TEMPDIR/tmpsharefolder 
echo "$DESTINATION" >  $TEMPDIR/tmpdestination 
read SHAREFOLDER < $TEMPDIR/tmpsharefolder 
read DESTINATION  <  $TEMPDIR/tmpdestination
fi

#  IF SAMBA TNG IS USED THEN CHECK FOR /mnt/network/PUPSERVER
sed -n '/\/mnt\/network\//p' $TEMPDIR/df.tmp > $TEMPDIR/df.tmp3
DESTINATION2=$(cat $TEMPDIR/df.tmp3  | awk '{ print$6 }')

# changewidth  if folder has been chosen
WIDTH=150
if [ -e  /root/.config/copy-fast/folder-choice ];then 
WIDTH=300
fi


NUM=`wc -l $TEMPDIR/df.tmp | awk '{ print$1 }'`

while [ $NUM -ne 0 ];do
let "NUM=$NUM-1"
read LINE < $TEMPDIR/df.tmp
DISK=`echo "$LINE" | grep "/dev/sd" | grep "/mnt/" | awk '{ print$6 }'`

# full install
DISK2=`echo "$LINE" | grep "/dev/sd" | awk '{ print$6 }'`
if [  "$DISK2" = "/" ];then
DISK="/mnt/home"
fi


#first booting -  no pupsave
if [ "${DISK:0:18}" = "/initrd/mnt/dev_ro" ];then
DISK=`echo "$LINE" | awk '{ print$1 }'`
REAL_DISK="$DISK2" 
DISK=/mnt/"${DISK:5}"  # do a substitution of name
fi

# pupsave file used
if [ "$DISK" = "/initrd/mnt/dev_save" ];then
DISK=`echo "$LINE" | awk '{ print$1 }'`
DISK=/mnt/"${DISK:5}"
if [ -e /mnt/home  ];then
DISK="/mnt/home"
fi;fi


[ -n "$DISK" ] && echo "${DISK}" >> $TEMPDIR/list
sed -i '1d' $TEMPDIR/df.tmp
done

# add more to list of locations
echo "/usr/bin" >> $TEMPDIR/list
echo "/usr/sbin" >> $TEMPDIR/list
echo "/root/my-documents" >> $TEMPDIR/list
echo "/root" >> $TEMPDIR/list



cp -f $TEMPDIR/list $TEMPDIR/list2 # change name to list2
# add previous choice
if [ -f $TEMPFOLDER/selection.temp ];then
read LINE < $TEMPFOLDER/selection.temp
cat $TEMPDIR/df.tmp2 | grep -q "$LINE"  
if [ $? -eq 0 ];then # put previous selection on top of list
cat $TEMPFOLDER/selection.temp $TEMPDIR/list  > $TEMPDIR/list2
fi
# continue to put previous selection on top of list
case "$LINE" in
    /root)
    cat $TEMPFOLDER/selection.temp $TEMPDIR/list  > $TEMPDIR/list2;;
    /usr/bin)
    cat $TEMPFOLDER/selection.temp $TEMPDIR/list  > $TEMPDIR/list2;;
    /root/my-documents)
    cat $TEMPFOLDER/selection.temp $TEMPDIR/list  > $TEMPDIR/list2;;
    /usr/sbin)
    cat $TEMPFOLDER/selection.temp $TEMPDIR/list  > $TEMPDIR/list2;;
esac
fi

if [ -f $TEMPFOLDER/folder-choice ];then # add folder chosen
    cat $TEMPFOLDER/folder-choice $TEMPDIR/list2  > $TEMPDIR/list
    mv -f $TEMPDIR/list $TEMPDIR/list2
fi

rm -f $TEMPDIR/list # start with cleared list
cat $TEMPDIR/list2 |
while read LINE  # check for existence of path
do
[ -d "$LINE"   ] && echo "$LINE" >>  $TEMPDIR/list
done
 mv -f $TEMPDIR/list $TEMPDIR/list2

# akita needs home partition  added
. /etc/DISTRO_SPECS
if [ "$DISTRO_NAME" = "Akita Linux"  ];then
cat $TEMPDIR/df.tmp2 | grep -q /initrd/mnt/dev_ro2
[  "$?" = 0 ] && echo "/initrd/mnt/dev_ro2" >> $TEMPDIR/list2
fi 

# if a networking is available then there should be a share folder
# add to list 2
[ -n  "$SHAREFOLDER" ]  && echo  "$SHAREFOLDER" >> $TEMPDIR/list2

# add share folder if samba-tng used

[ -n  "$DESTINATION2" ]  && echo  "$DESTINATION2" >> $TEMPDIR/list2

function launchagain() {
# add folder choice to top of folder-choice list, then relaunch  to form list2 again
     sort -u $TEMPFOLDER/folder-choice >  $TEMPFOLDER/folder-choice.temp
	cat - $TEMPFOLDER/folder-choice.temp <<<"$SELECTION" > $TEMPFOLDER/folder-choice
	rm -f $TEMPFOLDER/folder-choice.temp
copy-fast  "$FILEPATH"  &  # launch again  to form list2 again
exit 0
}

function foldercopy() {
# change the name to full path of network folder
[  "$DRIVE" = "$SHAREFOLDER" ] && DRIVE="$DESTINATION"

if  [ "$DRIVE"/"$(basename "$FILEPATH")" =  "$FILEPATH/"$(basename "$FILEPATH")"" ] ;then   
Xdialog   --rc-file /root/.config/pclock/orangebox.rc  --under-mouse --title "" --ok-label "'$(gettext 'OK')'" --backtitle "\n$(gettext 'Sorry')"   --msgbox "$(gettext 'It is not recommended to copy a folder into itself.')" 0 0 &
launchagain
fi

if  [ -d "$DRIVE"/"$(basename "$FILEPATH")" ] ;then  #  do rsync -a of two folders to make identical
	Xdialog --title "$(gettext 'Log')" --under-mouse --ok-label "$(gettext 'OK')" --no-cancel  --logbox /tmp/rsync.log 500x370 & 

rsync -av  --modify-window=3601 --log-file=/tmp/rsync.log "$FILEPATH" "$DRIVE" 
sync
cat /tmp/rsync.log | grep -q ">f"
[ "$?" -ne 0 ]  && echo -e  "\n$(gettext 'No files copied!')" >> /tmp/rsync.log 
echo -e  "\n$(gettext 'Finished!')" >> /tmp/rsync.log 
cp  /tmp/rsync.log   /tmp/rsync-results/rsync`date +%F_%T`
rm -rf /tmp/copy-fast
exit 0
fi

if  [ -f "$DRIVE"/"$(basename "$FILEPATH")" ] ;then
Xdialog  --rc-file /root/.config/pclock/orangebox.rc  --ok-label "$(gettext 'OK')" --backtitle "\n\n $(gettext 'Sorry. There is a file with the same name.')"  --msgbox  "" 0 0 & 
rm -rf /tmp/copy-fast
exit 0
fi

# simple copy of a folder	
Xdialog --title "$(gettext 'Log')" --under-mouse  --ok-label "$(gettext 'OK')" --no-cancel  --logbox /tmp/rsync.log 500x370 & 

rsync -a  --modify-window=3601 --log-file=/tmp/rsync.log "$FILEPATH" "$DRIVE"
sync
echo -e  "\n$(gettext 'Finished!')" >> /tmp/rsync.log
cp  /tmp/rsync.log   /tmp/rsync-results/rsync`date +%F_%T`
rm -rf /tmp/copy-fast	
}


# create the combobox with this function
function funccboTextCreate() {
      echo '<input file>'$TEMPDIR/list2'</input>'     	
	     
}

###############################################
#                                             #
#               M A I N   G U I               #
#                                             #
###############################################
export SETTINGS='
<window title="'$(gettext 'Fast Copy')' '$VERSION'" resizable="false" decorated="true">
<hbox> 
<vbox>
           <text height-request="10"><label>""</label></text>
           <text><label>"'$(basename "$FILEPATH")'"</label></text>
           <text height-request="10"><label>""</label></text>
             <hseparator></hseparator>
            <text>
                      <label>"'$(gettext 'Copy to...')'"</label>
            </text>
           
           <hbox homogeneous="true"> <comboboxtext case-sensitive="false" value-in-list="true" width-request="$WIDTH" space-expand="false" space-fill="false">
                    <variable>DRIVE</variable>
                    
                     '"$(funccboTextCreate)"'
            </comboboxtext>  
            </hbox>	  
			 
             
      <text height-request="20"><label>""</label></text>	
<hbox>
      <button  space-expand="false" space-fill="false"   tooltip-text="'$(gettext 'Help')'"> 
                      <input file stock="gtk-help"></input>
            <action>Xdialog --title "'$(gettext 'Help')'"  --rc-file /root/.config/pclock/orangebox.rc  --ok-label "'$(gettext 'OK')'" --backtitle "\n'$TEXT' \n "  --msgbox  "" 0 0 &</action>
      </button> 

       <text space-expand="true" space-fill="true">
            <label>""</label> 
       </text> 


<hbox> <text height-request="20"><label>""</label></text></hbox>
	
<hbox> 
<entry accept="directory" visible="false" width-request="10">
          <variable>SELECTION</variable>
</entry>
<button>
         <input file stock="gtk-add"></input>
          <action type="fileselect">SELECTION</action>
          <action>refresh:SELECTION</action>
          <action type="exit">SEARCH</action>
</button>
<text><label>"'$(gettext 'Add a folder to list')'"</label></text>
</hbox></hbox>
     			<hseparator></hseparator>
		
		  <hbox homogeneous="true"><hbox>
       <button width-request="120">
        <variable>CANCEL</variable>
        <label>"'$(gettext 'Cancel')'"</label>
        <input file icon="gtk-no"></input>
        <action>exit:EXIT</action>
      </button>
      
       <button can-default="true" has-default="true" use-stock="true" width-request="120">
      <input file stock="gtk-yes"></input>
        <label>"'$(gettext 'Copy')'"</label>
          <action>echo "$DRIVE" > $TEMPFOLDER/selection.temp</action>
        <action type="exit">COPY</action>
      </button>      
</hbox>	</hbox>
</vbox>

  </hbox> 
</window>
'
I=$IFS; IFS=""
for STATEMENTS in  $($GTKDIALOG -c -p SETTINGS); do
  eval $STATEMENTS
done
IFS=$I

[ "$EXIT" = "EXIT" ] || [ "$EXIT" = "abort" ] && exit 0

# to stop a copy after a cancel during folder-choice
if  [ -z "$SELECTION" ]  &&  [ "$EXIT" = "SEARCH" ];then 
copy-fast  "$FILEPATH"  &
exit 0
fi 

#launch again after a folder-choice  
if  [ -n "$SELECTION" ] ;then 
 launchagain
fi 
   
if [  -d "$FILEPATH" ];then
foldercopy 
sleep .1
killall yaf-splash 2>/dev/null
rm -rf /tmp/copy-fast
exit 0
fi




[  "$DRIVE" = "$SHAREFOLDER" ] && DRIVE="$DESTINATION"



if  [ -f "$DRIVE"/"$(basename "$FILEPATH")" ] ;then
Xdialog --wrap --title "$(gettext 'Question?')" \
        --yesno "$(gettext 'There is a file with the same name.') \

$(gettext 'Do you want to overwrite it?')\n" 0 0

case $? in
  0)
yaf-splash -timeout 0  -placement top  -bg orange  -outline 0 -margin 10 -text  "$(gettext 'Copying  a file, please wait...')"  & PID=$!
    cp -f "$FILEPATH" "$DRIVE"
    sync
    kill $PID 2>/dev/null
    exit 0
    ;;
    
  *)
  rm -rf /tmp/copy-fast
    exit ;;
esac

fi
# make copy if no file already exists
yaf-splash -timeout 0  -placement top  -bg orange  -outline 0 -margin 10 -text  "$(gettext 'Copying  a file, please wait...')"  & PID=$!
       cp  "$FILEPATH" "$DRIVE" 2> /dev/null
       sync 

kill $PID

rm -rf /tmp/copy-fast
exit 0
