#!/bin/bash

logo_shazoe(){

clear

 cat << "EOF"

         ____  _   _    _     ________  _____ ____  
        / ___|| | | |  / \   |__  / _ \| ____/ ___|
        \___ \| |_| | / _ \    / / | | |  _| \___ \
         ___) |  _  |/ ___ \  / /| |_| | |___ ___) |
        |____/|_| |_/_/   \_\/____\___/|_____|____/
                               services.shazoes.xyz


EOF

}

logo_shazoe;



read -p "Enter your MONIKER :" MONIKER
echo 'export MONIKER='$MONIKER

# set vars
echo "export MONIKER=$MONIKER" >> $HOME/.bash_profile
echo "export REALIO_CHAIN_ID="realionetwork_3301-1"" >> $HOME/.bash_profile
echo "export REALIO_PORT="6"" >> $HOME/.bash_profile
source $HOME/.bash_profile


echo -e "Moniker:        \e[1m\e[32m$MONIKER\e[0m"
echo -e "Chain id:       \e[1m\e[32m$REALIO_CHAIN_ID\e[0m"
echo -e "Node custom port:  \e[1m\e[32m$REALIO_CHAIN_PORT\e[0m"
sleep 1


# Update & install dependencies
sudo apt update && sudo apt upgrade -y
sudo apt install curl tar wget clang pkg-config libssl-dev jq build-essential bsdmainutils git make ncdu gcc git jq htop tmux chrony liblz4-tool -y

# Install Go
cd $HOME
VER="1.22.2"
wget "https://golang.org/dl/go$VER.linux-amd64.tar.gz"
sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf "go$VER.linux-amd64.tar.gz"
rm "go$VER.linux-amd64.tar.gz"
[ ! -f ~/.bash_profile ] && touch ~/.bash_profile
echo "export PATH=$PATH:/usr/local/go/bin:~/go/bin" >> ~/.bash_profile
source $HOME/.bash_profile
[ ! -d ~/go/bin ] && mkdir -p ~/go/bin
go version


# Download Binary
cd $HOME
rm -rf realio-network
git clone https://github.com/realiotech/realio-network.git
cd realio-network
git checkout v0.9.2
make install


# Config and Init App
realio-networkd config node tcp://localhost:${REALIO_PORT}57
realio-networkd config chain-id $REALIO_CHAIN_ID
realio-networkd init $MONIKER --chain-id $REALIO_CHAIN_ID


# Add Genesis File and Addrbook
wget -O $HOME/.realio-network/config/genesis.json https://files.shazoes.xyz/mainnets/realio/genesis.json
wget -O $HOME/.realio-network/config/addrbook.json https://files.shazoes.xyz/mainnets/realio/addrbook.json


#Configure Seeds and Peers
SEEDS=""
PEERS="2815cc1437461f808a7f022c0df679fa27918dbc@realio-mainnet-peer.itrocket.net:23656,759b796d1f7c8c8362b525aaad2531591762723a@88.198.32.17:46656,915667afd60b28cd01f676fd98fe7328fb189bf7@5.75.186.43:26656,ec87620afe1c9e73a4ba0eaeb0d3bd5ac2c7bff8@93.104.210.136:35656,dbbb1136289eafe81431e3d1c8761d637655f8e0@165.22.14.140:26656,6f5d99fac932cf4c01a3e408c5f3c1ad57ea507b@138.68.175.105:26656,ee5bcb1a7becf66d4fa06bd021b442afc2b92bc4@94.130.138.48:29656,daea809589ac871c6c9f450ca1cdfd5ab2320e06@54.36.115.42:26656,ae925f32ead79c394bb7f6f8abb5345f7a16b064@167.235.102.45:16656,ebd1438f2f6e583d6e5accf749eda5e8005702e9@148.251.66.248:26656,9fa9825f99b71c63d18e5c8aa35eff9c0626ee76@65.108.226.120:53656"
sed -i -e "s/^seeds *=.*/seeds = \"$SEEDS\"/; s/^persistent_peers *=.*/persistent_peers = \"$PEERS\"/" $HOME/.realio-network/config/config.toml
sed -i -e "s/^minimum-gas-prices *=.*/minimum-gas-prices = \"0.0ario\"/" $HOME/.realio-network/config/app.toml
sed -i -e 's|^indexer *=.*|indexer = "null"|' $HOME/.realio-network/config/config.toml
sed -i 's|^prometheus *=.*|prometheus = true|' $HOME/.realio-network/config/config.toml


# Config Pruning
pruning="custom"
pruning_keep_recent="100"
pruning_keep_every="0"
pruning_interval="10"
sed -i -e "s/^pruning *=.*/pruning = \"$pruning\"/" $HOME/.realio-network/config/app.toml
sed -i -e "s/^pruning-keep-recent *=.*/pruning-keep-recent = \"$pruning_keep_recent\"/" $HOME/.realio-network/config/app.toml
sed -i -e "s/^pruning-keep-every *=.*/pruning-keep-every = \"$pruning_keep_every\"/" $HOME/.realio-network/config/app.toml
sed -i -e "s/^pruning-interval *=.*/pruning-interval = \"$pruning_interval\"/" $HOME/.realio-network/config/app.toml


# Set Custom Port
sed -i -e "s%:1317%:${REALIO_PORT}17%; s%:8080%:${REALIO_PORT}80%; s%:9090%:${REALIO_PORT}90%; s%:9091%:${REALIO_PORT}91%; s%:8545%:${REALIO_PORT}45%; s%:8546%:${REALIO_PORT}46%; s%:6065%:${REALIO_PORT}65%" $HOME/.realio-network/config/app.toml
sed -i -e "s%:26658%:${REALIO_PORT}58%; s%:26657%:${REALIO_PORT}57%; s%:6060%:${REALIO_PORT}60%; s%:26656%:${REALIO_PORT}56%; s%:26660%:${REALIO_PORT}61%" $HOME/.realio-network/config/config.toml

sed -i.bak -e "s%:1317%:${REALIO_PORT}317%g;
s%:8080%:${REALIO_PORT}080%g;
s%:9090%:${REALIO_PORT}090%g;
s%:9091%:${REALIO_PORT}091%g;
s%:8545%:${REALIO_PORT}545%g;
s%:8546%:${REALIO_PORT}546%g;
s%:6065%:${REALIO_PORT}065%g" $HOME/.dhealth/config/app.toml
sed -i.bak -e "s%:26658%:${REALIO_PORT}658%g;
s%:26657%:${REALIO_PORT}657%g;
s%:6060%:${REALIO_PORT}060%g;
s%:26656%:${REALIO_PORT}656%g;
s%^external_address = \"\"%external_address = \"$(wget -qO- eth0.me):${REALIO_PORT}656\"%;
s%:26660%:${REALIO_PORT}660%g" $HOME/.dhealth/config/config.toml



# Set Service File
sudo tee /etc/systemd/system/realio-networkd.service > /dev/null <<EOF
[Unit]
Description=realio-mainnet
After=network-online.target

[Service]
User=$USER
ExecStart=$(which realio-networkd) start --home $HOME/.realio-network
Restart=on-failure
RestartSec=3
LimitNOFILE=65535

[Install]
WantedBy=multi-user.target
EOF


# Enable and Start Service
sudo systemctl daemon-reload && sudo systemctl enable realio-networkd && sudo systemctl start realio-networkd && sudo journalctl -fu realio-networkd -o cat