#! /bin/bash
#Pmirror - gui for mirdir
#Sigmund Berglund
#Copyright 2007, 2008, 2009, 2010, 2011, 2012, 2013
#120816 rodin.s: i18n
export TEXTDOMAIN=pmirror
. gettext.sh
#------------------------------
#Pmirror is released under the GNU General Public License (GPL). You have the right to use and modify this software in any way you like, so long as any derivative works remain under a GPL license.
    
#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY.
#See the GNU General Public License homepage for more details. <http://www.gnu.org/licenses/>.
#------------------------------

VERSION="0.6.2"
SOURCEDIR=`grep sourcedir $HOME/.pmirror_config 2> /dev/null | sed -e s/^.*\=//g`
MIRRORDIR=`grep mirrordir $HOME/.pmirror_config 2> /dev/null | sed -e s/^.*\=//g`
EXCEPTION_FILE=`grep exception_file $HOME/.pmirror_config 2> /dev/null | sed -e s/^.*\=//g`
[ ! "$EXCEPTION_FILE" ] && EXCEPTION_FILE=none
echo "echo " $SOURCEDIR > /tmp/pm_source_dir
echo "echo " $MIRRORDIR > /tmp/pm_mirror_dir
echo "echo " $EXCEPTION_FILE > /tmp/pm_exception_dir
echo -e "\c" > /tmp/pm_mirdir
chmod 777 /tmp/pm_source_dir
chmod 777 /tmp/pm_mir*
chmod 777 /tmp/pm_exc*

#-----get nr of days beetween last mirrorday and today
MIRRORDATE=`grep mirrordate $HOME/.pmirror_config 2> /dev/null | sed -e s/^.*\=//g`
TODAY_SEC=`date +%s`
TODAY_DAYS=`expr $TODAY_SEC / 60 / 60 / 24`
TMP=`echo $MIRRORDATE | sed -e "s/\// /g" | awk '{print $2} {print $1} {print $3}'` 
MIRRORDATE1=`echo $TMP | sed -e "s/ /\//g"`
MIRRORDATE_SEC=`date -d $MIRRORDATE1 +%s 2> /dev/null`
MIRRORDATE_DAYS=`expr $MIRRORDATE_SEC / 60 / 60 / 24 2> /dev/null`
DAYS=`expr $TODAY_DAYS - $MIRRORDATE_DAYS 2> /dev/null`
#-----
if test -z $MIRRORDATE; then
	echo -e " $(gettext 'It seems to be your first run of Pmirror.\n Please read HELP for information.')" > /tmp/pm_progress
else
	echo -e " `eval_gettext \"Last Mirror of listed directory was "\\\$DAYS" days ago.\"`" > /tmp/pm_progress
fi

export Pmirror='
<window title="'$(gettext 'Pmirror - mirror/backup utility')'">
<vbox>
 <hbox>
  <text><label>'$(gettext 'Source dir')'</label></text>
  <entry accept="directory"><variable>SOURCEDIR</variable><input>/tmp/pm_source_dir</input></entry>
  <button>
   <input file stock="gtk-open"></input>
   <action type="fileselect">SOURCEDIR</action>
  </button>
 </hbox>
 <hbox>
  <text><label>'$(gettext 'Mirror dir')'</label></text>
  <entry accept="directory"><variable>MIRRORDIR</variable><input>/tmp/pm_mirror_dir</input></entry>
  <button>
   <input file stock="gtk-open"></input>
   <action type="fileselect">MIRRORDIR</action>
  </button>
 </hbox>
 <frame '$(gettext 'Options')'>
  <checkbox>
   <label>'$(gettext 'Only simulate mirror')'</label>
   <variable>CHECK1</variable>
   <default>yes</default>
  </checkbox>
  <checkbox><label>'$(gettext 'View information')'</label><variable>CHECK2</variable></checkbox>
  <checkbox>
   <label>'$(gettext 'Quick file compare')'</label>
   <variable>CHECK3</variable>
   <default>yes</default>
  </checkbox>
  <checkbox><label>'$(gettext 'Do not compare ownership')'</label><variable>CHECK4</variable></checkbox>
  <checkbox><label>'$(gettext 'Do not compare date/time')'</label><variable>CHECK5</variable></checkbox>
  <hbox>
   <text><label>'$(gettext 'Exception file')'</label></text>
   <entry accept="directory"><variable>EXCEPTION_FILE</variable><input>/tmp/pm_exception_dir</input></entry>
   <button>
    <input file stock="gtk-open"></input>
	<action type="fileselect">EXCEPTION_FILE</action>
   </button>
  </hbox>
 </frame>
 <frame '$(gettext 'info')'>
  <text><input file>/tmp/pm_progress</input></text>
 </frame>
 <hbox>
  <button help>
   <action>`Xdialog --wrap --screencenter --left --title "'$(gettext 'Pmirror - HELP')'" --msgbox "'$(gettext 'Pmirror is a simple backup system to make one directory like another. First run it will copy the entire source-directory to the mirror-directory. The mirror function will then just update the mirror-directory. It will delete files on the mirror-directory if they dont exist on source-directory. Be careful, think twice and check settings again.\n\nThe mirror-directory should not be on the same harddisk that you stores your main data. Do NOT think your data is safe when saving mirror-files on another partition. It is still the same disk, and when it crashes, it all does. If you have only one disk, it may be better to burn files to CD/DVD.\n\n- Pmirror is a GUI for the mirdir package (19k).\n- $HOME/ is where your personal data is stored (e-mail, bookmarks...)\n- Config file: $HOME/.pmirror_config. Created after first mirror.\n- See also mirdir documentation.\n- Paths in exceptions file should point to destination directories.\n- No warranty.\n\n')'" 600x0`</action>
  </button>
  <button cancel></button>
  <button ok></button>
 </hbox>
