#!/bin/sh
# Generates the Makefile for PARI
# Bruno Haible 10.12.1992
# Modified by Babecool 23.7.1993

# Usage:
#   makemakefile
#   makemakefile sun3
#   makemakefile sparcv7
#   makemakefile sparcv8
#   makemakefile hppa
#   makemakefile i386
#   makemakefile dos

# Environment variables:
#   CC         C compiler
#   CFLAGS     flags for C compiler
#   RANLIB     ranlib command

rm -f Makefile

TARGET="$1"
if test -z "$TARGET"; then
  TARGET=port
fi

if [ $TARGET = dos ] ; then

EXE='.exe'
CC='gcc'
CFLAGS='-O -DLONG_IS_32BIT -DULONG_NOT_DEFINED'
RANLIB='ranlib'
INSTALL='copy'

else

EXE=''

# Check for GCC
if test -z "$CC"; then
  # echo checking for gcc
  saveifs="$IFS"; IFS="${IFS}:"
  for dir in $PATH; do
    test -z "$dir" && dir=.
    if test -f $dir/gcc; then
      CC="gcc"
      break
    fi
  done
  IFS="$saveifs"
fi
test -z "$CC" && CC="cc"
if test -z "$CFLAGS"; then
  if [ $CC = gcc ] ; then
    CFLAGS="-O2 -g -ansi -DLONG_IS_32BIT -DULONG_NOT_DEFINED" # may use up to -O4 except for plotsun.c
  else
    CFLAGS="-O -DLONG_IS_32BIT -DULONG_NOT_DEFINED"
  fi
fi
if [ $TARGET = hppa ] ; then
CC=cc
CFLAGS='-O -Aa -DHPPA -DLONG_IS_32BIT -DULONG_NOT_DEFINED'
fi

# Check for ranlib
if test -z "$RANLIB"; then
  # echo checking for ranlib
  saveifs="$IFS"; IFS="${IFS}:"
  for dir in $PATH; do
    test -z "$dir" && dir=.
    if test -f $dir/ranlib; then
      RANLIB="ranlib"
      break
    fi
  done
  IFS="$saveifs"
fi
test -z "$RANLIB" && RANLIB="\#"

# Check for install
# Make sure to not get the incompatible SysV /etc/install 
# /usr/sbin/install, which might be in PATH before a BSD-like install,
# or the SunOS /usr/etc/install directory, or the AIX /bin/install.
if test -z "$INSTALL"; then
  # echo checking for install
  saveifs="$IFS"; IFS="${IFS}:"
  for dir in $PATH; do
    test -z "$dir" && dir=.
    case $dir in
    /etc|/usr/sbin|/usr/etc|/usr/ucb) ;;
    *)
      if test -f $dir/install; then
        if grep dspmsg $dir/install >/dev/null 2>&1; then
          : AIX
        else
          INSTALL="$dir/install -c"
          INSTALL_PROGRAM='$(INSTALL)'
          INSTALL_DATA='$(INSTALL) -m 644'
          break
        fi
      fi
      ;;
    esac
  done
  IFS="$saveifs"
fi

fi

INSTALL=${INSTALL-cp}
INSTALL_PROGRAM=${INSTALL_PROGRAM-'$(INSTALL)'}
INSTALL_DATA=${INSTALL_DATA-'$(INSTALL)'}

case $TARGET in
  sun3) VERSIONFILE="version68k.c";;
  sparcv7) VERSIONFILE="versionsparcv7.c"; ASM_OBJ="sparc.o";;
  sparcv8) VERSIONFILE="versionsparcv8.c"; ASM_OBJ="sparc.o";;
  hppa) VERSIONFILE="versionhp.c"; ASM_OBJ="hppa.o";;
  i386 | dos) VERSIONFILE="version386.c"; ASM_OBJ="i386.o";;
  *) VERSIONFILE="versionport.c";;
esac
case $TARGET in
  sun3) INCLUDEFILE="gen68k.h"; GENPARIFILE="genpari68k.h";;
  *) INCLUDEFILE="genport.h"; GENPARIFILE="genpariother.h";;
esac
if [ "$CC" = gcc ] ; then
  PLOTCFLAGS=''
else
  PLOTCFLAGS=''
fi
if [ "$CC" = gcc -a $TARGET = sun4 ] ; then
  DEBUGOPTION='-gstabs'
