#!/bin/bash

current_date="`date '+%b %d, %Y'`"
label="Personal Backup, $current_date"

/usr/libexec/ubuntu-home-backup-gui "$label" | (

read action
read options
read media
read userlabel
read compression
read email_address
read ftp_server_address
read ftp_server_username
read ftp_server_password
read admin_home_backup
read admin_etc_backup
read admin_var_backup


#EXCLUDE FOR TEST PURPOSES $HOME/.mozilla/ $HOME/.evolution/ $HOME/.openoffice.org2/ 
CONFIGFILES="$HOME/.gaim/ $HOME/.gconf/ $HOME/.gconfd/ $HOME/.gnome/ $HOME/.gnome2/ $HOME/.gnome2_private/ $HOME/.themes/ $HOME/.icons"
TEMPDIR="/tmp/ubuntu-home-backup"
TEMPDIR_FINALDATA="/tmp/ubuntu-home-backup/$userlabel"
TEMPDIR_USERDATA="$TEMPDIR/$UID"
FILENAME="$userlabel"
NEWFILES_AGE="10"
LOGFILE="$userlabel.log"


if [ ! "$action" = "exit_signal" ]
then
mkdir "$TEMPDIR" 2> /dev/null
mkdir "$TEMPDIR_FINALDATA" 2> /dev/null
mkdir "$TEMPDIR_USERDATA" 2> /dev/null

echo "
============================================================
Backup Name:          $userlabel
Date Performed:       $current_date
Compression Used:     $compression

Medium Properties
Backup Medium Used:   $media
Email:                $email_address
FTP Server:           $ftp_server_address
FTP User:             $ftp_server_username

Administrator Backup
User HOME Backup:     $admin_home_backup
System ETC Backup:    $admin_etc_backup
System VAR Backup:    $admin_var_backup
============================================================
" > "$TEMPDIR_FINALDATA"/"$LOGFILE"

if [ "$action" = "Backup all files" ]
then
	if [ "$options" = "Backup new files only" ]
	then

		if [ ! -s $HOME/.ubuntu-home-backup/home/ ]
		then
		mkdir $HOME/.ubuntu-home-backup 2>/dev/null
		mkdir $HOME/.ubuntu-home-backup/home/ 2>/dev/null
		zenity --info --text="This is the first time you are going to perform a \"new files only\" in home directory backup. A copy of current files is needed. Click \"OK\" to continue."
		(cp -v -R $HOME $HOME/.ubuntu-home-backup/home/) | zenity --progress --pulsate --title="Creating comparison data" --text="Please wait while a copy of your home directory is being created" --auto-close
		fi

	(cp -v -R -u $HOME $HOME/.ubuntu-home-backup/home/
	 cp -v -R $HOME/.ubuntu-home-backup/home/ "$TEMPDIR_USERDATA") | zenity --progress --pulsate --title="Copying new home directory files" --text="Please wait while your home directory files are being copied" --auto-close 

	else
	(cp -v -R $HOME "$TEMPDIR_USERDATA") | zenity --progress --pulsate --title="Copying Home Directory" --text="Please wait while your home directory files are being copied" --auto-close
	fi

fi

if [ "$action" = "Backup all user settings" ]
then

	if [ "$options" = "Backup new files only" ]
	then

		if [ ! -s $HOME/.ubuntu-home-backup/settings/ ]
		then
		mkdir $HOME/.ubuntu-home-backup 2>/dev/null
		mkdir $HOME/.ubuntu-home-backup/settings/ 2>/dev/null
		zenity --info --text="This is the first time you are going to perform a \"new files only\" home settings backup. A copy of current files is needed. Click \"OK\" to continue."
		(cp -v -R $CONFIGFILES $HOME/.ubuntu-home-backup/settings/) | zenity --progress --pulsate --title="Creating comparison data" --text="Please wait while a copy of your settings is being created" --auto-close
		fi

	(cp -v -R -u $CONFIGFILES $HOME/.ubuntu-home-backup/settings/
	 cp -v -R $HOME/.ubuntu-home-backup/settings/ "$TEMPDIR_USERDATA") | zenity --progress --pulsate --title="Copying New Settings" --text="Please wait while your settings are being copied" --auto-close 

	else
	mkdir "$TEMPDIR_USERDATA/settings" 2>/dev/null
	(cp -v -R $CONFIGFILES $TEMPDIR_USERDATA/settings) | zenity --progress --pulsate --title="Copying All Settings" --text="Please wait while your settings are being copied" --auto-close
	fi

fi

if [ "$action" = "Administrator backup" ]
then
#zenity --info --text="This requires superuser authentication. Click OK to continue."

	if [ "$options" = "Backup new files only" ]
	then

		if [ ! -s $HOME/.ubuntu-home-backup/system/ ]
		then
		mkdir $HOME/.ubuntu-home-backup 2>/dev/null
		mkdir $HOME/.ubuntu-home-backup/system/ 2>/dev/null
		zenity --info --text="This is the first time you are going to perform a \"new files only\" in system backup. A copy of current files is needed. Click \"OK\" to continue."
		fi

		if [ "$admin_home_backup" = "Backup all home directory" ]
		then

			if [ ! -s $HOME/.ubuntu-home-backup/system/home/ ]
			then	
			(gksudo "cp -v -R /home $HOME/.ubuntu-home-backup/system"
			 gksudo "chown -R $UID $HOME/.ubuntu-home-backup"
			 gksudo "chgrp -R $GROUPS $HOME/.ubuntu-home-backup") | zenity --progress --pulsate --title="Creating comparison data for home" --text="Please wait while a copy of system home is being created" --auto-close
			fi
		
		(gksudo "cp -v -R -u /home $HOME/.ubuntu-home-backup/system"
		 gksudo "chown -R $UID $HOME/.ubuntu-home-backup/"
		 gksudo "chgrp -R $GROUPS $HOME/.ubuntu-home-backup/"
		 mkdir $TEMPDIR_USERDATA/system 2> /dev/null
		 cp -v -R $HOME/.ubuntu-home-backup/system/home "$TEMPDIR_USERDATA"/system) | zenity --progress --pulsate --title="Copying new system home" --text="Please wait while your system home directory files are being copied" --auto-close 

		fi

		if [ "$admin_etc_backup" = "Backup all system-wide settings" ]
		then

			if [ ! -s $HOME/.ubuntu-home-backup/system/etc/ ]
			then	
			(gksudo "cp -v -R /etc $HOME/.ubuntu-home-backup/system"
			 gksudo "chown -R $UID $HOME/.ubuntu-home-backup"
			 gksudo "chgrp -R $GROUPS $HOME/.ubuntu-home-backup") | zenity --progress --pulsate --title="Creating comparison data for system-wide settings" --text="Please wait while a copy of system-wide settings is being created" --auto-close
			fi

		(gksudo "cp -v -R -u /etc $HOME/.ubuntu-home-backup/system"
		 gksudo "chown -R $UID $HOME/.ubuntu-home-backup/"
		 gksudo "chgrp -R $GROUPS $HOME/.ubuntu-home-backup/"
		 mkdir $TEMPDIR_USERDATA/system 2> /dev/null
		 cp -v -R $HOME/.ubuntu-home-backup/system/etc "$TEMPDIR_USERDATA"/system) | zenity --progress --pulsate --title="Copying new system-wide settings" --text="Please wait while your system-wide settings are being copied" --auto-close 

		fi

		if [ "$admin_var_backup" = "Backup all system variable files" ]
		then

			if [ ! -s $HOME/.ubuntu-home-backup/system/var/ ]
			then
			(gksudo "cp -v -R /var $HOME/.ubuntu-home-backup/system"
			 gksudo "chown -R $UID $HOME/.ubuntu-home-backup"
			 gksudo "chgrp -R $GROUPS $HOME/.ubuntu-home-backup") | zenity --progress --pulsate --title="Creating comparison data for system variables" --text="Please wait while a copy of your system variables is being created" --auto-close
			fi

		(gksudo "cp -v -R -u /var $HOME/.ubuntu-home-backup/system"
		 gksudo "chown -R $UID $HOME/.ubuntu-home-backup/"
		 gksudo "chgrp -R $GROUPS $HOME/.ubuntu-home-backup/"
		 mkdir $TEMPDIR_USERDATA/system 2> /dev/null
		 cp -v -R $HOME/.ubuntu-home-backup/system/var "$TEMPDIR_USERDATA"/system) | zenity --progress --pulsate --title="Copying new system variables" --text="Please wait while your system variables are being copied" --auto-close 

		fi

	else
	mkdir "$TEMPDIR_USERDATA"/system 2>/dev/null

		if [ "$admin_home_backup" = "Backup all home directory" ]
		then	
		(gksudo "cp -v -R /home /tmp/ubuntu-home-backup/home"
		 gksudo "chown -R $UID /tmp/ubuntu-home-backup/"
		 gksudo "chgrp -R $GROUPS /tmp/ubuntu-home-backup/"
		 mv /tmp/ubuntu-home-backup/home "$TEMPDIR_USERDATA"/system/) | zenity --progress --pulsate --title="Copying system home" --text="Please wait while your system home directory files are being copied" --auto-close 
		fi

		if [ "$admin_etc_backup" = "Backup all system-wide settings" ]
		then
		(gksudo "cp -v -R /etc /tmp/ubuntu-home-backup/etc"
		 gksudo "chown -R $UID /tmp/ubuntu-home-backup/"
		 gksudo "chgrp -R $GROUPS /tmp/ubuntu-home-backup/"
		 mv /tmp/ubuntu-home-backup/etc "$TEMPDIR_USERDATA"/system/) | zenity --progress --pulsate --title="Copying system-wide settings" --text="Please wait while your system-wide settings are being copied" --auto-close 
		fi

		if [ "$admin_var_backup" = "Backup all system variable files" ]
		then
		(gksu "cp -v -R /var /tmp/ubuntu-home-backup/var"
		 gksudo "chown -R $UID /tmp/ubuntu-home-backup/"
		 gksudo "chgrp -R $GROUPS /tmp/ubuntu-home-backup/"
		 mv /tmp/ubuntu-home-backup/var "$TEMPDIR_USERDATA"/system/) | zenity --progress --pulsate --title="Copying new system variables" --text="Please wait while your system variables are being copied" --auto-close 
		fi
	fi

fi

if [ "$compression" = "tar gzip compression" ]
	then
		cd $TEMPDIR
		file-roller --add-to="$userlabel/$FILENAME.tar.gz" "$UID/"
		COMPRESSED_FILE="$FILENAME.tar.gz"
fi

if [ "$compression" = "zip compression" ]
	then
		cd $TEMPDIR
		file-roller --add-to="$userlabel/$FILENAME.zip" "$UID/"
		COMPRESSED_FILE="$FILENAME.tar.gz"
fi

if [ "$compression" = "tar bzip2 compression" ]
	then
		cd $TEMPDIR
		file-roller --add-to="$userlabel/$FILENAME.tar.bz2" "$UID/"
		COMPRESSED_FILE="$FILENAME.tar.gz"
fi

if [ "$media" = "Send as attachment to email" ]
	then
		cd "$TEMPDIR_FINALDATA"
		evolution "mailto:$email_address?subject=$userlabel&attachment=$COMPRESSED_FILE&attachment=$LOGFILE"
		rm -rf $TEMPDIR_USERDATA $TEMPDIR_FINALDATA
fi

if [ "$media" = "Burn to a cd-r or cd-rewritable" ]
	then
		cd "$TEMPDIR"
		(mkisofs -o "$TEMPDIR_USERDATA/$userlabel.iso" -R "$TEMPDIR_FINALDATA"/) | zenity --progress --pulsate --title="Creating ISO Image" --text="Please wait while an ISO image is being created" --auto-close
		nautilus-cd-burner --source-iso="/tmp/ubuntu-home-backup/$UID/$userlabel.iso" 
		rm -rf /tmp/ubuntu-home-backup
fi

if [ "$media" = "Copy to usb disk" ]
	then
		cd "$TEMPDIR"
		zenity --info --text="Please insert your USB Device to continue"
		(cp -R -v "$TEMPDIR_FINALDATA"/ /media/usbdisk/) | zenity --progress --pulsate --title="Copying Backup to USB Device" --text="Please wait while your backup are copied to a USB Device" --auto-close
		nautilus /media/usbdisk/"$userlabel"
		rm -rf /tmp/ubuntu-home-backup
fi

if [ "$media" = "Copy to home directory" ]
	then
		cd "$TEMPDIR"
		(cp -R -v "$TEMPDIR_FINALDATA"/ $HOME/) | zenity --progress --pulsate --title="Copying Backup to USB Device" --text="Please wait while your backup are copied to your home directory" --auto-close
		nautilus "$HOME"/"$userlabel"
		rm -rf /tmp/ubuntu-home-backup
fi

if [ "$media" = "Send to an ftp server" ]
	then
		zenity --info --text="FTP Transfer not yet implimented, performing a backup as an e-mail attachment"
		cd "$TEMPDIR_FINALDATA"
		evolution "mailto:$email_address?subject=$userlabel&attachment=$COMPRESSED_FILE&attachment=$LOGFILE"
		rm -rf $TEMPDIR_USERDATA $TEMPDIR_FINALDATA
fi

fi
)
