#!/bin/sh
set -eu

IMAGE='registry.shore.co.il/workbench'
CONTAINER='workbench'

is_latest() {
    podman image exists "$IMAGE" || return 1
    toolbox run --container "$CONTAINER" true 2>/dev/null || return 1
    current="$(podman container inspect "$CONTAINER" --format '{{ .Image }}')"
    podman image pull "$IMAGE" > /dev/null
    new="$(podman image inspect "$IMAGE" --format '{{.Digest}}')"
    [ "$new" = "$current" ] || return 1
}

update() {
    toolbox rm --force "$CONTAINER" 2>/dev/null || true
    yes | podman image prune --filter 'label=com.github.containers.toolbox=true'
    killall tmux || true
    toolbox create --image "$IMAGE"
}

if [ "$(hostname)" = 'toolbox' ]
then
    /usr/libexec/flatpak-xdg-utils/flatpak-spawn --host "$0" "$@"
elif [ "${1:-}" = "-d" ] || [ "${1:-}" = "--dry-run" ] || [ -n "${DRY_RUN:-}" ]
then
    podman image pull "$IMAGE" > /dev/null
elif ! (is_latest)
then
    update
fi
