#!/bin/bash
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.

. /usr/lib/pm-utils/functions

set_usb_persist()
{
  for I in /sys/bus/usb/devices/*/*
    do
      if [ -e $I/uevent ] 
      then
#
# For some reason [ -r $I/uevent ] returns
# true for some uevent files even when they
# are not readable, hence we stat them as
# and check if they are not writable too
#
        statflags=`stat -c %a $I/uevent`
        if [ -r $I/uevent -a x$statflags != 'x200' ]
        then
#
# Is the device a USB storage device?
#
          usbstorage=`grep DRIVER=usb-storage $I/uevent`
          if [ x$usbstorage != x ]
          then 
#
# If persist exists, set it
#
            if [ -e $I/../power/persist ]
            then
              echo setting persistant for $I
              echo 1 > $I/../power/persist
            fi
          fi
        fi
      fi
 done     
}


case "$1" in
        suspend)
                set_usb_persist
                ;;
        hibernate)
                ;;
esac

exit $?