else
  DEBUGOPTION='-g'
fi
if [ $TARGET = hppa ] ; then
  XLIBOPTION='-L/usr/lib/X11R4'
fi

# Begin outputting the Makefile.
case $TARGET in
  sun3) echo "# Makefile for gp on sun3 and MC680x0 based machines (x>=2)">>Makefile;;
  sparcv7) echo "# Makefile for gp on sparcv7">>Makefile;;
  sparcv8) echo "# Makefile for gp on sparcv8">>Makefile;;
  hppa) echo "# Makefile for gp on hppa-HPUX">>Makefile;;
  i386) echo "# Makefile for gp on i386">>Makefile;;
  dos) echo "# Makefile for gp on DOS with emx/gcc">>Makefile;;
  *) echo "# General Makefile for C version of gp">>Makefile;;
esac
cat >> Makefile <<!!

# Change these installation directories to suit your needs

PREFIX = /usr/local
LIBDIR = \$(PREFIX)/lib
BINDIR = \$(PREFIX)/bin
MANDIR = \$(PREFIX)/man
INCLUDEDIR = \$(PREFIX)/include/pari

CC = ${CC}
CFLAGS = ${CFLAGS}
LIBS = -lm
LFLAGS =

# You can (if you want to debug gp yourself) add the ${DEBUGOPTION} option
# in CFLAGS. However, remember that, while gcc accepts ${DEBUGOPTION} and -O
# simultaneously, cc usually does not.

!!
if [ $TARGET = dos ] ; then
cat >> Makefile  <<!!
LD = ld
RM = del
LN = copy
!!
else
cat >> Makefile  <<!!
RM = rm -f
LN = ln -s
!!
fi
cat >> Makefile  <<!!
RANLIB = ${RANLIB}
INSTALL = ${INSTALL}
INSTALL_PROGRAM = ${INSTALL_PROGRAM}
INSTALL_DATA = ${INSTALL_DATA}

# Choose the plot file:

# no plotting capabilities:
#PLOTFILE = plotnull.c
#PLOTCFLAGS =
#PLOTLIBS =

!!
if [ $TARGET = sun3 -o $TARGET = sparcv7 -o $TARGET = sparcv8 ] ; then
cat >> Makefile  <<!!
# plotting in Suntools windows:
#PLOTFILE = plotsun.c
#PLOTCFLAGS = ${PLOTCFLAGS}
#PLOTLIBS = -lsuntool -lsunwindow -lpixrect

!!
fi
cat >> Makefile  <<!!
# plotting in X windows:
PLOTFILE = plotX.c
PLOTCFLAGS = ${PLOTCFLAGS}
PLOTLIBS = ${XLIBOPTION} -lX11

# Choose whether you want the readline input facilities:
GPMAIN = gp.o
RLLIBS =
RLFLAGS =

#GPMAIN = gpreadline.o
#RLLIBS = readline/libreadline.a
#RLFLAGS = -Lreadline -lreadline -ltermcap

OBJS = ${ASM_OBJ} mp.o anal.o alglin1.o alglin2.o base1.o base2.o arith1.o arith2.o bibli1.o bibli2.o elliptic.o es.o gen1.o gen2.o gen3.o init.o errmessages.o helpmessages.o polarit1.o polarit2.o trans1.o trans2.o trans3.o version.o

all:	genpari.h gp${EXE}

gp:	genpari.h \$(GPMAIN) \$(OBJS) plot.o buch.o \$(RLLIBS)
!!
if [ $TARGET = dos ] ; then
cat >> Makefile  <<!!
	-\$(RM) gp1.o
	-\$(RM) gp2.o
	-\$(RM) gp3.o
	-\$(RM) gp4.o
	\$(LD) -o gp1.o -r mp.o i386.o anal.o alglin1.o alglin2.o base1.o base2.o
	\$(LD) -o gp2.o -r arith1.o arith2.o bibli1.o bibli2.o elliptic.o es.o
	\$(LD) -o gp3.o -r gen1.o gen2.o gen3.o init.o errmessages.o helpmessages.o
	\$(LD) -o gp4.o -r polarit1.o polarit2.o trans1.o trans2.o trans3.o version.o
	\$(CC) \$(CFLAGS) -o gp \$(GPMAIN) gp1.o gp2.o gp3.o gp4.o plot.o buch.o \$(LIBS) \$(PLOTLIBS) \$(RLFLAGS)
	-\$(RM) gp1.o
	-\$(RM) gp2.o
	-\$(RM) gp3.o
	-\$(RM) gp4.o

