#!/bin/sh
set -e

ARCH=x86_64
MODSUBDIR=x86_64

if test "x${NRNHOME}" = x ; then
	prefix=/Users/runner/work/1/s/build/cmake_install
	exec_prefix=/Users/runner/work/1/s/build/cmake_install
	bindir=${exec_prefix}/bin
	libdir=${exec_prefix}/lib
else
	prefix="$NRNHOME"
	exec_prefix="${prefix}/${ARCH}"
	#bindir="${prefix}/${ARCH}/bin"
	#libdir="${prefix}/${ARCH}/lib"
	bindir="${prefix}/bin"
	libdir="${prefix}/lib"
fi

if test "${NRNHOME_EXEC}" != "" ; then
	exec_prefix="${NRNHOME_EXEC}"
	bindir="${exec_prefix}/bin"
	libdir="${exec_prefix}/lib"
fi

# Besides NRNHOME, one may set NRNBIN to override the default location of user binaries
if test "x$NRNBIN" = "x"; then
	NRNBIN=$bindir
fi

export prefix
export bindir
export libdir

MAKEFILEDIR="${bindir}"

if which xcrun; then
    export SDKROOT=`xcrun --sdk macosx --show-sdk-path`
    export MACOSX_DEPLOYMENT_TARGET="10.9"
    if [ -z "${MACOSX_DEPLOYMENT_TARGET}" ]; then
        unset MACOSX_DEPLOYMENT_TARGET
    fi
fi

UserINCFLAGS=""
if [ "$1" = "-incflags" ] ; then
      UserINCFLAGS="$2"
      shift
      shift
fi

UserLDFLAGS=""
if [ "$1" = "-loadflags" ] ; then
       UserLDFLAGS="$2"
       shift
       shift
fi

if [ ! -d $MODSUBDIR ] ; then
	echo "Creating $MODSUBDIR directory for .o files."
	echo
	mkdir $MODSUBDIR
fi

pwd

files=""
incs=""
if test $# -gt 0 ; then
	for i in "$@" ; do
		if test -d "$i" ; then
			set +e
			files="$files $(unset LC_ALL; ls $i/*.mod)"
			incs="$incs $(unset LC_ALL; ls $i/*.inc)"
			set -e
		else
			files="$files $i"
		fi
	done
else
	# Unset LC_ALL for consistent mod order
	files=$(unset LC_ALL; ls *.mod)
fi
files=`echo "$files" | sed 's/^ *//'`

mfiles=""
for i in $files ; do
	mfiles="$mfiles `basename $i`"
done
mfiles=`echo "$mfiles" | sed 's/^ *//'`

echo $files
echo $mfiles

files=`echo "$files" | sed 's/\.mod//g'`

if test "$files" = '*' ; then
	files=""
fi

cd $MODSUBDIR
mdir=`pwd`
for i in $files; do
	rm -f "`basename $i`".mod
	ln -s ../$i.mod .
done

if test "$incs" != "" ; then
	for i in $incs ; do
		rm -f "`basename $i`"
		ln -s ../$i .
	done
fi

bfiles=`echo "$mfiles" | sed 's/\.mod//g'`

for i in $cfiles; do
	rm -f $i
	ln -s ../$i .
done

COBJS=
MODOBJS=
if [ `echo "\n"` ] ; then
	newline="\n"
else
	newline="\\\\n"
fi

echo '#include <stdio.h>
#include "hocdec.h"
extern int nrnmpi_myid;
extern int nrn_nobanner_;
' > mod_func.c
for i in $bfiles ; do
	echo 'extern void _'$i'_reg(void);'
done >> mod_func.c
echo '
void modl_reg(){
  if (!nrn_nobanner_) if (nrnmpi_myid < 1) {
    fprintf(stderr, "Additional mechanisms from files'$newline'");
' >> mod_func.c

for i in $files
do
	echo '    fprintf(stderr," '$i'.mod");'
done >>mod_func.c

echo '    fprintf(stderr, "'$newline'");
  }' >>mod_func.c

for i in $bfiles; do
	echo '  _'$i'_reg();'
	MODOBJS="$MODOBJS $i.o"
done >> mod_func.c

echo "}" >> mod_func.c


if test -n "$cfiles" ; then
	COBJS=`echo "$cfiles" | sed 's/\.c/.o/g'`
fi

#make -j 4 -f "${MAKEFILEDIR}/nrnmech_makefile" "ROOT=${prefix}" "MODOBJFILES=$MODOBJS" "COBJFILES=$COBJS" "UserLDFLAGS=$UserLDFLAGS" "UserINCFLAGS=$UserINCFLAGS" special &&
##make -j 4 -f "${MAKEFILEDIR}/nrniv_makefile" "ROOT=${prefix}" "MODOBJFILES=$MODOBJS" "COBJFILES=$COBJS" "UserLDFLAGS=$UserLDFLAGS" "UserINCFLAGS=$UserINCFLAGS" special &&
#  echo "Successfully created $MODSUBDIR/special"

MODLO=`echo "$MODOBJS" | sed 's/\.o/.lo/g'`
CLO=`echo "$COBJS" | sed 's/\.o/.lo/g'`
if test "${mdir}" = "${prefix}/share/nrn/demo/release/powerpc" ; then
  mdir='${NRNHOME}'/share/nrn/demo/release/${MODSUBDIR}
fi
#make -j 4 -f "$MAKEFILEDIR/nrnmech_makefile" "ROOT=${prefix}" "MODOBJFILES=$MODLO" "COBJFILES=$CLO" "UserLDFLAGS=$UserLDFLAGS" "UserINCFLAGS=$UserINCFLAGS" libnrnmech.la
make -j 4 -f "$MAKEFILEDIR/nrnmech_makefile" "ROOT=${prefix}" "MODOBJFILES=$MODOBJS" "COBJFILES=$COBJS" "UserLDFLAGS=$UserLDFLAGS" "UserINCFLAGS=$UserINCFLAGS" libnrnmech.la VERBOSE=1
  echo '#!/bin/sh
if test "x${NRNHOME}" = "x" ; then
    NRNHOME='"\"${prefix}\""'
fi
if test "${NEURONHOME}" = "" ; then
    NEURONHOME=${NRNHOME}/share/nrn
    export NEURONHOME
fi
if test "x${NRNBIN}" = "x" ; then
    NRNBIN='"\"${NRNBIN}\""'
fi
if test "" = "yes" ; then
    NRNIV="${NRNBIN}/nrniv.app/Contents/MacOS/nrniv"
else
    NRNIV="${NRNBIN}/nrniv"
fi
"${NRNIV}"'" -dll \"${mdir}/.libs/libnrnmech.so\" \"\$@\"" > special &&
  chmod 755 special &&
  echo "Successfully created $MODSUBDIR/special"

if false ; then
#if false ; then
#if false ; then
  false #for empty if block
fi
