#!/bin/bash

## -------------------------------------------------------------------
##
## stanchion: Stanchion launcher
##
## Copyright (c) 2014 Basho Technologies, Inc.,
##               2021 TI Tokyo.  All Rights Reserved.
##
## This file is provided to you under the Apache License,
## Version 2.0 (the "License"); you may not use this file
## except in compliance with the License.  You may obtain
## a copy of the License at
##
##   http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing,
## software distributed under the License is distributed on an
## "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
## KIND, either express or implied.  See the License for the
## specific language governing permissions and limitations
## under the License.
##
## -------------------------------------------------------------------

STN_USER=stanchion
RUNNER_GEN_DIR="${RUNNER_GEN_DIR:-/var/lib/stanchion}"
RELEASE_ROOT_DIR="${RELEASE_ROOT_DIR:-/usr/lib64/stanchion}"
PID_DIR=/run/stanchion/
COMMAND=/usr/lib64/stanchion/bin/stanchion
DEBUG_COMMAND=/usr/sbin/stanchion-debug
RUNNER_LOG_DIR=/var/log/stanchion
RELX_CONFIG_PATH=${RUNNER_GEN_DIR}/sys.config
VMARGS_PATH=${RUNNER_GEN_DIR}/vm.args
PIPE_DIR=/tmp/stanchion_pipes/

mkdir -p $PIPE_DIR
chown $STN_USER:$STN_USER $PIPE_DIR

# On first running the sys.config and vm.args will not be a link
# as cfconfig has not yet been run as a pre_start hook.  If there's no
# link use the default for now
if [ ! -L $RELX_CONFIG_PATH ]; then
    cat >$RELX_CONFIG_PATH <<EOF
[].
EOF
fi
if [ ! -L $VMARGS_PATH ]; then
    cat >$VMARGS_PATH <<EOF
-sname stanchion
-setcookie stanchion
EOF
fi

function write_pid_file {
    c=15
    while [[ $c -ne 0 ]]; do
	if [ "`${COMMAND} ping`" = "pong" ]; then
	    ${COMMAND} pid >$PID_DIR/stanchion.pid
	    break
	fi
	sleep 1
	c=$(($c - 1))
    done
}

function maybe_delete_pid_file {
    [ "`${COMMAND} ping`" != "pong" ] && rm -f $PID_DIR/stanchion.pid
}

# When running as a service, running as stanchion not as root, and systemd has created PID folder
if [[ $EUID -ne 0 ]]; then
    case "$1" in
        start|console|foreground)
            RELX_CONFIG_PATH=${RELX_CONFIG_PATH} VMARGS_PATH=${VMARGS_PATH} RUNNER_LOG_DIR=${RUNNER_LOG_DIR} PIPE_DIR=${PIPE_DIR} ${COMMAND} ${*} -pa /usr/lib64/stanchion/lib/patches && write_pid_file
            ;;
        *)
            RELX_CONFIG_PATH=${RELX_CONFIG_PATH} VMARGS_PATH=${VMARGS_PATH} RUNNER_LOG_DIR=${RUNNER_LOG_DIR} PIPE_DIR=${PIPE_DIR} ${COMMAND} ${*}
	    maybe_delete_pid_file
            ;;
    esac
else
    # In this case we're running sudo stanchion - so have root access, but cannot rely
    # systemd having created the PID dir, and need to sudo to the stanchion user
    if [ ! -d $PID_DIR ]; then
        mkdir $PID_DIR
        chown $STN_USER:$STN_USER $PID_DIR
    fi
    case "$1" in
        start|console|foreground)
            su - $STN_USER -c "NODETOOL_NODE_PREFIX=${NODETOOL_NODE_PREFIX} RUNNER_LOG_DIR=${RUNNER_LOG_DIR} PIPE_DIR=${PIPE_DIR} ${COMMAND} ${*} -pa /usr/lib64/stanchion/lib/patches" \
	       && write_pid_file
            ;;
        debug)
            # Drop the "debug" from the args as we're going to directly call stanchion-debug now
            shift
            # Debug may fail if run via relx script due to use of relative start location, and also need for root access
            NODETOOL_NODE_PREFIX=${NODETOOL_NODE_PREFIX} RELX_CONFIG_PATH=${RELX_CONFIG_PATH} VMARGS_PATH=${VMARGS_PATH} RUNNER_LOG_DIR=${RUNNER_LOG_DIR} PIPE_DIR=${PIPE_DIR} ${DEBUG_COMMAND} ${*}
            ;;
        *)
            ESCAPED_ARGS=`echo "$@" | sed -e 's/\([\\\(\\\){}"\x27]\)/\\\\\1/g'`
            su - $STN_USER -c "NODETOOL_NODE_PREFIX=${NODETOOL_NODE_PREFIX} RELX_CONFIG_PATH=${RELX_CONFIG_PATH} VMARGS_PATH=${VMARGS_PATH} RUNNER_LOG_DIR=${RUNNER_LOG_DIR} PIPE_DIR=${PIPE_DIR} ${COMMAND} ${ESCAPED_ARGS}"
	    maybe_delete_pid_file
            ;;
    esac
fi


