#!/bin/sh

#####
#     Changes
#     02 AUG 2017 - dropped options for old compulter, e.g.,
#       LINUX OSX OSF
#     22 OCT 2023 - added support for Intel M1/M2 
#        use evalresp 4.0.6 and no longer compile rdseed but use
#        precompiled versions in IRIS/rdseedv5.3
#     24 AUG 2024 - added support for clang/flang LLVM compilers
####

echo Checking for existence of bin and lib directories
for i in bin lib
do
if [ -f $i ]
then
	echo Found the ordinary file $i which does not belong
	echo Deleting this file to be able to create the $i directory
	rm -f $i
fi
if [ -d  $i ]
then
	echo Directory $i exists
else
	echo Creating Directory $i ; mkdir $i
fi
done

echo $1
case $1 in
	LINUX6440 )echo Linux AMD/Intel 64bit gcc/gfortran   ; OSYS=LNX6440 ;;
	OSXIntel ) echo OSX gcc gfortran for Apple  Intel    ; OSYS=OSXIntel ;;
	OSXM     ) echo OSX gcc gfortran for Apple  Silicon  ; OSYS=OSXM ;;
	LINUXllvm) echo Linux AMD/Intel 64bit  gcc/gfortran  ; OSYS=LNXllvm ;;
	OSXMllvm ) echo OSX clang/flang for Apple Silicon    ; OSYS=OSXMllvm ;;
	*)	echo 'Usage: ./Setup LINUX6440 | OSXM | OSXIntel  '
		echo '   LINUX6440  AMD/Intel Linux 64 bit with gcc/gfortran compilers'
		echo '   OSXIntel   Apple Intel 64-bit gcc/gfortran compilers'
		echo '   OSXM       Apple Silicon 64-bit gcc/gfortran compilers'
                echo '---------------------------------------------------------------'
                echo '       Developmental August 2024'
		echo '   LINUXllvm  AMD/Intel Linux 64 bit with cland/flang compilers'
		echo '   OSXMllvm   Apple Silicon 64-bot clang/flang compilers'
		exit ;;
esac

#####
#	create the proper C for compile
#####

touch C
chmod +x C
cat > C << EOF
#!${SHELL}
#####
#       establish the proper PATH to find the compilers
#####
PATH=$PATH
export PATH

EOF
echo CTARGET=$1 >> C

cat C.proto >> C


for DIR in VOLI/src VOLII/src VOLIII/src VOLIV/src VOLV/src VOLVI/src VOLVII/src VOLVIII/src VOLVIII/gsac.src  VOLIX/src  VOLX/src
do
if [ -d $DIR ]
then
	echo Copying ${DIR}/Makefile.${OSYS} ${DIR}/Makefile
	cp ${DIR}/Makefile.${OSYS} ${DIR}/Makefile
fi
done

for DIR in CALPLOT/src/cmd.unx CALPLOT/src/clib.unx CALPLOT/src/flib.unx CALPLOT/src/util
do
	echo Copying  ${DIR}/Makefile.${OSYS} ${DIR}/Makefile
	cp ${DIR}/Makefile.${OSYS} ${DIR}/Makefile
done

#####
#	special for IRIS routines 
#####
#    DO NOT COMPILE rdseed - use binaries for macos and Linux
#for DIR in IRIS/rdseedv5.3.slu
#do
#if [ -d $DIR ]
#then
	#echo Copying ${DIR}/Makefile.${OSYS} ${DIR}/Makefile
	#cp ${DIR}/Makefile.${OSYS} ${DIR}/Makefile
#fi
#done
for DIR in IRIS
do
if [ -d $DIR ]
then
	echo Copying ${DIR}/DOCOMPILE.${OSYS} ${DIR}/DOCOMPILE
	cp ${DIR}/DOCOMPILE.${OSYS} ${DIR}/DOCOMPILE
fi
done


for DIR in CALPLOT/src/XVIG/src CALPLOT/testf CALPLOT/testc \
		CALPLOT/Utility
do
	echo Copying ${DIR}/Makefile.${OSYS} ${DIR}/Makefile
	cp ${DIR}/Makefile.${OSYS} ${DIR}/Makefile
done



echo The proper Makefiles have been installed for your Operating System
echo Verify that these are correct with respect to compilers, flags
echo     and libraries
echo

echo If everything is correct, enter the command
echo './C > C.txt 2>&1 for the complete distribution '
echo '   [The C.txt will be useful for debugging the installation]'

echo 
echo Documentation is in the directory DOC in PDF format 
