Skip to content

Commit

Permalink
Refactor a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
rmens committed Mar 17, 2024
1 parent e6dde24 commit 338c1be
Showing 1 changed file with 67 additions and 35 deletions.
102 changes: 67 additions & 35 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
#!/usr/bin/env bash

# Define paths and URLs
FUNCTIONS_SH_PATH="/tmp/functions.sh"
FUNCTIONS_SH_URL="https://raw.githubusercontent.com/oszuidwest/bash-functions/main/common-functions.sh"

MICROMPX_DECODER_PATH="/opt/micrompx/MicroMPX_Decoder"
MICROMPX_DECODER_URL="https://download.thimeo.com/MicroMPX_Decoder_ARM64"
MICROMPX_SERVICE_PATH="/etc/systemd/system/micrompx.service"
MICROMPX_SERVICE_URL="https://raw.githubusercontent.com/oszuidwest/rpi-umpx-decoder/main/micrompx.service"
LOG_DIR="/home/micrompx/.MicroMPX_Decoder.log" # Log directory

RAMDISK_SERVICE_PATH="/etc/systemd/system/ramdisk.service"
RAMDISK_SERVICE_URL="https://raw.githubusercontent.com/oszuidwest/rpi-umpx-decoder/main/ramdisk.service"
RAMDISK_PATH="/mnt/ramdisk" # RAM disk path

CONFIG_FILE_PATHS=("/boot/firmware/config.txt" "/boot/config.txt") # Config file paths
FIRST_IP=$(hostname -I | awk '{print $1}') # Determine the first IP address upfront

# Start with a clean terminal
clear

# Remove old functions libraries and download the latest version
rm -f /tmp/functions.sh
if ! curl -s -o /tmp/functions.sh https://raw.githubusercontent.com/oszuidwest/bash-functions/main/common-functions.sh; then
# Remove old functions library and download the latest version
rm -f "$FUNCTIONS_SH_PATH"
if ! curl -s -o "$FUNCTIONS_SH_PATH" "$FUNCTIONS_SH_URL"; then
echo -e "*** Failed to download functions library. Please check your network connection! ***"
exit 1
fi

# Source the functions file
source /tmp/functions.sh
source "$FUNCTIONS_SH_PATH"

# Set color variables
set_colors
Expand All @@ -27,19 +44,20 @@ is_this_os_64bit
check_rpi_model 4

# Determine the correct config file path
if [ -f /boot/firmware/config.txt ]; then
CONFIG_FILE=/boot/firmware/config.txt
elif [ -f /boot/config.txt ]; then
CONFIG_FILE=/boot/config.txt
else
CONFIG_FILE=""
for path in "${CONFIG_FILE_PATHS[@]}"; do
if [ -f "$path" ]; then
CONFIG_FILE="$path"
break
fi
done

if [ -z "$CONFIG_FILE" ]; then
echo -e "${RED}Error: config.txt not found in known locations.${NC}"
exit 1
fi

# Determine the first IP address
FIRST_IP=$(hostname -I | awk '{print $1}')

# Something fancy for the sysadmin
# Banner
cat << "EOF"
______ _ ___ __ _ ______ __ __
|___ / (_) | \ \ / / | | | ____| \/ |
Expand All @@ -49,8 +67,12 @@ cat << "EOF"
/_____\__,_|_|\__,_| \/ \/ \___||___/\__| |_| |_| |_|
EOF

# Hi!
# Greeting
echo -e "${GREEN}⎎ MicroMPX Setup for Raspberry Pi${NC}\n\n"
ask_user "ENABLE_HEARTBEAT" "n" "Do you want to integrate heartbeat monitoring via UptimeRobot (y/n)" "y/n"
if [ "$ENABLE_HEARTBEAT" == "y" ]; then
ask_user "HEARTBEAT_URL" "https://heartbeat.uptimerobot.com/xxx" "Enter the URL to get every minute for heartbeat monitoring" "str"
fi

# Check and stop MicroMPX service if running
echo -e "${BLUE}►► Checking and stopping MicroMPX service if running...${NC}"
Expand Down Expand Up @@ -86,45 +108,55 @@ install_packages silent libasound2 libsndfile1
# Download MicroMPX from Thimeo
echo -e "${BLUE}►► Downloading and installing MicroMPX...${NC}"
mkdir -p /opt/micrompx > /dev/null
curl -s -o /opt/micrompx/MicroMPX_Decoder https://download.thimeo.com/MicroMPX_Decoder_ARM64
chmod +x /opt/micrompx/MicroMPX_Decoder > /dev/null
setcap CAP_NET_BIND_SERVICE=+eip /opt/micrompx/MicroMPX_Decoder > /dev/null
curl -s -o "$MICROMPX_DECODER_PATH" "$MICROMPX_DECODER_URL"
chmod +x "$MICROMPX_DECODER_PATH" > /dev/null
setcap CAP_NET_BIND_SERVICE=+eip "$MICROMPX_DECODER_PATH" > /dev/null

# Add service
echo -e "${BLUE}►► Installing MicroMPX service...${NC}"
rm -f /etc/systemd/system/micrompx.service > /dev/null
curl -s -o /etc/systemd/system/micrompx.service https://raw.githubusercontent.com/oszuidwest/rpi-umpx-decoder/main/micrompx.service
rm -f "$MICROMPX_SERVICE_PATH" > /dev/null
curl -s -o "$MICROMPX_SERVICE_PATH" "$MICROMPX_SERVICE_URL"
systemctl daemon-reload > /dev/null
systemctl enable micrompx > /dev/null

