#!/bin/bash
#
# Run-from-RAM sync script version 1.05, by Mark Lord.
#

OLDROOT=/.rw		## where the persistent real /     resides
OLDBOOT=$OLDROOT/boot	## where the persistent real /boot resides
NEWROOT=/.aufs		## where the aufs root was mounted
TMPFS=/.tmpfs		## where the tmpfs was mounted
BTMPFS=/.bmpfs		## where the btmpfs was mounted
RAM=/ram		## holds replacments for system files

TMPROOT=/.tmproot

function echo_exit(){
	echo "$*, aborted."
	exit 1
}

dry_run="--dry-run"
if [ "$1" = "--commit" ]; then
	dry_run=
	shift
fi
if [ "$1" != "" ]; then
	dir="$1"
	shift
	while [ "$dir" != "/" -a "$dir" != "${dir%*/}" ]; do
		dir="${dir%*/}"
	done
	[ -d "$dir" ] || echo_exit "$dir: not a directory"
	[ -h "$dir" ] && echo_exit "$dir: not a directory"
	pushd "$dir" >/dev/null || exit 1
	dir="`pwd -P`"
	cd ..
	parent="`pwd -P`"
	popd >/dev/null
fi
if [ "$1" = "--commit" ]; then
	dry_run=
	shift
fi
[ "$*" == "" ] || echo_exit "$*: excess args"

oldroot_was_rw=yes
awk "(\$2 == \"$OLDROOT\" && \$4 ~ \"^rw\"){exit 1}" < /proc/mounts && oldroot_was_rw=

oldboot_was_rw=yes
boot="`awk "(\\$2 == \\"${OLDBOOT}\\"){print \\$4; exit}" < /proc/mounts`"
[ "${boot:0:2}" = "ro" ] && oldboot_was_rw=

if [ "$dry_run" != "" ]; then
	oldroot_was_rw=yes
	oldboot_was_rw=yes
	echo
	echo "${0##*/}: Commencing DRY-RUN.  Nothing will be written for real."
	echo
fi

[ "$oldroot_was_rw" = "yes" ] || mount "$OLDROOT" -o remount,rw,relatime,errors=remount-ro || exit 1
[ "$oldboot_was_rw" = "yes" ] || mount "$OLDBOOT" -o remount,rw,relatime,errors=remount-ro || exit 1

if [ "$dir" = "" ]; then
	mkdir -p $TMPROOT || exit 1
	mount -n --bind / $TMPROOT || exit 1
	src="$TMPROOT/"
	dst="$OLDROOT/"
	hidden_dirs="$TMPROOT/"
	exclude_dirs="/tmp/ /var/log/"
else
	src="$dir"
	dst="$OLDROOT$parent"
	excludes=""
	exclude_dirs=""
	hidden_dirs=""
fi

$RAM/rearrange_sysfiles_for_ram_session restore "$RAM" || exit 1

function do_cleanup(){
	trap - SIGINT
	[ "$1" = "continue" ] || echo -n "Cleaning up, please wait.. "
	$RAM/rearrange_sysfiles_for_ram_session install "$RAM"
	if [ "$dir" = "" ]; then
		umount $TMPROOT
		rmdir  $TMPROOT &>/dev/null
	fi

	# Something (gnome-keyring daemon?) interferes with this here.
	sync
	##[ "$oldboot_was_rw" = "yes" ] || mount "$OLDBOOT" -oremount,ro
	##[ "$oldroot_was_rw" = "yes" ] || mount "$OLDROOT" -oremount,ro
	sync
	[ "$1" = "continue" ] || echo_exit "Done"
}
trap do_cleanup SIGINT

hidden_dirs="$hidden_dirs $OLDROOT/ $NEWROOT/ $TMPFS/ /home/*/.gvfs"
[ "$boot" = "" ] || hidden_dirs="$hidden_dirs $OLDBOOT/ $BTMPFS/"
excludes=""
for d in $hidden_dirs $exclude_dirs ; do
	excludes="$excludes --exclude $d/"
done

echo "rsync $dry_run -avxH --delete-delay --force $excludes \"$src\" \"$dst\""
      rsync $dry_run -avxH --delete-delay --force $excludes  "$src"   "$dst"

if [ "$dir" = "" -a "$boot" != "" ]; then
	echo "rsync $dry_run -avxH --delete-delay --force $excludes \"/boot/\" \"$OLDBOOT/\""
	      rsync $dry_run -avxH --delete-delay --force $excludes  "/boot/"   "$OLDBOOT/"
fi

do_cleanup continue
[ "$exclude_dirs" = "" ] || echo "Skipped these directories: $exclude_dirs" ; echo

if [ "$dry_run" != "" ]; then
	echo
	echo "${0##*/}: DRY-RUN completed.  Use --commit to do it for real."
	echo
fi
