################################################################################
#
# berkdb.cygclass - functions for building the Berkeley DB
#
# Part of cygport - Cygwin packaging application
# Copyright (C) 2006-2016 Yaakov Selkowitz
# Provided by the Cygwin Ports project <http://sourceware.org/cygwinports/>
#
# cygport is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cygport is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cygport.  If not, see <http://www.gnu.org/licenses/>.
#
################################################################################

#****h* Cygclasses/berkdb.cygclass
#  SYNOPSIS
#  inherit [java] [tcl] berkdb
#  DESCRIPTION
#  The Berkeley DB is an embedded database in the form of a C library and
#  several command-line utilities, and includes optional C++, Java, and Tcl
#  bindings.
#
#  This cygclass does all the work involved in building and packaging versions
#  3.x, 4.x, and 5.x of the Berkeley DB.  Older versions are not supported.
#
#  The C++ bindings are always enabled, but the Java and Tcl bindings will
#  only be enabled if their respective cygclasses are inherited _before_
#  berkdb.
#  INHERITS
#  autotools.cygclass
#****

case ${PV_MAJ_MIN} in
	3.*|4.*|5.*)	SLOT="${PV_MAJ_MIN}" ;;
	*)		error "berkdb: accepts only versions 3.x/4.x/5.x" ;;
esac

tarpv=${PV[1]}.${PV[2]}.${PV[3]}

#****o* berkdb.cygclass/CATEGORY (berkdb)
#  DEFINITION
CATEGORY="Database Libs"
#****
#****o* berkdb.cygclass/SUMMARY (berkdb)
#  DEFINITION
SUMMARY="Berkeley DB version ${SLOT}"
#****
#****o* berkdb.cygclass/DESCRIPTION (berkdb)
#  DEFINITION
DESCRIPTION="Oracle Berkeley DB is the industry-leading open source, embeddable
database engine that provides developers with fast, reliable, local
persistence with zero administration. Oracle Berkeley DB is a library
that links directly into your application. Your application makes simple
function calls, rather than sending messages to a remote server,
eliminating the performance penalty of client-server architectures."
#****
#****o* berkdb.cygclass/HOMEPAGE (berkdb)
#  DEFINITION
HOMEPAGE="http://www.oracle.com/technology/products/berkeley-db/db/index.html"
#****

#****o* berkdb.cygclass/SRC_URI (berkdb)
#  DESCRIPTION
#  Download location of the Berkeley DB source tarball.
#****
SRC_URI="http://download.oracle.com/berkeley-db/db-${tarpv}.tar.gz"

SRC_DIR="db-${tarpv}"

#****o* berkdb.cygclass/PATCH_URI (berkdb)
#  NOTE
#  Upstream patches are automatically downloaded based on the fourth component
#  of PV; e.g. version 4.6.21.4 will download patches 1-4 for the 4.6.21 tarball.
#  WARNING
#  If additional custom patches are required, be sure to add to, not override,
#  the PATCH_URI variable as so:
#    PATCH_URI+=" 4.6-libtool22.patch"
#  Failure to do so will result in upstream patches not being included in your
#  package.
#****
if defined PV[4]
then
	for ((p = 1; p <= PV[4]; p++))
	do
		PATCH_URI+=" ${HOMEPAGE%/*}/update/${tarpv}/patch.${tarpv}.${p}"
	done
fi

unset tarpv

