#!/bin/sh -e
#Copyright (C) 2001-2013  The PARI group.
#
#This file is part of the GP2C package.
#
#PARI/GP is free software; you can redistribute it and/or modify it under the
#terms of the GNU General Public License as published by the Free Software
#Foundation. It is distributed in the hope that it will be useful, but WITHOUT
#ANY WARRANTY WHATSOEVER.
#
#Check the License for details. You should have received a copy of it, along
#with the package; see the file 'COPYING'. If not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

name="stdin";
for p in "$@"; do
case "$p" in
  -h|--help) cat <<EOF
gp2c-dbg <options> <file.gp>
Compile file.gp with gp2c and launch a debugger session of gp
with functions of file.gp added.
<options> are passed to gp2c

ENVIRONMENT:
GP2C: path to the GP2C compiler, default: /usr/bin/gp2c
GP_DBG: path to the GP calculator, default: /usr/bin/gp
GP2C_DBG: command to start the debugger, default: gdb --args
EOF
exit
;;
  -l|-t|-v)  exec $GP2C "$@" ;;
  -*) ;; #We discard options
  *) name=$p;;
esac
done

if test "x$GP2C" = "x"; then
  GP2C="/usr/bin/gp2c"
fi

if test "x$GP_DBG" = "x"; then
  GP_DBG="/usr/bin/gp"
fi

if test "x$GP2C_DBG" = "x"; then
  GP2C_DBG="gdb --args"
fi

case $name in
*.c) name=${name%.c};;
*) $GP2C "$@" >$name.c;;
esac

command=`printf "cc -c -o %s.o -g -O0 -Wall -fno-strict-aliasing -ffp-contract=off -fPIC -I\"/usr/include/aarch64-linux-gnu\" %s.c && cc -o %s.so -shared -g -O0 -Wall -fno-strict-aliasing -ffp-contract=off -fPIC -Wl,-shared -Wl,-z,relro %s.o -Wl,-rpath,/usr/lib/aarch64-linux-gnu -lc -lm -L/usr/lib/aarch64-linux-gnu -lpari" $name $name $name $name`
eval $command || exit $?
grep "^GP;" $name.c | sed 's/^GP;//' >$name.run
$GP2C_DBG $GP_DBG $name.run
