/elec/audio-switcher

To get this branch, use:
bzr branch http://bzr.ed.am/elec/audio-switcher
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash

APP=`basename "$0"`
umask 002
shopt -s nullglob
function die()
{
    [[ -n "$@" ]] && echo -e "$APP: $@" >&2
    exit 1
}

function version
{
    echo "$APP: audio switch control, version 0.1"
}

function usage
{
    echo "Usage: $APP [OPTION]... COMMAND"
    echo
    echo "Options:"
    echo "      --quiet          Do not complain on error"
    echo "  -v, --version        Display version information"
    echo "  -h, --help           This message"
    echo
    echo "Commands:"
    echo "  on   Switch audio routing on"
    echo "  off  Switch audio routing off"
    #echo
    #     01234567890123456789012345678901234567890123456789012345678901234567890123456789
}

# defaults
QUIET=0

# parse command line args
PROGRAMARGS=`getopt -n "$APP" -l device:,quiet,version,help vh "$@"`
[[ $? != 0 ]] && exit 1
eval set -- "$PROGRAMARGS"
while true; do
    case "$1" in
	--quiet) QUIET=1; shift;;
	-v|--version) version; exit 0;;
	-h|--help) usage; exit 0;;
	--) shift; break;;
    esac
done

# check other args
[[ -z "$1" ]] && die "you did not specify a command, see --help"
[[ -n "$2" ]] && die "too many arguments, see --help"

# work out command
COMMAND=
case "$1" in
    on) COMMAND=1;;
    off) COMMAND=0;;
    *) die "invalid comand, see --help";;
esac

# detect audio switch
DEVICES=`grep -l edam /sys/bus/usb/devices/*/manufacturer | sed 's/\/[^\/]*$//'`
DEVICES=`for DIR in "$DEVICES"; do grep -li ba7f "$DIR"/idProduct; done | sed 's/\/[^\/]*$//'`
DEVICES=`for DIR in "$DEVICES"; do grep -li 03eb "$DIR"/idVendor; done | sed 's/\/[^\/]*$//'`
DEVICES=`for DIR in "$DEVICES"; do [[ -d "$DIR":1.0/tty ]] && echo "$DIR":1.0/tty/*; done | sed 's/^.*\///'`
TEMP=( $DEVICES )
[[ ${#TEMP[*]} -gt 1 ]] && die "more than one audio switch detetced, use --device"    
DEVICE=$DEVICES

# no device?
[[ -z "$DEVICE" ]] && die "audio switch not found"
[[ -c /dev/"$DEVICE" ]] || die "audio switch device should be there, but isn't!"

# do command
echo $COMMAND > /dev/$DEVICE