#!/bin/sh
set -e

ARCH=@host_cpu@
MODSUBDIR=@modsubdir@

if test "x${NRNHOME}" = x ; then
	prefix=@prefix@
	exec_prefix=@exec_prefix@
	bindir=@bindir@
	libdir=@libdir@
else
        prefix="$NRNHOME"
	exec_prefix="${prefix}/${ARCH}"
	bindir="${prefix}/${ARCH}/bin"
	libdir="${prefix}/${ARCH}/lib"
fi

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

export prefix
export exec_prefix
export bindir
export libdir

MAKEFILEDIR="${bindir}"

PURIFY="USEPURIFY=no"
if [ "$1" = "-p" ] ; then
	PURIFY="USEPURIFY=yes"
	shift
fi

TRACE="BGTRACE_LIBS="
if [ "$1" = "-trace" ] ; then
	TRACE="BGTRACE_LIBS = -L/bgl/hpct_bgl/mp_profiler/lib -lmpitrace.rts -L/bgl/hpct_bgl/binutils-2.14/lib -lbfd -liberty -lhandle_MPID_Win_start_error.rts"
	shift
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 $(ls $i/*.mod)"
			incs="$incs $(ls $i/*.inc)"
			set -e
		else
			files="$files $i"
		fi
	done
else
	files=$(ls *.mod)
fi

mfiles=""
for i in $files ; do
	mfiles="$mfiles `basename $i`"
done

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);' >> mod_func.c
done
echo '
void modl_reg(){
  if (!nrn_nobanner_) if (nrnmpi_myid < 1) {
    fprintf(stderr, "Additional mechanisms from files'$newline'");
' >> mod_func.c

if true ; then # the standard order
for i in $files
	do
		echo '    fprintf(stderr," '$i'.mod");' >>mod_func.c
	done
else
	# use "ls -x" to sort alphabetically by lines
	# use "ls -C" to sort alphabetically by columns
	ls -C $mfiles | sed 's/\(.*\)/    fprintf(stderr,"\1\\n");/' >> mod_func.c
fi
echo '    fprintf(stderr, "'$newline'");
  }' >>mod_func.c

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

echo "}" >> mod_func.c


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

@NRNMECH_DLL_STYLE_FALSE@make -j 4 -f "${MAKEFILEDIR}/nrniv_makefile" "$PURIFY" "$TRACE" "MODOBJFILES=$MODOBJS" "COBJFILES=$COBJS" "UserLDFLAGS=$UserLDFLAGS" "UserINCFLAGS=$UserINCFLAGS" special &&
@NRNMECH_DLL_STYLE_FALSE@  echo "Successfully created $MODSUBDIR/special"

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

@NRNMECH_DLL_STYLE_TRUE@@MAC_DARWIN_TRUE@if false ; then
@NRNMECH_DLL_STYLE_TRUE@@MAC_DARWIN_FALSE@if false ; then
@NRNMECH_DLL_STYLE_FALSE@if false ; then
vv=`sw_vers -productVersion|sed 's/.*\.\(.*\)\..*/\1/'`
#if test $vv -gt 6 ; then
if false ; then
echo "use @executable_path prefix for install_name for dylib absolute paths"
ff="${mdir}/.libs/libnrnmech.so"
f=`otool -L "$ff" | sed -n "s,.*$libdir/\(.*dylib\).*,\
-change $libdir/\1 @executable_path/../lib/\1,p"`
install_name_tool $f "$ff"
fi
fi
