#!/bin/bash

ARGS=""

if [[ "$SERVER_HOST" ]]; then
	ARGS="$ARGS -h $SERVER_HOST";
else
	# In the interests of sane convenient defaults, discover the host
	# IP address (only possible in bridged networks), and default to that
	GATEWAY_IP=$(printf "%d.%d.%d.%d" $(awk '$2 == 00000000 { for (i = 8; i >= 2; i=i-2) { print "0x" substr($3, i-1, 2) } }' /proc/net/route))

	if [[ "$GATEWAY_IP" != "0.0.0.0" ]]; then
		ARGS="$ARGS -h $GATEWAY_IP"
	fi
fi
if [[ "$SERVER_PORT" ]]; then
	ARGS="$ARGS -p $SERVER_PORT";
fi

exec /usr/local/bin/scansion $ARGS "$@"
