#!/bin/bash
# set -x # uncomment to enable debug
networkrpcURL=$(cat /root/.config/solana/cli/config.yml | grep json_rpc_url | grep -o '".*"' | tr -d '"')
if [ "$networkrpcURL" == "" ]; then networkrpcURL=$(cat /root/.config/solana/cli/config.yml | grep json_rpc_url | awk '{ print $2 }')
fi




if [ $networkrpcURL = https://api.testnet.solana.com ]; then
echo "OK. This is testnet!"
curl -fsSL https://api.margus.one/solana/finder.sh | bash
elif [ $networkrpcURL = https://api.mainnet-beta.solana.com ]; then
echo "OK. This is mainnet!"
echo "###################### WARNING!!! ###################################"
echo "###   This script will perform the following operations:          ###"
echo "###   * delete ledger and snapshots                               ###"
echo "###   * download snapshot                                         ###"
echo "###   * wait for catchup                                          ###"
echo "###                                                               ###"
echo "###   *** Script provided by MARGUS.ONE                           ###"
echo "#####################################################################"

catchup_info() {
  while true; do
    rpcPort=$(ps aux | grep solana-validator | grep -Po "\-\-rpc\-port\s+\K[0-9]+")
    sudo -i -u root solana catchup --our-localhost $rpcPort
    status=$?
    if [ $status -eq 0 ];then
      exit 0
    fi
    echo "waiting next 30 seconds for rpc"
    sleep 30
  done
}

service_file=/root/solana/solana.service
LEDGER=$(cat $service_file | grep "\--ledger" | awk '{ print $2 }' )
SNAPSHOTS=$(cat $service_file | grep "\--snapshots" | awk '{ print $2 }' )
INC_SNAPSHOTS=$(cat $service_file | grep "\--incremental-snapshot-archive-path" | awk '{ print $2 }' )
if [ "$INC_SNAPSHOTS" == "" ]; then INC_SNAPSHOTS=$SNAPSHOTS
fi
IP=$(wget -q -4 -O- http://icanhazip.com)

if [ "$LEDGER" == "" ]; then 
service_file=/root/solana/validator.sh
LEDGER=$(cat $service_file | grep "\--ledger" | awk '{ print $2 }' )
SNAPSHOTS=$(cat $service_file | grep "\--snapshots" | awk '{ print $2 }' )
INC_SNAPSHOTS=$(cat $service_file | grep "\--incremental-snapshot-archive-path" | awk '{ print $2 }' )
if [ "$INC_SNAPSHOTS" == "" ]; then INC_SNAPSHOTS=$SNAPSHOTS
fi
fi

if [ "$LEDGER" == "" ]; then LEDGER=/root/solana/ledger
fi

if [ -f $service_file ]; then
systemctl stop solana

if [ "$SNAPSHOTS" == "" ]; then 
    SNAPSHOTS=$LEDGER
    rm -fr $SNAPSHOTS/*
else
    rm -fr $LEDGER/*
    rm -fr $SNAPSHOTS/*
fi

if ! [ -d $SNAPSHOTS ]; then
mkdir $SNAPSHOTS
fi

cd $SNAPSHOTS

if [ ${IP:0:10} == "91.211.83." ];then
wget --trust-server-names http://mainnet.solana.margus.one/snapshot.tar.bz2 && wget --trust-server-names http://mainnet.solana.margus.one/incremental-snapshot.tar.bz2 -P $INC_SNAPSHOTS
elif [ ${IP:0:10} == "185.81.65." ];then
wget --trust-server-names http://176.99.142.182/snapshot.tar.bz2 && wget --trust-server-names http://176.99.142.182/incremental-snapshot.tar.bz2  -P $INC_SNAPSHOTS
elif [ ${IP} == "5.189.79.58" ];then
wget --trust-server-names http://mainnet.solana.margus.one/snapshot.tar.bz2 && wget --trust-server-names http://mainnet.solana.margus.one/incremental-snapshot.tar.bz2  -P $INC_SNAPSHOTS
elif [ ${IP} == "92.248.252.110" ];then
wget --trust-server-names http://mainnet.solana.margus.one/snapshot.tar.bz2 && wget --trust-server-names http://mainnet.solana.margus.one/incremental-snapshot.tar.bz2  -P $INC_SNAPSHOTS
elif [ ${IP:0:10} == "195.18.27." ];then
wget --trust-server-names http://mainnet.solana.margus.one/snapshot.tar.bz2 && wget --trust-server-names http://mainnet.solana.margus.one/incremental-snapshot.tar.bz2  -P $INC_SNAPSHOTS
elif [ ${IP:0:11} == "194.187.206" ];then
wget --trust-server-names http://mainnet.solana.margus.one/snapshot.tar.bz2 && wget --trust-server-names http://mainnet.solana.margus.one/incremental-snapshot.tar.bz2  -P $INC_SNAPSHOTS
elif [ ${IP:0:11} == "183.81.169." ];then
curl -fsSL https://api.margus.one/solana/finder.sh | bash
exit 0
elif [ ${IP:0:10} == "23.160.72." ];then
curl -fsSL https://api.margus.one/solana/finder.sh | bash
exit 0
else
curl -fsSL https://api.margus.one/solana/mainnet/get_snapshot.sh | bash
exit 0
fi

systemctl start solana
catchup_info

else
echo "solana.service not found! Default: /root/solana/solana.service"
fi
else
echo "ERROR"
fi
