#!/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 FIAMMA_CHAIN_ID="fiamma-testnet-1"" >> $HOME/.bash_profile
echo "export FIAMMA_PORT="28"" >> $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$FIAMMA_CHAIN_ID\e[0m"
echo -e "Node custom port:  \e[1m\e[32m$FIAMMA_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 aria2 -y

# Install Go
cd $HOME
VER="1.22.3"
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 fiamma
git clone https://github.com/fiamma-chain/fiamma.git
cd fiamma
git checkout v1.0.0
make install



# Config and Init App
fiammad init $MONIKER --chain-id $FIAMMA_CHAIN_ID


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



#Configure Seeds and Peers
SEEDS=""
PEERS="c0bb1182753ed71aa22b1427b8265e782cab260a@135.181.139.249:41656,2f1f3bee3f9c946d1f91f078de09e313956a618e@161.97.167.196:20656,48cc8b28f2b7d07263936e5e2a7130bb01df1872@84.247.187.77:37656,b0335d1c77fa96c27458390e6c48d2bf74c1533b@176.9.24.46:50656,fd8af2419e8a1cd9198066809465cb11c63b5428@148.251.128.49:29656,5ead2fb9ef45dd786cc9bb805400e0a75037f7f8@135.125.97.162:30256,cd574a3d8c022e8a6f19b010230069ba9987d905@164.132.247.253:56396,1c9def41279dfc0fe2cc7c8de683d852f70bb2fc@195.26.241.17:26656,1a0ff9e5c08df76b19556b50aff82ec6dfd5beee@65.109.53.24:45656"
sed -i -e "s/^seeds *=.*/seeds = \"$SEEDS\"/; s/^persistent_peers *=.*/persistent_peers = \"$PEERS\"/" $HOME/.fiamma/config/config.toml
sed -i -e "s/^minimum-gas-prices *=.*/minimum-gas-prices = \"0.00001ufia\"/" $HOME/.fiamma/config/app.toml
sed -i -e 's|^indexer *=.*|indexer = "null"|' $HOME/.fiamma/config/config.toml
sed -i 's|^prometheus *=.*|prometheus = true|' $HOME/.fiamma/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/.fiamma/config/app.toml
sed -i -e "s/^pruning-keep-recent *=.*/pruning-keep-recent = \"$pruning_keep_recent\"/" $HOME/.fiamma/config/app.toml
sed -i -e "s/^pruning-keep-every *=.*/pruning-keep-every = \"$pruning_keep_every\"/" $HOME/.fiamma/config/app.toml
sed -i -e "s/^pruning-interval *=.*/pruning-interval = \"$pruning_interval\"/" $HOME/.fiamma/config/app.toml


# Set Custom Port
sed -i.bak -e "s%:1317%:${FIAMMA_PORT}317%g;
s%:8080%:${FIAMMA_PORT}080%g;
s%:9090%:${FIAMMA_PORT}090%g;
s%:9091%:${FIAMMA_PORT}091%g;
s%:8545%:${FIAMMA_PORT}545%g;
s%:8546%:${FIAMMA_PORT}546%g;
s%:6065%:${FIAMMA_PORT}065%g" $HOME/.fiamma/config/app.toml
sed -i.bak -e "s%:26658%:${FIAMMA_PORT}658%g;
s%:26657%:${FIAMMA_PORT}657%g;
s%:6060%:${FIAMMA_PORT}060%g;
s%:26656%:${FIAMMA_PORT}656%g;
s%^external_address = \"\"%external_address = \"$(wget -qO- eth0.me):${FIAMMA_PORT}656\"%;
s%:26660%:${FIAMMA_PORT}660%g" $HOME/.fiamma/config/config.toml
sed -i \
  -e 's|^chain-id *=.*|chain-id = "fiamma-testnet-1"|' \
  -e 's|^keyring-backend *=.*|keyring-backend = "test"|' \
  -e 's|^node *=.*|node = "tcp://localhost:28657"|' \
  $HOME/.fiamma/config/client.toml


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

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

[Install]
WantedBy=multi-user.target
EOF


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