#!/bin/bash
ARGS="$@"

set -o pipefail
exec 5>&1
OUTPUT=$(bash -c "$RENPY_HOME/renpy.sh $RENPY_HOME/launcher $ARGS" | tee /dev/fd/5)
RESULT=$?

#Output multiline strings.
#https://trstringer.com/github-actions-multiline-strings/
# if [[ -n "$OUTPUT" ]]; then
#     echo "console<<EOF" >> "$GITHUB_OUTPUT"
#     echo "$OUTPUT" >> "$GITHUB_OUTPUT"
#     echo "EOF" >> "$GITHUB_OUTPUT"
# fi
if [[ -n "$OUTPUT" ]]; then
    OUTPUT="${OUTPUT//'%'/'%25'}"
    OUTPUT="${OUTPUT//$'\n'/'%0A'}"
    OUTPUT="${OUTPUT//$'\r'/'%0D'}"
    echo "::set-output name=console::$OUTPUT"
fi

echo "exitCode=$RESULT" >> "$GITHUB_OUTPUT"

if [[ "$CATCH_ERRORS" != "true" ]]; then
    exit $RESULT
fi
