#! /bin/bash
#Pmirror - gui for mirdir
#Sigmund Berglund
#Copyright 2007, 2008

#------------------------------
#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.3"
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`
echo "echo " $SOURCEDIR > /tmp/pm_source_dir
echo "echo " $MIRRORDIR > /tmp/pm_mirror_dir
echo -e "\c" > /tmp/pm_mirdir
chmod 777 /tmp/pm_source_dir
chmod 777 /tmp/pm_mir*

#-----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 " It seems to be your first run of Pmirror.\n Please read HELP for information." > /tmp/pm_progress
else
	echo -e " Last Mirror of listed directory was "$DAYS" days ago." > /tmp/pm_progress
fi

export Pmirror='
<window title="Pmirror - mirror/backup utility">
<vbox>
 <hbox>
  <text><label>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>
   <action>refresh:SOURCEDIR</action>
  </button>
 </hbox>
 <hbox>
  <text><label>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>
   <action>refresh:MIRRORDIR</action>
  </button>
 </hbox>
 <frame Options>
  <checkbox>
   <label>Only simulate mirror</label>
   <variable>CHECK1</variable>
   <default>yes</default>
  </checkbox>
  <checkbox><label>View information</label><variable>CHECK2</variable></checkbox>
  <checkbox>
   <label>Quick file compare</label>
   <variable>CHECK3</variable>
   <default>yes</default>
  </checkbox>
  <checkbox><label>Do not compare ownership</label><variable>CHECK4</variable></checkbox>
  <checkbox><label>Do not compare date/time</label><variable>CHECK5</variable></checkbox>
  <hbox>
   <text><label>Exception file</label></text>
   <entry><variable>EXCEPTION_FILE</variable><default>none</default></entry>
   <button>
    <input file stock="gtk-open"></input>
    <action type="fileselect">EXCEPTION_FILE</action>
    <action>refresh:EXCEPTION_FILE</action>
   </button>
  </hbox>
 </frame>
 <frame info>
  <text><input file>/tmp/pm_progress</input></text>
 </frame>
 <hbox>
  <button help>
   <action>`Xdialog --wrap --screencenter --left --title "Pmirror - HELP" --msgbox "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- No warranty.\n\n- Sigmund Berglund --- September 2007" 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"/`
	MIRRORDIR=`echo "$MIRRORDIR"/`
	#-----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 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 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\nIt seems that all went well.\n" >> /tmp/pm_progress
		else
			echo -e "\n\n\nSomething 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 -e "Configuration file is updated.\n" >> /tmp/pm_progress
		fi
	else
		rm -f /tmp/pm_*
		exit
	fi
else
	rm -f /tmp/pm_*
	exit
fi
