#!/bin/bash
set -euo pipefail

usage() {
  echo "Usage: $0 BINARY CHAIN_ID CHAIN_DIR RPC_PORT P2P_PORT PROFILING_PORT GRPC_PORT SAMOLEANS"
  echo "Example: $0 gaiad ibc-0 ./data 26657 26656 6060 9090 100"
  exit 1
}

missing() {
  echo "Missing $1 parameter. Please check if all parameters were specified."
  usage
}

if [ -z "$1" ]; then
  missing "[BINARY] (gaiad|akash)"
fi

if [ -z "$2" ]; then
  missing "[CHAIN_ID]"
fi

if [ -z "$3" ]; then
  missing "[CHAIN_DIR]"
fi

if [ -z "$4" ]; then
  missing "[RPC_PORT]"
fi

if [ -z "$5" ]; then
  missing "[P2P_PORT]"
fi

if [ -z "$6" ]; then
  missing "[PROFILING_PORT]"
fi

if [ -z "$7" ]; then
  missing "[GRPC_PORT]"
fi

if [ -z "$8" ]; then
  missing "[SAMOLEANS]"
fi

if [ "$#" -gt 8 ]; then
  echo "Incorrect number of parameters."
  usage
fi

BINARY=$1
CHAIN_ID=$2
CHAIN_DIR=$3
RPC_PORT=$4
P2P_PORT=$5
PROF_PORT=$6
GRPC_PORT=$7
SAMOLEANS=$8

echo "Creating $BINARY instance: home=$CHAIN_DIR | chain-id=$CHAIN_ID | p2p=:$P2P_PORT | rpc=:$RPC_PORT | profiling=:$PROF_PORT | grpc=:$GRPC_PORT | samoleans=:$SAMOLEANS"

# Add dir for chain, exit if error
if ! mkdir -p $CHAIN_DIR/$CHAIN_ID 2>/dev/null; then
    echo "Failed to create chain folder. Aborting..."
    exit 1
fi

# Build genesis file incl account for passed address
STAKE="100000000000stake"

# The user also needs stake to perform actions
USER_COINS="${STAKE},${SAMOLEANS}samoleans"

# Hermes also needs stake to perform actions
HERMES_COINS="${STAKE},${SAMOLEANS}samoleans"

$BINARY --home $CHAIN_DIR/$CHAIN_ID --chain-id $CHAIN_ID init $CHAIN_ID
sleep 1
echo "Creating validator key"
$BINARY --home $CHAIN_DIR/$CHAIN_ID keys add validator --keyring-backend="test" --output json > $CHAIN_DIR/$CHAIN_ID/validator_seed.json 2>&1
sleep 1
echo "Creating user key"
$BINARY --home $CHAIN_DIR/$CHAIN_ID keys add user --keyring-backend="test" --output json > $CHAIN_DIR/$CHAIN_ID/user_seed.json 2>&1
sleep 1
echo "Creating user2 key"
$BINARY --home $CHAIN_DIR/$CHAIN_ID keys add user2 --keyring-backend="test" --output json > $CHAIN_DIR/$CHAIN_ID/user2_seed.json 2>&1
sleep 1

# Add samoleans to user
USER=$($BINARY --home $CHAIN_DIR/$CHAIN_ID keys --keyring-backend="test" show user -a)
$BINARY --home $CHAIN_DIR/$CHAIN_ID genesis add-genesis-account $USER $USER_COINS
sleep 1

# Add samoleans to user2
USER2=$($BINARY --home $CHAIN_DIR/$CHAIN_ID keys --keyring-backend="test" show user2 -a)
$BINARY --home $CHAIN_DIR/$CHAIN_ID genesis add-genesis-account $USER2 $USER_COINS
sleep 1


# Add stake to validator
VALIDATOR=$($BINARY --home $CHAIN_DIR/$CHAIN_ID keys --keyring-backend="test" show validator -a)
$BINARY --home $CHAIN_DIR/$CHAIN_ID genesis add-genesis-account $VALIDATOR $STAKE
sleep 1

