#!/bin/bash

## -------------------------------------------------------------------
##
## riak-cs: Riak CS launcher
##
## Copyright (c) 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.
##
## -------------------------------------------------------------------

RUNNER_GEN_DIR=/var/lib/riak-cs
RELX_RIAK=/usr/lib64/riak-cs/bin/riak-cs
export PID_DIR=/run/riak-cs
export RUNNER_LOG_DIR=/var/log/riak-cs

mkdir -p $PID_DIR
chown riak_cs:riak_cs $PID_DIR

# cuttlefish should be doing this, but it doesn't:
VMARGS_PATH=`ls -1 ${RUNNER_GEN_DIR}/generated.conf/vm.*.args 2>/dev/null | tail -1`
if [ ! -r  "$VMARGS_PATH" ]; then
    VMARGS_PATH="/usr/lib64/riak-cs/releases/3.2.1/vm.args"
fi
export VMARGS_PATH

# centos7-based distros have a su that contacts pam and prints the "Last logged in" message
if [ "`cat /etc/redhat-release 2>&1`" = "CentOS Stream release 7" ] ||
   [ "`cat /etc/system-release 2>&1`" = "Amazon Linux release 2 (Karoo)" ]; then
    COPTION="--session-command"
else
    COPTION="-c"
fi

function maybe_su {
    if [[ $EUID -ne 0 ]]; then
        $*
    else
	# if we are executing an admin command that spins up a
	# (hidden) node to then execute custom erlang code via -eval,
	# we need to cd to a dir containing the erlang cookie
	# (previously implicitly done by su -, which option we have
	# removed in order to allow any env vars to be available for
	# the ultimate invocation of riak/riak-cs/stanchion)
	cd "/usr/lib64/riak-cs"
	# freebsd su is fairly limited, so:
	mkdir -p "$RUNNER_GEN_DIR"
	chown riak_cs:riak_cs "$RUNNER_GEN_DIR"
	f=`mktemp "$RUNNER_GEN_DIR"/su_piggy-XXXXXXX`
	cat >"$f" <<EOF
#!/bin/sh
$*
EOF
	chmod +x "$f"
	chown riak_cs:riak_cs "$f"
	su riak_cs $COPTION "$f"
        rm -f "$f"
    fi
}

case "$1" in
    stop)
        maybe_su $RELX_RIAK $* \
	    && rm -f $PID_DIR/riak-cs.pid
	;;
    *)
        maybe_su $RELX_RIAK $*
        ;;
esac
