#!/bin/sh -e
# hotplug.config
#

# source debconf library
. /usr/share/debconf/confmodule

# this module use multiselect
db_capb multiselect

KERNEL=`uname -r`

case $KERNEL in
 2.2.*) MODULE_DIR=/lib/modules/$KERNEL ;;
 *) MODULE_DIR=/lib/modules/$KERNEL/kernel/drivers;;
esac

usbmodules=
test -d $MODULE_DIR/usb &&
  cd $MODULE_DIR/usb && 
  # get available usb modules
  usbmodules=`find . -type f -name '*.ko' -o -name '*.o' | \
  	 sed -r -e 's,\.k?o$,,' -e 's,^.*/,,g' | \
	 grep -v -E '^(usbcore|usb-uhci|uhci|usb-ohci)$' | \
	 sort`

isusbmodule() {
  echo $usbmodules | grep -q "\<$1\>"
}

if dpkg --compare-versions "$2" lt 0.0.20040329-4; then
  # assume old behaviour
  NET_AGENT_POLICY="hotplug"
fi
if grep -q '^mapping[[:space:]][[:space:]]*hotplug' /etc/network/interfaces; then
  NET_AGENT_POLICY="hotplug"
fi

test -f /etc/hotplug/usb.option && . /etc/hotplug/usb.option
test -f /etc/default/hotplug.usb && . /etc/default/hotplug.usb
test -f /etc/default/hotplug && . /etc/default/hotplug
if [ "$USE_NET_LIFACE" = "true" ]; then
  NET_AGENT_POLICY="hotplug"
fi

if test "$X11_USBMICE_HACK" != ""; then
  db_set hotplug/x11_usbmice_hack "$X11_USBMICE_HACK" || true
fi
if test "$STATIC_MODULE_LIST" != ""; then
  db_set hotplug/static_module_list $(echo "$STATIC_MODULE_LIST" | sed -e 's/ /, /g') || true
fi
if test "$NET_AGENT_POLICY" != ""; then
  db_set hotplug/net_agent_policy "$NET_AGENT_POLICY" || true
fi
if test "$IGNORE_PCI_CLASS_DISPLAY" != ""; then
  db_set hotplug/ignore_pci_class_display "$IGNORE_PCI_CLASS_DISPLAY" || true
fi

db_go || true

# if mousedev and/or input are module, then ask to load for X11 support
if isusbmodule mousedev || isusbmodule input; then
  db_input high hotplug/x11_usbmice_hack || true
  db_go || true
fi

db_subst hotplug/static_module_list usbmodules $usbmodules
db_input low hotplug/static_module_list || true

db_input low hotplug/net_agent_policy || true
db_input low hotplug/ignore_pci_class_display || true
db_go || true
