#!/bin/csh
# Copyright (c) 2006 Autodesk Canada Inc. / Autodesk, Inc. All Rights Reserved.
#
# These coded instructions, statements, and computer programs contain
# unpublished proprietary information written by Autodesk and
# are protected by Federal copyright law. They may not be disclosed
# to third parties or copied or duplicated in any form, in whole or
# in part, without the prior written consent of Autodesk.
#
#

#
# This script is used to configure Backburner webmonitor for Apache on Linux
#

if ( "`whoami`" != "root" ) then
   echo "You must be root to run $0"
   exit 1
endif

set OSNAME = `uname | tr "[:lower:]" "[:upper:]" | tr -d "[:digit:]"`

if ( ${OSNAME} != "LINUX" ) then
   echo "Script supported on Linux only"
   exit 1
endif

set CHKCONFIG = "/sbin/chkconfig"
set CONFIGON = "reset"
set ISCONFIG = "/sbin/chkconfig --list"
set BB_ROOT = /usr/discreet/backburner
set BB_ROOT_WEBMON = ${BB_ROOT}/WebMonitor
set HTTPD = /etc/httpd
set HTTPD_AUTH = ${HTTPD}/auth
set HTTPD_CONF = ${HTTPD}/conf
set WWW = /var/www
set WWW_CGI = ${WWW}/cgi-bin
set WWW_HTML = ${WWW}/html

# Update cgi executables
rm -f ${WWW_CGI}/monitorCGI.cgi
rm -f ${WWW_CGI}/monitorCGI-auth.cgi
cp -f ${BB_ROOT_WEBMON}/monitorCGI.cgi ${WWW_CGI}/monitorCGI.cgi

# Adjust cgi owner & permissions
chown root.root ${WWW_CGI}/monitorCGI.cgi
chmod 4755 ${WWW_CGI}/monitorCGI.cgi

# Update html code
rm -rf ${WWW_HTML}/backburner
cp -prf ${BB_ROOT_WEBMON}/backburner_html ${WWW_HTML}
mv -f ${WWW_HTML}/backburner_html ${WWW_HTML}/backburner

# Install config file if not there
if ( ! -e ${WWW_CGI}/monitorCGI.cfg ) then
    cp -f ${BB_ROOT_WEBMON}/monitorCGI.cfg ${WWW_CGI}
    chown root.root ${WWW_CGI}/monitorCGI.cfg
endif

# Install password file if not there
if ( ! -e ${HTTPD_AUTH}/backburner.auth ) then
    mkdir -p ${HTTPD_AUTH}
    cp -f ${BB_ROOT_WEBMON}/backburner.auth ${HTTPD_AUTH}/backburner.auth
    chown root.root ${HTTPD_AUTH}/backburner.auth 
endif

# Check if the backburner authentication is already configured in httpd
grep -q monitorCGI.cgi ${HTTPD_CONF}/httpd.conf

# Append the backburner httpd configuration section to the httpd
# configuration file if not present
if ( $status == 1 ) then
    cat ${BB_ROOT_WEBMON}/webmonitor_httpd_conf >> ${HTTPD_CONF}/httpd.conf

    # httpd config changed - web server must be restarted
    echo "Restarting httpd server ..."
    /etc/init.d/httpd restart
endif

# Verify that the manager is set to run on the machine.  Turn it on or edit the config file to specify the manager
${ISCONFIG} backburner_manager >& /dev/null
set CHKCONFIG_MGR = $status

if ( $CHKCONFIG_MGR != 0 ) then
    set response=
    while ($response != "y" && $response != "n" && $response != "Y" && $response != "N")
        echo "The Backburner Manager is not set to run locally.  Do you wish to turn it on? (y/n)"
        set response=$<

        if ($response == "y" || $response == "Y") then
            $CHKCONFIG backburner_manager $CONFIGON

            # Restart BackBurner
            echo "Restarting Backburner ..."
            /etc/init.d/backburner restart
        endif
    end
endif

echo "Backburner WebMonitor configuration completed"
