#!/bin/bash
# TODO: Obsolete file not used any more - double check and remove
# condor   This is the glideinWMS factory startup script
# chkconfig: 35 90 30
# description: Starts and stops a glideinWMS factory

# SPDX-FileCopyrightText: 2009 Fermi Research Alliance, LLC
# SPDX-License-Identifier: Apache-2.0

# Emulate function library.
success() {
 echo -en "\033[60G[[32mOK[0m]"
 return 0
}

failure() {
 echo -en "\033[60G[[31mFAILED[0m]"
 return 1
}

[ -f /etc/sysconfig/gwms-factory ] && . /etc/sysconfig/gwms-factory

export HOME="/var/lib/gwms-factory"
export X509_CERT_DIR="/etc/grid-security/certificates"
factory_dir='/var/lib/gwms-factory/work-dir'
factory_config='/etc/gwms-factory/glideinWMS.xml'
export FACTORY_USER=gfactory

start() {
        cwd=`pwd`
        cd $factory_dir
        echo -n "Starting glideinWMS factory: "
        su -s /bin/bash $FACTORY_USER -c "nice -2 glideFactory.py $factory_dir 2>/dev/null 1>&2 </dev/null &"
        sleep 5
        "checkFactory.py" "$factory_dir"  2>/dev/null 1>&2 </dev/null && success || failure
        RETVAL=$?
        if [ -n "$cwd" ]; then
           cd $cwd
        fi
        echo
}

stop() {
        echo -n "Shutting down glideinWMS factory: "
        "stopFactory.py" "$factory_dir" 2>/dev/null 1>&2 </dev/null && success || failure
        RETVAL=$?
        echo
}

restart() {
        stop
        if [ $RETVAL -ne 0 ]; then
          exit $RETVAL
        fi
        start
}

reconfig() {
        if [ -f "$factory_config" ]; then
           has_arg=1
        else
           has_arg=0
           echo $"Usage: factory_startup reconfig <fname> <writeback yes|no>"
           echo "ERROR: configuration file does not exist: $factory_config"
           exit 1
        fi
        "checkFactory.py" "$factory_dir" >/dev/null 2>&1 </dev/null
        notrun=$?
        if [ $notrun -eq 0 ]; then
          stop
          if [ $RETVAL -ne 0 ]; then
            exit $RETVAL
          fi
        fi
	writeback="no"
	force_delete=""
        fix_rrd=""
        for var in "$@"
        do
           case "$var" in
              yes | no) writeback="$var"
                 ;;
              "-force_delete") force_delete="-force_delete"
                 ;;
              "-fix_rrd") fix_rrd="-fix_rrd"
                 ;;
           esac
	done
	if [ -n "$GLIDEIN_WRITEBACK" ]; then
           writeback="$GLIDEIN_WRITEBACK"
        fi
        pushd $factory_dir
        su -s /bin/bash $FACTORY_USER -c "reconfig_glidein -update_scripts no -xml $factory_config -writeback $writeback $fix_rrd $force_delete"
        reconfig_failed=$?
        echo -n "Reconfiguring the factory"
        test $reconfig_failed -eq 0 && success || failure
        RETVAL=$?
        echo
        if [ $notrun -eq 0 ]; then
          if [ $reconfig_failed -ne 0 ];then
            echo ".. starting factory with old configuration file"
          fi
          start
          if [ $RETVAL -eq 0 ] && [ $reconfig_failed -eq 0 ]; then
            RETVAL=0
          else
            RETVAL=1
          fi
        fi
}

upgrade() {
        if [ -f "$factory_config" ]; then
           has_arg=1
        else
           has_arg=0
           echo $"Usage: factory_startup update <fname>"
           echo "ERROR: configuration file does not exist: $factory_config"
           exit 1
        fi
        "checkFactory.py" "$factory_dir" >/dev/null 2>&1 </dev/null
        notrun=$?
        if [ $notrun -eq 0 ]; then
          stop
          if [ $RETVAL -ne 0 ]; then
            exit $RETVAL
          fi
        fi
        writeback="no"
        force_delete=""
        fix_rrd=""
        for var in "$@"
        do
           case "$var" in
              yes | no) writeback="$var"
                 ;;
              "-force_delete") force_delete="-force_delete"
                 ;;
              "-fix_rrd") fix_rrd="-fix_rrd"
                 ;;
           esac
        done

        pushd $factory_dir
        su -s /bin/bash $FACTORY_USER -c "reconfig_glidein -update_scripts yes -xml $factory_config -writeback $writeback $fix_rrd $force_delete"
        reconfig_failed=$?
        echo -n "Upgrading the factory"
        test $reconfig_failed -eq 0 && success || failure
        RETVAL=$?
        echo
        if [ $notrun -eq 0 ]; then
          if [ $reconfig_failed -ne 0 ];then
            echo ".. starting factory with old configuration file"
          fi
          start
          if [ $RETVAL -eq 0 ] && [ $reconfig_failed -eq 0 ]; then
            RETVAL=0
          else
            RETVAL=1
          fi
        fi
}

downtime() {
       if [ -z "$3" ]; then
           echo $"Usage: factory_startup $1 -entry 'factory'|'entries'|entry_name [-delay delay] [-frontend sec_name|'All'] [-security sec_class|'All'] [-comment comment]"
           exit 1
       fi

	 if [ "$1" == "down" ]; then
	   echo -n "Setting downtime..."
	 elif [ "$1" == "up" ]; then
	   echo -n "Removing downtime..."
	 else
	   echo -n "Infosys-based downtime management."
	 fi

	 "manageFactoryDowntimes.py" -cmd $1 -dir "$factory_dir" "$@" 2>/dev/null 1>&2 </dev/null && success || failure
	 RETVAL=$?
	 echo
}

case $1 in
        start)
            start
        ;;
        stop)
            stop
        ;;
        restart)
            restart
        ;;
        status)
            "checkFactory.py" "$factory_dir"
            RETVAL=$?
        ;;
        info)
            shift
            "info_glidein" $@ "/etc/gwms-factory/glideinWMS.xml"
            RETVAL=$?
        ;;
        reconfig)
            reconfig "$@"
        ;;
        upgrade)
            upgrade $2
        ;;
        down)
            downtime down "$@"
        ;;
        up)
            downtime up "$@"
        ;;
        infosysdown)
            downtime ress+bdii entries "$@"
        ;;
        statusdown)
            if [ -z "$2" ]; then
                echo $"Usage: factory_startup $1 -entry 'factory'|'entries'|entry_name [-delay delay]"
                exit 1
            fi
            "manageFactoryDowntimes.py" -cmd check -dir "$factory_dir" "$@"
            RETVAL=$?
        ;;
        *)
        echo $"Usage: factory_startup {start|stop|restart|status|info|reconfig|upgrade|down|up|infosysdown|statusdown}"
        exit 1
esac

exit $RETVAL
