#!/sbin/runscript
# Copyright 2005 Michiel Hazelhof
# Distributed under the terms of the GNU General Public License v2
# Revised 2006 by hse@ukr.net

depend () {
        # Just to make it look better
        after autoconfig
}

start() {

# Set ${NTFS} to a value, not sure if this is needed
NTFS=$"0"

# Get al excisting partitions usinf fdisk, and loop those to determine what to do with each
for x in $(fdisk -l | awk {'print $1'} | grep dev | cut -c6-99);
do

        # Just to make sure the type2 is empty and not used if not needed
        TYPE2=$"(0)"

        # Grep partition type, so we have the /dev/* and the type, now we can determinewhat to do with it.
        # Get $6 to for none bootable partitions
        TYPE="$(fdisk -l | grep "${x} " | awk {'print $7'})"
        TYPE2="$(fdisk -l | grep "${x} " | awk {'print $6'})"

                # If TYPE returns nothing we start using TYPE2 because it (probably) isa non-bootable partition
                if ?span> <span class="hl opt"> ; then

                        # Do not mount if TYPE2 returns extended
                        if ?span> <span class="hl opt"> ;  then
                        SHIT=$"1"

                        # Mount "none" bootable NTFS partition using captive-ntfs, and start captive-ntfs only if it is the first time
                        elif ?span> <span class="hl opt"> ;  then
                        if ?span> <span class="hl opt"> ; then
                        ebegin "Starting captive-ntfs!"
                        chmod 755 /
                        screen -d -m -S captive_ntfs captive-sandbox-server
                        NTFS=$"1"
                        fi
                        ebegin "Trying to mount /dev/${x} as ntfs, using captive-ntfs"
                        if [ ! -d /mnt/${x} ];
                          then
                            mkdir /mnt/${x}
                        fi
                        mount -t captive-ntfs /dev/${x} /mnt/${x} 2>/dev/null >/dev/null >/dev/null

                        # Use partition as swap if it is swap
                        elif ?span> <span class="hl opt"> ;  then
                        ebegin "Trying to use /dev/${x} as swap"
                        swapon /dev/${x} 2>/dev/null >/dev/null >/dev/null

                        # If none of the above exceptions is true, just try to mount it
                        else
                        ebegin "Trying to mount /dev/${x}"
                        if [ ! -d /mnt/${x} ];
                          then
                            mkdir /mnt/${x}
                        fi
                        mount /dev/${x} /mnt/${x} 2>/dev/null >/dev/null >/dev/null

                        fi

                # If it is a bootable partition we run this part of the loop
                else

                        # Don't mount if extended
                        if ?span> <span class="hl opt"> ; then
                        SHIT=$"1"

                        # If it TYPE returns W95, check for hidden
                        elif ?span> <span class="hl opt"> ; then
                        TYPE2="$(fdisk -l | grep ${x} | awk {'print $6'})"

                        # If W95 returns hidden mount it but mention it is hidden
                        elif ?span> <span class="hl opt"> ;  then
                        ebegin "Trying to mount /dev/${x}, but is is a hidden partitionthis might fail"
                        if [ ! -d /mnt/${x} ];
                          then
                            mkdir /mnt/${x}
                        fi
                        mount /dev/${x} /mnt/${x} 2>/dev/null >/dev/null >/dev/null

                        # Mount bootable NTFS partition using captive-ntfs, and start captive-ntfs only if it is the first time
                        elif ?span> <span class="hl opt"> ; then
                        if ?span> <span class="hl opt"> ; then
                        ebegin "Starting captive-ntfs!"
                        chmod 755 /
                        screen -d -m -S captive_ntfs captive-sandbox-server
                        NTFS=$"1"
                        fi
                        ebegin "Trying to mount /dev/${x} as ntfs, using captive-ntfs"
                        if [ ! -d /mnt/${x} ];
                          then
                            mkdir /mnt/${x}
                        fi
                        mount -t captive-ntfs /dev/${x} /mnt/${x} 2>/dev/null >/dev/null >/dev/null

                        # Use partition as swap if it is swap, this should not occur asswap normaly is not a bootable partition
                        elif ?span> <span class="hl opt"> ; then
                        ebegin "Trying to use /dev/${x} as swap"
                        swapon /dev/${x} 2>/dev/null >/dev/null >/dev/null

                        fi

                fi

done

}