# Add RAM disk
echo -e "${BLUE}►► Setting up RAM disk for logs...${NC}"
rm -f /etc/systemd/system/ramdisk.service > /dev/null
curl -s -o /etc/systemd/system/ramdisk.service https://raw.githubusercontent.com/oszuidwest/rpi-umpx-decoder/main/ramdisk.service
rm -f "$RAMDISK_SERVICE_PATH" > /dev/null
curl -s -o "$RAMDISK_SERVICE_PATH" "$RAMDISK_SERVICE_URL"
systemctl daemon-reload > /dev/null
systemctl enable ramdisk > /dev/null
systemctl start ramdisk

# Put MicroMPX logs on RAM disk
echo -e "${BLUE}►► Putting MicroMPX logs on the RAM disk...${NC}"
if [ -d "/home/micrompx/.MicroMPX_Decoder.log" ]; then
echo -e "${YELLOW}Log directory exists. Removed if before creating the symlink.${NC}"
rm -rf /home/micrompx/.MicroMPX_Decoder.log
if [ -d "$LOG_DIR" ]; then
echo -e "${YELLOW}Log directory exists. Removing it before creating the symlink.${NC}"
rm -rf "$LOG_DIR"
fi
ln -s "$RAMDISK_PATH" "$LOG_DIR"
chown -R micrompx:micrompx "$RAMDISK_PATH"

# Clean logs every 7 days to save space on the RAM disk (MicroMPX does this every 30 days)
LOGS_CRONJOB="0 0 * * * find -L $LOG_DIR -type f -mtime +7 -exec rm {} \;"
echo -e "${BLUE}►► Setting up log file deletion cronjob...${NC}"
if ! crontab -l | grep -F -- "$LOGS_CRONJOB" > /dev/null; then
(crontab -l 2>/dev/null; echo "$LOGS_CRONJOB") | crontab -
else
echo -e "${YELLOW}Log file deletion cronjob already exists. No changes made.${NC}"
fi
ln -s /mnt/ramdisk /home/micrompx/.MicroMPX_Decoder.log
chown -R micrompx:micrompx /mnt/ramdisk

# Heartbeat monitoring
ask_user "ENABLE_HEARTBEAT" "n" "Do you want to integrate heartbeat monitoring via UptimeRobot (y/n)" "y/n"
if [ "$ENABLE_HEARTBEAT" == "y" ]; then
ask_user "HEARTBEAT_URL" "https://heartbeat.uptimerobot.com/xxx" "Enter the URL to get every minute for heartbeat monitoring" "str"
# Add a cronjob that calls the HEARTBEAT_URL every minute
echo -e "${BLUE}►► Setting up heartbeat monitoring cronjob...${NC}"
(crontab -l 2>/dev/null; echo "* * * * * wget --spider $HEARTBEAT_URL > /dev/null 2>&1") | crontab -
echo -e "${GREEN}Heartbeat monitoring cronjob added.${NC}"
echo -e "${BLUE}►► Setting up heartbeat monitoring...${NC}"
HEARTBEAT_CRONJOB="* * * * * wget --spider $HEARTBEAT_URL > /dev/null 2>&1"
if ! crontab -l | grep -F -- "$HEARTBEAT_CRONJOB" > /dev/null; then
(crontab -l 2>/dev/null; echo "$HEARTBEAT_CRONJOB") | crontab -
else
echo -e "${YELLOW}Heartbeat monitoring cronjob already exists. No changes made.${NC}"
fi
fi

# Disable only the hdmi audio so we can use the minijack for monitoring
# Disable HDMI audio to use the mini-jack for monitoring
echo -e "${BLUE}►► Disabling onboard audio...${NC}"
sed -i '/dtoverlay=vc4-fkms-v3d/ { /audio=off/! s/$/,audio=off/ }' "$CONFIG_FILE" > /dev/null
sed -i '/dtoverlay=vc4-kms-v3d/ { /noaudio/! s/$/,noaudio/ }' "$CONFIG_FILE" > /dev/null
Expand All @@ -151,14 +183,14 @@ case $device_number in
*) echo -e "${RED}Invalid input, exiting.${NC}"; exit 1 ;;
esac
if ! grep -q "dtoverlay=$overlay" "$CONFIG_FILE" > /dev/null; then
echo -e "dtoverlay=$overlay" >> "$CONFIG_FILE"
echo "dtoverlay=$overlay" >> "$CONFIG_FILE"
fi

# Apply HifiBerry kernel fix if needed
echo -e "${BLUE}►► Checking Linux version and disabling onboard EEPROM if necessary...${NC}"
kernel_version=$(uname -r | awk -F. '{print $1 "." $2}')
if [ "$(printf '%s\n' "5.4" "$kernel_version" | sort -V | head -n1)" = "5.4" ] && [ "$kernel_version" != "5.4" ]; then
grep -q 'force_eeprom_read=0' "$CONFIG_FILE" || echo -e "force_eeprom_read=0" >> "$CONFIG_FILE"
grep -q 'force_eeprom_read=0' "$CONFIG_FILE" || echo "force_eeprom_read=0" >> "$CONFIG_FILE"
fi

# Reboot
Expand Down

0 comments on commit 338c1be

Please sign in to comment.