#!/bin/sh
# /etc/dev.d/default/mediamanager_usbstorage.dev
# Notify all KDE sessions (thanks to the mediamanager) that a new
# usb_storage device appeared or disappeared
# 

# to debug this script, uncomment the next line and see /tmp/mediamanager_usbstorage.debug after execution
#DEBUG=1

# exit immediately if /usr/bin/ is not yet available (during boot if /usr is a separate partition)
/bin/ls -d /usr/bin/ >/dev/null 2>&1 || exit

DEBUGOUT=/tmp/mediamanager_usbstorage.debug.$$
if [ "$DEBUG" = "1" -a -z "$2" ]; then
  echo "executing $0 $@" > $DEBUGOUT
  echo "with the following environment variables:" >> $DEBUGOUT
  env >> $DEBUGOUT
  echo "----" >> $DEBUGOUT
  sh -x $0 $@ debug >> $DEBUGOUT 2>&1
  exit
fi

# we only manage block devices
if [ "$1" != "block" ]; then exit; fi

# we only manage usb_storage devices
if [ "$ACTION" = "add" ]; then
  device="`ls /sys$DEVPATH/../device/../../../ 2> /dev/NULL | grep ':' | head -1`"
  if [ -z "$device" -o ! -e /sys/bus/usb/drivers/usb-storage/$device ]; then
    # The behavior is not the same for every kernel it seems.
    # Testing the driver/ directory just in case.
    device="`ls /sys$DEVPATH/../device/../../../driver 2> /dev/NULL | grep ':' | head -1`"
    if [ -z "$device" -o ! -e /sys/bus/usb/drivers/usb-storage/$device ]; then
      exit
    fi
  fi
fi

# functions for syslog
LOGGER="logger -t `basename $0`[$$] -p user.notice"
write_syslog () {
  echo ${@} | $LOGGER
}

# be sure the drivers are loaded
/sbin/modprobe -q usb_storage
/sbin/modprobe -q vfat

# create the FSH required /media directory
# See: http://www.pathname.com/fhs/pub/fhs-2.3.html#MEDIAMOUNTPOINT
MNT=media
if [ ! -d /$MNT ]; then
  mkdir /$MNT
  write_syslog "Created the /$MNT directory"
fi

# we need DEVPATH, DEVNAME and ACTION, so we warn the user that executes this script by hand
if [ -z "$DEVPATH" -a -z "$DEVNAME" -a -z "$ACTION" ]; then
  echo
  echo "This script must be called by udevd because it needs the following environment variables: DEVPATH, DEVNAME, ACTION"
  echo "So you must copy this script as /etc/dev.d/default/updfstab-2.6.dev and set it executable"
  echo "See: http://www.kernel.org/pub/linux/utils/kernel/hotplug/RFC-dev.d"
  echo
  exit
fi

# if $DEVPATH/device exists, we are a device, not a partition, so exit
if [ -d /sys${DEVPATH}/device ]; then exit; fi

dcop_users="`ps aux | grep dcopserver | grep -v grep | awk '{print $1}' | sort | uniq`"

# if the current device is being added
if [ "$ACTION" = "add" ]; then
  # get partition information
  partition="/sys${DEVPATH}/../device/../../.."
  # We check twice again... marvelous random kernel behaviour changes...
  if [ -e $partition/product ]; then
    product="`cat $partition/product`"
  else
    product="`cat $partition/../product`"
  fi
  if [ -e $partition/manufacturer ]; then
    manufacturer="`cat $partition/manufacturer`"
  else
    manufacturer="`cat $partition/../manufacturer`"
  fi

  write_syslog "Invoking dcop..."
  write_syslog "kded mediamanager removablePlug $DEVNAME \"$manufacturer $product\""

  method="kded mediamanager removablePlug"
  for user in $dcop_users ; do
    dcop --user $user --all-sessions $method $DEVNAME "$manufacturer $product"
  done
  
elif [ "$ACTION" = "remove" ]; then
  write_syslog "Invoking dcop..."
  write_syslog "kded mediamanager removableUnplug $DEVNAME"

  method="kded mediamanager removableUnplug"
  for user in $dcop_users ; do
    dcop --user $user --all-sessions $method $DEVNAME
  done
		  
  umount $DEVNAME
fi


