-
Notifications
You must be signed in to change notification settings - Fork 61
/
xbmc.sh
179 lines (159 loc) · 4.46 KB
/
xbmc.sh
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env bash
#
# xbmc.sh: Install and set up XBMC in stand-alone mode on Raspbian
# Copyright (C) 2014, Outernet Inc.
# Some rights reserved.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
#
set -e
# Constants
ROOT=0
OK=0
YES=0
NO=1
XBMC_USER=xbmc
XBMC_GROUPS=(input audio video dialout plugdev tty)
# Command aliases
WGET="wget --no-check-certificate"
# URLS and locations
TMPDIR=/tmp
PKGLISTS=/etc/apt/sources.list.d
INPUT_RULES=/etc/udev/rules.d/99-input.rules
LOG="install.log"
# checknet(URL)
#
# Performs wget dry-run on provided URL to see if it works. Echoes 0 if
# everything is OK, or non-0 otherwise.
#
checknet() {
$WGET -q --tries=10 --timeout=20 --spider "$1" > /dev/null || echo $NO
echo $?
}
# section(message)
#
# Echo section start message without newline.
#
section() {
echo -n "${1}... "
}
# fail()
#
# Echoes "FAIL" and exits
#
fail() {
echo "FAILED (see '$LOG' for details)"
exit 1
}
# do_or_fail()
#
# Runs a command and fails if commands returns with a non-0 status
#
do_or_fail() {
"$@" >> $LOG 2>&1 || fail
}
# do_or_pass()
#
# Runs a command and ignores non-0 return
#
do_or_pass() {
"$@" >> $LOG 2>&1 || true
}
# ensure_group(name)
#
# Ensure system group with given name exists.
#
ensure_group() {
grep "$1" /etc/group &> /dev/null || do_or_fail addgroup --system "$1"
}
# is_in_sources(name)
#
# Find out if any of the package source lists contain the name
is_in_sources() {
result=$(grep -h ^deb /etc/apt/sources.list /etc/apt/sources.list.d/*.list | grep -o "$1")
case "$result" in
"$1") echo $YES ;;
*) echo $NO ;;
esac
}
# backup()
#
# Back up a file by copying it to a path with '.old' suffix and echo about it
#
backup() {
if [[ -f "$1" ]] && ! [[ -f "${1}.old" ]]; then
cp "$1" "${1}.old"
echo "Backed up '$1' to '${1}.old'"
fi
}
###############################################################################
# Preflight checks
###############################################################################
section "Root permissions"
if [[ $EUID -ne $ROOT ]]; then
warn_and_die "Please run this script as root."
fi
echo "OK"
section "Internet connection"
if [[ $(checknet "http://example.com/") != $OK ]]; then
warn_and_die "Internet connection is required."
fi
echo "OK"
###############################################################################
# Packages
###############################################################################
section "Adding additional repositories"
if [[ $(is_in_sources mene.za.net) == $NO ]]; then
cat > "$PKGLISTS/mene.list" << EOF
deb http://archive.mene.za.net/raspbian wheezy contrib
EOF
fi
do_or_fail apt-key adv --keyserver keyserver.ubuntu.com --recv-key 5243CDED
echo "DONE"
section "Installing packages"
do_or_fail apt-get update
DEBIAN_FRONTEND=noninteractive do_or_fail apt-get -y --force-yes install \
python3.4 python3.4-dev python3-setuptools tvheadend
echo "DONE"
###############################################################################
# XBMC
###############################################################################
section "Configuring XBMC"
# Create 'xbmc' user
if ! [[ $(grep "$XBMC_USER" /etc/passwd) ]]; then
do_or_fail useradd "$XBMC_USER"
fi
# Create any missing groups
for grp in ${XBMC_GROUPS[@]}; do
ensure_group "$grp"
done
# Ensure it belongs to necessary groups
for grp in ${XBMC_GROUPS[@]}; do
do_or_pass gpasswd -a "$XBMC_USER" "$grp"
done
# Add udev rules
backup "$INPUT_RULES"
cat > "$INPUT_RULES" <<EOF
SUBSYSTEM=="input", GROUP="input", MODE="0660"
KERNEL=="tty[0-9]*", GROUP="tty", MODE="0660"
EOF
# Configure XBMC to start on boot
if ! [[ $(grep "ENABLED=1" /etc/default/xbmc) ]]; then
backup /etc/default/xbmc
cat /etc/default/xbmc.old | sed 's|ENABLED=0|ENABLED=1|' \
> /etc/default/xbmc
fi
do_or_fail update-rc.d xbmc defaults
echo "DONE"
echo "You may want to reboot the system now."