#!/bin/sh
# autogenerate content for debian/libfprint0.udev and debian/libfprint0.postinst

# Caveat: libfprint/drivers/aes1610.c is being recognized as binary file, hence the option "--binary-files=text"
ORIGDRIVERS=$(grep --binary-files=text "\.vendor" ../../libfprint/drivers/*.c  | \
sed -e 's/.*0x\(....\).\+0x\(....\).*/\1 \2/g' |\
sort | uniq)

#aes1610 driver used to be added through a patch so it must have been added manually
#ADDDRIVERS="\n08ff 1600"

DRIVERS="$ORIGDRIVERS$ADDDRIVERS"

#devices not to enable auto power/control for
#0483:2016 reported at http://lists.reactivated.net/pipermail/fprint/2009-October/001321.html, but IWFM and here as well:
#http://www.thinkwiki.org/wiki/How_to_enable_the_fingerprint_reader_with_ThinkFinger#Fix_for_the_fingerprint_reader_getting_too_hot
#045e:00bb reported at https://bugs.freedesktop.org/show_bug.cgi?id=66659
AUTOPOWERBLACKLIST='\(045e 00bb\|XXXX XXXX\|YYYY YYYY\)'

printf "# udev rules for libfprint0\n\n"

echo "# set permissions"

echo "$DRIVERS" |\
awk '{print "ATTRS{idVendor}==\"" $1 "\", ATTRS{idProduct}==\"" $2 "\", MODE=\"0664\", GROUP=\"plugdev\""}'

printf "\n# enable power saving\n"

echo "$DRIVERS" |\
sed /"$AUTOPOWERBLACKLIST"/Id |\
awk '{print "ATTRS{idVendor}==\"" $1 "\", ATTRS{idProduct}==\"" $2 "\", ATTR{power/control}==\"*\", ATTR{power/control}=\"auto\""}'

printf "\n__________________________________\n# snippet for libfprint0.postinst\n\n"

echo "$DRIVERS" |\
awk '{print "udevadm trigger --subsystem-match=usb --attr-match=idVendor=" $1 " --attr-match=idProduct=" $2 }'
