#!/usr/bin/env bash

SCRIPT_DIR=$(dirname $(realpath "$0"))

# docker update
echo "使新的docker compose生效"
cd /data/services && docker compose up -d --remove-orphans
echo


# report version
echo '报告当前版本'
cd $SCRIPT_DIR
source ./lib.sh
source ./version.sh

screenNum=$(getScreenNum)
env=$(getEnv)

api='https://api-magicscreen.xcastle.net/mp/screenVersion/reportingVersion'
if [ "$env" == 'dev' ]; then
  api='https://test-api-magicscreen.xcastle.net/mp/screenVersion/reportingVersion'
fi

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{
    "screenNumber": "'"$screenNum"'",
    "currentVersion": {
      "flutter": "'"$flutterVersion"'",
      "algorithm": "'"$algorithmServiceVersion"'",
      "print": "'"$printServiceVersion"'"
    }
  }' \
  --max-time 5 \
  --retry 10 \
  --retry-delay 10 \
  --retry-all-errors \
  -w "\n[URL]: %{url_effective}\n[HTTP_CODE]: %{http_code}\n" \
  $api