#!/bin/bash

_find_uberload_lib()
{
    local fullpath="/opt/ubernic/"$1
    if [ -x ${fullpath} ]; then
        UBERLOAD_LIB=${fullpath}
    fi
    local relpath="./"$1;
    if [ -x ${relpath} ]; then
        UBERLOAD_LIB=${relpath}
    fi
}

_usage()
{
    echo "uberload usage:"
    echo "uberload <options> <command> <command-options>"
    echo "example: uberload --thread-safe iperf3 -c 192.168.1.1"
    echo "options:"
    echo " -t-s, --thread-safe        Use uberload with thread safe socket calls"
    echo " -n-t-s, --non-thread-safe  Use uberload with disabled thread safety (default)"
    echo "                            one of the thread safety options is mandatory"
    echo
    echo " -i <if-ip>"
    echo " --bind-interface <if-ip>   Define uberload to use this ubernic interface when binding to ANY (0.0.0.0)"
    echo "                            Example: -i 10.10.1.6"
    echo " -t <tracedir>"
    echo " --tracedir <tracedir>      Enable trace and set destination dir for trace e.g. -t /tmp. Default is no trace"
    echo " --background-affinity      Set the affinity list for the background threads"

    export UBERLOAD_PRINT_VERSION=1
    _find_uberload_lib "libuberload.so"
    printf "Using uberload %s\n" ${UBERLOAD_LIB}
    LD_PRELOAD=${UBERLOAD_LIB} /bin/ls
    unset UBERLOAD_PRINT_VERSION
}


#default is using non threadsafe lib (as in onload)
THREADSAFE=0
export NOTRACE=1

export UBERLOAD_FULL_CMDLINE="$0 $@"

while [[ $# -gt 0 ]]; do
    case $1 in
        -t-s|--thread-safe)
          THREADSAFE=1
          shift
          ;;
        -n-t-s|--non-thread-safe)
          THREADSAFE=0
          shift
          ;;
        -i|--bind-interface)
          export UBERLOAD_BIND_INTERFACE="$2"
          shift
          shift
          ;;
        -t|--tracedir)
          export NOTRACE=0
          export UBERLOAD_LOGDIR="$2"
          shift
          shift
          ;;
        --background-affinity)
          export UBERLOAD_BACKGROUND_AFFINITY="$2"
          shift
          shift
          ;;
        -h|--help)
          _usage
          exit 0
          ;;
        *)
          break
          ;;
    esac
done

if [ -z "${UBERLOAD_LIB}" ]; then
    if [ "$THREADSAFE" -eq 0 ]; then
        _find_uberload_lib "libuberload_unsafe.so"
    else
        _find_uberload_lib "libuberload.so"
    fi
fi

if [ -z "${UBERLOAD_LIB}" ]; then
    echo "No Uberload library found (libuberload.so), aborting"
    exit 1;
fi

if [ -z "${UBERLOAD_STATUS_LOG}" ]; then
    export UBERLOAD_STATUS_LOG="./uberload_status.log"
fi

printf "Using uberload %s\n" ${UBERLOAD_LIB}

export LD_PRELOAD=${UBERLOAD_LIB}


########### Settings to be changed by user if necessary

#set logfile for socket call log and enable it:
#export UBERLOAD_LOGFILE="/tmp/uberload.log"

#enable trace directly to terminal:
#export UBERLOAD_LOGFILE="-"

#enable verbose tracing
#export VERBOSE=1

if [ -z "${NOTRACE}" ] && [ -z ${UBERLOAD_LOGFILE} ]; then
    export NOTRACE=1
fi


#call commandline
exec "$@"

