~jon-raiford/mythbuntu/lirc

« back to all changes in this revision

Viewing changes to debian/lirc.init.d

  • Committer: Jon Raiford
  • Date: 2010-02-09 00:11:45 UTC
  • Revision ID: jon@raiford.org-20100209001145-i6d4pos7vv354hxh
Rewrote lirc.init.d to allow for any number of devices

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /bin/sh
 
1
#! /bin/bash
2
2
### BEGIN INIT INFO
3
3
# Provides:          lirc
4
4
# Required-Start:    $remote_fs $syslog
38
38
{
39
39
        REMOTE_ARGS="$*"
40
40
 
41
 
        #For remote only detection support, we need
42
 
        #both REMOTE_DEVICE and TRANSMITTER_DEVICE undefined
43
 
        if [ -z "$REMOTE_DEVICE" ] && [ -z "$TRANSMITTER_DEVICE" ] && [ -c $dev ]; then
44
 
                REMOTE_DEVICE="$dev"
45
 
        fi
46
 
 
47
 
        #If we have a REMOTE_DEVICE or REMOTE_DRIVER defined (either because no devices
48
 
        #were defined, OR if we explicitly did), then populate REMOTE_ARGS
49
 
        if [ ! -z "$REMOTE_DEVICE" ] || [ ! -z "$REMOTE_DRIVER" ]; then
50
 
                if [ -n "$REMOTE_DEVICE" ] && [ "$REMOTE_DEVICE" != "none" ]; then
51
 
                        REMOTE_ARGS="--device=$REMOTE_DEVICE $REMOTE_ARGS"
52
 
                fi
53
 
                if [ -n "$REMOTE_DRIVER" ] && [ "$REMOTE_DRIVER" != "none" ]; then
54
 
                        REMOTE_ARGS="--driver=$REMOTE_DRIVER $REMOTE_ARGS"
55
 
                fi
56
 
 
57
 
                #Now, if we ALSO have a transmitter defined, add some args
58
 
                #To make the first lircd listen up
59
 
                if [ ! -z "$TRANSMITTER_DEVICE" ] || [ ! -z "$TRANSMITTER_DRIVER" ]; then
60
 
                        REMOTE_ARGS="$REMOTE_ARGS --listen"
61
 
                fi
62
 
                REMOTE_ARGS="--output=$REMOTE_SOCKET $REMOTE_ARGS"
63
 
        fi
 
41
        if [ "${REMOTE_DEVICE[$REMOTE_NUM]}" != "none" ]; then
 
42
                REMOTE_ARGS="--device=${REMOTE_DEVICE[$REMOTE_NUM]} $REMOTE_ARGS"
 
43
        fi
 
44
        if [ -n "${REMOTE_DRIVER[$REMOTE_NUM]}" ] && [ "${REMOTE_DRIVER[$REMOTE_NUM]}" != "none" ]; then
 
45
                REMOTE_ARGS="--driver=${REMOTE_DRIVER[$REMOTE_NUM]} $REMOTE_ARGS"
 
46
        fi
 
47
 
 
48
        if [ $REMOTE_COUNT -ge 2 ]; then
 
49
                if [ $REMOTE_NUM -gt 0 ]; then
 
50
                        REMOTE_ARGS="$REMOTE_ARGS --listen=${LIRC_PORT[$REMOTE_NUM]}"
 
51
                else
 
52
                        for I in $(seq $(($REMOTE_COUNT - 1)) -1 1)
 
53
                        do
 
54
                                REMOTE_ARGS="$REMOTE_ARGS --connect=localhost:${LIRC_PORT[$I]}"
 
55
                        done
 
56
                fi
 
57
        fi
 
58
        REMOTE_ARGS="--output=${REMOTE_SOCKET[$REMOTE_NUM]} $REMOTE_ARGS --pidfile=${REMOTE_PID[$REMOTE_NUM]}"
64
59
        echo $REMOTE_ARGS
65
60
}
66
61
 
67
 
build_transmitter_args ()
68
 
{
69
 
        TRANSMITTER_ARGS="$*"
70
 
 
71
 
        #Transmitters must be explicitly be defined
72
 
        if [ ! -z "$TRANSMITTER_DEVICE" ] || [ ! -z "$TRANSMITTER_DRIVER" ]; then
73
 
                if [ -n "$TRANSMITTER_DEVICE" ] && [ "$TRANSMITTER_DEVICE" != "none" ]; then
74
 
                        TRANSMITTER_ARGS="--device=$TRANSMITTER_DEVICE $TRANSMITTER_ARGS"
75
 
                fi
76
 
                if [ -n "$TRANSMITTER_DRIVER" ] && [ "$TRANSMITTER_DRIVER" != "none" ]; then
77
 
                        TRANSMITTER_ARGS="--driver=$TRANSMITTER_DRIVER $TRANSMITTER_ARGS"
78
 
                fi
79
 
 
80
 
                #Now, if we ALSO have a remote defined, add some args
81
 
                #To make the second lircd connect
82
 
                if [ ! -z "$REMOTE_DEVICE" ] || [ ! -z "$REMOTE_DRIVER" ]; then
83
 
                        TRANSMITTER_SOCKET="${TRANSMITTER_SOCKET}1"
84
 
                        TRANSMITTER_ARGS="$TRANSMITTER_ARGS --connect=localhost:8765 --pidfile=/var/run/lirc/lircd1.pid"
85
 
                fi
86
 
                TRANSMITTER_ARGS="--output=$TRANSMITTER_SOCKET $TRANSMITTER_ARGS"
87
 
        fi
88
 
        echo $TRANSMITTER_ARGS
89
 
}
90
 
 
91
62
. /lib/lsb/init-functions
92
63
 
