#!/bin/bash
exists()
{
  command -v "$1" >/dev/null 2>&1
}
if exists curl; then
echo ''
else
  sudo apt update && sudo apt install curl -y < "/dev/null"
fi
bash_profile=$HOME/.bash_profile
if [ -f "$bash_profile" ]; then
    . $HOME/.bash_profile
fi
sleep 1 && curl -s https://api.nodes.guru/logo.sh | bash && sleep 1

if [ ! $collator ]; then
read -p "Enter collator name: " collator
echo 'export collator='\"${collator}\" >> $HOME/.bash_profile
fi
echo 'source $HOME/.bashrc' >> $HOME/.bash_profile
. $HOME/.bash_profile

echo -e '\n\e[42mSet up swapfile\e[0m\n'
curl -s https://api.nodes.guru/swap8.sh | bash

sleep 1
cd $HOME
sudo apt update && sudo apt install --assume-yes build-essential git clang curl libssl-dev llvm libudev-dev git  make protobuf-compiler unzip
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
source $HOME/.cargo/env
rustup default stable
rustup update
rustup update nightly
rustup target add wasm32-unknown-unknown --toolchain nightly
rustup show
rustup +nightly show
echo -e '\n\e[42mInstalling the subkey tool\e[0m\n' && sleep 1
cd $HOME
rm -rf substrate
git clone https://github.com/paritytech/substrate.git
cd substrate
cargo +nightly build --package subkey --release
sudo mv target/release/subkey /usr/local/bin/ || exit
subkey generate >$HOME/key.txt
echo "-----------------------------------------------------------------------------------------------"
cat $HOME/key.txt
echo "-----------------------------------------------------------------------------------------------"
echo -e "\033[41m\033[30mPLEASE REMEMBER TO SAVE THE SECRET PHRASE AND KEY.\033[0m\n"
sleep 3

mkdir -p ~/t0rn/{data,specs}
curl -sSfL \
  https://github.com/t3rn/t3rn/releases/download/v1.2.0-rc.5/t0rn-collator-v1.2.0-rc.5-x86_64-unknown-linux-gnu.gz | gunzip > ~/t0rn/circuit-collator
chmod +x $HOME/t0rn/circuit-collator

curl -sSfL \
  -o ~/t0rn/specs/rococo.raw.json \
  https://raw.githubusercontent.com/t3rn/t3rn/v1.2.0-rc.5/specs/rococo.raw.json

curl -sSfL \
  -o ~/t0rn/specs/t0rn.raw.json \
  https://raw.githubusercontent.com/t3rn/t3rn/v1.2.0-rc.5/specs/t0rn.raw.json

#t0rn_boot_node=/ip4/159.69.77.34/tcp/33333/p2p/12D3KooWEXwTktuERRvV3NCqmBuco7bYBaU7zRnwQCp4GEDYpS1v
t0rn_boot_node=/dns/bootnode.t0rn.io/tcp/33333/p2p/12D3KooWEepV69XCJB4Zi193cZcm5W22ZR62DEP84iLFTUKVPtwp
rococo_boot_node=/ip4/34.90.151.124/tcp/30333/p2p/12D3KooWF7BUbG5ErMZ47ZdarRwtpZamgcZqxwpnFzkhjc1spHnP


echo "[Unit]
Description=Collator
After=network-online.target
[Service]
User=$USER
ExecStart=$HOME/t0rn/circuit-collator --collator --name $collator --base-path $HOME/t0rn/data --chain $HOME/t0rn/specs/t0rn.raw.json --bootnodes "$t0rn_boot_node" --port 33333 --rpc-port 8833 --prometheus-port 7001 --telemetry-url 'wss://telemetry.polkadot.io/submit 1' --ws-port 9933 --execution Wasm --pruning=archive --rpc-methods=unsafe -- --chain $HOME/t0rn/specs/rococo.raw.json --bootnodes "$rococo_boot_node" --port 10001 --rpc-port 8001 --ws-port 9001 --execution Wasm
Restart=always
RestartSec=10
LimitNOFILE=10000
[Install]
WantedBy=multi-user.target
" > $HOME/t0rn.service
sudo mv $HOME/t0rn.service /etc/systemd/system/

echo -e '\n\e[42mEnabling t0rn services\e[0m\n' && sleep 1
sudo systemctl daemon-reload
sudo systemctl enable t0rn
sudo systemctl restart t0rn

sleep 10

phrase=$(grep "Secret phrase:" $HOME/key.txt | sed 's/Secret phrase:       //')

$HOME/t0rn/circuit-collator \
  key \
  insert \
  --base-path ~/t0rn/data \
  --chain ~/t0rn/specs/t0rn.raw.json \
  --scheme Sr25519 \
  --suri "$phrase" \
  --key-type aura

