#!/bin/bash

GREEN="\e[1m\e[1;32m"    
RED="\e[1m\e[1;31m"      
BLUE='\033[0;34m'        
NC="\e[0m"
YELLOW='\033[1;33m'      
PURPLE='\033[0;35m'      
CYAN='\033[0;36m'        
WHITE='\033[1;37m'       
ORANGE='\033[0;33m'      
PINK='\033[1;35m'        

source <(curl -s https://raw.githubusercontent.com/SirBell/Nodevism-tools/main/autoinstall/common.sh)

printLogo

echo -e "${CYAN}Enter WALLET name:${NC}"
read -p "" WALLET
echo 'export WALLET='$WALLET
echo -e "${CYAN}Enter your MONIKER:${NC}"
read -p "" MONIKER
echo 'export MONIKER='$MONIKER
echo -e "${CYAN}Enter your PORT (for example 17, default port=26):${NC}"
read -p "" PORT
echo 'export PORT='$PORT

# set vars
echo "export WALLET="$WALLET"" >> $HOME/.bash_profile
echo "export MONIKER="$MONIKER"" >> $HOME/.bash_profile
echo "export LUMERA_CHAIN_ID="lumera-testnet-1"" >> $HOME/.bash_profile
echo "export LUMERA_PORT="$PORT"" >> $HOME/.bash_profile
source $HOME/.bash_profile

printLine
echo -e "${YELLOW}Moniker:${NC}        ${GREEN}$MONIKER${NC}"
echo -e "${YELLOW}Wallet:${NC}         ${GREEN}$WALLET${NC}"
echo -e "${YELLOW}Chain id:${NC}       ${GREEN}$LUMERA_CHAIN_ID${NC}"
echo -e "${YELLOW}Node custom port:${NC}  ${GREEN}$LUMERA_PORT${NC}"
printLine
sleep 1

printGreen "1. Installing go..." && sleep 1
# install go, if needed
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

echo $(go version) && sleep 1

source <(curl -s https://raw.githubusercontent.com/SirBell/Nodevism-tools/main/autoinstall/depedencies_install)

printGreen "4. Installing binary..." && sleep 1
# download binary
cd $HOME
rm -rf lumera
curl -LO https://github.com/LumeraProtocol/lumera/releases/download/v0.4.1/lumera_v0.4.1_linux_amd64.tar.gz
tar -xvf lumera_v0.4.1_linux_amd64.tar.gz
rm lumera_v0.4.1_linux_amd64.tar.gz
sudo mv libwasmvm.x86_64.so /usr/lib/
chmod +x lumerad
sudo mv lumerad $HOME/go/bin/
echo done

printGreen "5. Configuring and init app..." && sleep 1
# config and init app
lumerad init $MONIKER --chain-id $LUMERA_CHAIN_ID
sed -i -e "s|^node *=.*|node = \"tcp://localhost:${LUMERA_PORT}657\"|" $HOME/.lumera/config/client.toml
sed -i -e "s|^keyring-backend *=.*|keyring-backend = \"os\"|" $HOME/.lumera/config/client.toml
sed -i -e "s|^chain-id *=.*|chain-id = \"lumera_11822-1\"|" $HOME/.lumera/config/client.toml
sleep 1
echo done

printGreen "6. Downloading genesis and addrbook..." && sleep 1
# download genesis and addrbook
curl -Ls https://snapshot.nodevism.com/testnet/lumera/genesis.json > $HOME/.lumera/config/genesis.json
curl -Ls https://snapshot.nodevism.com/testnet/lumera/addrbook.json > $HOME/.lumera/config/addrbook.json
sleep 1
echo done

printGreen "7. Adding seeds, peers, configuring custom ports, pruning, minimum gas price..." && sleep 1
# set seeds and peers
SEEDS="74db5018248af4207209d8de6f2f68f44480688@193.34.212.38:31656"
PEERS="$(curl -sS https://rpc.lumera-t.nodevism.com/net_info | jq -r '.result.peers[] | "\(.node_info.id)@\(.remote_ip):\(.node_info.listen_addr)"' | awk -F ':' '{print $1":"$(NF)}' | sed -z 's|\n|,|g;s|.$||')"
sed -i.bak -e "s/^seeds *=.*/seeds = \"$SEEDS\"/; s/^persistent_peers *=.*/persistent_peers = \"$PEERS\"/" $HOME/.lumera/config/config.toml

# set custom ports in app.toml
sed -i.bak -e "s%:1317%:${LUMERA_PORT}317%g;
s%:8080%:${LUMERA_PORT}080%g;
s%:9090%:${LUMERA_PORT}090%g;
s%:9091%:${LUMERA_PORT}091%g;
s%:8545%:${LUMERA_PORT}545%g;
s%:8546%:${LUMERA_PORT}546%g;
s%:6065%:${LUMERA_PORT}065%g" $HOME/.lumera/config/app.toml


# set custom ports in config.toml file
sed -i.bak -e "s%:26658%:${LUMERA_PORT}658%g;
s%:26657%:${LUMERA_PORT}657%g;
s%:6060%:${LUMERA_PORT}060%g;
s%:26656%:${LUMERA_PORT}656%g;
s%^external_address = \"\"%external_address = \"$(wget -qO- eth0.me):${LUMERA_PORT}656\"%;
s%:26660%:${LUMERA_PORT}660%g" $HOME/.lumera/config/config.toml

# config pruning
sed -i -e "s/^pruning *=.*/pruning = \"custom\"/" $HOME/.lumera/config/app.toml
sed -i -e "s/^pruning-keep-recent *=.*/pruning-keep-recent = \"100\"/" $HOME/.lumera/config/app.toml
sed -i -e "s/^pruning-interval *=.*/pruning-interval = \"20\"/" $HOME/.lumera/config/app.toml

# set minimum gas price, enable prometheus and disable indexing
sed -i 's|minimum-gas-prices =.*|minimum-gas-prices = "0.25art"|g' $HOME/.lumera/config/app.toml
sed -i -e "s/prometheus = false/prometheus = true/" $HOME/.lumera/config/config.toml
sed -i -e "s/^indexer *=.*/indexer = \"null\"/" $HOME/.lumera/config/config.toml
sleep 1
echo done

# create service file
sudo tee /etc/systemd/system/lumerad.service > /dev/null <<EOF
[Unit]
Description=lumera node
After=network-online.target
[Service]
User=$USER
WorkingDirectory=$HOME/.lumera
ExecStart=$(which lumerad) start --home $HOME/.lumera
Restart=on-failure
RestartSec=5
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
EOF

printGreen "8. Downloading snapshot and starting node..." && sleep 1
# reset and download snapshot
lumerad tendermint unsafe-reset-all --home $HOME/.lumera
if curl -s --head curl https://snapshot.nodevism.com/testnet/lumera/lumera-snapshot.tar.lz4 | head -n 1 | grep "200" > /dev/null; then
  curl https://snapshot.nodevism.com/testnet/lumera/lumera-snapshot.tar.lz4 | lz4 -dc - | tar -xf - -C $HOME/.lumera
    else
  echo "${GREEN}No snapshot found${NC}"
fi

# enable and start service
sudo systemctl daemon-reload
sudo systemctl enable lumerad
sudo systemctl restart lumerad

# Print a line separator
printLine

# Print setup finished notification and logo
echo -e "${CYAN}=============== SETUP FINISHED ===================${NC}"
source <(curl -s https://raw.githubusercontent.com/SirBell/Nodevism-tools/main/autoinstall/common.sh)
printLogo

# Output various status checks
echo -e "${GREEN}Check running logs${NC}:            ${CYAN}sudo journalctl -u lumerad -f --no-hostname -o cat${NC}"
echo -e "${GREEN}Check sync status${NC}:             ${CYAN}lumerad status | jq${NC}"
echo -e "${GREEN}Get Last Height${NC}:               ${CYAN}lumerad status 2>&1 | jq -r '.SyncInfo.latest_block_height // .sync_info.latest_block_height'${NC}"
echo -e "${GREEN}Check node status${NC}:             ${CYAN}curl -s localhost:${lumerad_PORT}657/status | jq .result${NC}"
