luniebox/luniebox.sh
2022-02-06 10:19:03 +01:00

349 lines
9.0 KiB
Bash
Executable File

#!/usr/bin/env bash
AUTOMATIC=false
NO_UPDATES=false
NO_SPOTIFYDL=false
NO_SPOTIFY=false
NO_MPD=false
NO_AUDIO=false
MPU=false
AUDIO_DEVICE=false
AUDIO_DEVICES=("pimoroni" "adafruit")
DEVICE_NAME=false
help() {
local IFS="|"
printf "Usage: ${SCRIPTNAME} [-h] [-a] [--audio-device AUDIO_DEVICE] [--device-name DEVICE_NAME] [--no-updates] [--no-spotifydl] [--no-spotify] [--no-mpd] [--mpu] \n"
printf " -h\t\t\t\tshow this help\n"
printf " -a\t\t\t\tautomatic/non-interactive mode (without --audio-device, no Audio device is set up)\n"
printf " --audio-device [AUDIO_DEVICE]\tset Audio device (one of [${AUDIO_DEVICES[*]}])\n"
printf " --device-name [DEVICE_NAME]\tset luniebox device name (default: luniebox)\n"
printf " --no-updates\t\t\tskip system updates \n"
printf " --no-spotifydl\t\t\tskip installation of zspotify (no offline support for Spotify) \n"
printf " --no-mpd\t\t\tskip installation of Music Player Daemon (no offline support, also skips zspotify)\n"
printf " --no-spotify\t\t\tskip installation of spotifyd (no Spotify support, also skips zspotify)\n"
printf " --mpu\t\t\t\tsetup MPU9250 on I2C port (default is no MPU9250)\n"
exit 0
}
while [ -n "$1" ]; do
case "$1" in
"-h" | "--help")
help
;;
"-a" | "--automatic")
AUTOMATIC=true
;;
"--no-updates")
NO_UPDATES=true
;;
"--no-spotifydl")
NO_SPOTIFYDL=true
;;
"--no-spotify")
NO_SPOTIFY=true
;;
"--audio-device")
AUDIO_DEVICE="$2"
shift
;;
"--device-name")
DEVICE_NAME="$2"
shift
;;
"--mpu")
MPU=true
;;
*)
printf "\nargument '$1' not supported!\n\n"
help
;;
esac
shift
done
if [[ ${AUDIO_DEVICE} != "false" ]] && ! [[ ${AUDIO_DEVICES[*]} =~ "$AUDIO_DEVICE" ]]; then
NO_AUDIO=true
printf "Audio device '${AUDIO_DEVICE}' not supported, please set it up manually.\n"
fi
AUDIO_DEVICES+=("other")
start() {
if [[ ${AUTOMATIC} == "false" ]]; then
echo "##########################################
# _ _ _ #
# | |_ _ _ __ (_) ___| |__ _____ __ #
# | | | | | '_ \| |/ _ \ '_ \ / _ \ \/ / #
# | | |_| | | | | | __/ |_) | (_) > < #
# |_|\__,_|_| |_|_|\___|_.__/ \___/_/\_\ #
# #
##########################################
Welcome to luniebox setup. This script will perfom some interactive
steps to install and configure everything neccessary to convert this
device into a standalone spotify music box with RFID functionality.
If you want to run the script automatically, press n and restart script with -a option.
Run the script with -h to get more information about possible options to be applied for setup process.
"
read -rp "Do you want to setup luniebox now? [Y/n] " response
echo ""
case "$response" in
[nN][oO] | [nN])
echo "Bye...."
exit 0
;;
esac
fi
setup
}
setup() {
cd /home/pi
if [[ ${NO_UPDATES} == "false" ]]; then
update
fi
setup_software
set_device_name
setup_rfid
setup_mpu
if [[ ${NO_AUDIO} == "false" ]]; then
setup_audio
fi
finalize
}
update() {
if [[ ${AUTOMATIC} == "false" ]]; then
read -rp "Do you want to update system now? [Y/n] (recommended) " response
echo ""
case "$response" in
[nN][oO] | [nN])
return 0
;;
esac
fi
echo "Update system"
sudo apt update
sudo apt upgrade -y
}
setup_software() {
setup_app
if [[ ${NO_SPOTIFY} == "false" ]]; then
setup_spotify
fi
if [[ ${NO_MPD} == "false" ]]; then
setup_mpd
fi
if [[ ${NO_SPOTIFYDL} == "false" ]]; then
setup_zspotify
fi
}
setup_app() {
echo "Install required packages..."
sudo apt install -y git python3-venv python3-pip
echo "Fetch luniebox repository..."
git clone https://git.bstly.de/Lurkars/luniebox.git /home/pi/luniebox
cd /home/pi/luniebox/application
echo "Setup python venv..."
python -m venv venv
source venv/bin/activate
export CFLAGS=-fcommon
echo "Install pip requirements..."
pip install -r requirements.txt
deactivate
mkdir /home/pi/luniebox/config
echo "Setup config and systemd services..."
cp /home/pi/luniebox/contrib/config/luniebox.cfg /home/pi/luniebox/config/luniebox.cfg
sudo cp /home/pi/luniebox/contrib/service/luniebox-app.service /etc/systemd/system/
sudo cp /home/pi/luniebox/contrib/service/luniebox-daemon.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable luniebox-app luniebox-daemon
}
setup_spotify() {
if [[ ${AUTOMATIC} == "false" ]]; then
read -rp "Do you want to install spotifyd for Spofity support? [Y/n] " response
echo ""
case "$response" in
[nN][oO] | [nN])
NO_SPOTIFY=true
return 0
;;
esac
fi
mkdir /home/pi/luniebox/bin
echo "Download spotifyd..."
wget -c https://github.com/Spotifyd/spotifyd/releases/download/v0.3.3/spotifyd-linux-armv6-slim.tar.gz -O - | tar -xz -C /home/pi/luniebox/bin
echo "Setup config and systemd services..."
if [[ ${NO_AUDIO} == "true" ]]; then
echo "WARNING: you may need to adjust the file '/home/pi/luniebox/config/spotifyd.cfg' manually to setup your audio device!"
echo ""
fi
cp /home/pi/luniebox/contrib/config/spotifyd.cfg /home/pi/luniebox/config/spotifyd.cfg
sudo cp /home/pi/luniebox/contrib/service/spotifyd.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable spotifyd
}
setup_mpd() {
if [[ ${AUTOMATIC} == "false" ]]; then
read -rp "Do you want to install mpd (Music Player Daemon) for offline file support? [Y/n] " response
echo ""
case "$response" in
[nN][oO] | [nN])
NO_MPD=true
return 0
;;
esac
fi
mkdir /home/pi/luniebox/library
echo "Install required packages..."
sudo apt install -y mpd
echo "Setup config..."
if [[ ${NO_AUDIO} == "true" ]]; then
echo "WARNING: you may need to adjust the file '/etc/mpd.conf' manually to setup your audio device!"
echo ""
fi
sudo cp /home/pi/luniebox/contrib/config/mpd.conf /etc/mpd.conf
}
setup_zspotify() {
if [[ ${NO_SPOTIFY} == "false" ]] && [[ ${NO_MPD} == "false" ]]; then
if [[ ${AUTOMATIC} == "false" ]]; then
read -rp "Do you want to install zspotify for downloading Spotify tracks for offline support? [Y/n] " response
echo ""
case "$response" in
[nN][oO] | [nN])
NO_MPD=true
return 0
;;
esac
fi
echo "Fetch clspotify repository..."
git clone https://github.com/agent255/clspotify.git /home/pi/clspotify
cd /home/pi/clspotify
echo "Setup python venv..."
python -m venv venv
source venv/bin/activate
echo "Install pip requirements..."
pip install -r requirements.txt
deactivate
echo "Setup config..."
sed -i "s/^zspotify_path =.*$/zspotify_path = \/home\/pi\/clspotify\//" /home/pi/luniebox/config/luniebox.cfg
fi
}
set_device_name() {
if [[ ${AUTOMATIC} == "false" ]] && [[ ${DEVICE_NAME} == "false" ]]; then
read -rp "Name for your Luniebox devices [default: luniebox]: " DEVICE_NAME
fi
if ! [[ $DEVICE_NAME ]] || [[ ${DEVICE_NAME} == "false" ]]; then
DEVICE_NAME="luniebox"
fi
echo "Set device name to '${DEVICE_NAME}'"
printf "${DEVICE_NAME}" | sudo tee /etc/hostname 1>/dev/null
sudo sed -i "/^127.0.0.1/s/$/ ${DEVICE_NAME}/g" /etc/hosts
if [[ NO_SPOTIFY == "false" ]]; then
sed -i "s/^device_name.*=.*$/device_name = \"${DEVICE_NAME}\"/" /home/pi/luniebox/config/spotifyd.cfg
fi
}
setup_rfid() {
echo "Enable SPI for RFID reader"
sudo sed -i "/dtparam=spi=on/s/^#//g" /boot/config.txt
}
setup_mpu() {
if [[ ${AUTOMATIC} == "false" ]] && [[ ${MPU} == "false" ]]; then
read -rp "Do you want to setup I2C for MPU9250 9-axis sensor? [y/N] " response
echo ""
case "$response" in
[yY][eE][sS] | [yY])
MPU=true
break
;;
*)
return 0
;;
esac
fi
if [[ ${MPU} == "true" ]]; then
echo "Install I2C software, enable i2c bus 4, enable mpu"
sudo apt install -y i2c-tools python3-smbus
sudo sed -i "/dtparam=i2c_arm=on/s/^#//g" /boot/config.txt
printf "dtoverlay=i2c-gpio,bus=4,i2c_gpio_delay_us=1,i2c_gpio_sda=23,i2c_gpio_scl=24" | sudo tee -a /boot/config.txt 1>/dev/null
sed -i "s/^mpu =.*$/mpu = True/" /home/pi/luniebox/config/luniebox.cfg
fi
}
setup_audio() {
if [[ ${AUTOMATIC} == "false" ]]; then
echo "Choose your audio hardware"
select response in "${AUDIO_DEVICES[@]}"; do
echo ""
AUDIO_DEVICE = "$response"
done
fi
case "$AUDIO_DEVICE" in
"pimoroni")
setup_pimoroni
;;
"adafruit")
setup_adafruit
;;
*)
NO_AUDIO=true
echo "WARNING: please setup your audio device manually!"
;;
esac
}
setup_pimoroni() {
echo "Setup Pimoroni Shim"
sudo sed -i "/dtparam=audio=on/s/^/#/g" /boot/config.txt
printf "dtoverlay=hifiberry-dac\ngpio=25=op,dh" | sudo tee -a /boot/config.txt 1>/dev/null
}
setup_adafruit() {
echo "Setup Adafruit Speaker Bonnet"
sudo sed -i "/dtparam=audio=on/s/^/#/g" /boot/config.txt
printf "dtoverlay=hifiberry-dac\ndtoverlay=i2s-mmap" | sudo tee -a /boot/config.txt 1>/dev/null
cat /home/pi/luniebox/contrib/config/asound.conf | sudo tee /etc/asound.conf 1>/dev/null
}
finalize() {
if [[ ${AUTOMATIC} == "false" ]]; then
read -rp "Setup finished and reboot required. Do you want to automatically reboot your luniebox now? [Y/n] " response
echo ""
case "$response" in
[nN][oO] | [nN])
echo "Bye...."
exit 0
;;
esac
fi
echo "Rebooting now...."
sleep 2
sudo reboot
exit
}
start