kiekus/kiekmon-ptp

59 lines
1.3 KiB
Bash
Executable File

#!/bin/sh
set -e
device=
usbbid=
usbpid=
usbvid=
usage() {
echo "usage: $0 [-h] [-r DEVICE | -w [-b BUSID] [-v VENDORID] [-p PRODUCTID] [PATTERN]]" >&2
}
while getopts hwr:b:v:p: opt; do
case "$opt" in
w) action=watch;;
r) action=remove; device="$OPTARG";;
b) usbbid="$OPTARG";;
v) usbvid="$(printf "%04x" 0x$OPTARG)";;
p) usbpid="$(printf "%04x" 0x$OPTARG)";;
h) usage; exit 0;;
?) usage; exit 255;;
esac
done
shift $(($OPTIND - 1))
pattern="$1"
case "$action" in
watch)
kiekmon-usb -w \
${usbbid:+-b $usbbid} \
${usbvid:+-v $usbvid} \
${usbpid:+-p $usbpid} \
-c 6 -C 1 -P 1 | \
rp-get . rm_command | \
while read -r sysdir && read -r rmcmd; do (
trap "$rmcmd" INT TERM
busnum=$(cat "$sysdir/busnum")
devnum=$(cat "$sysdir/devnum")
info=$(ptpcam --bus=$busnum --dev=$devnum -i)
if [ -z "$info" ]; then
$rmcmd
exit
fi
if [ -n "$pattern" ]; then
model="$(printf "%s\n" "$info" | grep '^Model:' | cut -d ' ' -f2-)"
case "$model" in
$pattern) ;;
*) "$rmcmd"; exit;
esac
fi
rp-make "$sysdir" protocol=ptp rm_command="$rmcmd"
) || true; done
;;
remove)
kiekmonitor-usb -r "$device"
;;
esac