#!/bin/bash

#===============================================================================
# RPL/2 version 4.1.4
#   Interprteur du langage de programmation du calculateur HP-28S,
#
#   Date de cration    : 02 Avril 1.998
#
#   Tous droits rservs  l'auteur, Jol BERTRAND
#===============================================================================

#===============================================================================
# Copyright (C) 2001 BERTRAND Jol
#
# This file is part of RPL/2.
#
# RPL/2 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, or (at your option) any
# later version.
#
# RPL/2 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 Octave; see the file COPYING.  If not, write to the Free
# Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#===============================================================================

#===============================================================================
# Script de compilation des bibliothques partages du langage RPL/2
#
#    Tous droits rservs, J. BERTRAND 29.03.2001
#===============================================================================

#===============================================================================
# Les paramtres du script sont identiques  ceux de g77. Seul
# ajout, l'option -strip retirant la table des symboles de la bibliothque
# dynamique.
#
# Exemples :
#   mkrplso essai.c -o essai.rplso -strip
#   mkrplso essai.c -o essai.rplso -O6
#   mkrplso essai.c lib.a -o essai.rplso
#===============================================================================

echo +++RPL/2 4.1.4 shared library build tool

PRESENCE_FICHIER_SORTIE=FAUX
PRESENCE_OPTION_STRIP=FAUX
COMPILATION_SEULE=FAUX
DRAPEAU=FAUX
P=1

for i in $@;
do
	P=$(($P+1))

	if [ $DRAPEAU = "VRAI" ]; then
		DRAPEAU=FAUX
		FICHIER_SORTIE=$i
	fi

	if [ $i = "-o" ]; then
		if [ $PRESENCE_FICHIER_SORTIE = VRAI ]; then
			echo +++Error: more than one output file
			exit 1
		fi

		PRESENCE_FICHIER_SORTIE=VRAI

		if [ $P -gt $# ]; then
			echo +++Error: no output file
			exit 1
		else
			DRAPEAU=VRAI
		fi
	fi

	if [ $i = "-c" ]; then
		COMPILATION_SEULE=VRAI
	fi
done

if [ $PRESENCE_FICHIER_SORTIE = FAUX ]; then
	echo +++Error: no output file
	exit 1
fi

PROCESSEUR=$(uname -m)
test $PROCESSEUR = i386 && OPTIONS=-mieee-fp
test $PROCESSEUR = i486 && OPTIONS=-mieee-fp
test $PROCESSEUR = i586 && OPTIONS=-mieee-fp
test $PROCESSEUR = i686 && OPTIONS=-mieee-fp
test $PROCESSEUR = alpha && OPTIONS=-mieee
test $PROCESSEUR = sparc && OPTIONS=
test $PROCESSEUR = i86pc && OPTIONS=

ARGUMENTS_G77=""

for i in $@;
do
	if [ $i = "-strip" ]; then
		PRESENCE_OPTION_STRIP=VRAI
	else
		ARGUMENTS_G77="$ARGUMENTS_G77 $i"
	fi
done

P=0
for i in $(which $0 | tr '/' ' ');
do
	P=$(($P+1))
done

if [ $COMPILATION_SEULE = "VRAI" ]; then
	echo +++Compiler for RPL/2 version 4.1.4
	gfortran -x c $ARGUMENTS_G77 -fPIC -Wall -DRPLCONFIG $OPTIONS $CFLAGS\
			-I$(which $0 | cut -d '/' -f -$(($P-1)))/include -funsigned-char\
			-D_d_version_rpl=\"4.1.4\" -std=gnu99
else
	echo +++Linker for RPL/2 version 4.1.4
	gfortran $ARGUMENTS_G77 -shared -fPIC -Wall -DRPLCONFIG $OPTIONS $CFLAGS\
			-I$(which $0 | cut -d '/' -f -$(($P-1)))/include -funsigned-char
fi

test $PRESENCE_OPTION_STRIP = VRAI && strip -s $FICHIER_SORTIE
chmod 644 $FICHIER_SORTIE

exit 0
