#!/bin/bash

##########################################
## Created by                           ##
## Jae Chung, HKEOS                     ##
## Igor Lins e Silva, EOS Rio           ##
## For Ghostbusters Testnet             ##
##                                      ##
##########################################
cd "$(dirname "$0")"
GLOBAL_PATH=$(pwd)
CONFIG=$GLOBAL_PATH/config.ini

SOURCES_FOLDER="$( jq -r '.SOURCES_FOLDER' "00_CONFIG.conf" )"
PUB_KEY="$( jq -r '.EOSIO_PRODUCER_PUB_KEY' "00_CONFIG.conf" )"
PRIV_KEY="$( jq -r '.EOSIO_PRODUCER_PRIV_KEY' "00_CONFIG.conf" )"
NODE_HOST="$( jq -r '.WIREGUARD_IP' "00_CONFIG.conf" )"
HTTP_PORT="$( jq -r '.HTTP_PORT' "00_CONFIG.conf" )"
P2P_PORT="$( jq -r '.P2P_PORT' "00_CONFIG.conf" )"
SEED_P2P="$( jq -r '.SEED_P2P_PORT' "00_CONFIG.conf" )"

echo -ne "\n\n..:: Creating config.ini ::..\n\n";

echo "### EOS Ghostbusters Testnet Bios Node Config file. Autogenerated by script." > $CONFIG

echo '
get-transactions-time-limit = 3
genesis-json = "'$GLOBAL_PATH'/genesis.json"
block-log-dir = "'$GLOBAL_PATH'/blocks"
http-server-address = 127.0.0.1:'$HTTP_PORT'
p2p-listen-endpoint = '$NODE_HOST':'$P2P_PORT'
p2p-server-address = '$NODE_HOST':'$P2P_PORT'
access-control-allow-origin = *
' >> $CONFIG

echo '
allowed-connection = specified
log-level-net-plugin = info
max-clients = 120
connection-cleanup-period = 30
network-version-match = 1
sync-fetch-span = 2000
enable-stale-production = true
required-participation = 33
plugin = eosio::chain_plugin
plugin = eosio::chain_api_plugin
plugin = eosio::history_plugin
plugin = eosio::history_api_plugin
plugin = eosio::producer_plugin
' >> $CONFIG

echo '
agent-name = eosio
peer-private-key = ["'$PUB_KEY'","'$PRIV_KEY'"]
private-key = ["'$PUB_KEY'","'$PRIV_KEY'"]
producer-name = eosio
' >> $CONFIG

echo '
p2p-peer-address = '$NODE_HOST':'$SEED_P2P'
peer-key = "'$PUB_KEY'"
' >> $CONFIG

echo -ne "\n\n..:: config.ini has been generated. Please check that the inputs were entered correctly. ::..\n\n";
