#!/bin/sh
#
#
# chkconfig: 345 12 05
# description: This startup script populate /etc/fstab with no-removable FS

# XXX: What about MS Windows Hibernate ?

WITHOUT_RC_COMPAT=1

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

RETVAL=0

MNT_PREFIX="/mnt"
FSTAB="/etc/fstab"
UDEVADM="/sbin/udevadm"
MOUNT_OPTIONS_VFAT="users,noatime,codepage=866,shortname=winnt,iocharset=utf8,gid=0100,fmask=111,dmask=0000"
MOUNT_OPTIONS_NTFS3G="users,locale=ru_RU.utf8,gid=100,fmask=111,force,dmask=000" # force
MOUNT_OPTIONS_OTHER="noauto"

log() {
    initlog $INITLOG_ARGS -n $0 -s "$1"
    return 0
}

start()
{
    msg_starting  $"populate /etc/fstab"

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

    if ! which "hal-find-by-property" >/dev/null 2>&1; then
        failure "Can't locate utils from hal package"
        echo
        return 1
    fi

    ##
    # Restore original /etc/fstab
    #
    restored=
    if [ -r "/image/live" ]; then
        tmpdir="$(mktemp --tmpdir=/tmp -d 2>/dev/null)"
        if [ -n "$tmpdir" ]; then
            unsquashfs -n -f -d "$tmpdir" /image/live /etc/fstab >/dev/null 2>&1
            if [ -r "$tmpdir/etc/fstab" ]; then
                mv -f "$tmpdir/etc/fstab" "/etc/fstab" && restored=yes
            fi
        fi
    fi

    if is_yes "$restored"; then
        log "Restored original /etc/fstab file from squashfs image"
    else
        log "Can't restore original /etc/fstab file from squashfs image. Use existing."
    fi

    blkid -o value -s UUID | while read uuid; do
        dev="$(blkid -o device -l -t UUID="$uuid")"

        if [ -z "$dev" ]; then
            log "Ignore bogus device with UUID=$uuid"
            continue
        fi

        if echo "$dev" | grep -q -s -e 'dm-' -e 'evms'; then
            log "Ignore $dev: device mapper or raid chunks"
            continue
        fi

        if grep -q -s -e "$uuid" -e "$dev" "$FSTAB"; then
            log "Ignore $dev: already exist in $FSTAB"
            continue
        fi

        if grep -q -e "$dev" /proc/mounts; then
            log "Ignore $dev: already mounted"
            continue
        fi

        udev_info=$("$UDEVADM" info --query=all --name="$dev" 2>/dev/null)
        if [ -z "$udev_info" ]; then
            log "Ignore $dev: can't get partition info from udev database"
            continue
        fi

        HAL_UDI="$(hal-find-by-property --key block.device --string "$dev" 2>/dev/null)"
        if [ -z "$HAL_UDI" ]; then
            log "Ignore $dev: can't get HAL_UDI"
            continue
        fi
        HAL_STORAGE_DEVICE="$(hal-get-property --udi "$HAL_UDI" --key block.storage_device 2>/dev/null)"
        if [ -z "$HAL_STORAGE_DEVICE" ]; then
            log "Ignore $dev: can't get HAL_STORAGE_DEVICE"
            continue
        fi
        HAL_STORAGE_REMOVABLE="$(hal-get-property --udi "$HAL_STORAGE_DEVICE" --key storage.removable 2>/dev/null)"
        if [ -z "$HAL_STORAGE_REMOVABLE" ]; then
            log "Ignore $dev: can't get HAL_STORAGE_REMOVABLE"
            continue
        fi
        if [ "$HAL_STORAGE_REMOVABLE" = "true" ]; then
            log "Ignore $dev: is REMOVABLE"
            continue
        fi

        fstype="$(echo "$udev_info" | grep "ID_FS_TYPE" | sed -n "s/.*ID_FS_TYPE=//p")"
        if [ -z "$fstype" ]; then
            log "$Ignore $dev: unknown ID_FS_TYPE"
            continue
        fi

        case "$fstype" in
            "mdraid" )
            log "Ignore $dev: mdraid"
            continue
            ;;
            "swsuspend" | "suspend")
            log "Ignore $dev: in suspend mode"
            continue
            ;;
            "swap")
            echo -e "UUID=${uuid}\tswap\tswap\tdefaults\t0\t0" >> "$FSTAB"
            /sbin/swapon -a
            log "Added $dev: swap"
            continue
            ;;
            "vfat")
            mnt_options="$MOUNT_OPTIONS_VFAT"
            ;;
            "ntfs")
            mnt_options="$MOUNT_OPTIONS_NTFS3G"
            fstype="ntfs-3g"
            ;;
            *)
            mnt_options="$MOUNT_OPTIONS_OTHER"
        esac

        mnt_point="$MNT_PREFIX/$(basename $dev)"
        mkdir -p "$mnt_point"

        echo -e "UUID=${uuid}\t${mnt_point}\t${fstype}\t${mnt_options}\t0\t0" >> "$FSTAB"
        /bin/mount -a
        log "Added $dev: $fstype"
    done

    success
    echo

    return 0
}

case "$1" in
    start)
    start
        ;;
    stop|condstop|status)
        ;;
    restart|reload|condrestart|condreload)
        ;;
    *)
        msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
        RETVAL=1
esac

exit $RETVAL
