#!/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-run <options> <file.gp>
Compile file.gp with gp2c and launch a gp session with functions of file.gp
added.
<options> are passed to gp2c

gp2c-run <file.c>
Compile file.c and launch a gp session with functions of file.c added.

ENVIRONMENT:
GP2C: path to the gp2c compiler, default: /usr/bin/gp2c
GP: path to the gp calculator, default: /usr/bin/gp
EOF
exit
;;
  -l|-t|-v)  exec $GP2C "$@" ;;
  -o) echo "gp2c-run does not support option -o" 1>&2; exit 1;;
  -*) ;; #We discard options
  *) name=$p;;
esac
done

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


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

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

command=`echo "x86_64-pc-linux-gnu-gcc -c -o %s.o -O3 -Wall -ffp-contract=off -fno-strict-aliasing -O2 -pipe -march=x86-64 -mtune=generic -Werror=odr -Werror=lto-type-mismatch -Werror=strict-aliasing -fPIC -I\"/usr/include\" %s.c && x86_64-pc-linux-gnu-gcc -o %s.so -shared -O3 -Wall -ffp-contract=off -fno-strict-aliasing -O2 -pipe -march=x86-64 -mtune=generic -Werror=odr -Werror=lto-type-mismatch -Werror=strict-aliasing -fPIC -Wl,-shared -Wl,-O1 -Wl,--as-needed -Wl,-z,pack-relative-relocs -Werror=odr -Werror=lto-type-mismatch -Werror=strict-aliasing %s.o -Wl,-rpath,/usr/lib64 -lc -lm -L/usr/lib64 -lpari" | sed -e "s %s $name g"`
eval $command || exit $?
grep "^GP;" $name.c | sed 's/^GP;//' >$name.run
$GP $name.run
