#!/usr/bin/env bash # update-quests.sh — fetch EFT quests and write atomically to quests.json set -euo pipefail umask 022 # ---- Environment safe for cron ---- export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" OUT="/var/www/EFT_COMPANION/public_html/quests.json" OUT_DIR="$(dirname "$OUT")" ENDPOINT="https://api.tarkov.dev/graphql" # ---- Helpers ---- die() { echo "[update-quests] ERROR: $*" >&2; exit 1; } # Confirm required tools exist (cron PATH can be minimal) for bin in curl mktemp mv chmod head date; do command -v "$bin" >/dev/null 2>&1 || die "Required binary '$bin' not found in PATH" done # Ensure output directory exists mkdir -p "$OUT_DIR" # GraphQL query — single line to avoid escaping double quotes # (Removed fields that caused schema errors: Skill.normalizedName, craftUnlock.name) QUERY='query AllTasks { tasks { id name trader { id name imageLink } wikiLink objectives { id type description maps { normalizedName } ... on TaskObjectiveItem { count foundInRaid item { id name shortName iconLink } items { id name shortName iconLink } } ... on TaskObjectiveShoot { targetNames count } } finishRewards { items { count item { id name shortName iconLink } } traderStanding { standing trader { id name } } offerUnlock { item { id name } trader { id name } } skillLevelReward { level skill { id name } } traderUnlock { id name } craftUnlock { id } achievement { id name } customization { id name } } } }' # Minimal JSON payload (no double quotes inside QUERY) PAYLOAD=$(printf '{"query":"%s"}' "$QUERY") # Create temp file in the same directory, then atomically mv into place TMP="$(mktemp -p "$OUT_DIR" 'quests.json.tmp.XXXXXX')" || die "mktemp failed" # Clean up the temp file on any exit path (cleared later after mv) cleanup() { [ -n "${TMP:-}" ] && [ -f "$TMP" ] && rm -f "$TMP" || true; } trap cleanup EXIT echo "[update-quests] Posting to $ENDPOINT ..." HTTP_STATUS="$( curl -sS -o "$TMP" -w '%{http_code}' \ -X POST "$ENDPOINT" \ -H 'Content-Type: application/json' \ --data-binary "$PAYLOAD" )" if [ "$HTTP_STATUS" != "200" ]; then echo "[update-quests] ERROR: HTTP $HTTP_STATUS" echo "[update-quests] Response body (first 2000 bytes):" head -c 2000 "$TMP" || true; echo exit 1 fi # Ensure readable by web server chmod 0644 "$TMP" # Atomic replace mv -f "$TMP" "$OUT" # Prevent trap from trying to remove a moved file TMP="" trap - EXIT echo "[update-quests] Wrote $OUT at $(date -Is)"