#!/bin/sh

# If relative path doesn't exist, default to home directory
STOREDIR=$HOME/.config/paloma-lightnode

OPTIND=1
while getopts "fd:" opt; do
  case "$opt" in
    d)
      # If we have the `-d` flag, use it
      STOREDIR=$OPTARG
      ;;
    f)
      # Force the command to run without any other checks
      FORCERUN=true
      ;;
  esac
done

shift $((OPTIND-1))

if [ -z "$FORCERUN" ]; then
  if [ -z "`command -v docker 2>/dev/null`" ]; then
    if [ -z "`which docker 2>/dev/null`" ]; then
      echo "docker could not be found. Please install docker first."
      exit 1
    fi
  fi
fi

mkdir -p $STOREDIR

RUN_CMD=${@:-setup-node}

docker run --rm --pull=always -ti \
  --privileged \
  -v $STOREDIR:/root/lightnode \
  palomachain/lightnode-client:v2 \
  ${RUN_CMD}
