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

game_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
client_dir="$game_dir/.stardew-modsync"
update_control="$game_dir/thors-fjord-update.txt"
update_mode=""

if [[ -f "$update_control" ]]; then
  update_mode="$(tr '\r\n' '  ' < "$update_control" | tr '[:upper:]' '[:lower:]' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e 's/[[:space:]][[:space:]]*/ /g')"
fi

if [[ "$update_mode" == "disable remote update" ]]; then
  echo "Remote mod update disabled by $update_control; using the installed mod set."
else
  if ! command -v python3 >/dev/null 2>&1; then
    echo "Python 3 is required by the Stardew mod synchronizer." >&2
    exit 2
  fi

  python3 "$client_dir/modsync.py" --game-dir "$game_dir"
fi

if [[ "$(uname -s)" == "Darwin" ]]; then
  exec "$game_dir/StardewModdingAPI" "$@"
fi
exec "$game_dir/StardewModdingAPI" "$@"
