#!/bin/sh

CC=gcc
CXX=g++
CFLAGS="-O3"
CXXFLAGS="-O3"
BUILTOBJS=""
LIBCFILES="adler32.c compress.c crc32.c deflate.c infblock.c infcodes.c inffast.c inflate.c inftrees.c infutil.c trees.c uncompr.c zutil.c"
LIBCPPFILES="bstring.cpp cfcreate.cpp cfdblock.cpp cffdrmgr.cpp cffile.cpp cffolder.cpp cfheader.cpp cfreader.cpp cftypes.cpp object.cpp"
CPPPROGRAMFILES="listcab.cpp"
LIBINSTALLDIR=/usr/local/lib
VERSION=0.30.0
LIBRARY_NAME=cabinet
PROGRAM_NAME=listcab

#				Compile C files

for file in ${LIBCFILES}
do
	echo "${file}..."
	libtool --mode=compile ${CC} ${CFLAGS} -c ${file}

	if [ $? = 0 ]; then
		echo "compiled"
		BUILTOBJS="${BUILTOBJS} "`echo -n "${file}" | sed 's/.c$/.lo/'`
	else
		exit 1
	fi
done

#				Compile C++ files

for file in ${LIBCPPFILES}
do
	echo "${file}..."
	libtool --mode=compile ${CXX} ${CXXFLAGS} -c ${file}

	if [ $? = 0 ]; then
		echo "compiled"
		BUILTOBJS="${BUILTOBJS} "`echo -n "${file}" | sed 's/.cpp$/.lo/'`
	else
		exit 1
	fi
done

libtool --mode=link ${CXX} ${CXXFLAGS} -o lib${LIBRARY_NAME}.la ${BUILTOBJS} -rpath ${LIBINSTALLDIR} -release ${VERSION}
libtool --mode=link ${CXX} ${CXXFLAGS} -o ${PROGRAM_NAME} ${CPPPROGRAMFILES} lib${LIBRARY_NAME}.la

if [ "$?" != "0" ]; then
	exit 1
fi

echo "done"
echo "To test library please run ./testlib"

exit 0
