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

EAPI=5
#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"

HOMEPAGE="http://www.microsoft.com/en-us/download/details.aspx?id=28160"

SRC_URI="http://download.microsoft.com/download/6/A/B/6AB27E13-46AE-4CE9-AFFD-406367CADC1D/Linux6/${P}.tar.gz"
#md5:  36aca6fdfaa6e87754c477e08969fed4
#sha1: ba8a27b1fc91b933a31d987805a6f78e29f893b4

LICENSE="MSsqlncli-11"

RESTRICT="mirror"

SLOT="0"

KEYWORDS="~amd64"

IUSE="doc examples"

MERGE_TYPE=binary

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

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

RDEPEND="virtual/libc
		 virtual/krb5
		 dev-libs/openssl
		 >dev-db/unixODBC-2.3.1[iconv-char-enc-utf8(+),iconv-ucode-enc-utf16le(+)]
		 sys-apps/util-linux
		 sys-libs/e2fsprogs-libs"

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}
	 		  opt/microsoft/sqlncli/bin/sqlcmd-${PV}
	 		  opt/microsoft/sqlncli/bin/bcp-${PV}
			"
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
	fi
}

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=(
				    [libcrypto.so.10]=libcrypto.so
			           [libssl.so.10]=libssl.so
			           [libodbc.so.1]=libodbc.so
			       [libodbcinst.so.1]=libodbcinst.so
				  )

	#    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}"
	done

	# 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
	done

	# 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/"*
	fi

	# 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
	fi

	# 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
}