Files
shell_sequencer/seqs/kodi.sh

231 lines
5.8 KiB
Bash
Executable File

#!/bin/bash
## Installing kodi on a raspberry pi
## Some measures are taken to prevent corrupting existing files
## but it's intendet use is for a blank rasperry pi.
toolName="Kodi"
toolUser="kodi"
toolServiceFile="/etc/systemd/system/kodi.service"
toolProfileDir="/home/kodi/.kodi"
step_1_info() { echo "Install $toolName via apt"; }
step_1_alias() { echo "install"; }
step_1() {
exe apt update && apt install kodi kodi-inputstream-adaptive kodi-eventclients-kodi-send
}
step_2_info() { echo "Set localisation and keyboard layout"; }
step_2() {
echo "Goto \"Localisation Options\" in the following menu."
read -p "Press ENTER to continue..."
exe raspi-config
saveReturn $?
endReturn
}
step_3_info() { echo "Add $toolName user ($toolUser)"; }
step_3() {
exep "id -u $toolUser >>/dev/null 2>&1"
if [ $? -eq 0 ] ; then
echo
echo "User $toolUser exists."
echo "Skipping user creation."
return 0
fi
exe adduser \
--disabled-password \
--gecos "User to run $toolName Media Center" \
$toolUser
exe usermod -a -G audio,video,input,dialout,plugdev,netdev,users,cdrom,tty $toolUser
saveReturn $?
endReturn
echo "User $toolUser details:"
exe id $toolUser
}
step_4_info() { echo "Create systemd service"; }
step_4() {
addConf -c "$toolServiceContent" "$toolServiceFile"
if [ $? -eq 0 ] ; then
exe systemctl daemon-reload
exe systemctl enable kodi.service
saveReturn $?
endReturn
else
echo "[ERROR] failed to setup systemd service"
fi
}
toolServiceContent="\
[Unit]
Description = Kodi Media Center
# if you don't need the MySQL DB backend, this should be sufficient
After = systemd-user-sessions.service network.target sound.target
# if you need the MySQL DB backend, use this block instead of the previous
# After = systemd-user-sessions.service network.target sound.target mysql.service
# Wants = mysql.service
[Service]
User = ${toolUser}
Group = ${toolUser}
Type = simple
ExecStart = /usr/bin/kodi-standalone
ExecStop = /usr/bin/kodi-send --action=\"Quit\"
ExecStopPost = sleep 15
Restart = always
RestartSec = 15
[Install]
WantedBy = multi-user.target"
step_5_info() { echo "Enable text input for USB keyboards within $toolName"; }
step_5() {
local udevFile="/etc/udev/rules.d/99-kodi.rules"
local inputRule="KERNEL==\"tty[0-9]*\", GROUP=\"tty\", MODE=\"0660\""
addConf -c "$inputRule" "$udevFile"
}
step_6_info() { echo "Increase raspberry pi GPU memory to 320"; }
step_6() {
if ! exep "grep -e '^[ ]*gpu_mem' \"$bootConfigLoc\" >>/dev/null 2>&1" && [ -f "$bootConfigLoc" ] ; then
# attach settings to raspberry boot configuration
exe mount -o remount,rw /boot
addConf -a "$bootConfig" "$bootConfigLoc"
exe mount -o remount,ro /boot
else
# add to missing conf
addConf -m "$bootConfig" "$bootConfigLoc"
fi
}
bootConfigLoc="/boot/config.txt"
bootConfig="
# Modifications for $toolName
start_x=1
gpu_mem=320"
step_7_info() { echo "Reboot is needed for all changes to take effect"; }
step_7() {
if quiet ; then
answer=n
else
read -r -p "Reboot now? y/n(default)" answer
fi
case $answer in
y|Y)
exe reboot;;
*)
echo
echo "Start $toolName manually:"
echo "service kodi start"
;;
esac
}
step_10_info() { echo "Send $toolName logs to syslog"; }
step_10_alias() { echo "syslog"; }
step_10() {
addConf -s "$kodiSyslog" "$kodiSyslogLoc"
exe chmod -f +x "$kodiSyslogLoc"
}
kodiSyslogLoc="/usr/local/bin/kodisyslog"
kodiSyslog="#!/bin/bash
tail -f /home/kodi/.kodi/temp/kodi.log |
while read -r line ; do
logger -t KODI \"\${line:37}\"
done"
# Using previously generated executable
step_11_info() { echo "Create systemd service for logging to syslog"; }
step_11() {
[ ! -f "$kodiSyslogLoc" ] && return 1
addConf -s "$kodiSyslogService" "$kodiSyslogServiceLoc"
info "Consider limiting systemd journal size"
info " [/etc/systemd/journald.conf]"
info " SystemMaxUse=50M"
}
kodiSyslogServiceLoc="/etc/systemd/system/kodisyslog.service"
kodiSyslogService="[Unit]
Description=Pipe kodi logs to syslog
After=network.target syslog.service
[Service]
User=root
ExecStart=/usr/local/bin/kodisyslog
[Install]
WantedBy=multi-user.target"
step_12_info() { echo "Enable and start kodisyslog service"; }
step_12() {
local kodiSyslogName
kodiSyslogName="$(basename $kodiSyslogServiceLoc)"
exe systemctl daemon-reload
exe systemctl enable "$kodiSyslogName"
exe systemctl start "$kodiSyslogName"
}
step_14_info() { echo "Add ufw rules for kodi"; }
step_14_alias() { echo "ufw"; }
step_14() {
exe ufw allow in on eth0 to any proto tcp port 8080 comment "kodi remote"
exe ufw allow in on eth0 to any proto tcp port 9090 comment "kodi jsonrpc"
exe ufw allow in on eth0 to any proto udp port 9777 comment "kodi event client"
}
step_40_info() { echo "Reset music database"; }
step_40_alias() { echo "resetmusic"; }
step_40() {
local i
local answer="y"
local musicDb=( "$toolProfileDir/userdata/Database/MyMusic"*.db )
warning "Erasing:"
for i in "${musicDb[@]}"; do
info "$i"
done
interactive && read -r -p "Are you sure? (y)/[n] " answer
case "$answer" in
y|Y)
exe rm "${musicDb[@]}";;
*)
info "Abort reset"
return 1;
esac
}
step_100_info() { echo "Notes"; }
step_100_alias() { echo "notes"; }
step_100() {
cat <<NOTES_EOF
# Install additional packages
kodi-inputstream-adaptive - needed for Youtube plugin
kodi-eventclients-kodi-send - recommended way to stop kodi gracefully
# Sudoers change for Kodi Matrix
\`\`\`
Cmnd_Alias KODI_VT = /bin/fgconsole, /bin/chvt *
Cmnd_Alias KODI_VT2 = /bin/openvt
%video ALL = (root) NOPASSWD: KODI_VT
%video ALL = (root) NOPASSWD: KODI_VT2
\`\`\`
NOTES_EOF
}
# shellcheck disable=SC2034 # Appears unused
readonly sqr_minVersion=16
# shellcheck disable=SC1091 # Don't follow this source
. /usr/local/bin/sequencer.sh