93
64
test -f /usr/sbin/lircd || exit 0
123
94
fi
124
95
 
125
96
#We need default socket locations
126
 
OLD_SOCKET="/dev/lircd"
127
 
if [ -z "$REMOTE_SOCKET" ]; then
128
 
        REMOTE_SOCKET="/var/run/lirc/lircd"
129
 
fi
130
 
if [ -z "$TRANSMITTER_SOCKET" ]; then
131
 
        TRANSMITTER_SOCKET="/var/run/lirc/lircd"
 
97
LEGACY_SOCKET="/dev/lircd"
 
98
DEFAULT_SOCKET="/var/run/lirc/lircd"
 
99
REMOTE_PID="/var/run/lirc/lircd.pid"
 
100
 
 
101
# Get the remote count
 
102
REMOTE_COUNT=0
 
103
if [ ! -z "${REMOTE_DEVICE[0]}" ]; then
 
104
        REMOTE_COUNT=${#REMOTE_DEVICE[*]}
 
105
fi
 
106
 
 
107
# This section should eventually be removed
 
108
# Since there is nothing special about the transmitter variables, define as an additional remote instead
 
109
if [ ! -z "$TRANSMITTER_DEVICE" ]; then
 
110
        REMOTE[$REMOTE_COUNT]="$TRANSMITTER"
 
111
        REMOTE_MODULES="$REMOTE_MODULES $TRANSMITTER_MODULES"
 
112
        REMOTE_DRIVER[$REMOTE_COUNT]="$TRANSMITTER_DRIVER"
 
113
        REMOTE_DEVICE[$REMOTE_COUNT]="$TRANSMITTER_DEVICE"
 
114
        REMOTE_SOCKET[$REMOTE_COUNT]="$TRANSMITTER_SOCKET"
 
115
        REMOTE_LIRCD_CONF[$REMOTE_COUNT]="$TRANSMITTER_LIRCD_CONF"
 
116
        REMOTE_LIRCD_ARGS[$REMOTE_COUNT]="$TRANSMITTER_LIRCD_ARGS"
 
117
        REMOTE_COUNT=$(($REMOTE_COUNT + 1))
 
118
fi
 
119
 
 
120
if [ $REMOTE_COUNT -eq 0 ] && [ -c $dev ]; then
 
121
        REMOTE_DEVICE="$dev"
 
122
        REMOTE_COUNT=1
132
123
fi
133
124
 
134
125
case "$1" in
135
126
        start)
 
127
                # Load the modules
136
128
                if [ "$LOAD_MODULES" = "true" ] && [ "$START_LIRCD" = "true" ]; then
137
 
                        load_modules $2 $REMOTE_MODULES $TRANSMITTER_MODULES $MODULES
 
129
                        load_modules $2 $REMOTE_MODULES $MODULES
138
130
                fi
139
131
 
 
132
                # Start the lircd instance(s)
140
133
                if [ "$START_LIRCD" = "true" ]; then
 
134
                        # make sure the /var/run/lirc directory exists
141
135
                        [ -d "/var/run/lirc" ] || mkdir -p "/var/run/lirc"
 
136
 
142
137
                        log_daemon_msg "Starting remote control daemon(s) : LIRC "
143
 
                        REMOTE_LIRCD_ARGS=`build_remote_args $REMOTE_LIRCD_ARGS`
144
 
                        TRANSMITTER_LIRCD_ARGS=`build_transmitter_args $TRANSMITTER_LIRCD_ARGS`
145
 
 
146
 
                        #if we have a remote defined, it is primary process
147
 
                        if [ ! -z "$REMOTE_LIRCD_ARGS" ]; then
148
 
                                start-stop-daemon --start --quiet --oknodo --exec /usr/sbin/lircd -- $REMOTE_LIRCD_ARGS < /dev/null
149
 
                                log_end_msg $?
150
 
                                if [ -S "$REMOTE_SOCKET" -a "$OLD_SOCKET" != "$REMOTE_SOCKET" ]; then
