#!/usr/bin/env bash set -e if [ ! "$BASH_VERSION" ] ; then echo "You must use bash to run this script. If running this script from curl, make sure the final word is 'bash'" 1>&2 exit 1 fi # Check if this is a Debian-based system if ! command -v apt-get &> /dev/null; then echo "Error: This script is designed for Debian-based systems (Debian, Ubuntu, Raspbian, etc.)" echo "The 'apt-get' package manager was not found on your system." echo "Please use a Debian-based distribution to install Companion." exit 1 fi echo "This will attempt to install Companion as a system service on this device." echo "It is designed to be run on headless servers, but can be used on desktop machines if you are happy to not have the tray icon." echo "A user called 'companion' will be created to run the service, and various scripts will be installed to manage the service" CURRENT_ARCH=$(dpkg --print-architecture) if [[ "$CURRENT_ARCH" != "x64" && "$CURRENT_ARCH" != "amd64" && "$CURRENT_ARCH" != "arm64" ]]; then echo "$CURRENT_ARCH is not a supported cpu architecture for running Companion." echo "If you are running on an arm device (such as a Raspberry Pi), make sure to use an arm64 image." exit 1 fi # Check if system is compatible (Debian 11+ or Ubuntu 20.04+ or equivalent) if [ -f /etc/os-release ]; then . /etc/os-release if [ "$ID" = "debian" ]; then if [ "${VERSION_ID%%.*}" -lt 11 ] 2>/dev/null; then echo "Error: This system is running Debian $VERSION_ID." echo "Companion requires Debian 11 (Bullseye) or later." echo "Please upgrade your system before installing Companion." exit 1 fi elif [ "$ID" = "ubuntu" ]; then VERSION_NUM=$(echo "$VERSION_ID" | tr -d '.') if [ "$VERSION_NUM" -lt 2004 ] 2>/dev/null; then echo "Error: This system is running Ubuntu $VERSION_ID." echo "Companion requires Ubuntu 20.04 LTS or later." echo "Please upgrade your system before installing Companion." exit 1 fi else # For other Debian derivatives, check glibc version without mentioning it GLIBC_VERSION=$(ldd --version 2>/dev/null | head -n1 | grep -oP '\d+\.\d+$' || echo "0.0") GLIBC_MAJOR=$(echo $GLIBC_VERSION | cut -d. -f1) GLIBC_MINOR=$(echo $GLIBC_VERSION | cut -d. -f2) if [ "$GLIBC_MAJOR" -lt 2 ] || ([ "$GLIBC_MAJOR" -eq 2 ] && [ "$GLIBC_MINOR" -lt 31 ]); then echo "Error: Your system version appears to be too old to run Companion." echo "Please ensure you are running:" echo " - Debian 11 (Bullseye) or later" echo " - Ubuntu 20.04 LTS or later" echo " - Or an equivalent recent version of your distribution" exit 1 fi fi fi if [ $(/usr/bin/id -u) -ne 0 ]; then echo "Must be run as root" exit 1 fi # Which build to install: # stable (default) - the latest stable release # beta - the latest beta build # - a specific stable version (e.g. 4.3.4). Advised against, as # attempting to install a build that doesn't exist can leave # your system in a broken state that needs fixing manually COMPANION_BUILD="${COMPANION_BUILD:-stable}" # Development only: Allow building using a testing branch of this updater COMPANIONPI_BRANCH="${COMPANIONPI_BRANCH:-main}" # install some dependencies # python3 is used by the update version picker (update-prompt/main.py) apt-get update apt-get install -yq git zip unzip curl libusb-1.0-0-dev libudev-dev adduser wget python3 apt-get clean # add a system user, if the user doesn't already exist id -u companion &>/dev/null || adduser --disabled-password companion --gecos "" # clone the companionpi repository rm -R /usr/local/src/companionpi &>/dev/null || true git clone https://github.com/bitfocus/companion-pi.git -b $COMPANIONPI_BRANCH /usr/local/src/companionpi cd /usr/local/src/companionpi # configure git for future updates git config --global pull.rebase false # run the update script if [ "$COMPANION_BUILD" == "beta" ] || [ "$COMPANION_BUILD" == "experimental" ]; then ./update.sh beta elif [ "$COMPANION_BUILD" == "stable" ]; then ./update.sh stable else ./update.sh stable "$COMPANION_BUILD" fi # enable start on boot systemctl enable companion # check that a build of companion was installed if [ ! -d "/opt/companion" ] then echo "No Companion build was installed!\nIt should be possible to recover from this with \"sudo companion-update\"" exit 9999 # die with error code 9999 fi echo "Companion is installed!" echo "You can start it with \"sudo systemctl start companion\" or \"sudo companion-update\""