#!/bin/bash
set -e

PREFIX=/opt/niccli
LOCK_FILE=/var/lock/niccli.lock

# Execute niccli binary from package installation prefix
if [ -f ${PREFIX}/niccli.$( uname -m ) ]; then
  # 'exec' replaces the shell with flock
  # flock waits for the lock, then runs the binary with your arguments
  exec flock $LOCK_FILE ${PREFIX}/niccli.$( uname -m ) "$@"
fi

# Execute niccli binary from current directory
if [ -f ./niccli.$( uname -m ) ]; then
  exec flock $LOCK_FILE ./niccli.$( uname -m ) "$@"
fi

echo niccli is not installed in the expected location: ${PREFIX}/niccli.$( uname -m ) or in the current working directory.
exit 1
