#!/bin/sh

set -e

belongs_to_floppy_group() {
    stat -c %G ${1}* | grep -q '^floppy$'
}

case "${1}" in
    configure)
        . /lib/bilibop/common.sh
        get_udev_root

        # Be sure the needed virtual filesystems are mounted, and udev is
        # running; or do nothing:
        if [ -h /proc/mounts -a -d /sys/block -a -c ${udev_root}/null ] &&
            invoke-rc.d udev status >${udev_root}/null 2>&1 &&
            BILIBOP_DISK="$(physical_hard_disk /)" &&
            query_sysfs_attrs ${BILIBOP_DISK} | grep -Eq '^[[:blank:]]*SUBSYSTEMS=="(usb|firewire|memstick|mmc)"'; then

            # Trigger uevents for the disk hosting the root filesystem and its
            # partitions:
            udevadm trigger --sysname-match=${BILIBOP_DISK##*/}*
            udevadm settle

            # But it can happen that this doesn't work and new rules must be
            # explicitly loaded before triggering uevents:
            if belongs_to_floppy_group ${BILIBOP_DISK}; then
                udevadm control --reload-rules
                udevadm trigger --sysname-match=${BILIBOP_DISK##*/}*
                udevadm settle

                if belongs_to_floppy_group ${BILIBOP_DISK}; then
                    [ -f /etc/udev/rules.d/66-bilibop.rules ] || exit 2
                fi
            fi
        fi
        ;;
esac

#DEBHELPER#
:
# vim: et ts=4 sts=4 sw=4
