#! /bin/sh

srcdir=$(dirname "$0")

# some configure scripts seem to iterate over srcdir and other paths
# with for-loop using path_separator (most cases colon.)
PATH_SEPARATOR=:
(PATH='/bin;/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 && {
(PATH='/bin:/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 ||
  PATH_SEPARATOR=';'
}
IFS=$PATH_SEPARATOR
for i in $srcdir
do
if [ "$i" != "$srcdir" ]
then
    echo "failed to extract $srcdir using path separator $PATH_SEPARATOR"
    exit 1
fi
done

for i in "$@"
do
case $i in
    --prefix=*)
    PREFIX="${i#*=}"
    shift
    ;;
    --libdir=*)
    LIBDIR="${i#*=}"
    shift
    ;;
    --includedir=*)
    INCDIR="${i#*=}"
    shift
    ;;
    --libext=*)
    LIBEXT="${i#*=}"
    shift
    ;;
    *)
    shift
    ;;
esac
done

DEP_ARGS=$(pkg-config --cflags --libs somelib)

cat > Makefile << EOL
all: libfoo.$LIBEXT

libfoo.$LIBEXT:
	$CC "$srcdir/libfoo.c" -shared -fPIC $DEP_ARGS -o \$@

install: libfoo.$LIBEXT
	mkdir -p "\$(DESTDIR)$LIBDIR";
	mkdir -p "\$(DESTDIR)$LIBDIR/pkgconfig";
	mkdir -p "\$(DESTDIR)$INCDIR";
	cp \$< "\$(DESTDIR)$LIBDIR";
	cp "$srcdir/libfoo.h" "\$(DESTDIR)$INCDIR";
EOL