# Stake everything
$BINARY --home $CHAIN_DIR/$CHAIN_ID genesis gentx validator --keyring-backend="test" --chain-id $CHAIN_ID $STAKE
sleep 1

$BINARY --home $CHAIN_DIR/$CHAIN_ID genesis collect-gentxs
sleep 1

# Check platform
platform='unknown'
unamestr=`uname`
if [ "$unamestr" = 'Linux' ]; then
   platform='linux'
fi

# Set proper defaults and change ports (use a different sed for Mac or Linux)
echo "Change settings in config.toml file..."
if [ $platform = 'linux' ]; then
  sed -i 's#"172800s"#"30s"#g' $CHAIN_DIR/$CHAIN_ID/config/genesis.json
  sed -i 's#"tcp://127.0.0.1:26657"#"tcp://0.0.0.0:'"$RPC_PORT"'"#g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
  sed -i 's#"tcp://0.0.0.0:26656"#"tcp://0.0.0.0:'"$P2P_PORT"'"#g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
  sed -i 's#"localhost:6060"#"localhost:'"$PROF_PORT"'"#g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
  sed -i 's/timeout_commit = "5s"/timeout_commit = "1s"/g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
  sed -i 's/timeout_propose = "3s"/timeout_propose = "1s"/g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
  sed -i 's/trust_period = "168h0m0s"/trust_period = "3m"/g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
  sed -i 's/index_all_keys = false/index_all_keys = true/g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
  # sed -i '' 's#index-events = \[\]#index-events = \["message.action","send_packet.packet_src_channel","send_packet.packet_sequence"\]#g' $CHAIN_DIR/$CHAIN_ID/config/app.toml
else
  sed -i '' 's#"172800s"#"30s"#g' $CHAIN_DIR/$CHAIN_ID/config/genesis.json
  sed -i '' 's#"tcp://127.0.0.1:26657"#"tcp://0.0.0.0:'"$RPC_PORT"'"#g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
  sed -i '' 's#"tcp://0.0.0.0:26656"#"tcp://0.0.0.0:'"$P2P_PORT"'"#g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
  sed -i '' 's#"localhost:6060"#"localhost:'"$PROF_PORT"'"#g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
  sed -i '' 's/timeout_commit = "5s"/timeout_commit = "1s"/g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
  sed -i '' 's/timeout_propose = "3s"/timeout_propose = "1s"/g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
  sed -i '' 's/trust_period = "168h0m0s"/trust_period = "3m"/g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
  sed -i '' 's/index_all_keys = false/index_all_keys = true/g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
  # sed -i '' 's/min-retain-blocks = 0/min-retain-blocks = 100/g' $CHAIN_DIR/$CHAIN_ID/config/app.toml
  # sed -i '' 's#index-events = \[\]#index-events = \["message.action","send_packet.packet_src_channel","send_packet.packet_sequence"\]#g' $CHAIN_DIR/$CHAIN_ID/config/app.toml
  # sed -i '' 's/error/debug/g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
  # sed -i '' 's/info/debug/g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
fi

# Start gaia
echo "Start gaia on grpc port: $GRPC_PORT..."
$BINARY --home $CHAIN_DIR/$CHAIN_ID start --minimum-gas-prices=0stake --pruning=nothing --grpc.address="0.0.0.0:$GRPC_PORT" --log_level=error > $CHAIN_DIR/$CHAIN_ID.log 2>&1 &

# Show validator's and user's balance
sleep 3
RPC_ADDR="tcp://localhost:$RPC_PORT"
echo "Balances for validator '$VALIDATOR' @ '$RPC_ADDR'"
$BINARY --node "$RPC_ADDR" query bank balances $VALIDATOR --log_level error
echo "Balances for user '$USER' @ '$RPC_ADDR'"
$BINARY --node "$RPC_ADDR" query bank balances $USER --log_level error
echo "Balances for user '$USER2' @ '$RPC_ADDR'"
$BINARY --node "$RPC_ADDR" query bank balances $USER2 --log_level error
