#!/bin/bash

customMotd()
{
    MOTD_FILE="/etc/motd"
    export CONTAINER=`docker ps | grep remoteconnect.avado.dnp.dappnode.eth | cut -d" " -f1`
    NETWORK=""
    export NETWORK=`docker exec ${CONTAINER} getnetwork`
    
    if [ -f ${MOTD_FILE} ]; then
        cat <<EOF > ${MOTD_FILE}

============================================================
Welcome to AVADO. Your RemoteConnect network is ${NETWORK}
To connect to this AVADO from your RemoteConnect client

On this AVADO
 1. Enter this command:
    "addclient <NodeID>"
    where <NodeID> is the ID of your client

On your client device
 1. Join the network ${NETWORK} (tip : scan the QR code below)
 2. Open your browser and go to http://my.ava.do/

EOF
        qrencode -t ANSI ${NETWORK} >> ${MOTD_FILE}
        echo "============================================================" >> ${MOTD_FILE}
    fi
}

if [ "$EUID" -ne 0 ]
  then 
   echo "Please run as root by typing"
   echo "sudo <script>"
  exit
fi

if [[ $(docker ps -aq|wc -l) -eq 0 ]]
then
  echo "No containers running"
else
 echo "Stopping containers"
 docker ps -aq | xargs docker rm -f
fi
 docker system prune --volumes -f
 echo "Reinstalling system"
 sudo /usr/src/dappnode/scripts/dappnode_install.sh

 reboot


