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


inherit eutils rpm versionator

MY_PV=$(replace_version_separator 3 '-')
HP_FILE32="hpasm-${MY_PV}.sles10.i586.rpm"
HP_FILE64="hpasm-${MY_PV}.sles10.x86_64.rpm"

DESCRIPTION="hp Server Management Drivers and Agents"
HOMEPAGE="http://h18000.www1.hp.com/products/servers/linux/documentation.html"
SRC_URI="x86? ( ftp://ftp.compaq.com/pub/products/servers/supportsoftware/linux/${HP_FILE32} )
	amd64? ( ftp://ftp.compaq.com/pub/products/servers/supportsoftware/linux/${HP_FILE64} )"

LICENSE="hp-value"
SLOT="0"
KEYWORDS="~x86 ~amd64"
IUSE=""
RESTRICT="mirror"

DEPEND="${RDEPEND}
	virtual/linux-sources
	mail-client/mailx
	sys-apps/pciutils
	sys-libs/ncurses
	net-analyzer/net-snmp 
	!app-admin/cmastor"
	##X? ( tix virtual/x11 tclx )"

S="${WORKDIR}"

src_unpack() {
	if use x86
	then
	    hparch=i586
	    hplib=lib
	    rpm_unpack "${DISTDIR}"/${HP_FILE32}
	elif use amd64
	then
	    hparch=x86_64
	    hplib=lib64
	    rpm_unpack "${DISTDIR}"/${HP_FILE64}
	fi
}

src_install() {
#	into /opt/novell/ncl
#	dobin "${WORKDIR}"/${PN}-${MY_PV}/opt/novell/ncl/bin/novfsd
#	newbin "${WORKDIR}"/${PN}-${MY_PV}/opt/novell/ncl/bin/Debug/novfsd novfsd-Debug
#	dodoc "${WORKDIR}"/${PN}-${MY_PV}/usr/share/doc/packages/${PN}/*
#	exeinto /etc/opt/novell/xtier/xregd/init/
#	doexe "${FILESDIR}"/*.sh

#	newinitd "${FILESDIR}"/novfsd20.initd novfsd
#	keepdir /var/opt/novell/run



	HPASM_HOME="/opt/compaq"
	HPASMHP_HOME="/opt/hp"

	dodir ${HPASM_HOME}
	dodir ${HPASMHP_HOME}

	cp -Rdp \
	opt/compaq/* \
	${D}${HPASM_HOME}

	cp -Rdp \
    opt/hp/* \
	${D}${HPASMHP_HOME}

	dodir /etc
	cp ${FILESDIR}/initlog.conf ${D}/etc

	exeinto /opt/compaq/hpasm/etc
	doexe ${FILESDIR}/functions || die

	into /

	cp ${FILESDIR}/initlog .
	dosbin initlog

	dosym /opt/compaq/hpasm/addon/libcpqci.so.1.0 /opt/compaq/hpasm/addon/libcpqci.so.1
	dosym /opt/compaq/hpasm/addon/libcpqci.so.1.0 /opt/compaq/hpasm/addon/libcpqci.so

	dodir /usr/share/pixmaps
	dosym /opt/compaq/utils/hplogo.xbm /usr/share/pixmaps/hplogo.xbm
	dosym /opt/compaq/utils/m_blue.gif /usr/share/pixmaps/m_blue.gif
	dosym /opt/compaq/utils/m_fail.gif /usr/share/pixmaps/m_fail.gif
	dosym /opt/compaq/utils/m_green.gif /usr/share/pixmaps/m_green.gif
	dosym /opt/compaq/utils/m_red.gif /usr/share/pixmaps/m_red.gif
	dosym /opt/compaq/utils/m_yellow.gif /usr/share/pixmaps/m_yellow.gif

	dosym /opt/compaq/utils/cpqimlview /sbin/cpqimlview
	dosym /opt/compaq/utils/hpimlview /sbin/hpimlview
	dosym /opt/compaq/utils/cpqimlview.tcl /sbin/cpqimlview.tcl
	dosym /opt/compaq/utils/hplog /sbin/hplog
	dosym /opt/compaq/utils/hpuid /sbin/hpuid
	dosym /opt/compaq/utils/imlbe /sbin/imlbe
	dosym /opt/compaq/utils/hpasmcli /sbin/hpasmcli
	dosym /opt/compaq/utils/bootcfg	/sbin/bootcfg

	dodir /usr/lib

	if [ ! -f ${ROOT}/usr/share/snmp/snmpd.conf ]
    		then
		insinto /usr/share/snmp
		doins ${FILESDIR}/snmpd.conf || die
		else
		insinto /usr/share/snmp
		newins ${FILESDIR}/snmpd.conf snmpd.conf.cma || die
	fi

	if [ ! -f /usr/lib/libcrypto.so.2 ]
    		then
		dosym /usr/lib/libcrypto.so.0.9.6 /usr/lib/libcrypto.so.2
	fi

	if [ ! -f /usr/lib/libssl.so.2 ]
    		then
		dosym /usr/lib/libssl.so.0.9.6 /usr/lib/libssl.so.2
	fi

	dodir /var/spool/compaq

	exeinto /etc/init.d
	doexe ${FILESDIR}/hpasm || die


	doman usr/share/man/man4/hpasm.4.gz \
		usr/share/man/man8/cpqimlview.8.gz usr/share/man/man8/hplog.8.gz \
		usr/share/man/man8/hpuid.8.gz


	for i in opt/compaq/hpasm/addon/cmad.sh \
			opt/compaq/hpasm/etc/functions \
		

	do
	cat ${D}/$i | \
	sed 's/VENDOR="Unknown"/VENDOR="RedHat"/' >\
	${D}/$i-new
	mv ${D}/$i-new ${D}/$i

	cat ${D}/$i | \
	sed 's/^\.\ \/etc\/init.d\/functions/\.\ \/opt\/compaq\/hpasm\/etc\/functions/' >\
	${D}/$i-new
	mv ${D}/$i-new ${D}/$i

	cat ${D}/$i | \
	sed 's/^\.\ \/etc\/rc.d\/init.d\/functions/\.\ \/opt\/compaq\/hpasm\/etc\/functions/' >\
	${D}/$i-new
	mv ${D}/$i-new ${D}/$i

	cat ${D}/$i | \
	sed 's/usleep 250000/sleep 0\.25/'>\
	${D}/$i-new
	mv ${D}/$i-new ${D}/$i


	cat ${D}/$i | \
	    sed 's/usleep 100000/sleep 0\.1/'>\
		    ${D}/$i-new
			    mv ${D}/$i-new ${D}/$i

	chmod 755 ${D}/$i
	done


	echo "exclude cmhp"	>> ${D}/opt/compaq/cma.conf
	echo "trapemail /bin/mail -s 'HP Insight Management Agents Trap Alarm' root" >> ${D}/opt/compaq/cma.conf

	echo "Gentoo:2.x:2005"  >>  ${D}/opt/compaq/hpasm/distro.txt 
##    echo "RELEASE=Unknown" >> ${D}/opt/compaq/hpasm/distro.txt	
##   	echo "PRODUCT=Unknown" >> ${D}/opt/compaq/hpasm/distro.txt	

##	for i in /opt/compaq/foundation/etc/cmafdtnobjects.conf /opt/compaq/server/etc/cmasvrobjects.conf /opt/compaq/storage/etc/cmastorobjects.conf
	
##	do
##	echo $i >> ${D}/opt/compaq/cmaobjects.conf
##	done

	echo "/usr/bin/wish /sbin/cpqimlview.tcl -tclx /usr/lib/libtclx.so -tix /usr/lib/libtix8.2.so" > ${D}/opt/compaq/utils/cpqimlview-new
	mv ${D}/opt/compaq/utils/cpqimlview-new	${D}/opt/compaq/utils/cpqimlview

	chmod 755 ${D}/opt/compaq/utils/cpqimlview

	cat ${D}/opt/compaq/utils/cpqimlview.tcl | \
	sed 's/set DriverInfo \[exec rpm -q hpasm\]/\#set DriverInfo \[exec rpm -q hpasm\]/' >\
	${D}/opt/compaq/utils/cpqimlview.tcl-new
	mv ${D}/opt/compaq/utils/cpqimlview.tcl-new \
	${D}/opt/compaq/utils/cpqimlview.tcl

	cat ${D}/opt/compaq/utils/cpqimlview.tcl | \
	sed 's/wm title $form "$IML_GUI_MSGS(MSG_APP_TITLE) $DriverInfo $newTitle"/wm title $form "$IML_GUI_MSGS(MSG_APP_TITLE) $newTitle"/' >\
	${D}/opt/compaq/utils/cpqimlview.tcl-new
	mv ${D}/opt/compaq/utils/cpqimlview.tcl-new \
	${D}/opt/compaq/utils/cpqimlview.tcl

	cat ${D}/opt/compaq/utils/cpqimlview.tcl | \
	sed 's/wm title $form "$IML_GUI_MSGS(MSG_APP_TITLE) - $DriverInfo"/wm title $form "$IML_GUI_MSGS(MSG_APP_TITLE) - "/' >\
	${D}/opt/compaq/utils/cpqimlview.tcl-new
	mv ${D}/opt/compaq/utils/cpqimlview.tcl-new \
	${D}/opt/compaq/utils/cpqimlview.tcl
}

pkg_postinst() {
	einfo "If you want to run cpqimlview you will"
	einfo "need to emerge xfree, tclx, and tix."
	einfo ""
	einfo "If you want to use the web agent and other features"
	einfo "then configure your /usr/share/snmp/snmpd.conf,"
	einfo "emerge net-snmp before starting hpasm."
	einfo ""
	einfo "If you didn't already have an snmpd.conf then a"
	einfo "generic one has been place in /usr/share/snmp/"
	einfo ""
	einfo "It is not required to have net-snmp"
	einfo "running for basic hpasm functionality."
	einfo ""
	einfo "You now need to execute /etc/init.d/hpasm start"
	einfo "in order to use the installed package. The kernel"
	einfo "modules will automatically build for you."
	einfo ""
}