#!/bin/bash
ulimit -c unlimited*
set -- flexisip "$@"

# Wait for needed container startup
/wait || exit $?

echo "Flexisip docker params : $*"

if [[ $@ == *"--server"* ]]; then
  echo "--server param found, starting only one Flexisip instance"
  "$@" &
else
  echo "Server param not found, starting 3 Flexisip instances (proxy, presence, conference)"
  ( "$@" --server proxy 2>&1| tee /var/opt/belledonne-communications/log/flexisip/flexisip_proxy_stdout.log ) &
  ( "$@" --server presence 2>&1| tee /var/opt/belledonne-communications/log/flexisip/flexisip_presence_stdout.log ) &
  ( "$@" --server conference 2>&1| tee /var/opt/belledonne-communications/log/flexisip/flexisip_conference_stdout.log ) &
fi

wait -n

echo "At least one server crashed, stopping every sub process that is still alive"
pkill -P $$

# coredump management, used in unit tests
# we execute gdb on each coredump, with the options given in backtrace.gdb file
# we search in /root because it's the workdir set in Dockerfile and because by default our coredumps are generated by default in the working directory
if [ -n "$(find /root -type f -name "core*")" ]; then
	find /root -type f -name "core*" | xargs -L1 gdb flexisip -x /backtrace.gdb;
fi