#****o* berkdb.cygclass/PKG_NAMES (berkdb)
#  DESCRIPTION
#  The following binary packages are created automatically:
#  * dbX.Y: command-line utilities, Cygwin and upstream READMEs;
#  * dbX.Y-doc: HTML documentation;
#  * libdbX.Y: C and C++ runtime libraries;
#  * libdbX.Y-devel: C and C++ headers and import libraries.
#  The following packages are created if their bindings are enabled:
#  * java-dbX.Y: JNI binding and Java jar;
#  * tcl-dbX.Y: Tcl bindings.
#****
PKG_NAMES="${PN} lib${PN} lib${PN}-devel ${PN}-doc"
declare ${PN//./_}_CONTENTS="--exclude=html usr/bin/db${SLOT}_*.exe usr/share/doc/"
declare lib${PN//./_}_CONTENTS="--exclude=*java* usr/bin/*-${SLOT}.dll"
declare lib${PN//./_}_devel_CONTENTS="etc/p*/lib${PN}-devel.sh usr/include/ usr/lib/lib*"
declare ${PN//./_}_doc_CONTENTS="usr/share/doc/${PN}/html/"

if inherited java
then
	PKG_NAMES+=" java-${PN}"
	declare java_${PN//./_}_CONTENTS="usr/bin/cygdb_java-${SLOT}.dll ${JAVA_DIR#/}"
fi

if inherited tcl
then
	PKG_NAMES+=" tcl-${PN}"
	declare tcl_${PN//./_}_CONTENTS="${TCL_LIBDIR#/}"
fi

DIFF_EXCLUDES="config.hin db.jar libtool.ac lt*.ac"

#****C* berkdb.cygclass/berkdb_autoreconf
#  DESCRIPTION
#  This function is used instead of cygautoreconf due to the unique build
#  system of the Berkeley DB.  This step is required, as several build system
#  files will need to be patched for a working build:
#  * chmod +w dist/Makefile.in dist/aclocal/tcl.ac dist/configure.ac
#  * Add LT_OUTPUT after AC_PROG_LIBTOOL in dist/configure.ac
#  * sed -i -e 's/aix\*/aix*|cygwin*/g' dist/aclocal/tcl.ac
#  * Modify/define MAKEFILE_TSOLINK (not needed with 4.5+)
#  * Undefine __lock_* macros in lock/lock_region.c (not needed with 4.3+)
#  REQUIRES
#  autoconf, automake, libtool
#****
berkdb_autoreconf() {
	check_prog_req libtoolize libtool

	[ -f s_config ] && [ -d aclocal ] || error "not in dist/ directory"

	__config_set with_aclocal 1
	__config_set with_autoconf 1
	__config_set with_libtool 1

	ac_macrodir=aclocal libtoolize --copy --force --install || error "berkdb: libtoolize failed"

	# some versions require .ac extension, others use .m4
	if [ -f aclocal/libtool.ac ]
	then
		rename .m4 .ac aclocal/*.m4
	fi

	./s_config || error "s_config failed"
}

#****C* berkdb.cygclass/berkdb_compile
#  DESCRIPTION
#  This function calls cygconf with the necessary options for building the
#  the C library shared and static, and the C++, Java and/or Tcl bindings.
#  NOTE
#  The C++ library, JNI bindings, and Tcl bindings are specially built to link
#  against the C library instead of including the sources thereof.  This means
#  that the java-dbX.Y and tcl-dbX.Y packages must depend on libdbX.Y.
#****
berkdb_compile() {
	local objvar use_java use_sql use_tcl

	case ${PV} in
		5.*)  use_sql="--enable-sql" ;;
	esac

	if inherited java
	then
		use_java="--enable-java"
	else
		use_java="--disable-java"
	fi

	if inherited tcl
	then
		use_tcl="--enable-tcl --with-tcl=/usr/lib --enable-test"
	else
		use_tcl="--disable-tcl --disable-test"
	fi

	CYGCONF_SOURCE=${S}/dist \
	cygconf \
		--includedir=/usr/include/db${SLOT} \
		--with-mutex=x86/gcc-assembly \
		--enable-compat185 \
		--enable-cxx \
		--enable-dynamic \
		${use_sql} ${use_java} ${use_tcl}

	cygmake libdb-${SLOT}.la

	case ${SLOT} in
		3.[0-2]) objvar=OBJS ;;
		3.*|4.*|5.*) objvar=C_OBJS ;;
	esac

	cygmake ${objvar}="libdb-${SLOT}.la"
}

#****I* berkdb.cygclass/berkdb_install
#  DESCRIPTION
#  This function installs the Berkeley DB into $D and moves some files so that
#  multiple versions may be installed in parallel.
#****
berkdb_install() {
	case ${SLOT} in
		4.[012])
			USE_DESTDIR=0 cyginstall \
				includedir=${D}/usr/include/db${SLOT} \
				docdir=${D}/usr/share/doc/${PN}/html \
				emode=755 fmode=644
			;;
		3.*)
			USE_DESTDIR=0 cyginstall \
				includedir=${D}/usr/include/db${SLOT} \
				docdir=${D}/usr/share/doc/${PN}/html \
				emode=755 fmode=644 \
				chmod=true ln=true strip=true
			;;
		4.*|5.*)
			USE_DESTDIR=1 cyginstall \
				docdir=/usr/share/doc/${PN}/html \
				emode=755 fmode=644
			;;
	esac

	# remove unslotted static libs
	rm -f ${D}/usr/lib/libdb{_cxx,}.a

	# slot executables
	pushd ${D}/usr/bin
	rename db_ db${SLOT}_ *.exe
	popd

	if inherited java
	then
		rm -f ${D}/usr/lib/libdb_java-${SLOT}.*
		# relocate and slot Java jar
		dodir ${JAVA_DIR}
		mv ${D}/usr/lib/db.jar ${D}${JAVA_DIR}/db-${SLOT}.jar
	fi

	if inherited tcl
	then
		rm -f ${D}/usr/lib/libdb_tcl-${SLOT}.*
		# relocate and install Tcl module
		dodir ${TCL_LIBDIR}/db${SLOT}
		mv ${D}/usr/lib/cygdb_tcl-${SLOT}.dll ${D}${TCL_LIBDIR}/db${SLOT}/
		echo pkg_mkIndex ${D}${TCL_LIBDIR}/db${SLOT} cygdb_tcl-${SLOT}.dll | PATH=${D}/usr/bin:$PATH ${TCLSH}
		sed -i -e "s#${D}##g" ${D}${TCL_LIBDIR}/db${SLOT}/pkgIndex.tcl
	fi
}

#****I* berkdb.cygclass/berkdb_postinst
#  DESCRIPTION
#  This function creates postinstall and preremove scripts which manage the
#  unversioned symlinks for the headers and link libraries with alternatives(8).
#  No such symlinks are created for the command-line utilities, as the database
#  and/or log formats do change from one version to the next.
#  NOTE
#  All libdbX.Y-devel packages must depend on alternatives.
#****
berkdb_postinst() {
	local h l lx prog

	dodir /etc/postinstall

	# headers and link libraries
	cat >> ${D}/etc/postinstall/lib${PN}-devel.sh <<-_EOF
		incdir=/usr/include
		libdir=/usr/lib
		slot=${SLOT}
		rank=$(( PV[1] * 100 + PV[2] ))

		/usr/sbin/update-alternatives \\
		  --install \${incdir}/db.h      db.h       \${incdir}/db\${slot}/db.h \${rank} \\
		  --slave   \${incdir}/db_185.h  db_185.h   \${incdir}/db\${slot}/db_185.h \\
		  --slave   \${incdir}/db_cxx.h  db_cxx.h   \${incdir}/db\${slot}/db_cxx.h \\
		  --slave   \${incdir}/db        db         \${incdir}/db\${slot} \\
		  --slave   \${libdir}/libdb.a      libdb.a      \${libdir}/libdb-\${slot}.a \\
		  --slave   \${libdir}/libdb.dll.a  libdb.dll.a  \${libdir}/libdb-\${slot}.dll.a \\
		  --slave   \${libdir}/libdb.la     libdb.la     \${libdir}/libdb-\${slot}.la \\
		  --slave   \${libdir}/libdb_cxx.a      libdb_cxx.a      \${libdir}/libdb_cxx-\${slot}.a \\
		  --slave   \${libdir}/libdb_cxx.dll.a  libdb_cxx.dll.a  \${libdir}/libdb_cxx-\${slot}.dll.a \\
		  --slave   \${libdir}/libdb_cxx.la     libdb_cxx.la     \${libdir}/libdb_cxx-\${slot}.la

		/usr/sbin/update-alternatives \\
		  --install \${incdir}/db${PV[1]}  db${PV[1]}  \${incdir}/db\${slot} \${rank} \\
		  --slave   \${libdir}/libdb${PV[1]}.a      libdb${PV[1]}.a      \${libdir}/libdb-\${slot}.a \\
		  --slave   \${libdir}/libdb${PV[1]}.dll.a  libdb${PV[1]}.dll.a  \${libdir}/libdb-\${slot}.dll.a \\
		  --slave   \${libdir}/libdb${PV[1]}.la     libdb${PV[1]}.la     \${libdir}/libdb-\${slot}.la \\
		  --slave   \${libdir}/libdb${PV[1]}_cxx.a      libdb${PV[1]}_cxx.a      \${libdir}/libdb_cxx-\${slot}.a \\
		  --slave   \${libdir}/libdb${PV[1]}_cxx.dll.a  libdb${PV[1]}_cxx.dll.a  \${libdir}/libdb_cxx-\${slot}.dll.a \\
		  --slave   \${libdir}/libdb${PV[1]}_cxx.la     libdb${PV[1]}_cxx.la     \${libdir}/libdb_cxx-\${slot}.la
		_EOF

	dodir /etc/preremove

	cat >> ${D}/etc/preremove/lib${PN}-devel.sh <<-_EOF
		incdir=/usr/include
		slot=${SLOT}

		/usr/sbin/update-alternatives --remove db.h \${incdir}/db\${slot}/db.h
		/usr/sbin/update-alternatives --remove db${PV[1]} \${incdir}/db\${slot}
		_EOF
}

#****o* berkdb.cygclass/src_compile (berkdb)
#  DEFINITION
src_compile() {
	cd ${S}/dist
	berkdb_autoreconf
	cd ${B}
	berkdb_compile
}
#****

#****o* berkdb.cygclass/src_install (berkdb)
#  DEFINITION
src_install() {
	cd ${B}
	berkdb_install
	berkdb_postinst
}
#****

readonly -f berkdb_autoreconf berkdb_compile berkdb_install berkdb_postinst
