#!/bin/bash

set -euo pipefail

API_URL="http://localhost:2283/api/server/version-check"
# API Key med bare les på server.versionCheck 
# Account Settings -> API Keys -> + New API Key -> velg
API_KEY="696969696969696969696969"
GITHUB_URL="https://api.github.com/repos/immich-app/immich/releases/latest"

get_local_version() {
  curl -fsS -H "x-api-key: $API_KEY" "$API_URL" | jq -r '.releaseVersion'
}

get_latest_version() {
  curl -fsS "$GITHUB_URL" jq -r '.tag_name'
}

LOCAL=$(get_local_version)
LATEST=$(get_latest_version)

printf "Local : %s\n" "$LOCAL"
printf "Latest: %s\n" "$LATEST"

if [[ "$LOCAL" != "$LATEST" ]]; then
  echo "New version available!!"
  exit 1
else
  echo "Up to date"
  exit 0
fi
