#!/bin/bash

RPC_PORT=

usage() {
  cat <<EOF 1>&2
Usage: $0 [-r RPC_PORT] [abewalletctl flags]
EOF
}

exit_abnormal() {
  usage
  exit 1
}

while getopts "r:" opts; do
  case "${opts}" in
    r)
      RPC_PORT=${OPTARG}
      ;;
    *)
      exit_abnormal
      ;;
  esac
done
shift $((OPTIND-1))

RELEASE_DIR=$ABEWALLET_RELEASE_HOME
PROG="$RELEASE_DIR/abewalletctl"
CONFIG_FILE="$RELEASE_DIR/abewallet.conf"
LIB_DIR="$RELEASE_DIR/lib"

if [[ "$OSTYPE" == *"darwin"* ]]; then
  export DYLD_LIBRARY_PATH="$LIB_DIR"
elif [[ "$OSTYPE" == *"linux"* ]]; then
  export LD_LIBRARY_PATH="$LIB_DIR"
else
  >&2 echo "ERROR: Unknown platform ($OSTYPE)."
  exit 1;
fi

exec "$PROG" \
  --configfile="$CONFIG_FILE" \
  --rpcuser=localuser \
  --rpcpass=localpass \
  --rpcserver=localhost:${RPC_PORT} \
  --notls \
  "$@"