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

# 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-frontend ] && . /etc/sysconfig/gwms-frontend

frontend_root_dir='/var/lib/gwms-frontend'
frontend_dir="$frontend_root_dir/vofrontend"
glideinWMS_dir=$frontend_dir
frontend_config=/etc/gwms-frontend/frontend.xml
export HOME="/var/lib/gwms-frontend"
export FRONTEND_USER=frontend

if [ -s ${frontend_config} ]
then
    frontend_name=`grep "^<frontend" $frontend_config  | sed 's/ /\n/g' | grep ^frontend_name | awk 'BEGIN { FS = "\""} ; { print $2
 }'`
fi

if [ -z ${frontend_name} ] && [ -d ${frontend_dir} ]
then
    frontend_name=`awk '/^FrontendName /{print $2}' $frontend_dir/frontend.descript`
fi

if [ -z ${frontend_name} ]
then
    echo 'Cannot determine frontend name!'
    failure
fi
id_str="$frontend_name"

check_idtoken_password() {
    # Make sure that the IDTOKEN password exists
    if [ ! -f "$frontend_root_dir"/passwords.d/FRONTEND ]; then
        local htc_frontend_password=/etc/condor/passwords.d/FRONTEND
        if [ ! -f "$htc_frontend_password" ]; then
            openssl rand -base64 64 | /usr/sbin/condor_store_cred -u "frontend@$(hostname -f)" -f "$htc_frontend_password" add > /dev/null 2>&1
        fi
        /bin/cp "$htc_frontend_password" "$frontend_root_dir"/passwords.d/FRONTEND
        chown $FRONTEND_USER: "$frontend_root_dir"/passwords.d/FRONTEND
        if [ ! -f "$frontend_root_dir"/passwords.d/FRONTEND ]; then
            echo 'Cannot create IDTOKENs password!'
            failure
        fi
    fi
}

start() {
    check_idtoken_password
    echo -n "Starting glideinWMS frontend $id_str: "
    su -s /bin/bash $FRONTEND_USER -c "nice -2 \"glideinFrontend\" \"$frontend_dir\"" 2>/var/log/gwms-frontend/frontend/startup.log 1>&2 </dev/null &
    sleep 5
    "checkFrontend" "$frontend_dir"  2>/dev/null 1>&2 </dev/null && success || failure
    RETVAL=$?
    echo
}

stop() {
    echo -n "Shutting down glideinWMS frontend $id_str: "
    "stopFrontend" "$frontend_dir" 2>/dev/null 1>&2 </dev/null && success || failure
    RETVAL=$?
    echo
}

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

reconfig() {
    "checkFrontend" "$frontend_dir" >/dev/null 2>&1 </dev/null
    notrun=$?
    if [ $notrun -eq 0 ]; then
      stop
      if [ $RETVAL -ne 0 ]; then
        exit $RETVAL
      fi
    fi
    pushd $frontend_dir
    su -s /bin/bash $FRONTEND_USER -c "reconfig_frontend -force_name \"$frontend_name\" -update_scripts \"no\" -xml $frontend_config"
  RETVAL=$?
    reconfig_failed=$?
    echo -n "Reconfiguring the frontend"
    test $reconfig_failed -eq 0 && success || failure
    echo
    if [ $notrun -eq 0 ]; then
      start
    fi
}

upgrade() {
    "checkFrontend" "$frontend_dir" >/dev/null 2>&1 </dev/null
    notrun=$?
    if [ $notrun -eq 0 ]; then
      stop
      if [ $RETVAL -ne 0 ]; then
        exit $RETVAL
      fi
    fi
    pushd $frontend_dir
    su -s /bin/bash $FRONTEND_USER -c "reconfig_frontend -force_name \"$frontend_name\" -writeback \"yes\" -update_scripts \"yes\" -xml $frontend_config"
    reconfig_failed=$?
    echo -n "Reconfiguring the frontend"
    test $reconfig_failed -eq 0 && success || failure
      RETVAL=$?
    echo
    if [ $notrun -eq 0 ]; then
      start
    fi
}

downtime() {
    if [ -z "$2" ]; then
        echo $"Usage: frontend_startup $1 'frontend'|'entries'|entry_name [delay]"
        exit 1
    fi

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

    if [ "$2" == "frontend" ]; then
       echo -n " frontend:"
       else
       echo -n " entry $2:"
    fi

    "manageFrontendDowntimes" "$frontend_dir" $2 $1 $3 2>/dev/null 1>&2 </dev/null && success || failure
    RETVAL=$?
    echo
}

case $1 in
    start)
            start
    ;;
    stop)
            stop
    ;;
    restart)
            restart
    ;;
    status)
           "checkFrontend" "$frontend_dir"
         RETVAL=$?
    ;;
    reconfig)
            reconfig "$@"
    ;;
    upgrade)
            upgrade $2
    ;;
    *)
    echo $"Usage: frontend_startup {start|stop|restart|status|reconfig}"
    exit 1
esac

exit $RETVAL
