#!/bin/bash

echo -e "\033[0;35m"
echo "  ___          _ _       ___        _           ";
echo " / __|_ _  ___| | |_  _ / __|___ __| |_ ___ _ _ ";
echo " \__ \ ' \/ _ \ | | || | (_ / _ (_-<  _/ -_) '_|";
echo " |___/_||_\___/_|_|\_, |\___\___/__/\__\___|_|  ";
echo "                   |__/                         ";
echo -e "\e[0m"


sleep 2

# set vars
if [ ! $NODENAME ]; then
    read -p "Enter your moniker: " MONIKER
    echo 'export MONIKER='$MONIKER >> $HOME/.bash_profile
fi

read -p "Enter your desired port (default :26): " PORT
PORT=${PORT}

CHAIN_ID="initiation-1"
CHAIN_DENOM="uinit"
BINARY="initiad"

echo '================================================='
echo -e "Node moniker: \e[1m\e[32m$MONIKER\e[0m"
echo -e "Chain id:     \e[1m\e[32m$CHAIN_ID\e[0m"
echo -e "Chain demon:  \e[1m\e[32m$CHAIN_DENOM\e[0m"
echo '================================================='
sleep 2

echo -e "\e[1m\e[32m1. Updating packages... \e[0m" && sleep 1
# update & upgrade
sudo apt update && sudo apt upgrade -y

echo -e "\e[1m\e[32m2. Installing dependencies... \e[0m" && sleep 1
# install packages
sudo apt install curl tar wget clang pkg-config libssl-dev jq build-essential bsdmainutils git make ncdu gcc git jq chrony liblz4-tool -y

echo - e "\e[1m\e[32m3. Installing Golang... \e[0m" && sleep 1
# install go
if ! [ -x "$(command -v go)" ]; then
cd $HOME
sudo rm -rf /usr/local/go
curl -Ls https://go.dev/dl/go1.22.3.linux-amd64.tar.gz | sudo tar -xzf - -C /usr/local
eval $(echo 'export PATH=$PATH:/usr/local/go/bin' | sudo tee /etc/profile.d/golang.sh)
eval $(echo 'export PATH=$PATH:$HOME/go/bin' | tee -a $HOME/.profile)
fi

echo -e "\e[1m\e[32m4. Downloading and building binaries... \e[0m" && sleep 1
# download and build binaries
go install cosmossdk.io/tools/cosmovisor/cmd/cosmovisor@v1.5.0


cd
rm -rf initia
git clone git@github.com:initia-labs/initia.git
cd initia
git fetch
git checkout v0.2.14
make build
mkdir -p $HOME/.initia/cosmovisor/genesis/bin
mkdir -p $HOME/.initia/cosmovisor/upgrades
sudo ln -s $HOME/.initia/cosmovisor/genesis $HOME/.initia/cosmovisor/current -f
sudo ln -s $HOME/.initia/cosmovisor/current/bin/initiad /usr/local/bin/initiad -f

echo -e "\e[1m\e[32m5. Setting config... \e[0m" && sleep 1
initiad config chain-id $CHAIN_ID
initiad init $MONIKER  --chain-id $CHAIN_ID
initiad config keyring-backend file
initiad config node tcp://localhost:${PORT}657

# download genesis and addrbook
curl -Ls https://snapshots.kjnodes.com/initia-testnet/genesis.json > $HOME/.initia/config/genesis.json
curl -Ls https://snapshots.kjnodes.com/initia-testnet/addrbook.json > $HOME/.initia/config/addrbook.json

# Set minimum gas price , seeds , and peers
#SEEDS=`curl -sL https://raw.githubusercontent.com/planq-network/networks/main/mainnet/seeds.txt | awk '{print $1}' | paste -s -d, -`
sed -i.bak -e "s/^seeds =.*/seeds = \"3f472746f46493309650e5a033076689996c8881@initia-testnet.rpc.kjnodes.com:17959\"/" ~/.initia/config/config.toml
#sed -i 's/max_num_inbound_peers =.*/max_num_inbound_peers = 100/g' $HOME/.initia/config/config.toml
#sed -i 's/max_num_outbound_peers =.*/max_num_outbound_peers = 100/g' $HOME/.initia/config/config.toml
sed -i.bak -e "s/^minimum-gas-prices *=.*/minimum-gas-prices = \"0.15uinit,0.01uusdc\"/;" ~/.initia/config/app.toml
indexer="null" && \
sed -i -e "s/^indexer *=.*/indexer = \"$indexer\"/" $HOME/.initia/config/config.toml
sed -i -e "s/prometheus = false/prometheus = true/" $HOME/.initia/config/config.toml
#initiad tendermint unsafe-reset-all --home $HOME/.initia --keep-addr-book

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

# Custom Port
sed -i.bak -e "s%^proxy_app = \"tcp://127.0.0.1:26658\"%proxy_app = \"tcp://127.0.0.1:${PORT}658\"%; s%^laddr = \"tcp://127.0.0.1:26657\"%laddr = \"tcp://127.0.0.1:${PORT}657\"%; s%^pprof_laddr = \"localhost:6060\"%pprof_laddr = \"localhost:${PORT}060\"%; s%^laddr = \"tcp://0.0.0.0:26656\"%laddr = \"tcp://0.0.0.0:${PORT}656\"%; s%^prometheus_listen_addr = \":26660\"%prometheus_listen_addr = \"14660\"%" $HOME/.initia/config/config.toml
sed -i.bak -e "s%^address = \"tcp://0.0.0.0:1317\"%address = \"tcp://0.0.0.0:${PORT}17\"%; s%^address = \":8080\"%address = \":${PORT}80\"%; s%^address = \"0.0.0.0:9090\"%address = \"0.0.0.0:${PORT}90\"%; s%^address = \"0.0.0.0:9091\"%address = \"0.0.0.0:${PORT}91\"%; s%^address = \"0.0.0.0:8545\"%address = \"0.0.0.0:${PORT}45\"%; s%^ws-address = \"0.0.0.0:8546\"%ws-address = \"0.0.0.0:${PORT}46\"%" $HOME/.initia/config/app.toml

echo -e "\e[1m\e[32m6. Starting service... \e[0m" && sleep 1
# create service
sudo tee /etc/systemd/system/initia.service > /dev/null << EOF
[Unit]
Description=initia service
After=network-online.target

[Service]
User=$USER
ExecStart=$(which cosmovisor) run start
Restart=on-failure
RestartSec=10
LimitNOFILE=65535
Environment="DAEMON_HOME=$HOME/.initia"
Environment="DAEMON_NAME=initiad"
Environment="UNSAFE_SKIP_BACKUP=true"

[Install]
WantedBy=multi-user.target
EOF

# start service
sudo systemctl enable initia && sudo systemctl daemon-reload

echo 'SETUP COMPLETED, YOU NEED TO INSTALL YOUR MONIKER AND SNAPSHOT BEFORE START SERVICE'
echo 'sudo systemctl start initia' to start service
echo 'sudo journalctl -fu initia -o cat' to check your logs

echo '=============== SETUP FINISHED ==================='