#
# This spec file defines the input constraints and build system for
# Zenoss when it is installed onto an RPM based system.
#
# For information on what arguments are passed to %pre, %post, %preun,
# and %postun see the following URL:
#   http://tinyurl.com/sjyfp
#

# the location where zenoss is installed
%define zenhome /opt/zenoss

# mysql is used for the events database
%define mysql_username zenoss
%define mysql_passwd zenoss
%define mysql_database events

# zope is used for the web interface
%define zope_passwd zenoss

# a shell account controls the zenoss processes
%define os_username zenoss
%define os_home /home/zenoss
%define os_shell /bin/bash

# set to 1 if the version of the software to be built is the trunk
# if trunk is set to 0 the version will be extrapolated from the
# rpm information contained in the %{version} and %{release} vars
%define trunk 0

# the name of the ctl file for zenoss (in /etc/rc.d/rc#.d)
%define S_zenoss S99zenoss

# the location of the snmp configuration file and sudoers
%define snmpd_conf /etc/snmp/snmpd.conf
%define my_cnf /etc/my.cnf
%define sudoers /etc/sudoers


# the RPM meta information
Name:     zenoss
Summary:  The Open Source Network Management System
Version: 1.1.2
Release: 0
License:  GPL
Vendor:   Zenoss, Inc.
Packager: Christopher Blunck <chris@zenoss.com>
Group:    Management/Network
Source0:  %{name}-%{version}-%{release}.tar.bz2
Prereq:   /usr/sbin/useradd
Prereq:   /usr/sbin/userdel
BuildRequires: python-devel >= 2.3.4
BuildRequires: python >= 2.3.4
BuildRequires: sudo
BuildRequires: gcc-c++ 
BuildRequires: gcc 
BuildRequires: subversion
BuildRequires: zlib-devel
BuildRequires: make
BuildRequires: patch
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
BuildRequires: mysql-devel >= 5.0.22 
BuildRequires: mysql >= 5.0.22 
Requires: mysql-server >= 5.0.22 
Requires: mysql >= 5.0.22 
Requires: python >= 2.3.4 
Requires: net-snmp 
Requires: sudo 

# ART
Requires: perl-Crypt-DES perl-Net-SNMP


# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
%description
Zenoss is an IT infrastructure monitoring product that allows you to
monitor your entire infrastructure within a single, integrated
software application.

Key features include:
  * Monitors the entire stack
    o networks, servers, applications, services, power, environment, etc...
  * Monitors across all perspectives
    o discovery, configuration, availability, performance, events, alerts, etc.
  * Affordable and easy to use
    o unlike the big suites offered by IBM, HP, BMC, CA, etc...
    o unlike first generation open source tools...
  * Complete open source package
    o complete solution available as free, open source software


# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
%prep


# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
%setup


# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
%build
# clean up the old build if it is still laying around
rm -rf ${RPM_BUILD_ROOT}

# map environment variables to .spec variables
export ZENHOME=%{zenhome}
export ZOPEPASSWORD=%{zope_passwd}
export PYTHON=/usr/bin/python
export DESTDIR=${RPM_BUILD_ROOT}

# load up the build functions
ZEN_BUILD_DIR=${RPM_BUILD_DIR}/%{name}-%{version}
. ${ZEN_BUILD_DIR}/build-functions.sh

# set up make and the python path
set_make

# set the SVNTAG variable and honor the trunk flag
if [ "%{trunk}" = "1" ]; then
    SVNTAG="trunk"
else
    SVNTAG="tags/%{name}-%{version}"
fi

# compile external libs and pull down zenoss
compile

# remove the nagios-plugins perl scripts that force ugly deps
DIR=${RPM_BUILD_ROOT}/${ZENHOME}/libexec
DIRNAME=${RPM_BUILD_ROOT}/${ZENHOME}/libexec

for file in \
  `grep perl $DIRNAME/* | awk '{print $1}' | cut -d: -f1 | sort -u`
do
  rm -f $file
done


# remove the CM artifacts
ZEN_INST_DIR=${RPM_BUILD_ROOT}/${ZENHOME}
find ${ZEN_INST_DIR} -name .svn | xargs rm -rf

