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

EAPI="5"

inherit eutils

LEGACY_VER=9.5.5.1
MY_SRC="http://www.lsi.com/support/Pages/download-results.aspx?productcode=P00080&assettype=0&component=Storage%20Component&productfamily=Legacy%20RAID%20Controllers&productname=3ware%209650SE-4LPML"
MY_ARCH="${ARCH/amd64/x86_64}"

DESCRIPTION="3ware Disk Managment web utility and RAID controller CLI tool"
HOMEPAGE="http://www.lsi.com/"
SRC_URI="3DM2_CLI-linux_${PV}_${LEGACY_VER}.zip"

LICENSE="LSI"
SLOT="0"
KEYWORDS="amd64 ~x86"
IUSE="cli +web"

RESTRICT="mirror fetch"
PROPERTIES="interactive"

RDEPEND="virtual/libc
	virtual/logger
	virtual/mta"

DEPEND="cli? ( !sys-block/tw_cli )"

QA_PRESTRIPPED="usr/sbin/3dm2
	usr/sbin/tw_cli"

S=${WORKDIR}

pkg_nofetch() {
	einfo "This software can no longer be automatically downloaded."
	echo
	einfo "Please manually download the following file:"
	einfo "${SRC_URI}"
	echo
	einfo "titled:"
	einfo "3DM2 CLI Linux from the ${PV}/${MY_REV} code set 8xxx/95xx/96xx/97xx - Linux"
	echo
	einfo "from the following URL:"
	einfo "${MY_SRC}"
	echo
	einfo "and move to: ${DISTDIR}/"
}

pkg_setup() {
	# Validate USE flags
	if (! use cli && ! use web); then
		ewarn
		ewarn "You must specify at least one USE flag for this package."
		ewarn
		die "No USE flags enabled."
	fi

	# Display some supplimental information about controller support
	echo
	einfo "This binary should support the following series of 3ware controllers:"
	einfo "7000 / 8000, 9500S, 9550SX, 9590SE, 9650SE, 9690SA, 9750"
	echo
}

src_unpack() {
	unpack ${A}
	unpack "./tdmCliLnx.tgz"
	mkdir help msg
	tar zxf tdm2Help.tgz -C help
	tar zxf tdm2Msg.tgz -C msg
}

src_prepare() {
	# update conf paths for Gentoo standards
	sed -i -e 's;MsgPath /opt/3ware/3DM2/msg;MsgPath /usr/share/3dm2/msg;' \
		-e 's;Help /opt/3ware/3DM2/help;Help /usr/share/3dm2/help;' \
		-e 's;imgPath /etc/3dm2;imgPath /usr/share/3dm2;' \
		3dm2.conf || die "sed update 3dm2.conf"
}

src_install() {
	if use web; then
		newsbin "3dm2.${MY_ARCH}" ${PN} || die "dosbin 3dm2.${MY_ARCH}"

		dodir /etc/${PN}
		insinto /etc/${PN}
		doins 3dm2.conf || die "doins 3dm2.conf"

		insinto /usr/share/${PN}
		doins logo.gif || die "doins logo.gif"
		doins -r help || die "doins help"
		doins -r msg || die "doins msg"

		newinitd "${FILESDIR}/${PN}.init" ${PN} || die "newinitd 3dm2.init"
	fi

	if use cli; then
		newsbin tw_cli.${MY_ARCH} tw_cli || die "dosbin tw_cli.${MY_ARCH}"
		newman tw_cli.8.nroff tw_cli.8
		dodoc tw_cli.8.html
	fi

	dodoc LGPL_License.txt OpenSSL.txt
}

pkg_preinst() {
	export RESTART=0
	if use web; then
		if [ $(pgrep 3dm2 >/dev/null; echo $?) -eq 0 ]; then
			/etc/init.d/${PN} stop
			RESTART=1
		fi
	fi
}

pkg_postinst() {
	if [ ${RESTART} -eq 0 ]; then
		echo
		einfo "Start 3dm2, then connect to the server at https://localhost:888/"
		einfo "Default password for both user and administrator is: 3ware"
		einfo "Note that remote access is *enabled* by default."
		einfo
		einfo "To change the ssl cert, place a file called 3dm2.pem in /etc/3dm2"
		einfo "It must contain the certificate and the key."
		einfo "Under normal circumstances you don't need to change it."
	else
		echo
		ewarn "Note: 3dm2 was automatically stopped to complete this upgrade."
		ewarn "You should restart it now with: /etc/init.d/${PN} start"
	fi
	echo
}