#!/usr/bin/bash

# file: irtrans-server.sh #

#   Stupid IRTrans server does not recognize symlinks. Invoking it as
#       irtrans-server /dev/irtrans
#   where `/dev/irtrans` is a symlink to `/dev/ttyUSB0`, causes error "Cannot resolve device
#   /dev/irtrans". Let's resolve symlink to let it work:

set -e

args=( "$@" )
if [[ ${#args[@]} -gt 0 ]]; then
    idx=$(( ${#args[@]} - 1 ))
    device=${args[$idx]}
    if [[ -L $device ]]; then
        device=$( /usr/bin/realpath -P "$device" )
    fi
    args[$idx]=$device
fi

exec /usr/libexec/irtrans/irtrans-server.bin "${args[@]}"

# end of file #
