#!/bin/sh set -eu base_url=${CREDENZA_BASE_URL:-https://credenza.tv/releases} say() { printf '%s\n' "$*"; } die() { printf 'credenza: %s\n' "$*" >&2 exit 1 } usage() { cat <<'EOF' Install the Credenza media server. curl -fsSL https://credenza.tv/install.sh | sh curl -fsSL https://credenza.tv/install.sh | sh -s -- [options] --version install this version rather than the latest (or CREDENZA_VERSION=) --no-setup install only; run `sudo credenza-setup` yourself later --help this text Any other option is passed to credenza-setup, e.g. --database docker --yes. EOF } main() { version=${CREDENZA_VERSION:-} run_setup=true setup_args='' while [ $# -gt 0 ]; do case "$1" in --version) [ $# -ge 2 ] || die "--version needs a value" version=$2 shift 2 ;; --version=*) version=${1#*=}; shift ;; --no-setup) run_setup=false; shift ;; -h | --help) usage; exit 0 ;; *) setup_args="$setup_args '$(printf '%s' "$1" | sed "s/'/'\\\\''/g")'" shift ;; esac done if [ "$(id -u)" -eq 0 ]; then sudo='' elif command -v sudo >/dev/null 2>&1; then sudo='sudo' else die "run this as root, or install sudo." fi if command -v curl >/dev/null 2>&1; then fetch() { curl -fsSL --retry 3 -o "$2" "$1"; } fetch_text() { curl -fsSL --retry 3 "$1"; } elif command -v wget >/dev/null 2>&1; then fetch() { wget -q -O "$2" "$1"; } fetch_text() { wget -q -O - "$1"; } else die "neither curl nor wget is installed." fi [ -d /run/systemd/system ] || die "Credenza runs as a systemd service, and systemd is not running here." case "$(uname -m)" in x86_64 | amd64) deb_arch=amd64 rpm_arch=x86_64 tar_arch=x64 ;; aarch64 | arm64) deb_arch=arm64 rpm_arch=aarch64 tar_arch=arm64 ;; *) die "there is no Credenza build for $(uname -m); x86-64 and ARM64 are supported." ;; esac tar_prefix=/opt/credenza tar_installed=false [ -x "$tar_prefix/credenza" ] && tar_installed=true format=${CREDENZA_FORMAT:-} if [ -n "$format" ] && [ "$format" != tar ] && [ "$tar_installed" = true ]; then die "Credenza is installed from the tarball in $tar_prefix; a package on top of it would never run. Leave CREDENZA_FORMAT unset to upgrade the tarball, or remove it first with the tarball's uninstall.sh." fi if [ -z "$format" ]; then if [ "$tar_installed" = true ]; then format=tar elif command -v apt-get >/dev/null 2>&1 && command -v dpkg >/dev/null 2>&1; then format=deb elif command -v dnf >/dev/null 2>&1; then format=rpm else format=tar fi fi if [ -z "$version" ]; then latest=$(fetch_text "$base_url/latest.json") || die "could not reach $base_url/latest.json." version=$(printf '%s' "$latest" | sed -n 's/.*"version"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p') [ -n "$version" ] || die "$base_url/latest.json does not name a version." fi case "$format" in deb) file="credenza_${version}-1_${deb_arch}.deb" ;; rpm) file="credenza-${version}-1.${rpm_arch}.rpm" ;; tar) file="credenza-${version}-linux-${tar_arch}.tar.gz" ;; *) die "CREDENZA_FORMAT must be deb, rpm or tar." ;; esac installed='' if [ "$format" = deb ]; then installed=$(dpkg-query -W -f='${Status} ${Version}' credenza 2>/dev/null | sed -n 's/^install ok installed \(.*\)-[0-9]*$/\1/p' || true) elif [ "$format" = rpm ]; then installed=$(rpm -q --qf '%{VERSION}' credenza 2>/dev/null || true) case "$installed" in *"not installed"*) installed='' ;; esac elif [ "$tar_installed" = true ]; then installed=$(cat "$tar_prefix/host/release" 2>/dev/null || echo unknown) fi if [ -n "$installed" ] && [ "$installed" = "$version" ]; then say "Credenza $version is already installed." exit 0 fi if [ "$format" = rpm ] && ! command -v ffmpeg >/dev/null 2>&1 && ! dnf -q repoquery --whatprovides /usr/bin/ffmpeg 2>/dev/null | grep -q .; then el=$(rpm -E '%{?rhel}' 2>/dev/null || true) say "Credenza needs ffmpeg, which this system's repositories do not have. Enable EPEL and" say "RPM Fusion, then run this installer again:" say "" say " sudo dnf install epel-release" say " sudo dnf install https://mirrors.rpmfusion.org/free/el/rpmfusion-free-release-${el:-9}.noarch.rpm" say " sudo crb enable" exit 1 fi tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT chmod 0755 "$tmp" say "Downloading Credenza $version ($file)…" fetch "$base_url/$version/SHA256SUMS" "$tmp/SHA256SUMS" || die "Credenza $version was not found at $base_url/$version/." fetch "$base_url/$version/$file" "$tmp/$file" || die "could not download $file." chmod 0644 "$tmp/$file" expected=$(awk -v f="$file" '$2 == f { print $1 }' "$tmp/SHA256SUMS") [ -n "$expected" ] || die "$file is not listed in the release's SHA256SUMS." if command -v sha256sum >/dev/null 2>&1; then actual=$(sha256sum "$tmp/$file" | awk '{ print $1 }') else actual=$(shasum -a 256 "$tmp/$file" | awk '{ print $1 }') fi [ "$actual" = "$expected" ] || die "$file does not match its checksum; nothing was installed." say "Installing…" case "$format" in deb) $sudo apt-get update -qq /dev/null $sudo env DEBIAN_FRONTEND=noninteractive apt-get install -y -qq "$tmp/$file" /dev/null ;; rpm) $sudo dnf install -y -q "$tmp/$file" /dev/null ;; tar) tar -xzf "$tmp/$file" -C "$tmp" $sudo "$tmp/credenza-${version}-linux-${tar_arch}/install.sh" /dev/null ;; esac rm -rf "$tmp" trap - EXIT if [ "$format" = tar ]; then setup_command="$tar_prefix/host/credenza-setup" else setup_command=/usr/sbin/credenza-setup fi if [ -n "$installed" ]; then say "Credenza upgraded to $version." choice=$(sed -n 's/^DATABASE="\{0,1\}\([a-z]*\)"\{0,1\}$/\1/p' /etc/credenza/database.env 2>/dev/null || true) if [ -z "$choice" ]; then say "Next: sudo $setup_command" exit 0 fi [ "$run_setup" = true ] || exit 0 setup_args=" '--database' '$choice'$setup_args" else say "Credenza $version is installed." fi if [ "$format" = tar ] && ! command -v ffmpeg >/dev/null 2>&1; then say "" say "Note: ffmpeg is not installed, and nothing will play until it is. The tarball does not" say "install dependencies: install ffmpeg (and ICU, if this system lacks it) with your" say "package manager." fi if [ "$format" = rpm ] && rpm -q ffmpeg-free >/dev/null 2>&1; then say "" say "Note: Fedora's ffmpeg-free cannot transcode H.264. For full playback:" say " sudo dnf swap ffmpeg-free ffmpeg --allowerasing (needs RPM Fusion)" fi if [ "$run_setup" = false ]; then say "Next: sudo $setup_command" exit 0 fi if (exec /dev/null; then say "" eval "set -- $setup_args" exec $sudo "$setup_command" "$@"