#!/usr/bin/env bash # Update BirdNET-Pi trap 'exit 1' SIGINT SIGHUP USER=$(awk -F: '/1000/ {print $1}' /etc/passwd) HOME=$(awk -F: '/1000/ {print $6}' /etc/passwd) my_dir=$HOME/BirdNET-Pi/scripts # Sets proper permissions and ownership find $HOME/Bird* -type f ! -perm -g+wr -exec chmod g+wr {} + 2>/dev/null find $HOME/Bird* -not -user $USER -execdir sudo -E chown $USER:$USER {} \+ chmod 666 ~/BirdNET-Pi/scripts/*.txt chmod 666 ~/BirdNET-Pi/*.txt find $HOME/BirdNET-Pi -path "$HOME/BirdNET-Pi/birdnet" -prune -o -type f ! -perm /o=w -exec chmod a+w {} \; # remove world-writable perms chmod -R o-w ~/BirdNET-Pi/templates/* APT_UPDATED=0 PIP_UPDATED=0 # helpers sudo_with_user () { set -x sudo -u $USER "$@" set +x } ensure_apt_updated () { [[ $APT_UPDATED != "UPDATED" ]] && apt-get update && APT_UPDATED="UPDATED" } ensure_pip_updated () { [[ $PIP_UPDATED != "UPDATED" ]] && sudo_with_user $HOME/BirdNET-Pi/birdnet/bin/pip3 install -U pip && PIP_UPDATED="UPDATED" } remove_unit_file() { # remove_unit_file pushed_notifications.service $HOME/BirdNET-Pi/templates/pushed_notifications.service if systemctl list-unit-files "${1}" &>/dev/null;then systemctl disable --now "${1}" rm -f "/usr/lib/systemd/system/${1}" rm "$HOME/BirdNET-Pi/templates/${1}" if [ $# == 2 ]; then rm -f "${2}" fi fi } ensure_python_package() { # ensure_python_package pytest pytest==7.1.2 pytest_installation_status=$(~/BirdNET-Pi/birdnet/bin/python3 -c 'import pkgutil; import sys; print("installed" if pkgutil.find_loader(sys.argv[1]) else "not installed")' "$1") if [[ "$pytest_installation_status" = "not installed" ]];then ensure_pip_updated sudo_with_user $HOME/BirdNET-Pi/birdnet/bin/pip3 install "$2" fi } # update snippets below if ! which inotifywait &>/dev/null;then ensure_apt_updated apt-get -y install inotify-tools fi if ! which gcc &>/dev/null;then ensure_apt_updated apt-get -y install gcc python3-dev fi ensure_python_package inotify inotify if ! grep 'daemon' $HOME/BirdNET-Pi/templates/chart_viewer.service &>/dev/null;then sed -i "s|daily_plot.py.*|daily_plot.py --daemon --sleep 2|" ~/BirdNET-Pi/templates/chart_viewer.service systemctl daemon-reload && restart_services.sh fi if grep -q 'birdnet_server.service' "$HOME/BirdNET-Pi/templates/birdnet_analysis.service"&>/dev/null; then sed -i '/After=.*/d' "$HOME/BirdNET-Pi/templates/birdnet_analysis.service" sed -i '/Requires=.*/d' "$HOME/BirdNET-Pi/templates/birdnet_analysis.service" sed -i '/RuntimeMaxSec=.*/d' "$HOME/BirdNET-Pi/templates/birdnet_analysis.service" sed -i "s|ExecStart=.*|ExecStart=$HOME/BirdNET-Pi/birdnet/bin/python3 /usr/local/bin/birdnet_analysis.py|" "$HOME/BirdNET-Pi/templates/birdnet_analysis.service" systemctl daemon-reload && restart_services.sh fi # update snippets above systemctl daemon-reload restart_services.sh