#!/bin/sh -e

find "$1" | grep include > includes
sort includes|uniq -u > includes2
IDIR=$(cat includes2|tr '\n' ' '|sed -ne 's/\.\.\//-I\.\.\//gp')

PNME=$(grep PACKAGE_NAME Makefile|cut -f3 -d' ')
VNUM=$(sed -ne 's/.*-version-number \([0-9][0-9]*:[0-9][0-9]*:[0-9][0-9]*\).*/\1/p' Makefile)

# the below might need adjusting if the linker args span multiple lines... guess we'll find out
LIBS=$(sed -ne 's/.*_LIBS = \(-.*\)/\1/p' Makefile)

for c in $(ls *.c|cut -f1 -d.)
do
    cc -DHAVE_CONFIG_H -I. -I.. -I../include $IDIR -c $c.c -o $c.o
done

cc *.o -shared -o "$PNME.so.$(echo $VNUM|cut -f1 -d:).$(echo $VNUM|cut -f2 -d:).$(echo $VNUM|cut -f3 -d:)" $LIBS
ln -s "$PNME.so.$(echo $VNUM|cut -f1 -d:).$(echo $VNUM|cut -f2 -d:).$(echo $VNUM|cut -f3 -d:)" "$PNME.so.$(echo $VNUM|cut -f1 -d:)"
ln -s "$PNME.so.$(echo $VNUM|cut -f1 -d:).$(echo $VNUM|cut -f2 -d:).$(echo $VNUM|cut -f3 -d:)" "$PNME.so"

cc *.o -r -o "$PNME.a"