gp.exe:	gp
	emxbind -q c:\emx\bin\emx.exe gp gp.exe

!!
else
cat >> Makefile  <<!!
	\$(CC) \$(CFLAGS) \$(LFLAGS) -o gp \$(GPMAIN) \$(OBJS) plot.o buch.o \$(LIBS) \$(PLOTLIBS) \$(RLFLAGS)

!!
fi
cat >> Makefile  <<!!
genpari.h:	${GENPARIFILE}
	-\$(RM) genpari.h
	\$(LN) ${GENPARIFILE} genpari.h

!!
if [ $TARGET = sun3 ] ; then
cat >> Makefile  <<!!
mp.o:	mp.s
	\$(CC) \$(CFLAGS) -c mp.s

!!
else
cat >> Makefile  <<!!
mp.o:	mp.c
	\$(CC) \$(CFLAGS) -c mp.c

!!
fi
if [ $TARGET = sparcv7 ] ; then
cat >> Makefile  <<!!
sparc.o:	sparcv7.s
	as -P -o sparc.o sparcv7.s

!!
fi
if [ $TARGET = sparcv8 ] ; then
cat >> Makefile  <<!!
sparc.o:	sparcv8.s
	as -P -o sparc.o sparcv8.s

!!
fi
if [ $TARGET = hppa ] ; then
cat >> Makefile  <<!!
hppa.o:	hppa.s
	cc -c -o hppa.o hppa.s

!!
fi
if [ $TARGET = i386 -o $TARGET = dos ] ; then
cat >> Makefile  <<!!
i386.o:	i386.s
	\$(CC) \$(CFLAGS) -c i386.s

i386.s:	i386.c
	\$(CC) -E i386.c > i386.s

!!
fi
for f in gp alglin1 alglin2 anal arith1 arith2 base1 base2 buch bibli1 bibli2 elliptic es gen1 gen2 gen3 init errmessages helpmessages dummy polarit1 polarit2 trans1 trans2 trans3
do
cat >> Makefile  <<!!
$f.o: $f.c
	\$(CC) \$(CFLAGS) -c $f.c

!!
done
for f in gpreadline
do
cat >> Makefile  <<!!
$f.o: $f.c
	\$(CC) \$(CFLAGS) -I. -c $f.c

!!
done
cat >> Makefile  <<!!
plot.c:	\$(PLOTFILE)
	-\$(RM) plot.c
	\$(LN) \$(PLOTFILE) plot.c

plot.o:	plot.c
	\$(CC) \$(CFLAGS) \$(PLOTCFLAGS) -c plot.c

version.o:	${VERSIONFILE}
	\$(CC) \$(CFLAGS) -c -o version.o ${VERSIONFILE}

readline/libreadline.a:
	(cd readline ; make CC='\$(CC)' CFLAGS='\$(CFLAGS)' RANLIB='\$(RANLIB)')

install: install-lib install-bin install-man

install-lib: \$(OBJS) dummy.o
	-mkdir \$(INCLUDEDIR)
	\$(INSTALL_DATA) genpari.h \$(INCLUDEDIR)
	\$(INSTALL_DATA) gencom.h  \$(INCLUDEDIR)
	\$(INSTALL_DATA) genport.h \$(INCLUDEDIR)
	\$(INSTALL_DATA) gen68k.h \$(INCLUDEDIR)
	\$(INSTALL_DATA) erreurs.h \$(INCLUDEDIR)
	\$(INSTALL_DATA) mpansi.h \$(INCLUDEDIR)
	\$(INSTALL_DATA) mpin.h \$(INCLUDEDIR)
	-\$(RM) libpari.a
	ar r libpari.a \$(OBJS) dummy.o
	\$(INSTALL_DATA) libpari.a \$(LIBDIR)
	\$(RANLIB) \$(LIBDIR)/libpari.a

install-bin: gp
	\$(INSTALL_PROGRAM) gp \$(BINDIR)
	strip \$(BINDIR)/gp

install-man: gp.1
	\$(INSTALL_DATA) gp.1 \$(MANDIR)/man1

clean:	
	-\$(RM) *.o libpari.a gp genpari.h plot.c i386.s *.log *.dvi *.std *.idx *.toc

!!

