#!/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 20.07.2007
#===============================================================================

#===============================================================================
# Les paramtres du script sont identiques  ceux de gfortran. Le script
# cre un excutable  partir des objets
#
# Exemples :
#   rpllink -o essai essai.o
#===============================================================================

echo +++Linker for RPL/2 version 4.1.4
echo $0 $@

PRESENCE_FICHIER_SORTIE=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
done

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

ARGUMENTS_G77=""

for i in $@;
do
	ARGUMENTS_G77="$ARGUMENTS_G77 $i"
done

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

gfortran $ARGUMENTS_G77 -DRPLCONFIG $OPTIONS $CFLAGS\
		$(which $0 | cut -d '/' -f -$(($P-1)))/lib/librpl.a \
		-lz  

exit 0
