#!/bin/sh

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/root
PASSPHRASE=passphrase

cd /root

if [ "$1" = "genesis" ]; then
  if [ ! -f "/root/.nymd/config/genesis.json" ]; then
    ./nyxd init nymnet --chain-id nymnet 2> /dev/null
    # staking/governance token is hardcoded in config, change this
    sed -i "s/\"stake\"/\"u${STAKE_DENOM}\"/" /root/.nyxd/config/genesis.json
    sed -i 's/minimum-gas-prices = ""/minimum-gas-prices = "0.025u'"${DENOM}"'"/' /root/.nyxd/config/app.toml
    sed -i '0,/enable = false/s//enable = true/g' /root/.nyxd/config/app.toml
    sed -i 's/cors_allowed_origins = \[\]/cors_allowed_origins = \["*"\]/' /root/.nyxd/config/config.toml
    sed -i 's/create_empty_blocks = true/create_empty_blocks = false/' /root/.nyxd/config/config.toml
    sed -i 's/laddr = "tcp:\/\/127.0.0.1:26657"/laddr = "tcp:\/\/0.0.0.0:26657"/' /root/.nyxd/config/config.toml

#    create accounts
    yes "${PASSPHRASE}" | ./nyxd keys add node_admin 2>&1 >/dev/null | tail -n 1 > /root/.nyxd/mnemonic
    yes "${PASSPHRASE}" | ./nyxd keys add secondary 2>&1 >/dev/null | tail -n 1 > /root/.nyxd/secondary_mnemonic
    cp /root/.nyxd/mnemonic /genesis_volume/genesis_mnemonic
    cp /root/.nyxd/secondary_mnemonic /genesis_volume/secondary_mnemonic

#    add genesis accounts with some initial tokens
    GENESIS_ADDRESS=$(yes "${PASSPHRASE}" | ./nyxd keys show node_admin -a)
    SECONDARY_ADDRESS=$(yes "${PASSPHRASE}" | ./nyxd keys show secondary -a)
    yes "${PASSPHRASE}" | ./nyxd add-genesis-account "${GENESIS_ADDRESS}" 1000000000000000u"${DENOM}",1000000000000000u"${STAKE_DENOM}"
    yes "${PASSPHRASE}" | ./nyxd add-genesis-account "${SECONDARY_ADDRESS}" 1000000000000000u"${DENOM}",1000000000000000u"${STAKE_DENOM}"

    yes "${PASSPHRASE}" | ./nyxd gentx node_admin 1000000000u"${STAKE_DENOM}" --chain-id nymnet 2> /dev/null
    ./nyxd collect-gentxs 2> /dev/null
    ./nyxd validate-genesis > /dev/null
    cp /root/.nyxd/config/genesis.json /genesis_volume/genesis.json
  else
    echo "Validator already initialized, starting with the existing configuration."
    echo "If you want to re-init the validator, destroy the existing container"
	fi
	./nyxd start
elif [ "$1" = "secondary" ]; then
  if [ ! -f "/root/.nymd/config/genesis.json" ]; then
    ./nyxd init nymnet --chain-id nym-secondary 2> /dev/null

    # Wait until the genesis node writes the genesis.json to the shared volume
    while ! [ -s /genesis_volume/genesis.json ]; do
      sleep 1
    done

# wait for the actual validator to start up
    sleep 5

    cp /genesis_volume/genesis.json /root/.nyxd/config/genesis.json
    GENESIS_PEER=$(cat /root/.nyxd/config/genesis.json | grep '"memo"' | cut -d'"' -f 4)
    GENESIS_IP=$(cat /root/.nyxd/config/genesis.json | grep '"memo"' | cut -d'@' -f2 | cut -d: -f1)
    sed -i 's/persistent_peers = ""/persistent_peers = "'"${GENESIS_PEER}"'"/' /root/.nyxd/config/config.toml
    sed -i 's/minimum-gas-prices = ""/minimum-gas-prices = "0.025u'"${BECH32_PREFIX}"'"/' /root/.nyxd/config/app.toml
    sed -i '0,/enable = false/s//enable = true/g' /root/.nyxd/config/app.toml
    sed -i 's/cors_allowed_origins = \[\]/cors_allowed_origins = \["*"\]/' /root/.nyxd/config/config.toml
    sed -i 's/create_empty_blocks = true/create_empty_blocks = false/' /root/.nyxd/config/config.toml
    sed -i 's/laddr = "tcp:\/\/127.0.0.1:26657"/laddr = "tcp:\/\/0.0.0.0:26657"/' /root/.nyxd/config/config.toml

#    import mnemonic generated by the genesis validator (have a local copy for ease of use)
    cp  /genesis_volume/secondary_mnemonic /root/.nyxd/mnemonic
    { cat /root/.nyxd/mnemonic; echo "${PASSPHRASE}"; echo "${PASSPHRASE}"; } | ./nyxd keys add node_admin --recover #> /dev/null
    ./nyxd validate-genesis > /dev/null

#    create validator
#  don't even ask about those sleeps...
  { echo "${PASSPHRASE}"; sleep 10; yes; sleep 10; } | ./nyxd tx staking create-validator --amount=10000000u"${STAKE_DENOM}" --fees 100000u"${DENOM}" --pubkey="$(./nyxd tendermint show-validator)" --moniker="secondary" --commission-rate="0.10" --commission-max-rate="0.20" --commission-max-change-rate="0.01" --min-self-delegation="1" --chain-id=nymnet --from=node_admin -b async --node http://"${GENESIS_IP}":26657
  else
    echo "Validator already initialized, starting with the existing configuration."
    echo "If you want to re-init the validator, destroy the existing container"
  fi
	./nyxd start
else
	echo "Wrong command. Usage: ./$0 [genesis/secondary]"
fi
