#!/bin/sh
###
### BITPIM
###
### Copyright (C) 2007 Joe Pham <djpham@bitpim.org>
###
### This program is free software; you can redistribute it and/or modify
### it under the terms of the BitPim license as detailed in the LICENSE file.
###
### $Id: bpudev 4265 2007-06-05 01:39:10Z djpham $
#
# $1=$DEVNUM
# $2=kernel number
# $3=SYSFS{devnum}
#

if [ "x$1" = "x--del" ]; then
    action=del
    # Compensate for udev quirkiness
    test -n "$2"  ||  set del "$DEVNAME" "$3" "$4"
    shift
else
    action=add
fi

# Some basic checking for args
if [ $# -ne 3 ]; then
     exit 1
fi
 
# udev fires off a whole bunch of devices, most of which we don't need.
# By visual inspection, the one that we want has the same Kernel No and
# SYSFS{devnum}.
if [ "$2" != "$3" ]; then
     exit 2
fi

BPUDEV_USER=
BPUDEV_GROUP=
BPUDEV_MODE=0664

if test -r /etc/default/bitpim; then
    . /etc/default/bitpim
fi

if [ "$action" = "add" ]; then
    [ -z "$BPUDEV_USER"  ] || chown "$BPUDEV_USER"  "$1" || true
    [ -z "$BPUDEV_GROUP" ] || chgrp "$BPUDEV_GROUP" "$1" || true
    chmod "$BPUDEV_MODE" "$1" || true
fi

# Notify BitPim that there's a new USB device
if [ ! -d /var/run/bitpim ]
then
     mkdir /var/run/bitpim
fi
echo $action $1 > /var/run/bitpim/dnotify.log
