#!/bin/sh
OLDPATH="$PATH"
WHEREAMI="`pwd`"

# $HYBRIXD/node/scripts/npm  => $HYBRIXD
SCRIPTDIR="`dirname \"$0\"`"
CLI_WALLET="`cd \"$SCRIPTDIR\" && pwd`"

LIBRARIES="$CLI_WALLET/lib"

export PATH="$CLI_WALLET/node_binaries/bin:$PATH"
NODEINST="`which node`"

if [ -z "$NODEINST" ]; then
  echo ""
  echo "No version of NodeJS found, please install NodeJS version 8.12 or higher"
  echo ""
  exit 1
fi

if [ -z "$1" ]; then
  echo ""
  echo "Welcome to the hybrix cli-wallet. A command line wallet for"
  echo "checking balances and performing transactions."
  echo ""
  echo "Type:  cli-wallet --help  ... to find out how to use the wallet."
  echo ""
  exit 0
elif [ "$1" = "setup" ]; then
  echo "This will setup your cli wallet with credentials. Please not that this will store your keys in a .keys file.";
  read -p "Username " USERNAME
  read -s -p "Password " PASSWORD
  echo "$USERNAME $PASSWORD" > "./.keys"
  echo "Your keys were stored succesfully."
  exit 0
fi

cd "$CLI_WALLET" 2>&1 > /dev/null
node --no-warnings "$LIBRARIES/cli-wallet.js" $@
if [ $? -eq 0 ]; then
  export PATH="$OLDPATH"
  cd "$WHEREAMI"
else
  export PATH="$OLDPATH"
  cd "$WHEREAMI"
  exit 1
fi
