#!/bin/sh
#
# Copyright (c) 2008--2012 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public License,
# version 2 (GPLv2). There is NO WARRANTY for this software, express or
# implied, including the implied warranties of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
# along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
#
# Red Hat trademarks are not licensed under GPLv2. No permission is
# granted to use or replicate Red Hat trademarks that are incorporated
# in this software or its documentation.
#

if [ $EUID -ne 0 ]; then
   echo -e "This script must be run as root!\n"
   exit 1
fi



spacewalk_target_services() {
    FEXISTS=""
    for t in /usr/lib/systemd/system/*.service; do
        FEXISTS="$FEXISTS $(basename $t)"
    done
    awk -F= 'BEGIN {split("'"$EXCLUDE"'", keys, / +/);
                    split("'"$FEXISTS"'", srvs, / +/);
                    for (i in keys) {exclude[keys[i]]};
                    for (j in srvs) {fexists[srvs[j]]}
                   }
             /Requires=/ {if (!($2 in exclude)) {print $2}}
             /Wants=/ {if (($2 in fexists) && !($2 in exclude)) {print $2}}' \
             /usr/lib/systemd/system/spacewalk.target
}

turn_on() {
   echo "Enabling spacewalk services..."
   spacewalk_target_services | xargs systemctl enable --quiet
   systemctl enable --quiet spacewalk.target
   echo "Done"
   return 0
}

turn_off() {
   echo "Disabling spacewalk services..."
   spacewalk_target_services | xargs systemctl disable --quiet
   systemctl disable --quiet spacewalk.target
   echo "Done"
   return 0
}

list() {
   echo "Listing spacewalk services..."
   systemctl list-unit-files --type=service | grep -F "$(spacewalk_target_services)"
   echo "Done"
   return 0
}

start() {
    echo "Starting spacewalk services..."
    DISABLE_FILE=/run/spacewalk-wait-for-tomcat-disable
    if [ "$WAIT_FOR_TOMCAT" == "1" ] ; then
        rm -f $DISABLE_FILE
    else
        touch $DISABLE_FILE
    fi
    echo "  Running DB schema upgrade. This may take a while."
    echo "  Call the following command to see progress: journalctl -f -u uyuni-check-database.service"
    if grep -E -m1 "^db_host[[:space:]]*=[[:space:]]*localhost" /etc/rhn/rhn.conf >/dev/null && \
       grep "md5" /var/lib/pgsql/data/pg_hba.conf >/dev/null; then
         echo
         echo "INFO: Migrating password encryption mechanism to scram-sha-256"
    fi
    MSG1=$(systemctl start spacewalk.target 2>&1) || {
        MSG2=$(systemctl status uyuni-check-database.service)
        if [ $? -ne 0 ]; then
            echo -e "$MSG2"
        else
            echo -e "$MSG1"
        fi
        echo "FAILED"
        return 1
    }
    if systemctl is-enabled osa-dispatcher > /dev/null 2>&1; then
        systemctl start jabberd
        /usr/sbin/spacewalk-startup-helper wait-for-jabberd
        systemctl start osa-dispatcher
    fi
    rm -f $DISABLE_FILE
    echo "Done."
    return 0
}

stop() {
    echo "Shutting down spacewalk services..."
    if systemctl is-enabled osa-dispatcher > /dev/null 2>&1; then
        systemctl stop jabberd
    fi
    spacewalk_target_services | xargs systemctl stop
    echo "Done."
    return 0
}

status() {
    spacewalk_target_services | xargs systemctl status --no-pager -n0
    systemctl status -n0 spacewalk.target
    systemctl status -n0 jabberd.service
    systemctl status -n0 osa-dispatcher.service
    return $?
}

OPTS=$(getopt --longoptions=exclude:,level:,no-wait-for-tomcat -n ${0##*/} -- e:l:T "$@")

if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

eval set -- "$OPTS"

WAIT_FOR_TOMCAT=1
while true ; do
    case "$1" in
        -e|--exclude)
            EXCLUDE=$2
            shift
            ;;
        -l|--level)
            LEVEL="--level $2"
            shift
            ;;
        -T|--no-wait-for-tomcat)
            WAIT_FOR_TOMCAT=0
            ;;
        --)
            shift
            break
            ;;
        *)
            echo "Internal error [$1]!" >&2
            exit 1
            ;;
    esac
    shift
done

case "$1" in
    start)
	start
        ;;
    stop)
	stop
        ;;
    enable)
        turn_on $LEVEL
        ;;
    disable)
        turn_off $LEVEL
        ;;
    list)
        list
        ;;
    status)
	status
        ;;
    restart|reload)
        stop

        /usr/sbin/spacewalk-startup-helper ensure-httpd-down

        start
        ;;
    *)
        echo "Usage: $(basename $0) {start|stop|status|reload|restart|enable|disable}"
        exit 1
        ;;
esac
exit $?
