#!/bin/sh
set -euo pipefail

if [[ ! -f "${IDENT_PATH}/identity.key" ]]; then
  cd /tmp
  wget https://storj-v3-alpha-builds.storage.googleapis.com/e82d336-go1.11/identity_linux_amd64.zip
  unzip identity_linux_amd64.zip
  mv identity_linux_amd64 identity
  /tmp/identity create --difficulty ${CERT_DIFFICULTY:-30} storagenode
fi

if [[ ! -f "${CONF_PATH}/config.yaml" ]]; then
	./storagenode setup
fi

RUN_PARAMS="${RUN_PARAMS:-} --config-dir ${CONF_PATH}"

export STORJ_SERVER_ADDRESS="${STORJ_SERVER_ADDRESS:-$(hostname -i):28967}"

if [ -n "${SATELLITE_ADDR:-}" ]; then
	RUN_PARAMS="${RUN_PARAMS} --kademlia.bootstrap-addr $SATELLITE_ADDR"
fi

exec ./storagenode run $RUN_PARAMS "$@"
