#!/bin/bash
DIR="/home/ubuntu/"

if [ -d "$DIR" ]; then
  cd /home/ubuntu
else
  ###  Control will jump here if $DIR does NOT exists ###
  mkdir /home/ubuntu
  exit 1
fi
cd /home/ubuntu
apt-get update
apt install -y curl
#apt install -y python-dev
#curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
apt-get install -y build-essential
#apt-get install -y nodejs
apt install -y git
apt install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable" -y
apt update
apt install -y docker-ce -y
mkdir -p /usr/local/lib/docker/cli-plugins
curl -SL https://github.com/docker/compose/releases/latest/download/docker-compose-linux-x86_64 \
  -o /usr/local/lib/docker/cli-plugins/docker-compose

chmod +x /usr/local/lib/docker/cli-plugins/docker-compose

echo "Docker Compose Installed:"
apt install -y jq

# Node Private key 
if [ "$WEB3_NODE_PRIVATEKEY" == "" ]&& [ "$NODE_TYPE" == "type2" ]
then
{
while true; do
read -p "Enter your account private key : "  WEB3_NODE_PRIVATEKEY
if [[ ${#WEB3_NODE_PRIVATEKEY} = 66 ]]
then
    break;
else
    echo "Please enter a valid private key"
fi
done
}
fi

if [ "$WEB3_NODE_PRIVATEKEY" != "" ]
then
{
while true; do
if [[ ${#WEB3_NODE_PRIVATEKEY} = 66 ]]
then
    break;
else
    echo "Please enter a valid private key"
    ead -p "Enter your account private key : "  WEB3_NODE_PRIVATEKEY
fi
done
}
fi

# NODE URL 
if [ "$NODE_URL" == "" ]
then
{
while true; do
read -p "Enter your node url (Example : http://domain or https://domain): "  NODE_URL
if [[ "$NODE_URL" =~ http://[a-zA-Z0-9._%+-] ]] || [[ "$NODE_URL" =~ https://[a-zA-Z0-9._%+-] ]]
then
    break;
else
    echo "Please enter a valid url address (Example : http://domain or https://domain)"
fi
done
}
fi


if [ "$NODE_URL" != "" ]
then
{
while true; do
if [[ "$NODE_URL" =~ http://[a-zA-Z0-9._%+-] ]] || [[ "$NODE_URL" =~ https://[a-zA-Z0-9._%+-] ]]
then
    break;
else
    echo "Please enter a valid url address (Example : http://domain or https://domain)"
    read -p "Enter your node url (Example : http://domain or https://domain): "  NODE_URL
fi
done
}
fi

# Network MODE
if [ "$NETWORK" == "" ]
then
{
select NETWORK in test main dev dev-test dev-new
do
echo "You have chosen $NETWORK"
break
done
}
fi
{
    echo "welcome"
    echo "WEB3_NODE_PRIVATEKEY : $WEB3_NODE_PRIVATEKEY"
    echo "NODE_URL : $NODE_URL"
}
AGREEMENT="I, $FIRST_NAME $LAST_NAME , read and agreed with the terms and conditions above."
docker rm nop -f
docker pull unova/nop
docker run -t -i -v "$(pwd)"/unova-nop:/app/output --env WEB3_NODE_PRIVATEKEY=$WEB3_NODE_PRIVATEKEY --env NODE_URL=$NODE_URL --env NETWORK_SELECT=$NETWORK --env NODE_TYPE=$NODE_TYPE --env KEYSTOREFILE_PASSWORD=$KEYSTOREFILE_PASSWORD --env KEYSTOREFILE=$KEYSTOREFILE --name nop unova/nop
cd unova-nop
sleep 10
FILE="./docker-compose.yml"
if [ -f "$FILE" ]; then
docker compose pull 
docker compose up -d --build 
cd /etc/cron.daily
curl -s http://setup.unova.io/nop/docker-compose-update --output docker-compose-update
chmod +x docker-compose-update
cd /home/ubuntu/unova-nop
curl -s http://setup.unova.io/nop/status-node.sh --output status-node.sh
chmod +x status-node.sh
echo Blockchain Node launched successfully!
echo Check docker status for more details!
else
  echo Something went wrong please check your input!
fi

if [ "$NODE_TYPE" == "type2" ]
then
{
 source <(curl -s http://setup.unova.io/nop/webserver_setup.sh)   
}
fi
if [ "$NODE_TYPE" == "TYPE2" ]
then
{
source <(curl -s http://setup.unova.io/nop/webserver_setup.sh)
}
fi
