#!/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 GNOLAND_PORT="42"" >> $HOME/.bash_profile
source $HOME/.bash_profile


echo -e "Moniker:        \e[1m\e[32m$MONIKER\e[0m"
echo -e "Node custom port:  \e[1m\e[32m$GNOLAND_PORT\e[0m"
sleep 1


# Update & install dependencies
sudo apt update && sudo apt upgrade -y
sudo apt install curl unzip 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.23.1"
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 gno
git clone https://github.com/gnolang/gno.git
cd gno
git checkout chain/test11
make -C gno.land install.gnoland && make -C contribs/gnogenesis install && make install_gnokey
gnoland --help


# Config and Init App
cd $HOME
gnoland secrets init && gnoland config init
gnoland config set rpc.laddr tcp://0.0.0.0:${GNOLAND_PORT}657
gnoland config set p2p.laddr tcp://0.0.0.0:${GNOLAND_PORT}656
gnoland config set moniker $MONIKER
gnoland config set consensus.peer_gossip_sleep_duration 10ms
gnoland config set consensus.timeout_commit 3s
gnoland config set mempool.size 10000
gnoland config set p2p.flush_throttle_timeout 10ms
gnoland config set p2p.max_num_outbound_peers 40
gnoland config set p2p.persistent_peers g10l8g7l5u0ahysgj0jczkvwdvct506khfrvf2yz@gnolan-testnet-rpc.shazoes.xyz:42656,g1vgvqg94xy8qj23dc8zpw6wns7q0hj9g8mx03ha@gno-core-sen-01.test11.testnets.gno.land:26656
gnoland config set p2p.seeds g10l8g7l5u0ahysgj0jczkvwdvct506khfrvf2yz@gnolan-testnet-rpc.shazoes.xyz:42656,g1vgvqg94xy8qj23dc8zpw6wns7q0hj9g8mx03ha@gno-core-sen-01.test11.testnets.gno.land:26656


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


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

[Service]
User=$USER
ExecStart=$(which gnoland) start --genesis $HOME/gnoland-data/config/genesis.json --data-dir $HOME/gnoland-data --skip-genesis-sig-verification
Restart=on-failure
RestartSec=3
LimitNOFILE=65535

[Install]
WantedBy=multi-user.target
EOF


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