citadel-core/scripts/citadel-os/external-storage/update-from-sdcard
2022-03-02 09:39:13 +01:00

70 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2020 Umbrel. https://getumbrel.com
# SPDX-FileCopyrightText: 2021-2022 Citadel and contributors
#
# SPDX-License-Identifier: GPL-3.0-or-later
set -euo pipefail
CITADEL_ROOT="$(readlink -f $(dirname "${BASH_SOURCE[0]}")/../../..)"
SD_MOUNT_POINT="/sd-root"
SD_CITADEL_ROOT="${SD_MOUNT_POINT}${CITADEL_ROOT}"
check_root () {
if [[ $UID != 0 ]]; then
echo "This script must be run as root"
exit 1
fi
}
check_dependencies () {
for cmd in "$@"; do
if ! command -v $cmd >/dev/null 2>&1; then
echo "This script requires \"${cmd}\" to be installed"
exit 1
fi
done
}
check_semver_range () {
local range="${1}"
local version="${2}"
"${CITADEL_ROOT}/scripts/citadel-os/semver" -r "${range}" "${version}" | grep --quiet "^${version}$"
}
main () {
check_root
check_dependencies jq
echo "Checking if SD card Citadel is newer than external storage..."
local external_version=$(cat "${CITADEL_ROOT}/info.json" | jq -r .version | cut -d "-" -f "1")
local sd_version=$(cat "${SD_CITADEL_ROOT}/info.json" | jq -r .version | cut -d "-" -f "1")
if [[ "${external_version}" == "0.5"* ]] || [[ "${external_version}" == "99.99.99" ]]; then
echo "External storage is on a pre version number reset version, updating to the latest version."
"${CITADEL_ROOT}/scripts/update/update" --path "${SD_CITADEL_ROOT}"
exit 0
fi
if ! check_semver_range ">${external_version}" "${sd_version}"; then
echo "No, SD version is not newer, exiting."
exit 0
fi
echo "Yes, SD version is newer."
# This will fail if we ever have multiple ranges with pre-release tags.
# e.g `1.2.3-beta <2.0.0` will become `1.2.3` due to striping the `-`
local update_requirement=$(cat "${SD_CITADEL_ROOT}/info.json" | jq -r .requires | cut -d "-" -f "1")
echo "Checking if the external storage version \"${external_version}\" satisfies update requirement \"${update_requirement}\"..."
if ! check_semver_range "${update_requirement}" "${external_version}"; then
echo "No, we can't do an automatic update, exiting."
exit 0
fi
echo "Yes, it does, attempting an automatic update..."
"${CITADEL_ROOT}/scripts/update/update" --path "${SD_CITADEL_ROOT}"
}
main