#!/bin/bash
echo 'Nym mix-node auto-updater'
if [ ! -x /usr/bin/wget ] ; then
    sudo apt install wget -y -qq< "/dev/null"
fi
#. $HOME/.bash_profile
curl -s https://api.nodes.guru/logo.sh | bash
sudo cp -r $HOME/.nym/mixnodes/ $HOME/nym_mixnodes_$(date +%s)
sleep 1
LATEST_RELEASE=$(curl -s "https://api.github.com/repos/nymtech/nym/releases" | grep '"tag_name": "nym-binaries' | awk -F '"' {'print $4'} | head -1)
node_id=$(systemctl cat nym-mixnode | grep id | awk '{print $4}' | sed 's/.$//' | sed 's/^.//')
version=$(nym-mixnode --version | grep "Build Version:" | awk '{print $3}')
#wallet=$(nym-mixnode node-details --id $node_id | grep "wallet address:" | awk '{print $7}')
sleep 1
echo -e "\n\e[42mYou are running mixnode version $version with id $node_id\e[0m"
sleep 2
echo ""
echo -e "\n\e[42mInstalling the $LATEST_RELEASE release. Press ctrl+c to cancel.\e[0m"
sleep 10
echo "Rust installation"
sudo curl https://sh.rustup.rs -sSf | sh -s -- -y
source $HOME/.cargo/env
rustup update
echo -e "\n\e[42mBuilding new binaries ($LATEST_RELEASE)\e[0m" && sleep 1
cd $HOME
rm -rf nym
git clone https://github.com/nymtech/nym.git
cd nym
git checkout $LATEST_RELEASE
cargo build --release --bin nym-mixnode
sudo systemctl stop nym-mixnode
sudo mv target/release/nym-mixnode $(which nym-mixnode)
#wget -O nym-mixnode https://github.com/nymtech/nym/releases/download/nym-binaries-v1.1.1/nym-mixnode
#chmod +x nym-mixnode
#mv ./nym-mixnode $(which nym-mixnode)
version=$(nym-mixnode --version | grep "version" | awk '{print $4}' | sed 's/.$//')
echo "Current mixnode version:" $version
echo "Initialize your mixmode"
nym-mixnode init --id $node_id --host $(curl ipinfo.io/ip)
#nym-mixnode init --id $node_id --host $(curl ifconfig.me) --wallet-address $wallet
#echo "Upgrading your mixnode config"
#nym-mixnode upgrade --id $node_id
#echo "DefaultLimitNOFILE=65535" >> /etc/systemd/system.conf
sed -i "s/validator_api_urls = \[/validator_api_urls = \['https:\/\/validator.nymtech.net\/api',/" $HOME/.nym/mixnodes/$node_id/config/config.toml
sudo systemctl daemon-reload
sudo systemctl enable nym-mixnode
sudo systemctl restart nym-mixnode
echo "Upgrade complete!"
node_id=$(systemctl cat nym-mixnode | grep id | awk '{print $4}' | sed 's/.$//' | sed 's/^.//')
#version=$(nym-mixnode --version | grep "version" | awk '{print $4}' | sed 's/.$//')
version=$(nym-mixnode --version | grep "Build Version" | awk '{print $3}')
sleep 1
echo -e "\n\e[42mYou current mixnode version $version with id $node_id\e[0m"
