#!/bin/bash

#  add a line to quickjs.c, at the end,
#  to tell edbrowse where the pending jobs are in struct JSRuntime.
#  This is not necessary as of 3.8.4, but I'm keeping it around for a while,
#  in case we need to resurrect this strategy.

#  The optional argument is the directory of the quickjs tree,
#  otherwise I use QUICKJS_DIR if defined, an absolute path please,
#  otherwise I assume quickjs is adjacent to your edbrowse working tree.

if [ $# = 1 ]
then
p2="$1"
elif [ -n "$QUICKJS_DIR" ]
then
p2="$QUICKJS_DIR"
else
p1=`pwd`
p2=${p1/%edbrowse/quickjs}
if [ "$p2" = "$p1" ]; then
p2=${p1/%edbrowse\/*/quickjs}
fi
if [ "$p2" = "$p1" ]; then
echo "current directory $p1 is not in the edbrowse tree"
exit 1
fi
fi
if [ ! -d "$p2" ] ; then
echo "no such directory $p2"
exit 1
fi

p3="$p2/quickjs.c"
if [ ! -f "$p3" ] ; then
echo "no such file $p3"
exit 1
fi

if fgrep -q edbrowse "$p3"
then
echo "quickjs.c has already been fixed up for edbrowse"
exit 0
fi

echo "modifying quickjs.c"
echo -e '\n// Tell edbrowse where the jobs field is within struct JSRuntime\nint JSRuntimeJobIndex = (int) offsetof(struct JSRuntime, job_list);' >>"$p3"
echo ok
exit 0
