#! /usr/bin/env bash

#
# zenoss_upgrade_pre
#
# This script is intended to be run before the zenoss processes have
# been started for the first time after an upgrade.
#
# Note: it is run by root
#

#############################################################################
# This program is part of Zenoss Core, an open source monitoring platform.
# Copyright (C) 2007, Zenoss Inc.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
#
# For complete information please visit: http://www.zenoss.com/oss/
#############################################################################
# environment variables
export OS_USERNAME="**OS_USERNAME**"
export OS_UID="**OS_UID**"
export ZENHOME="**ZENHOME**"
export MYSQLHOST="**MYSQL_HOST**"
export MYSQLPORT="**MYSQL_PORT**"
export MYSQLROOTUSER="**MYSQL_ROOT_USERNAME**"
export MYSQLROOTPASSWD="**MYSQL_ROOT_PASSWD**"
export MYSQLUSER="**MYSQL_USERNAME**"
export MYSQLPASS="**MYSQL_PASSWD**"
export MYSQLEVENTDB="**MYSQL_DATABASE**"
export ZOPEPASSWORD="**ZOPE_PASSWD**"
export ZOPEHOME="**ZOPEHOME**"
export PYTHON="**PYTHON**"

# load the installation functions
. ${ZENHOME}/bin/shared-functions.sh
. ${ZENHOME}/bin/install-functions.sh


# Test for the pre upgrade zenpack, this should have been removed
# as a part of the 3.x upgrade but if it is present it will cause an error
# on migration, so we need to make sure it is removed.
if grep -q PreUpgrade30 ${ZENHOME}/ZenPacks/easy-install.pth 2>/dev/null; then
    cat <<EOF
    Please remove the PreUpgrade zenpack before continuing.
    You can remove the zenpack with the following command:

EOF

    echo "   " ${ZENHOME} "/bin/zenpack --remove ZenPacks.zenoss.PreUpgrade30 "
    exit 1
fi

# copy the etc/*.conf.example files to etc/*.conf if etc/*.conf don't
# already exist (we don't want to clobber people's configs)
update_conf_files

# version 2.1.1 and below used the system python, but in 2.1.70 we
# started shipping our own.  replace the old scripts with references
# to the new python
/usr/bin/replace /usr/bin/python $ZENHOME/bin/python -- $ZENHOME/bin/* 2>/dev/null
/usr/bin/replace /usr/bin/python $ZENHOME/bin/python -- $ZENHOME/etc/zeo.conf

/usr/bin/replace "default-zpublisher-encoding latin-1" \
                 "default-zpublisher-encoding utf-8" -- \
    $ZENHOME/etc/zope.conf

# set the python shebang to be the proper python
shebang

# has run_mkzeoinstance been run?  need to do this especially when
# going from Zope 2.11 -> 2.12 since that is the mechanism for generating
# the required non-easy_install versions of zeoctl and runzeo.
#
if [ -f "$ZENHOME/bin/zeoctl" ] ;then
    if grep EASY-INSTALL-ENTRY-SCRIPT $ZENHOME/bin/zeoctl $ZENHOME/bin/runzeo 2>/dev/null 1>&2;then
        run_mkzeoinstance
    fi
fi

# patch any ZenPack code that isn't compatible with this version of core
. ${ZENHOME}/bin/zenoss_upgrade_patch_zenpacks.sh

# migrate the zeo database
start_zeodb
run_zenmigrate
stop_zeodb
