#!/bin/bash

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )/"
PROJECT_ROOT="$(realpath $SCRIPT_DIR'../../')/"
SOURCE_PATH="${PROJECT_ROOT}application/source/"
TEST_PATH="$(realpath $SCRIPT_DIR'../test/')/"

TMPFILE=monster.temp
touch $TMPFILE
trap "rm -f $TMPFILE" 0 2 3 15

${PROJECT_ROOT}development/node_modules/.bin/esbuild --platform=browser --bundle ${SOURCE_PATH}monster.mjs --outfile=${TMPFILE} 
url="$(${PROJECT_ROOT}development/node_modules/.bin/create-polyfill-service-url analyse --file "$TMPFILE")"

if [ ! -x {$url} ] 
then
  sed -i -E "/id=\"polyfill\"/s|.*|   <script id=\"polyfill\" src=\"${url}\"|g"  ${TEST_PATH}web/test.html  
  sed -i -E "/id=\"polyfill\"/s|.*|<script id=\"polyfill\" src=\"${url}\"|g"  ${PROJECT_ROOT}README.md  
fi
