#!/bin/sh

. /etc/profile
. /etc/flenv

mkdir -p $flinstpath

targetdir=`find $srcpath -maxdepth 1 -type d -name pthreads-\*`
if [ -z "$targetdir" ] || [ ! -d "$targetdir" ]; then
	echo "ERROR: target source directory not found"
	exit 1
fi

pushd $targetdir

patchfile=`find $flpath/patch -maxdepth 1 -type f -name flpthreads-\*.diff`
if [ -z "$patchfile" ] || [ ! -f "$patchfile" ]; then
	echo "ERROR: patch file not found"
	popd; exit 1
fi
patch -p1 -N < $patchfile \
|| { echo "INFO: patching pthread failed." ; exit 1 ; }

# build fl-static version
make clean GC-fl-static \
|| { echo "ERROR: make failed" ; popd; exit 1 ; }

cp -f pthread.h $mingwpath/include/pthread.flstatic.h
cp -f sched.h $mingwpath/include/sched.flstatic.h
cp -f semaphore.h $mingwpath/include/semaphore.flstatic.h
cp -f libpthreadGC2.a $mingwpath/lib/libpthread.flstatic.a
cp -f libpthreadGC2.a $mingwpath/lib/libpthreadGC2.flstatic.a

# build dll version
#make clean GC \
#|| { echo "ERROR: make failed" ; popd; exit 1 ; }
#cp -f libpthreadGC2.a $mingwpath/lib/libpthread.dll.a.esc
#cp -f libpthreadGC2.a $mingwpath/lib/libpthreadGC2.dll.a.esc

$flpath/bin/dllpthread.sh static

popd

echo "INFO: pthread is now installed"
