#!/bin/sh
#
#
# chkconfig: 2345 04 99
# description: save livecd state to squashfs file.
#

WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

RETVAL=0

# aufs RW branch for root fs
root="/mnt/root"
img="/tmp/live2.img"
# XXX: dst="${dst:?Please specify destination device}"
# e.g. dst="/dev/hdb"

magic="SQUASHFS_IMAGE_END_MAGIC_123"

[ -n "$dst" ] || exit 0

stop()
{
    msg_stopping $"Save livecd state"

    if ! which "mksquashfs" > /dev/null 2>&1; then
        failure "can't find mksquashfs"
        echo
        return 1
    fi

    if ! [ -d "$root" ]; then
        failure "can't find $root"
        echo
        return 1
    fi

    rm -rf "$img"
    mksquashfs -no-progress "$root" "$img" >/dev/null 2>&1

    if ! [ -r "$img" ]; then
        failure "can't create squashfs image"
        echo
        return 1
    fi

    { cat "$img"; echo "$magic"; } > "$dst"
    sync

    success
    echo

    return 0
}

# See how we were called.
case "$1" in
    stop)
        stop
        ;;
    start|condstop|status|restart|reload|condrestart|condreload)
        ;;
    *)
        msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
        RETVAL=1

esac

exit $RETVAL