# remove the .conf files but leave the .conf.examples
rm ${ZEN_INST_DIR}/etc/*.conf

# copy some configuration files to the /etc installation directory
ZEN_BUILD_DIR=${RPM_BUILD_DIR}/%{name}-%{version}
CONF_DIR=${ZEN_BUILD_DIR}/conf
mkdir -p ${ZEN_INST_DIR}/etc
cp ${CONF_DIR}/snmpd.conf ${ZEN_INST_DIR}/etc
cp ${CONF_DIR}/sudoers ${ZEN_INST_DIR}/etc
mkdir ${ZEN_INST_DIR}/log

# land the zenctl into the init.d directory as "zenoss"
INIT_SCRIPT_DIR=${RPM_BUILD_ROOT}/etc/rc.d/init.d
START_SCRIPT=${INIT_SCRIPT_DIR}/zenoss
INIT_PRE_SCRIPT=${ZEN_INST_DIR}/bin/zenoss_init_pre
INIT_POST_SCRIPT=${ZEN_INST_DIR}/bin/zenoss_init_post
UPGRADE_PRE_SCRIPT=${ZEN_INST_DIR}/bin/zenoss_upgrade_pre
UPGRADE_POST_SCRIPT=${ZEN_INST_DIR}/bin/zenoss_upgrade_post
mkdir -p ${INIT_SCRIPT_DIR}
cp ${RPM_BUILD_ROOT}/%{zenhome}/bin/zenctl ${START_SCRIPT}

for file in \
  ${START_SCRIPT} \
  ${INIT_PRE_SCRIPT} \
  ${INIT_POST_SCRIPT} \
  ${UPGRADE_PRE_SCRIPT} \
  ${UPGRADE_POST_SCRIPT}
do
    replace "**OS_USERNAME**" "%{os_username}" -- ${file}
    replace "**ZENHOME**" "%{zenhome}" -- ${file}
    replace "**MYSQL_USERNAME**" "%{mysql_username}" -- ${file}
    replace "**MYSQL_PASSWD**" "%{mysql_passwd}" -- ${file}
    replace "**MYSQL_DATABASE**" "%{mysql_database}" -- ${file}
    replace "**ZOPE_PASSWD**" "%{zope_passwd}" -- ${file}
    replace "**SNMPD_CONF**" "%{snmpd_conf}" -- ${file}
    replace "**MY_CNF**" "%{my_cnf}" -- ${file}
    replace "**SUDOERS**" "%{sudoers}" -- ${file}
done

# copy the [install,shared]-functions because they are used by zenoss_init
cp ${ZEN_BUILD_DIR}/shared-functions.sh ${ZEN_INST_DIR}/bin
cp ${ZEN_BUILD_DIR}/install-functions.sh ${ZEN_INST_DIR}/bin

# copy filesystem scripts to the real filesystem
#cd ${ZEN_BUILD_DIR}/extras
#for file in \
#    `find . -type f | grep -v .svn`
#do
#    DEST=${RPM_BUILD_ROOT}/etc/${file}
#    DIR=`dirname $DEST`
#    if [ ! -d ${DIR} ]; then
#        mkdir -p ${DIR}
#    fi
#
#    cp ${file} ${DEST}
#done

# update ZENHOME in the /etc/logrotate.d scripts
#LOGROTATE_DIR=${RPM_BUILD_ROOT}/etc/logrotate.d
#for file in \
#    `ls ${LOGROTATE_DIR}`
#do
#    replace "**ZENHOME**" "%{zenhome}" -- ${LOGROTATE_DIR}/${file}
#done


# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
%install


# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
%files
%defattr(-,root,root)
%doc %{zenhome}/Products/LICENSE.txt
%doc %{zenhome}/Products/COPYRIGHT.txt
%{zenhome}
/etc


# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
%pre
export ZENHOME=%{zenhome}
if [ $1 -eq 2 ]; then
    TEMP_DIR=/tmp/zenossrpmupgrade/$$
    mkdir -p $TEMP_DIR
    cp ${ZENHOME}/etc/*.conf $TEMP_DIR
    echo $TEMP_DIR > /tmp/zenossrpmupgrade/dirname.txt
fi


# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
%post
# load up the install functions
export ZENHOME=%{zenhome}
. ${ZENHOME}/bin/shared-functions.sh
. ${ZENHOME}/bin/install-functions.sh

# kill off any zeo/zope/zen processes that are running
kill_running

# run these commands when installing, but not upgrading or reinstalling
if [ $1 -eq 1 ]; then
    # set environment variables
    export OS_USERNAME=%{os_username}
    export OS_HOME=%{os_home}
    export OS_SHELL=%{os_shell}

    # create the zenoss user
    add_user

    # set up symlinks for all the runlevels
    for runlevel in \
        1 2 3 4 5 6
    do
        ln -sf ../init.d/zenoss /etc/rc.d/rc${runlevel}.d/%{S_zenoss}
    done

    # test if we need to set ZENHOME in the user's .bashrc
    LOGINZENHOME=`grep ZENHOME %{os_home}`
    if [ -z "${LOGINZENHOME}" ]; then
        echo "export ZENHOME=%{zenhome}" >> %{os_home}/.bashrc 
        echo "export PATH=${ZENHOME}/bin:${PATH}" >> %{os_home}/.bashrc
        echo "export PYTHONPATH=%{zenhome}/lib/python" >> %{os_home}/.bashrc
        chown %{os_username} %{os_home}/.bashrc
    fi

    # set the flag to identify this as a fresh install (rather than an
    # upgrade)
    touch ${ZENHOME}/.fresh_install
fi

# run these commands when upgrading (but not installing or reinstalling)
if [ $1 -eq 2 ]; then
    # set the flag to identify this as an upgrade (rather than a fresh
    # install)
    touch ${ZENHOME}/.upgraded
fi

# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
%preun
# load up the install functions
. %{zenhome}/bin/shared-functions.sh
. %{zenhome}/bin/install-functions.sh

# kill off any zeo/zope/zen processes that are running
kill_running


# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
%postun
# called when we are erasing the package (but not upgrading)
if [ $1 -eq 0 ]; then
    # restore sudoers and snmpd.conf
    cp %{sudoers}.orig %{sudoers}
    cp %{snmpd_conf}.orig %{snmpd_conf}
    cp %{my_cnf}.orig %{my_cnf}

    # remove shell account 
    /usr/sbin/userdel %{os_username}

    # clean up the various files written by the application
    for target in \
        %{zenhome}/Products \
        %{zenhome}/log \
        %{zenhome}/.fresh_install \
        %{zenhome}/.upgraded
    do
        rm -rf ${target}
    done
fi


# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
%clean
#rm -rf ${RPM_BUILD_ROOT}