#!/bin/bash

# Selection of default global array handler can be set in this variable.
# Use "mpsglobal_bdb" for the Berkeley DB-based array handler,
#     "mpsglobal_native" for the native implementation.

SHELL=/bin/sh
INSTALL=install
VERSION=10

# Use the following if your installation was
# to system directories:
#
# Use the following if your installation was to
# your own directories.  Be sure ~/mumps_compiler
# is in you PATH variable.  Use /usr for system
# wide installs.
#

PREFIX=/usr

INCLUDEDIR=$PREFIX/include
BINDIR=$PREFIX/bin
LIBDIR=$PREFIX/lib
DOCDIR=$PREFIX/share/doc/mumpsc
MANDIR=$PREFIX/share/man
MAN1DIR=$MANDIR/man1
SRCDIR=mumpsc
ARGS=$*

FLAGS="-w -I $INCLUDEDIR  "

FILEBITS="-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE"

MUMPS_COMPILER=$BINDIR/mumps2c
GBLLIB=mpsglobal_native
MUMPSLIB=mumps
USAGE="usage: $0 [-h|-?|--help|-v|--ver|-n|--native|-b|-sT|-sU|-g|-p|--postgres|--berkeley|--server=udp|tcp|--debug] file ..."
POSTGRES=""
POSTGRESDEF=""
FILE_FOUND=n
CC="gcc"
CXX="g++"

echo

for var in $* 
      do
      if [ $var = "--help" -o $var = "-h" -o $var = "-?" -o $var = "-v" -o $var = "--ver" ]
            then
            mumps2c -v
            echo "Copyrights: (see individual modules for details)"
            echo "  Copyright (c) 2000, 2001, 2002, 2003 by Kevin C. O'Kane"
            echo "  Copyright (c) 2002 by Matthew J. Lockner"
            echo "  Copyright (c) 2002 by Michael D. Janssen"
            echo "License: GNU GPL and GNU LGPL (see individual modules)"
            echo
            echo "mumpsc [options] filename.mps"
            echo
            echo " Options: "
            echo "  -h|-?|-v|--help|--ver     This help display"
            echo "  -n|--native               Native global arrays (~2GB max)"
            echo "  -b|--berkeley             Berkeley global arrays (default)"
            echo "  -p|--postgres             PostgreSQL enabled"
            echo "  -sT|-sU|--server=udp|tcp  UDP or TCP server enabled"
            echo "  -g|--debug                Enable debug facility"
            echo
            echo " Output: filename.c and filename"
            echo
            exit
            fi
      done

TEMP=`getopt -o pnbs::d:g -l postgres,native,berkeley,server::,debug,default_ip: -n 'mumpsc' -- "$@"`

if [ $? != 0 ] ; then
	echo $USAGE;
	echo "Terminating..." >&2 ;
	exit 16 ;
fi

objs=''

for arg in $ARGS
do
    base=`basename $arg`
    ext=${base#*.}

    if [ $ext = 'o' ]
    then
        objs="$objs $arg"
    fi
done

Sobjs=''

for arg in $ARGS
do
    base=`basename $arg`
    ext=${base#*.}

    if [ $ext = 'so' ]
    then
	  sofile_base=`basename $arg .so`
        Sobjs="$Sobjs -l$sofile_base"
    fi
done

# Quotes around TEMP are IMPORTANT
eval set -- "$TEMP"

while true ; do
	case "$1" in
		-n|--native)
			echo "Compiling with native db"
			GBLLIB=mpsglobal_native
			shift
			;;
		-p|--postgres)
			echo "Compiling with the POSTGRES library"
			POSTGRESDEF=-DPOSTGRES
#			MUMPSLIB=$LIBDIR/mumpsp
			MUMPSLIB=mumpsp
			POSTGRES=-lpq
			shift
			;;
		--) shift ; break ;;
		*) echo "Internal error!" ; exit 1 ;;
	esac
done

if [ $DEBUG ]
then
	echo "GBLLIB = $GBLLIB"
fi

common_libs="-lm -L$LIBDIR -lmpscpp -l$MUMPSLIB -l$GBLLIB $POSTGRES -lpcre"

for arg in $ARGS
do
    base=`basename $arg`
    ext=${base#*.}

    if [ "$ext" = 'mps' -o "$ext" = 'c' -o "$ext" = 'cpp' ]
    then

	FILE_FOUND=y
	infile_dir=`dirname $arg`
	infile_base=`basename $arg .mps`
	infile="$infile_dir/$infile_base"
	infileB="$infile"

      if [ $ext = 'mps' ]
      then
            echo "Compiling from Mumps source ..."
            $MUMPS_COMPILER $infile.mps
		rm $infile.m

            if [ $? != 0 ]
            then
                  echo "terminating due to compiler detected source code error"
                  exit 16
                  fi

	      infile_base=`basename $arg .mps`
            infile="$infile.cpp"
            fi

      if [ $ext = 'c' ]
      then
            echo "Compiling from C module only."
	      infile_base=`basename $arg .c`
	      infile="$infile_dir/$infile_base.c"
            echo $infile
            fi

      Cflg=1

      if [ $ext = 'cpp' ]
      then
#           common_libs="$common_libs -lmpsdummy"
            Cflg=0
	      infile_base=`basename $arg .cpp`
	      infile="$infile_dir/$infile_base.cpp"
            echo "Compiling " $infile 
#            common_libs="-L$LIBDIR -lm -lmpscpp -l$MUMPSLIB -l$GBLLIB $POSTGRES -lpcre"
            CC="g++"
            FLAGS="$FLAGS -D_MDH_"
            fi

      echo "using $GBLLIB file system"

	if [ $? = 0 ]
	then
		if [ $Cflg = 1 ]
			then
			echo Compiling generated C++ code...
			fi
		$CXX -O3 $DEBUG $POSTGRESDEF $FLAGS -L$LIBDIR $FILEBITS -o $infile_base $infile \
				$objs -lmumps $common_libs
		echo
	else
		echo C compilation surpressed.
		echo 
		# Uncomment the following if you wish to halt a batch
		# compilation if a single file fails to compile
		#
		# exit
	fi
	fi
done

if [ $FILE_FOUND = n ]
then
	echo $USAGE
fi

exit 0