</vbox>
</window>'

I=$IFS; IFS=""
for STATEMENTS in  $(gtkdialog3 --program Pmirror); do
	eval $STATEMENTS
done
IFS=$I
if [ "$EXIT" = "OK" ]; then
	SOURCEDIR=`echo "$SOURCEDIR"/ | sed -e "s%//%/%g"`
	MIRRORDIR=`echo "$MIRRORDIR"/ | sed -e "s%//%/%g"`
	[ ! -f "$EXCEPTION_FILE" ] && EXCEPTION_FILE=none
	#-----bulid mirdir command
	echo -e "mirdir \c" > /tmp/pm_mirdir
	echo -e "-e "$EXCEPTION_FILE "\c" >> /tmp/pm_mirdir
	if test $CHECK1 = true; then
		echo -e "-i \c" >> /tmp/pm_mirdir
		echo -e "\n $(gettext 'Pmirror will now simulate the backup process.\n If it all goes right, turn off the simulate option,\n and make a real mirror ....\n\n Choose OK to continue, and CANCEL to quit.')" > /tmp/pm_progress
	else
		echo -e "\n\n `eval_gettext \"WARNING!!!\n\n\n It may happend that Pmirror will delete files in \n \\\$MIRRORDIR .... \n\n Choose OK to continue, and CANCEL to quit.\"`" > /tmp/pm_progress
	fi
	if test $CHECK2 = true; then echo -e "-v \c" >> /tmp/pm_mirdir;fi
	if test $CHECK3 = false; then echo -e "-s \c" >> /tmp/pm_mirdir;fi
	if test $CHECK4 = true; then echo -e "-o \c" >> /tmp/pm_mirdir;fi
	if test $CHECK5 = true; then echo -e "-d \c" >> /tmp/pm_mirdir;fi
	echo -e '"'"$SOURCEDIR"'"' "\c" >> /tmp/pm_mirdir
	echo -e '"'"$MIRRORDIR"'"' "\c" >> /tmp/pm_mirdir
    	
	Xdialog --title "Pmirror" --screen-center --logbox /tmp/pm_progress 400x370
	if test "$?" = "0"; then
		#-----Run mirdir
		Xdialog --title "Pmirror" --screen-center --no-cancel --logbox /tmp/pm_progress 400x370 &
		/tmp/pm_mirdir >> /tmp/pm_progress 2>&1
		FINALCHECK=`cat /tmp/pm_progress | grep -m 1 "compared"`
		if test -n "$FINALCHECK"; then
			echo -e "\n\n\n$(gettext 'It seems that all went well.')\n" >> /tmp/pm_progress
		else
			echo -e "\n\n\n$(gettext 'Something went wrong during backup.\n- Are the "mirdir" package installed?\n- Does source- and mirror-directory exist?\n- Have you defined a exception file in right terms?\n- Read messages above and Help.')" >> /tmp/pm_progress
		fi
		#-----update config-file, if mirror without simulate option
		if test $CHECK1 = false; then
			echo "mirrordate=`date +%d/%m/%Y`" > $HOME/.pmirror_config
			echo "sourcedir=$SOURCEDIR" >> $HOME/.pmirror_config
			echo "mirrordir=$MIRRORDIR" >> $HOME/.pmirror_config
			echo "exception_file=\"$EXCEPTION_FILE\"" >> $HOME/.pmirror_config
			
			echo -e "$(gettext 'Configuration file is updated.')\n" >> /tmp/pm_progress
		fi
	else
		rm -f /tmp/pm_*
		exit
	fi
else
	rm -f /tmp/pm_*
	exit
fi
