#!/bin/sh
## +----------------------------------------------------------------------+
## | NEXCESS.NET InterWorx                                                |
## +----------------------------------------------------------------------+
## | Copyright (c) 2000-2003 NEXCESS.NET L.L.C., All Rights Reserved.     |
## +----------------------------------------------------------------------+
## | Redistribution and use in source form, with or without modification  |
## | is NOT permitted without consent from the copyright holder.          |
## |                                                                      |
## | THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND |
## | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,    |
## | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A          |
## | PARTICULAR PURPOSE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,    |
## | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  |
## | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR   |
## | PROFITS; OF BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY  |
## | OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT         |
## | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE    |
## | USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH     |
## | DAMAGE.                                                              |
## +----------------------------------------------------------------------+
## | Authors: Chris Wells <clwells@nexcess.net>                           |
## |          Paul Oehler <poehler@nexcess.net>                           |
## +----------------------------------------------------------------------+
##
## $Id: iworx,v 1.10 2009/06/30 22:54:19 poehler Exp $

#
# Startup script for the InterWorx Server Management Suite
#
# chkconfig: 345 24 86
# description: InterWorx
# processname: iworx

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

IWORXHOME='/home/interworx'
IWORXUSER=iworx
IWORXGROUP=iworx

start_apache() {
        $IWORXHOME/bin/iworx-web -f $IWORXHOME/etc/httpd/httpd.conf -DSSL
        ret=$?
        if [ $ret -eq 0 ]; then
            action "Starting InterWorx-web: " /bin/true
        else
            action "Starting InterWorx-web: " /bin/false
        fi
        touch $IWORXHOME/var/lock/subsys/httpd
}

stop_apache() {
	if [ -f $IWORXHOME/var/run/httpd.pid ] ; then
	    httpd_pid=`cat $IWORXHOME/var/run/httpd.pid`
	    kill $httpd_pid
        fi
        ret=$?
        if [ $ret -eq 0 ]; then
            action "Stopping InterWorx-web: " /bin/true
        else
            action "Stopping InterWorx-web: " /bin/false
        fi
        [ $ret -eq 0 ] && rm -f $IWORXHOME/var/lock/subsys/httpd
        return $ret
}

restart_apache_graceful() {
        /usr/bin/killall -USR1 iworx-web
        ret=$?
        if [ $ret -eq 0 ]; then
            action "Gracefully restarting iworx-web: " /bin/true
        else
            action "Gracefully restarting iworx-web: " /bin/false;
            echo "Starting iworx-web manually ";
            start_apache;
        fi
}

start_mysql() {
        if [ ! -d $IWORXHOME/var/lib/mysql ] ; then
	    if [ -f $IWORXHOME/bin/mysql/mysql_install_db ]; then
		action "Initializing InterWorx database" $IWORXHOME/bin/mysql/mysql_install_db --defaults-file=$IWORXHOME/etc/my.cnf --force 2>&1 >/dev/null
	    else
		action "Initializing InterWorx database" mysql_install_db --defaults-file=$IWORXHOME/etc/my.cnf --force  2>&1 >/dev/null
	    fi
            ret=$?
            chown -R $IWORXUSER.$IWORXGROUP $IWORXHOME/var/lib/mysql
            if [ $ret -ne 0 ] ; then
                return $ret
            fi
        fi

	if [ -f $IWORXHOME/bin/mysql/mysqld_safe ]; then
	    $IWORXHOME/bin/mysql/mysqld_safe --defaults-file=$IWORXHOME/etc/my.cnf > /dev/null 2>&1 &
	else
	    safe_mysqld --defaults-file=$IWORXHOME/etc/my.cnf > /dev/null 2>&1 &
	fi
        ret=$?
        if [ $ret -eq 0 ]; then
            action "Starting InterWorx-db: " /bin/true
        else
            action "Starting InterWorx-db: " /bin/false
        fi
        [ $ret -eq 0 ] && touch $IWORXHOME/var/lock/subsys/mysqld
        return $ret
}

stop_mysql() {
	if [ -f $IWORXHOME/var/run/mysqld.pid ] ; then
	    mysqld_pid=`cat $IWORXHOME/var/run/mysqld.pid`
	    kill $mysqld_pid
        fi
        ret=$?
        if [ $ret -eq 0 ]; then
            action "Stopping InterWorx-db: " /bin/true
        else
            action "Stopping InterWorx-db: " /bin/false
        fi
        [ $ret -eq 0 ] && rm -f $IWORXHOME/var/lock/subsys/mysqld
        [ $ret -eq 0 ] && rm -f $IWORXHOME/var/run/mysql.sock
        return $ret
}

bind_ips() {
    if [ -f $IWORXHOME/bin/ip.pex ]; then
	$IWORXHOME/bin/ip.pex --bind-aliases
	
        ret=$?
        if [ $ret -eq 0 ]; then
            action "Binding IP Aliases: " /bin/true
        else
            action "Binding IP Aliases: " /bin/false
        fi	
    fi
}

case "$1" in
  startweb)
	start_apache
        ;;
  stopweb)
	stop_apache
        ;;
  startdb)
	start_mysql
        ;;
  stopdb)
	stop_mysql
        ;;
  restartweb)
        $0 stopweb
        $0 stopweb
        sleep 3
        $0 startweb
        ;;
  restartdb)
        $0 stopdb
        sleep 3
        $0 startdb
        ;;
  start)
	start_mysql
	start_apache
	bind_ips
        ;;
  bind_ips)
	bind_ips
	;;
  stop)
	stop_apache
	stop_mysql
        ;;
  restart)
        $0 stop
        sleep 3
        $0 stop > /dev/null 2>&1
        sleep 1
        $0 start
        ;;
  graceful)
    restart_apache_graceful
        ;;
esac

exit 0
