#!/bin/sh
#
# Parts of this code are taken by the livecd developed by 
# Edson Brandi. As the licence says, here is the copyright
# note
#
# Copyright 2001 2002 Edson Brandi <ebrandi.home@uol.com.br>
# All Rights Reserved.
#
# THIS SOFTWARE IS PROVIDED BY EDSON BRANDI ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT EDSON BRANDI BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.

if [ !  -e /root/.part_mount ]; then

dialog --title "FreeSBIE Boot Options" --yesno "FreeSBIE can find and mount all the slices it finds in your hard disks. If you want to use this feature, you're doing this AT YOUR RISK. Do you want FreeSBIE to mount the slices in your hard disks ?" 10 60

if [ ! $? -eq 1 ]
        then
############################################
# We will try to detect all IDE disks and existing 
# slices and try to mount every FAT16/32, NTFS, UFS, EXT2 partition.
#

echo "Checking for slices on hard drives..."
#
DRIVER="ext2fs msdos ntfs"
EXT_LOGICAL="5 6 7 8"
UFS_PART=1
MSDOS_PRIMARY=1
NTFS_PRIMARY=1
EXT2FS_PRIMARY=1

MSDOS_LOGICAL=1
NTFS_LOGICAL=1
EXT2FS_LOGICAL=1

UFS_TOO_MANY=0
MSDOS_TOO_MANY=0
NTFS_TOO_MANY=0
EXT2FS_TOO_MANY=0

for i in `egrep "^(ad|da|cd|acd)[0-9]:" /var/run/dmesg.boot | cut -d':' -f1 | sort | uniq`
do
    a=1
    for x in `fdisk /dev/$i | grep sysid | awk -F"," '{print $1}' | awk '{print $2}'`
    do
        case ${x} in
        1|4|6|11|12|13)
            echo "FAT16/32 partition found at /dev/${i}s${a}, mounting it under /mnt/dos.${MSDOS_PRIMARY} ..."
            mount -t msdos /dev/${i}s${a} /mnt/dos.${MSDOS_PRIMARY} >/dev/null 2>&1
            MSDOS_PRIMARY=`expr $MSDOS_PRIMARY + 1`
            ;;
        5|15)
            echo "Extended partition found, trying to detect and mount logical partitions ..."
            for y in $EXT_LOGICAL
            do
                for w in $DRIVER
                do
                    mount -t $w /dev/${i}s${y} /mnt/tmp >/dev/null 2>&1
                    RETVAL=$?
                    if [ $RETVAL = 0 ]; then
                        umount /mnt/tmp
                        if [ $w = "msdos" ]; then
                            if [ $MSDOS_LOGICAL -lt 5 ]; then
                                echo "FAT32 partition found at /dev/${i}s${y}, mounting it under /mnt/dos.ext.${MSDOS_LOGICAL} ..."
                                mount -t msdos /dev/${i}s${y} /mnt/dos.ext.${MSDOS_LOGICAL} >/dev/null 2>&1
                                MSDOS_LOGICAL=`expr $MSDOS_LOGICAL + 1`
                            else
                                if [ $MSDOS_TOO_MANY -eq 0 ]; then
                                    echo "Too many msdos logical partitions: only 4 can be mounted simultaneously."
                                    MSDOS_TOO_MANY=1
                                fi
                            fi
                        fi
                        if [ $w = "ntfs" ]; then
                            if [ $NTFS_LOGICAL -lt 5 ]; then
                                echo "NTFS partition found at /dev/${i}s${y}, mounting it under /mnt/ntfs.ext.${NTFS_LOGICAL} ..."
                                mount -t ntfs /dev/${i}s${y} /mnt/ntfs.ext.${NTFS_LOGICAL} >/dev/null 2>&1
                                NTFS_LOGICAL=`expr $NTFS_LOGICAL + 1`
                            else
                                if [ $NTFS_TOO_MANY -eq 0 ]; then
                                    echo "Too many ntfs logical partitions: only 4 can be mounted simultaneously."
                                    NTFS_TOO_MANY=1
                                fi
                            fi
                        fi
                        if [ $w = "ext2fs" ]; then
                            if [ $EXT2FS_LOGICAL -lt 5 ]; then
                                echo "EXT2FS partition found at /dev/${i}s${y}, mounting it under /mnt/ext2fs.ext.${EXT2FS_LOGICAL} ..."
                                mount -t ext2fs /dev/${i}s${y} /mnt/ext2fs.ext.${EXT2FS_LOGICAL} >/dev/null 2>&1
                                EXT2FS_LOGICAL=`expr $EXT2FS_LOGICAL + 1`
                            else
                                if [ $EXT2FS_TOO_MANY -eq 0 ]; then
                                    echo "Too many ext2fs logical partitions: only 4 can be mounted simultaneously."
                                    EXT2FS_TOO_MANY=1
                                fi
                            fi
                        fi
                    fi
                done
            done
            ;;
        7)
            echo "NTFS partition found at /dev/${i}s${a}, mounting it under /mnt/ntfs.${NTFS_PRIMARY} ..."
            mount -t ntfs /dev/${i}s${a} /mnt/ntfs.${NTFS_PRIMARY} >/dev/null 2>&1
            NTFS_PRIMARY=`expr $NTFS_PRIMARY + 1`
            ;;
        131)
            echo "EXT2FS partition found at /dev/${i}s${a}, mounting it under /mnt/ext2fs.${EXT2FS_PRIMARY} ..."
            mount -t ext2fs /dev/${i}s${a} /mnt/ext2fs.${EXT2FS_PRIMARY} >/dev/null 2>&1
            EXT2FS_PRIMARY=`expr $EXT2FS_PRIMARY + 1`
            ;;
        165)
            echo "UFS partition found at /dev/${i}s${a}, detecting slices..."
            for j in `disklabel -r /dev/${i}s${a} 2>/dev/null | grep BSD | awk '{print substr($1,1,length($1)-1)}'`
            do
                if [ $UFS_PART -lt 9 ]; then
                    echo "UFS slice found at /dev/${i}s${a}${j}, mounting it under /mnt/ufs.${UFS_PART}"
                    mount  /dev/${i}s${a}${j} /mnt/ufs.${UFS_PART} >/dev/null 2>&1
                    UFS_PART=`expr ${UFS_PART} + 1`
                else
                    if [ $UFS_TOO_MANY -eq 0 ]; then
                        echo "Too many slices found, only 8 can be mounted simultaneously."    
                        UFS_TOO_MANY=1
                    fi
                fi
            done
            ;;
        esac
        a=`expr $a + 1`
    done
done
fi
fi
touch /root/.part_mount
