#!/bin/sh
prefix=@prefix@
exec_prefix=@exec_prefix@
bindir=@bindir@
libdir=@libdir@
LIBOBJDIR=${libdir}
MAKEFILEDIR=${bindir}
ARCH=@host_cpu@

PURIFY="DUMMY=nothing"
if [ "$1" = "-p" ] ; then
	PURIFY="CXXLD=\"purify g++\""
	shift
fi

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

if test $# -gt 0 ; then
	files=$*
else
	files=`echo *.mod`
fi

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

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

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

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"
modl_reg(){
	NOT_PARALLEL_SUB(fprintf(stderr, "Additional mechanisms from files'$newline'");)
' > mod_func.c

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

for i in $files
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 -f $MAKEFILEDIR/nrniv_makefile "$PURIFY" "MODOBJFILES=$MODOBJS" "COBJFILES=$COBJS" special &&
@NRNMECH_DLL_STYLE_FALSE@  echo "Successfully created $ARCH/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@make -f $MAKEFILEDIR/nrnmech_makefile "MODOBJFILES=$MODLO" "COBJFILES=$CLO" libnrnmech.la &&
@NRNMECH_DLL_STYLE_TRUE@  echo "${bindir}/nrniv -dll ${mdir}/.libs/libnrnmech.so \$*" > special &&
@NRNMECH_DLL_STYLE_TRUE@  chmod 755 special &&
@NRNMECH_DLL_STYLE_TRUE@  echo "Successfully created $ARCH/special"

