#!/usr/bin/env bash
set -euo pipefail

# This script is used to bootstrap the mender update process
# The update server references it in the form:
# https://raw.githubusercontent.com/getumbrel/umbrel/<tag>/scripts/update-script

update_url=""
download_prefix="https://download.umbrel.com/release/1.7.4"

# This is a legacy mender install, migrate to rugix
if command -v mender &> /dev/null
then
	if cat /var/lib/mender/device_type | grep --quiet 'device_type=raspberrypi'
	then
		update_url="${download_prefix}/umbrelos-pi-legacy-migration.update"
	fi

	if cat /var/lib/mender/device_type | grep --silent 'device_type=amd64'
	then
		update_url="${download_prefix}/umbrelos-amd64-legacy-migration.update"
	fi

	# Fix /etc/mender/artifact_info not existing in some OS builds
	if [[ ! -f /etc/mender/artifact_info ]]
	then
		echo "artifact_name=umbrelOS" > /etc/mender/artifact_info
	fi

	if [[ "${update_url}" == "" ]]
	then
		echo umbrel-update: '{"error": "Unsupported device type"}'
		exit 1
	fi

	# Install mender update
	mender install "${update_url}"	

# This is a rugix install, update to the latest version
elif command -v rugix-ctrl &> /dev/null
then
	boot_flow=$(rugix-ctrl system info 2> /dev/null| jq -r ".boot.bootFlow")

	# This is a mender flashed device that has already been migrated to rugix
	# Provide it with an update artifact that supports the legacy mender partition layout and boot flow
	if [[ "${boot_flow}" == "mender-grub" ]]
	then
		update_url="${download_prefix}/umbrelos-amd64-legacy.update"
	fi

	# This a native rugix flashed amd64 device
	if [[ "${boot_flow}" == "grub" ]]
	then
		update_url="${download_prefix}/umbrelos-amd64.update"
	fi

	# This is a Raspberry Pi device
	if [[  "${boot_flow}" == "rpi-tryboot" ]]
	then
		update_url="${download_prefix}/umbrelos-pi.update"
	fi

	if [[ "${update_url}" == "" ]]
	then
		echo umbrel-update: '{"error": "Unsupported boot flow"}'
		exit 1
	fi

	# Rugix < 1.0 doesn't support the 1.0 update install flags.
	# umbrelOS 1.6.0 and 1.6.1 are on Rugix 0.8
	rugix_version=$(rugix-ctrl --version 2> /dev/null || true)
	if [[ "${rugix_version}" == rugix-ctrl\ v0.* ]]
	then
		# Rugix 0.x
		rugix-ctrl update install --reboot deferred "${update_url}"
	else
		# Rugix >=1.0
		rugix-ctrl update install --reboot set --insecure-skip-bundle-verification "${update_url}"
	fi
else
	echo umbrel-update: '{"error": "No supported update mechanism found"}'
	exit 1
fi