151
 
                                        rm -f $OLD_SOCKET && ln -s $REMOTE_SOCKET $OLD_SOCKET
152
 
                                fi 
153
 
 
154
 
                                #now if we additionally have a transmitter defined, it is secondary process
155
 
                                if [ ! -z "$TRANSMITTER_LIRCD_ARGS" ]; then
156
 
                                        /usr/sbin/lircd $TRANSMITTER_LIRCD_ARGS < /dev/null
157
 
                                        if [ -S "$TRANSMITTER_SOCKET" ]; then
158
 
                                                rm -f ${OLD_SOCKET}1 && ln -s $TRANSMITTER_SOCKET ${OLD_SOCKET}1
159
 
                                        fi 
160
 
                                fi
161
 
                        elif [ ! -z "$TRANSMITTER_LIRCD_ARGS" ]; then
162
 
                                start-stop-daemon --start --quiet --oknodo --exec /usr/sbin/lircd -- $TRANSMITTER_LIRCD_ARGS < /dev/null
163
 
                                log_end_msg $?
164
 
                                if [ -S "$TRANSMITTER_SOCKET" -a "$OLD_SOCKET" != "$TRANSMITTER_SOCKET" ]; then
165
 
                                        rm -f $OLD_SOCKET && ln -s $TRANSMITTER_SOCKET $OLD_SOCKET
166
 
                                fi 
167
 
                        else
168
 
                                log_end_msg 1
169
 
                        fi
 
138
 
 
139
                        # loop over all remotes defined - go backwards so the final one can connect to the rest
 
140
                        for REMOTE_NUM in $(seq $(($REMOTE_COUNT - 1)) -1 0)
 
141
                        do
 
142
                                # determine the defaults to use for this remote
 
143
                                if [ $REMOTE_NUM -gt 0 ]; then
 
144
                                        LEGACY_SOCKET[$REMOTE_NUM]="$LEGACY_SOCKET$REMOTE_NUM"
 
145
                                        DEFAULT_SOCKET[$REMOTE_NUM]="$DEFAULT_SOCKET$REMOTE_NUM"
 
146
                                        REMOTE_PID[$REMOTE_NUM]="/var/run/lirc/lircd$REMOTE_NUM.pid"
 
147
                                        if [ -z ${LIRC_PORT[$REMOTE_NUM]} ]; then
 
148
                                                LIRC_PORT[$REMOTE_NUM]=$((8765 + $REMOTE_NUM))
 
149
                                        fi
 
150
                                fi
 
151
 
 
152
                                # if no socket was specified, use the default
 
153
                                if [ -z ${REMOTE_SOCKET[$REMOTE_NUM]} ]; then
 
154
                                        REMOTE_SOCKET[$REMOTE_NUM]=${DEFAULT_SOCKET[$REMOTE_NUM]}
 
155
                                fi
 
156
 
 
157
                                # call build_remote_args
 
158
                                CURRENT_LIRCD_ARGS=`build_remote_args ${REMOTE_LIRCD_ARGS[$REMOTE_NUM]}`
 
159
 
 
160
                                # launch the lircd daemon with the supplied arguments
 
161
                                start-stop-daemon --start --quiet --oknodo --pidfile ${REMOTE_PID[$REMOTE_NUM]} --exec /usr/sbin/lircd -- $CURRENT_LIRCD_ARGS < /dev/null
 
162
                                # If lircd created the new socket and its not the same as the legacy socket, link the legacy socket to the newly created one
 
163
                                if [ -S "${REMOTE_SOCKET[$REMOTE_NUM]}" -a "${LEGACY_SOCKET[$REMOTE_NUM]}" != "${REMOTE_SOCKET[$REMOTE_NUM]}" ]; then
 
164
                                        rm -f ${LEGACY_SOCKET[$REMOTE_NUM]} && ln -s ${REMOTE_SOCKET[$REMOTE_NUM]} ${LEGACY_SOCKET[$REMOTE_NUM]}
 
165
                                fi
 
166
                        done
 
167
                        log_end_msg $?
170
168
                fi
171
169
 
172
170
                if [ "$START_LIRCMD" = "true" ]; then
200
198
                        log_daemon_msg "Stopping remote control daemon(s): LIRC"
201
199
                        start-stop-daemon --stop --quiet --exec /usr/sbin/lircd
202
200
                        log_end_msg $?
203
 
                        [ -h "$OLD_SOCKET" ] && rm -f $OLD_SOCKET
204
 
                        [ -h "${OLD_SOCKET}1" ] && rm -f ${OLD_SOCKET}1
 
201
                        [ -h "$LEGACY_SOCKET" ] && rm -f $LEGACY_SOCKET
 
202
                        [ -h "${LEGACY_SOCKET}1" ] && rm -f ${LEGACY_SOCKET}1
205
203
                fi
206
204
                ;;
207
205
        reload|force-reload)