#!/bin/bash

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#+
#+     Mumps Bioinformatics Software Library
#+     Copyright (C) 2015 - 2025 by Kevin C. O'Kane
#+
#+     Kevin C. O'Kane
#+     kc.okane@gmail.com
#+     https://threadsafebooks.com/
#+     https://www.cs.uni.edu/~okane
#+
#+ This program 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; either version 2 of the License, or
#+ (at your option) any later version.
#+
#+ This program is distributed in the hope that it will be useful,
#+ but WITHOUT ANY WARRANTY; without even the implied warranty of
#+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#+ GNU General Public License for more details.
#+
#+ You should have received a copy of the GNU General Public License
#+ along with this program; if not, write to the Free Software
#+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#+
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# June 21, 2025

SHELL=/bin/sh
INSTALL=install

PREFIX=/home/okane/Desktop/mumps/Mumps-Language-Processors/Mumps-Interpreter-Compiler-Library/local_install

INCLUDEDIR=$PREFIX/include
BINDIR=$PREFIX/bin
LIBDIR=$PREFIX/lib

ARGS=$*

FLAGS="-fno-var-tracking-assignments -w -pipe -I $INCLUDEDIR   -fno-strict-aliasing -g -DNDEBUG -L/usr/lib/x86_64-linux-gnu -lsqlite3 -ldl -lpthread -lm " 

FILEBITS="-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE"

GBLLIB=mpsglobal_native
MUMPSLIB=mumps

FILE_FOUND=n
CPP="g++"
Cflg=0

echo


objs=""

# compiling with object modules?

for arg in $ARGS
	do

	base=`basename $arg`
	ext=${base#*.}

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

	done

#------------------------------------------------------------------------------------------
#  will contain "-lgmp -Lx86-64-linux-gnu -lmpfr" if hardware math is turned off.
#------------------------------------------------------------------------------------------

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

for arg in $ARGS 
	do

	base=`basename $arg`
	ext=${base#*.}

	if [ "$ext" = 'mps' -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 program ..."
			Cflg=1

			#----------------------------
			# compile .mps program
			#----------------------------

			mumps2c $infile.mps

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

			#-------------------------------------
			# format cpp code
			# if astyle option is used, mumpsc
			# can only be invoked once at a time
			#-------------------------------------

			# astyle -q -A6 -T $infile_base.cpp

			#----------------------------
			# delete intermediate code
			#----------------------------

			rm -f $infile.m

			#-----------------------------------------------
			# the new infile will be the generated .cpp file
			#-----------------------------------------------

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

		if [ $ext = 'cpp' ]
			then
			Cflg=0
			infile_base=`basename $arg .cpp`
			infile="$infile_dir/$infile_base.cpp"
			echo "Compiling C++ source"
			echo "Compiling " $infile 
			FLAGS="$FLAGS -D_MDH_"
			fi

		if [ $Cflg = 1 ]
			then
			echo "Compiling generated C++ code ..."
			else
			echo "Compiling MDH C++ file ..."
			fi

		$CPP  -Ofast  \
			-fdiagnostics-color=never -fpermissive \
			$DEBUG $FLAGS -pthread \
			`pkg-config --cflags --libs glib-2.0` -L$LIBDIR $FILEBITS \
			-o $infile_base $infile \
			 $objs -lmumps $common_libs -fno-strict-aliasing -g -DNDEBUG -L/usr/lib/x86_64-linux-gnu -lsqlite3 -ldl -lpthread -lm  \
			`pkg-config --libs glib-2.0` -lpthread \
			`pkg-config --cflags --libs gtk+-3.0` -export-dynamic
		exit $?

		fi
	done

exit 0
