#!/bin/sh

TIMESTAMP_ORIG="$(ls -v /mnt/cdrom/snapshots | tail -n 1 | sed -e 's/gentoo-//' -e 's/.tar.bz2.DIGESTS//')"
TIMESTAMP_SAVE="$(date -u +%Y%m%dT%H%M%SZ)"
TEMP_DIR="/dev/shm/temp-livegui-save-changes"

mkdir -p ${TEMP_DIR}/root.dir
mount -o bind / ${TEMP_DIR}/root.dir
rsync -XxAaHm --exclude="tmp/***" --exclude="var/tmp/***" --exclude="dev/***" --exclude="home/***" --compare-dest=/mnt/livecd ${TEMP_DIR}/root.dir/ ${TEMP_DIR}/new.dir
umount ${TEMP_DIR}/root.dir
cd ${TEMP_DIR}/new.dir
for i in `find . -type d | sort -r`; do if [ -z "$(ls -A ${i})" ]; then rmdir "${i}"; fi; done;
mksquashfs . ../persist-${TIMESTAMP_ORIG}-${TIMESTAMP_SAVE}.squashfs -comp gzip -b 131072
echo "Persistent image saved as ${TEMP_DIR}/persist-${TIMESTAMP_ORIG}-${TIMESTAMP_SAVE}.squashfs ... Copy it to your boot medium."
