#!/bin/sh
#
# Dump OSGi runner started in the foreground of another shell, presumably as user osgi in a docker container
#

export OSGI_PERFORM_REGROUP_BUNDLES="no"

if test "$OSGI_RUNNER_CONFIG_INCLUDED" != xRHTd4BvVmV8kdWh
then
    . /usr/share/osgi-runner/sh/load-config-docker.sh
fi

if test "$1" = "--help" -o "$1" = "-h" -o "$1" = "help"
then
    echo "usage: /usr/bin/osgi-runner-docker-dump"
    echo "       Sends -3 to the osgi-runner running under the current user."
    #echo "       Then exports a thread dump and a snapshot of osgi.log to"
    #echo "       the dump directory $OSGI_DUMPDIR"
    #echo "       This directory might be configured in /etc/default/osgi-runner"
    #echo "       by setting the variable OSGI_DUMPDIR."
    exit 0
fi
    
OSGI_PID=$(ps -ef -u $(id -u) |egrep "org.clazzes.osgi.runner.Main$" |awk '{print $2}')

echo "Dumping for osgi-runner logs to $OSGI_DUMPDIR..."
if test -n "$OSGI_PID"
then
    echo "Performing kill -3 $OSGI_PID"
    #iso_rx='s/^([0-9]{4,4})-([0-9]{2,2})-([0-9]{2,2})T([0-9]{2,2}):([0-9]{2,2}):([0-9]{2,2}).*$/\1\2\3\4\5\6/p;d'
    #timestamp=$(date -Iseconds --utc | sed -E $iso_rx)
    kill -3 $OSGI_PID
    #tag=$(date +%Y%m%d-%H%M%S)
    #echo "Creating $OSGI_DUMPDIR/$tag-osgi.log.gz"
    #gzip -c -9 $OSGI_LOGDIR/osgi.log > $OSGI_DUMPDIR/$tag-osgi.log.gz
else
    echo "Cannot determine osgi-runner PID, does not seem to be running under the current user."
    exit 1
fi

