#!/bin/sh -e
#Copyright (C) 2001, 2002  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

name="stdin";
for p in $*; do
case "$p" in
  -h) 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 $* ;;
  -*) ;; #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 "$@" >$name.c;;
esac

command=`printf "/usr/bin/gcc -c -o %s.o -O3 -DGCC_INLINE -Wall -Wno-implicit -malign-loops=2 -malign-jumps=2 -malign-functions=2 -fomit-frame-pointer  -I/usr/include %s.c && /usr/bin/ld -o %s.so -shared  -lc -lm %s.o " $name $name $name $name`
eval $command || exit $?
grep "^install" $name.c >gp2c_start.gp
if test -f $HOME/.gprc; then
  cp $HOME/.gprc gp2c_gprc
else
  : > gp2c_gprc 
fi
#Make sure gp2c_gprc end by a newline
echo >> gp2c_gprc
echo "read \"gp2c_start.gp\"" >> gp2c_gprc
GPRC=./gp2c_gprc $GP
