#!/bin/bash
PRG="$0"
while [ -h "$PRG" ] ; do
  ls=$(ls -ld "$PRG")
  link=$(echo "$ls" | sed 's/.*-> //')
  if [[ "$link" == /* ]]; then
    PRG="$link"
  else
    PRG=$(dirname "$PRG")/"$link"
  fi
done
SCRIPT_DIR=$(dirname "$PRG")

# Detect host IP address (avoid 127.0.0.1)
# Shared function used by tis and datax-worker scripts
detect_host_ip() {
    local ip=""
    if [ "$(uname)" = "Darwin" ]; then
        ip=$(ipconfig getifaddr en0 2>/dev/null || ifconfig | grep 'inet ' | grep -v '127.0.0.1' | head -1 | awk '{print $2}')
    else
        ip=$(hostname -I 2>/dev/null | awk '{print $1}')
        if [ -z "$ip" ]; then
            ip=$(ip route get 1.0.0.0 2>/dev/null | awk '{for(i=1;i<=NF;i++) if ($i=="src") print $(i+1)}')
        fi
    fi
    if [ -z "$ip" ]; then
        echo "Warning: Could not detect host IP address for AKKA" >&2
        ip="127.0.0.1"
    fi
    echo "$ip"
}
AKKA_HOST_IP=$(detect_host_ip)

# AKKA_PORT shall be distinction with each other on dataX Worker node
export AKKA_PORT=2552
export AKKA_SEED_NODES="akka://TIS-DAG-System@${AKKA_HOST_IP}:2551"
export JAVA_JVM_OPTS="-Xms3G -Xmx3G -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=1024m "

export TIS_LAUNCH_MODE="datax-worker"
exec "$SCRIPT_DIR/tis" "$@"
