#
# DESCRIPTION:
#
#    The master directory structure is available in $MASTER_MNT.
#    Now get the system image $MASTER_MNT/system/<xxx> 
#    mounted under $IMAGE_MNT
#
# part of the LiveBackup project:  
#
#                     http://livebackup.sourceforge.net/
#
# Copyright 2006-2010 by Wolfgang Rohrmoser <info@rohrmoser-engineering.de>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#


#
# ensure mount point exists
#
mkdir -p  $IMAGE_MNT


#
# check for proper mount command options
#
if [ -w /etc/mtab ]; then
    m_opt=""
else
    m_opt="-n"
fi


############################################################################
#
# provide system image under $IMAGE_MNT 
# (if not already mounted manually in debug mode) 
#
if [ ! $(is_mounted $IMAGE_MNT) ]; then

    case "$image_format" in

        directory)
	    mount --bind $MASTER_MNT/system/$SYSTEM $IMAGE_MNT
	    ;;

        cloop)
	    losetup /dev/cloop $MASTER_MNT/system/$SYSTEM.cloop
            # TODO: losetup may require option -r (read only)
	    #       mount may need "-t <fstype>" option
            mount $m_opt -o ro /dev/cloop $IMAGE_MNT
	    ;;

        squashfs)
            if [ $AUTOUPDATE ] && [ -f $MASTER_MNT/system/$SYSTEM.sqsh-update ]; then
                mount $m_opt -o remount,rw $MASTER_MNT > /dev/null 2>&1
                if [ $? -eq 0 ]; then
                    msg -i "$SYSTEM.sqsh-update on writable media found, updating ..."
                    mv $MASTER_MNT/system/$SYSTEM.sqsh $MASTER_MNT/system/$SYSTEM.sqsh-old
                    mv $MASTER_MNT/system/$SYSTEM.sqsh-update $MASTER_MNT/system/$SYSTEM.sqsh
                    mount $m_opt -o remount,ro $MASTER_MNT
                fi
            fi
	    mount $m_opt -t squashfs -o loop $MASTER_MNT/system/$SYSTEM.sqsh $IMAGE_MNT
	    ;;

        diskimage)
            if [ "$SYSTEM_MOUNT_OPTS" ]; then
                mount $m_opt -o $SYSTEM_MOUNT_OPTS $MASTER_MNT/system/$SYSTEM.img $IMAGE_MNT
            else
                lomount -diskimage $MASTER_MNT/system/$SYSTEM.img -partition 1 $IMAGE_MNT
            fi
	    ;;

        blockdevice)
            mount $m_opt -o ro $SYSTEM $IMAGE_MNT
	    ;;

	script)
	    cd $MASTER_MNT/system
	    /bin/sh $SYSTEM.script
	    ;;

        *)
            msg -c "FATAL:  !! unsupported image format in $0!!"
	    exit 1
    esac

fi


# check it
if [ $(is_mounted $IMAGE_MNT) ]; then
    msg -i "$SYSTEM available under $IMAGE_MNT"
else
    ls $MASTER_MNT/system
    msg -c "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    msg -c "ERROR: cannot mount $SYSTEM, abort ..."
    msg -c "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    rescue_shell "now you can try to setup $IMAGE_MNT manually";
fi




#
# Local Variables: 
# mode: shell-script
# End: 
#
