#! /bin/bash
#========================================================
##GUI for shred
#Author: D.M.-Wilhelm (leiche)
#kellerleicheorig at aol.com
#Website kellerleiche.bplaced.net
#license gpl
#Wed Jan 20 2010
#Release 2010-2020
#last modification Mon Sep 26 2011
#move to yad
#Special thanks goes to Archie
#Add directory to shred, idea by travisn000 
#Reform GUI on Mon Oct 10 2011 by travisn000
#Modification Sun Mar 29 2020:
#The combobox (GtkCombo) widget has been removed from 
#GTK+ 3 and comboboxtext or comboboxentry are recommended 
#as replacements.
#Modification Sat Jan 09 2021:
#adjust GUI for the GERMAN LANGUAGE
#======================================================== 

Encoding=UTF-8
export WICON="/usr/share/icons/shred.png"
export TITLE="ShredGUI"
export VERSION=1.4

#
# i18n - Internationalization - Internationalisierung
export TEXTDOMAIN=shred_GUI
export TEXTDOMAINDIR="/usr/share/locale"







#=================================================
# function - Funktion 
parse_settings() {
  SHRED_OPTIONS="fvz" ##force,verbose,zero
  if [ "echo $SHRED_DELETE" == "echo true" ]; then SHRED_OPTIONS+="u"; fi
  if [ $SHRED_TIMES -gt 0 ] && [ $SHRED_TIMES -le 50 ]; then
    SHRED_OPTIONS+="n $SHRED_TIMES"
  fi
  echo "SHRED_OPTIONS: $SHRED_OPTIONS"

}



#=================================================
# function - Funktion 
main_dialog () {


export MAIN_DIALOG='
<window title=" - ShredGUI - '$VERSION'" resizable="true" icon-name="archiving_section">
<vbox spacing="25" >
	<pixmap>
		<input file>/usr/share/icons/archiving_section.png</input>
	</pixmap>
	<hbox spacing="25" >
		<text use-markup="true" justify="2" >
			<label>"'`gettext $"<big>Welcome to Shred_GUI</big>

With this utility you can delete files in a way that makes them non-recoverable.  
Multiple file and directory selection is enabled. Selecting a directory will 
recursively include all files in all of its sub-directories.

<b>Please use cautiously as the effects are permanent!</b>"`'"</label>
		</text>
	</hbox>

	<frame '`gettext $" Settings "`'>
		<checkbox>
		  <label>"'`gettext $" Remove files after shred "`'"</label>
		  <variable>SHRED_DELETE</variable>
		  <action>if true enable:SHRED_FOLDERS</action>
		  <action>if false disable:SHRED_FOLDERS</action>
		</checkbox>
		<checkbox sensitive="false">
		  <label>"'`gettext $" Remove folders after shred "`'"</label>
		  <variable>SHRED_FOLDERS</variable>
		</checkbox>
	<hbox>
		<text>
			<label>"'`gettext $"# of times to shred each file: "`'"</label>
		</text>
		<comboboxentry value-in-list="true">
		  <variable>SHRED_TIMES</variable>
		  <item>1</item>
		  <item>2</item>
		  <item>3</item>
		  <item>4</item>
		  <item>5</item>
		  <item>15</item>
		  <item>25</item>
		  <item>50</item>
		</comboboxentry>
	</hbox>
	</frame>
	<hbox start="true">
		<button>
			<label>"'`gettext $" Select Files / Folders "`'"</label>
			<input file icon="document-save"></input>
			<action>export USR_SELECT=$(yad --window-icon="$WICON" --title="- $TITLE-$VERSION -" --file-selection --multiple --separator="|" --width=800 --height=600 ) && echo "USR_SELECT=\"$USR_SELECT\""</action>
			<action type="exit">continue</action>
		</button>
		<button>
			<label>"'`gettext $" Cancel "`'"</label>
			<input file icon="gtk-delete"></input>
			<action type="exit">cancel</action>
		</button>
	</hbox>
</vbox>
</window>
'


echo
#	Eval gtkdialog stdout into bash variables
I=$IFS; IFS=""
for STATEMENT in  $(gtkdialog --center --program=MAIN_DIALOG); do
	echo $STATEMENT && eval $STATEMENT
done
IFS=$I


if [ "$EXIT" == "cancel" ]; then 
    yad --window-icon=$WICON --title=$TITLE-$VERSION \
    --image=gtk-cancel \
    --text $"Shred operation aborted..  \nExiting." \
    --button="gtk-ok:0" --width=250
    exit
fi

parse_settings

while [ "$EXIT" != "cancel" ] && [ "$USR_SELECT" == "" ]; do 
  main_dialog;
done
confirm_items

}  ############# end main_dialog ###############




#=================================================
# function - Funktion 
confirm_items () {
  unset file_list
  my_UID=`id -u`
  my_delimiter="|"

  echo -e $"User selection: \n$USR_SELECT\n"

##########  start IFS change #############
  default_IFS="$IFS"
  IFS="$my_delimiter"

  for usr_selection in $USR_SELECT; do 
      file_list+=$( find "$usr_selection" -type f -uid $my_UID -exec echo -n "{}$my_delimiter" \; )
done

  yad --window-icon=$WICON --title=$TITLE-$VERSION --window-icon="$WICON" --title="$TITLE-$VERSION" --width=500 --height=250 --separator="$my_delimiter" \
	--text $"The following files have been selected for shredding:" \
	--list --column=$"Files to be Shredded" `echo "$file_list"` --print-all

  response=$?
  if [ $response -eq 0 ]; then 
    shred_items 
  else
    main_dialog;
  fi

  IFS="$default_IFS"; unset IFS
##########  end IFS change #############

}






#=================================================
# function - Funktion 
shred_items () {
  error_status=0
  my_UID=`id -u`
  my_delimiter="|"


##########  start IFS change #############
  default_IFS="$IFS"
  IFS="$my_delimiter"

  for usr_selection in $USR_SELECT; do 
    find "$usr_selection" -type f -uid $my_UID -exec shred -$SHRED_OPTIONS '{}' \;
      error_status=`echo $error_status + $? |bc`
    echo -e $"\n\nerror_status = $error_status\n\n"
  done

  if [ "$SHRED_FOLDERS" == "true" ]; then
    for usr_selection in $USR_SELECT; do 
      if [ -d "$usr_selection" ]; then
	find "$usr_selection" -depth -type d -empty -uid $my_UID -exec rmdir -v {} \;
	error_status=`echo $error_status + $? |bc`
	echo -e $"\n\nerror_status = $error_status\n\n"
      fi
    done
  fi

  IFS="$default_IFS"; unset IFS
##########  end IFS change #############

  if [ $error_status -eq 0 ]; then
    yad --window-icon=$WICON --title=$TITLE-$VERSION --image=gtk-dialog-info --text $"Shred operation completed successfully!" --button="gtk-ok:0"
    exit
  else
    yad --window-icon=$WICON --title=$TITLE-$VERSION --image=gtk-dialog-error --text $"Shred operation completed, but some directories may require manual removal." --button="gtk-ok:0"
    exit
  fi
}




#main_dialog

while [ "$EXIT" != "cancel" ] && [ "$USR_SELECT" == "" ]; do 
  main_dialog;
done
