#!/bin/sh
# Push content subrepo before pushing main repo
remote="$1"
#export GIT_TRACE=1
#export GIT_TRACE_PACKET=1
#export GIT_TRACE_PERFORMANCE=1
#export GIT_SSH_COMMAND="ssh -vvv"

set -euo pipefail
set -x

node src/app.js >/dev/null 2>&1 &
  APP_PID=$!

sleep 2

yarn test:prepush
TEST_RESULT=$?

  # Clean up the app process
  if kill -0 $APP_PID 2>/dev/null; then
    echo "Stopping app (PID: $APP_PID)..."
    kill $APP_PID
    # Give it time to shut down gracefully
    sleep 1
    # Force kill if still running
    if kill -0 $APP_PID 2>/dev/null; then
      kill -9 $APP_PID 2>/dev/null || true
    fi
  fi
  
  # Wait for process to fully terminate
  wait $APP_PID 2>/dev/null || true
  

if [ $TEST_RESULT -ne 0 ]; then
  echo "Tests failed. Push aborted."
  exit 1
fi

cd content
git push "$remote" main
cd ..
set +x
