#!/bin/sh

n=rundbg
S=`dirname $0`/../main
D=$HOME/.cache/easylang

mkdir -p $D

test ! -e $D/$n && compile=1
h="$S/kfunc.h $S/klib.h $S/klex.h $S/kparse.h $S/../native/kbgr.c $S/kwasm.h $0"
f="$S/kbas.c"
for i in $f $h; do
  test $i -nt $D/$n && compile=1
done

if test -d /System; then
  if test $compile; then
    rm -f $D/$n
    echo "Compiling easylang debug ..." >&2
    fl="-g3 -Wall -Wextra"
    fl="$fl -Wdouble-promotion -Wno-unused-parameter"
    fl="$fl -fsanitize=undefined"
    #fl="$fl -fsanitize=undefined,address"
    clang $fl -D__NOSDL__ -I$S -O $f -lm -o $D/$n
    echo "done" >&2
  fi

  if test -x $D/$n; then
    $D/$n $*
  fi
  exit 0
fi

if test $compile; then
  rm -f $D/$n
  cc=clang
  which $cc >/dev/null || cc=gcc
  echo "Compiling easylang debug (with $cc) ..." >&2

  $cc -Wall -g -D__NOSDL__ -I$S -O $f -lm -o $D/$n
  echo "done" >&2
fi

if test -x $D/$n; then
#	gdb -ex=run --batch --args $D/$n $*
#	valgrind --leak-check=full --show-leak-kinds=all $D/$n $*
	valgrind $D/$n $*
fi

