# Copyright 1999-2013 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

#EAPI=5-hdepend # make HDEPEND work

#multilib currently only used for get_libdir
inherit eutils multilib versionator

DESCRIPTION="Microsoft SQL Server ODBC Driver 1.0 for Linux"


#md5:  36aca6fdfaa6e87754c477e08969fed4
#sha1: ba8a27b1fc91b933a31d987805a6f78e29f893b4





IUSE="doc examples"


HDEPEND="dev-util/patchelf" # does not work with EAPI=5 and portage-
DEPEND=${HDEPEND}		    # workaround

# The somewhat strange looking dependencies are explained by:
# sys-apps/util-linux      <-libuuid.so
# sys-libs/e2fsprogs-libs  <-libcom_err.so


MY_PV=$(get_version_component_range 1-2)
MY_BV=$(get_version_component_range 3-4) # MS build number?

# libsqlncli must remain the first entry because it is referenced referenced
QA_PREBUILT=" opt/microsoft/sqlncli/$(get_libdir)/libsqlncli-${MY_PV}.so.${MY_BV}
src_prepare() {
	epatch "${FILESDIR}/${P}"-use-destdir-tempdir.patch
	if use examples; then
		mkdir -p "${S}/docs/examples" || die
		cp -a "${FILESDIR}/examples" "${S}/docs" || die

src_install() {
	# Microsoft's install.sh assumes that these directories already exist
	dodir "/usr/bin"
	dodir "/usr/$(get_libdir)"

	# run the verify step
	TEMPDIR="${T}" DESTDIR="${ED}" ./install.sh verify

	# run the install step
	TEMPDIR="${T}" DESTDIR="${ED}" ./install.sh install --accept-license --force

	# Remove image path from the driver configuration file which is somehow
	# generated by install.sh. It is later used by odbcinst called from 
	# pkg_postinst()
	sed -i -e "s:${ED}:/:" "${ED}/opt/microsoft/sqlncli/${PV}/sqlncli.ini"||die

	# This binary package was released by Microsoft for 64-bit Red Hat
	# Enterprise Linux 6 only. Thus it looks in /usr/lib64 for shared 
	# library versions, which partly aren't available in the portage 
	# tree anymore. A simple solution for this is problem would consist 
	# in creating symlinks for each of the missing libraries in /usr/lib64.
	# However, this would endanger the system integrity and should be avoided.
	# Thus the overall strategy is:
	# 1.) Create symbolic links for the needed libraries in 
	#     /opt/microsoft/sqlncli/lib64, pointing to the surrogate library 
	#     available from /usr/lib64/
	# 2.) Set DT_NEEDED in the precompiled binaries to
	#     /opt/microsoft/sqlncli/lib46, so each pre-compiled binary
	#     knows where to look for
	# 3.) create /usr/lib64/libsqlncli.so as a linker script so
	#     folks can link the driver directly by simply appending -lsqlncli
	# So ...

	# 1.) Create symlinks in /opt/microsoft/[...]/lib64
	#     The local variable below is an associative array of shared library 
	#     names which are needed by the pre-compiled Microsoft binaries 
	#     relating these names to the names of actually existing libraries. 
	#     This list was manually created using scanelf --needed
	local -A libs=(

	#    create a symlink for each needed library 
	#    e.g. /opt/[...]/lib64/libcrypto.so.10 -> /usr/lib64/libcrypto.so
	for i in ${!libs[*]}; do
		local replacement=${EPREFIX}/usr/$(get_libdir)/${libs[${i}]}
	    local needed=opt/microsoft/sqlncli/$(get_libdir)/${i}
		dosym "${replacement}" "${needed}"

	# 2.) convert QA_PREBUILT into an array (portage needs it as a string) 
	read -a binaries <<<${QA_PREBUILT}

	#   ... and set DT_NEEDED for each of the files named in $binaries
	for prebuild in ${binaries[@]}; do
		local runpath=${EPREFIX}/opt/microsoft/sqlncli/$(get_libdir)/
		patchelf --set-rpath "${runpath}" "${ED}/${prebuild}" || die

	# 3.) provide the possibility to link the client library directly 
	#     via -lsqlncli w/o having to specify the full installation path
	local sqllib=${EPREFIX}/${binaries[0]}
	local script=${ED}/usr/$(get_libdir)/libsqlncli.so
	echo "GROUP ( ${sqllib} )"  > "${script}" || die

	# create a symlink  from /usr/include/${PN} to /opt/[...]/include
	dosym "/opt/microsoft/sqlncli/${PV}/include" "/usr/include/${PN}"

	# install MS produced documentation into /usr instead of /opt 
	if use doc; then
		dodoc  "${ED}/opt/microsoft/sqlncli/${PV}/"{README,WARNING}
		dohtml -r "${ED}/opt/microsoft/sqlncli/${PV}/docs/en_US/"*

	# also add the added example code to /usr/doc/${PF}
	if use examples; then
		docompress -x usr/share/doc/${PF}/examples
		dodoc -r "${S}"/docs/examples  || die

	# Prevent inadequate scripts or copies of files already installed in 
	# or /usr/share/doc/sqlncli or /usr/portage/licenses to get installed 
	# in /opt/microsoft/sqlncli/ again.
	# All this stuff can't simply be removed already in src_prepare() because
	# that implies a complete rewrite of Microsoft's install.sh.
	local opt="${ED}/opt/microsoft/sqlncli/${PV}"
	rm -r "${opt}/"{docs,install.sh,build_dm.sh,LICENSE,README,WARNING} || die

pkg_postinst() {
	odbcinst -i -d -f "${EPREFIX}/opt/microsoft/sqlncli/${PV}/sqlncli.ini" || die
	# If ldconfig should complain about $(basename ${binaries[0]}) being 
	# truncated, then you may have hit a bug in ldconfig. See:
	# http://old.nabble.com/
	# ldconfig-problem-with-patchelf-and-64-bit-libs-to28466603.html

pkg_prerm() {
	odbcinst -u -d -n "SQL Server Native Client 11.0" || die