#!/bin/bash
LOG="${GLAB_SHIM_LOG:-/tmp/issue-lead-33299-r1/repro/glab-calls.log}"
if [[ " $* " == *" graphql "* ]]; then body=$(cat); else body=""; fi
op=$(printf '%s' "$body" | head -c 4000 | tr '\n' ' ' | grep -o 'project(fullPath[^)]*)\|projects(fullPaths\|metadata { version }\|currentUser\|mergeRequests(iids' | head -1)
vars=$(printf '%s' "$body" | grep -o '"fullPath":"[^"]*"' | head -1)
echo "$(date +%s.%N) $$ start $* | $op $vars" | cut -c1-500 >> "$LOG"
err=$(mktemp)
if [ -n "$body" ]; then printf '%s' "$body" | PATH="${GLAB_SHIM_REAL_PATH}" glab "$@" 2>"$err"; rc=$?; else PATH="${GLAB_SHIM_REAL_PATH}" glab "$@" 2>"$err"; rc=$?; fi
cat "$err" >&2
echo "$(date +%s.%N) $$ end $rc $(head -c 200 "$err" | tr '\n' ' ')" >> "$LOG"
rm -f "$err"
exit $rc
