#
# DESCRIPTION:
#
#     mount the backup media under $MEDIA_MNT
#
# part of the LiveBackup project:  
#
#                     http://livebackup.sourceforge.net/
#
# Copyright 2006 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
#


#
# Try to mount if not already mounted, 
# argument fs is optional. 
#
# If path is defined the 
# media is searched for a subdirectory 
# containing the media files
#
# SYNOPSIS:
#             try_mount <dev> [<fs>]
# 
_try_mount()
{
    dev=$1
    fs=$2

    if [ "$(is_lb_mounted)" ]; then
        # nothing to do
        return
    fi

    if [ $fs ]; then
	fs_opt="-t $fs"
    else
	fs_opt=""
    fi

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

    if [ "$dev" ]; then 

	if [ $LB_DEBUG -gt 0 ]; then 
	    mount $m_opt -o ro $fs_opt $dev $MEDIA_MNT 
            ret=$?
	else
	    mount $m_opt -o ro $fs_opt $dev $MEDIA_MNT > /dev/null 2>&1
            ret=$?
	fi

        if [ "$(is_lb_mounted)" ]; then
            msg -i "backup found on device $dev"
            return
        fi

        if [ $(echo $path | wc -c) -gt 2 ] && [ $ret -eq 0 ]; then 
            if [ -f $MEDIA_MNT/$path/$POLICYFILE ]; then
                mount -n --bind $MEDIA_MNT/$path $MEDIA_MNT
            fi
        fi

	if [ $(is_lb_mounted) ]; then 
	    msg -i "backup found on device $dev"
	else
	    umount $m_opt $MEDIA_MNT > /dev/null 2>&1
	fi

        if [ $ISOFILE != "not_set" ]; then
            _try_isomount $dev $fs
        fi
    fi

}


#
# experimental hack
# 
_try_isomount()
{
    dev=$1
    fs=$2

    if [ $fs ]; then
	fs_opt="-t $fs"
    else
	fs_opt=""
    fi

    m_opt="-n"

    if [ "$dev" ] && [ ! "$(is_lb_mounted)" ]; then 

        mkdir -p ${MEDIA_MNT}1
        mount $m_opt -o ro $fs_opt $dev ${MEDIA_MNT}1 > /dev/null 2>&1
        if [ -f ${MEDIA_MNT}1/$ISOFILE ]; then
            mount -o ro,loop ${MEDIA_MNT}1/$ISOFILE $MEDIA_MNT 
        fi

	if [ $(is_lb_mounted) ]; then 
	    msg -i "backup found on device $dev:$ISOFILE"
	else
	    umount $m_opt $MEDIA_MNT    > /dev/null 2>&1
	    umount $m_opt ${MEDIA_MNT}1 > /dev/null 2>&1
	fi

    fi
}


#
# scriplet starts here
#

if [ ! $(is_lb_mounted) ]; then

    msg -i "searching for boot media ...."


    #
    # support for IDE devices
    #
    IDE_DEVICES="/dev/hd?"
    IDE_FS="iso9660"

    #
    # support for SCSI CDROM devices
    #
    SCSI_DEVICES="/dev/scd?"
    SCSI_FS="iso9660"


    #
    # usb device support 
    #
    USB_DEVICES=""
    if [ -d /proc/bus/usb ]; then
	mount -n -t usbfs none /proc/bus/usb
	if [ -f /proc/bus/usb/devices ]; then 
	    if [ "$(grep Driver /proc/bus/usb/devices | grep storage)" ]; then 
		USB_DEVICES="/dev/sda? /dev/sdb?"
		USB_FS="vfat ext2 ext3 iso9660"
	    fi
	fi
    fi

    if [ $LB_DEBUG -gt 0 ]; then 
	msg -e -i "supported filesystems:\n $(cat /proc/filesystems)"
    fi

    #
    # ensure the mount point exists
    # 
    mkdir -p $MEDIA_MNT

    #
    # try a suggested device first
    #
    if [ "$MEDIADEV" != "not_set" ]; then
	dev=$(echo $MEDIADEV | cut -f 1 -d ':')
        fs=$(echo $MEDIADEV | cut -s -f 2 -d ':')
        path=$(echo $MEDIADEV | cut -s -f 3 -d ':')

	_try_mount $dev $fs $path
        # wait and retry 
	for i in $(seq 1 3); do
	    if [ ! "$(is_lb_mounted)" ]; then
		msg -i "($i) retry mounting $dev $fs"
		sleep 3
		_try_mount $dev $fs 
	    fi
	done
    fi

    #
    # search IDE devices
    #
    for dev in $IDE_DEVICES; do
	for fs in $IDE_FS; do 
	    _try_mount $dev $fs
	done
    done
    for dev in $IDE_DEVICES; do
	_try_mount $dev 
    done

    #
    # search SCSI devices
    #
    for dev in $SCSI_DEVICES; do
	for fs in $SCSI_FS; do 
	    _try_mount $dev $fs
	done
    done
    for dev in $SCSI_DEVICES; do
	_try_mount $dev 
    done

    #
    # search USB media
    #
    if [ ! "$(is_lb_mounted)" ] && [ "$USB_DEVICES" ]; then
        msg -i "waiting for usb storage device ..."
        sleep 10
        for dev in $USB_DEVICES; do
            for fs in $USB_FS; do 
                _try_mount $dev $fs
            done
        done
        for dev in $USB_DEVICES; do
            _try_mount $dev 
        done
    fi


    #
    # final check
    #
    while [ ! $(is_lb_mounted) ]; do

	msg -c "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
	msg -c "ERROR: no backup media found, stopped ..."
	msg -c "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"

	rescue_shell "mount media under $MEDIA_MNT, boot continues after exit" 

    done

fi


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