#!/bin/bash

# 	------------------------------------------------------------------
#	parameter will be lazarus-fp name (pV308)
#	so we setup languageONE name
#	add in the '.'
#	remove the leading 'p'
# 	------------------------------------------------------------------
pProg=$1																# Parameter
lProg=$pProg               							# setup lProg
lProg="${lProg:0:3}.${lProg:3}"					# and a '.'
lProg=${lProg:1:99}											# Remove the 1st character

echo
echo pascal=$pProg
echo languageONE=$lProg
pwd

# 	------------------------------------------------------------------
#	Bring a copy of the languageONE backend into languageONE directory
#	Run it thru the reWriter
#	Assemble it
#	move the .ASM into the lazarus directory
#	move the .o into the lazarus directory
# 	------------------------------------------------------------------

cd ..																		# stepback into the languageONE directory
pwd																			# echo the directory name
cp @lazarus-fp/examples/$lProg.ONE .		# get the languageONE pProgram
if ! bin/languageONE $lProg; then				# If reWriter failed then
	echo failed reWriter process					# echo that
	echo																	# some blank line
	echo																	# some blank line
	exit 1																# and exit
else																		# else
	echo completed reWriter process				# echo completed
fi 																			# end if

if ! ./assemble $lProg; then						# If assembler failed then
	echo failed assemble process					# echo that
	echo																	# some blank line
	echo																	# some blank line
	exit 1																# and exit
else																		# else
	echo completed assembler process			# echo completed
fi																			# end if

#	-----------------------------------------------------------------
#	Cleanup and relocate languageONE Program
#	-----------------------------------------------------------------
cp $lProg.ASM @lazarus-fp/_Application	# move the source for debugging
mv $lProg.ASM @lazarus-fp/$lProg				# lazxarus-fp IDE wants the source here
cp $lProg.o @lazarus-fp/_Application		# copy object into app
mv $lProg.o @lazarus-fp/$lProg					# move the object into lazarus-fp/projectname
rm $lProg.ONE														# remove the languageONE pProgram

# 	------------------------------------------------------------------
#	NOTE;-	languageONE & FLTK build in their root folder but
#			lazarus-fp must go into its project folder
# 	------------------------------------------------------------------
#	Change to the lazarus-fp/projectname directory
#	Bring a copy of the lazarus-fp from end into lazarus-fp directory
#	Compile/Link
#	move the result to the _Application directory
#	remove any intermediate files
# 	------------------------------------------------------------------

echo
cd @lazarus-fp/$lProg										# enter the lazaris-fp/projectname directory

if ! lazbuild -B -q -q $pProg.lpr; then	# If build failed
	echo failed pascal process						# echo that
	echo																	# some blank line
	echo																	# some blank line
	exit 1																# and exit
else 																		# else
	echo completed pascal process					# echo completed
fi																			# end if

# mv $pProg ../_Application							# lazbuild does this move
cd ../..																# back to the languageONE root directory
echo
