#!/usr/local/bin/bash

# Copyright (c) Inform Systems Group LLC by Alexey Ignatev, www.isg.dev
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# Update Polkadot version 1.1.7

SRC_DIR="/usr/local/polkadot-sdk"
LOG="/var/log/recompile_and_update.log"
SERVICE_NAME="polkadot" # name for command: service $SERV_NAME stop and start
OLDSERVICE_DIR="/usr/local/bin"
NEWSERVICE_DIR="${SRC_DIR}/target/release"
STOP_TIMEOUT="300"
POLKADOT_BINS="polkadot polkadot-execute-worker polkadot-prepare-worker polkadot-omni-node polkadot-parachain substrate-node"

send_log() {
    echo `date "+%Y.%m.%d %H:%M:%S"` ${1} >>${LOG}
    echo `date "+%Y.%m.%d %H:%M:%S"` ${1}
}

if [ -d "${SRC_DIR}" ]; then
    cd ${SRC_DIR}
else
    send_log "Directory ${SRC_DIR} with sources NOT FOUND! Exiting"
    exit 1
fi

source $HOME/.cargo/env >>${LOG} 2>&1

nice -n 20 cargo clean >>${LOG} 2>&1
if [ "$?" = "0" ]; then
    send_log "Sources clean successfull complete"
else
    send_log "Sources clean ERROR! Exiting"
    exit 1
fi

#cargo update >>${LOG} 2>&1
#if [ "$?" = "0" ]; then
#    send_log "Cargo update successfull complete"
#else
#    send_log "Cargo update ERROR! Exiting"
#    exit 1
#fi

nice -n 20 cargo build --release >>${LOG} 2>&1
if [ "$?" = "0" ]; then
    send_log "Sources build ${SERVICE_NAME} successfull complete"
else
    send_log "Compiling ${SERVICE_NAME} binary ERROR! Exiting"
    exit 1
fi

# stopping running version
DOT_PID="`cat /var/run/${SERVICE_NAME}.pid`"

send_log "Stopping polkadot service with PID: ${DOT_PID}"
service ${SERVICE_NAME} stop >>${LOG} 2>&1

STOP_COUNT="0"
send_log "Start waiting for pid "${DOT_PID}" successful stopped (timeout seconds: ${STOP_TIMEOUT})"
while ps -A | grep "${DOT_PID}" |grep -v "grep ${DOT_PID}"; do
    if [ "${STOP_COUNT}" -gt "${STOP_TIMEOUT}" ]; then
        send_log "Stopping ${SERVICE_NAME} TIMEOUT! Exiting..."
        exit 1
    else
        send_log "Waiting for ${SERVICE_NAME} successful stopped... (${STOP_COUNT} seconds...)"
        sleep 1
    fi
    STOP_COUNT=$((${STOP_COUNT} + 1 ))
done

sleep 5

# backup
for BIN in ${POLKADOT_BINS}; do
    if [ -x "${OLDSERVICE_DIR}/${BIN}" ]; then
	send_log "Backing up old binary: ${OLDSERVICE_DIR}/${BIN}"
	mv -f ${OLDSERVICE_DIR}/${BIN} ${OLDSERVICE_DIR}/${BIN}.old >>${LOG} 2>&1
    else
	send_log "Binary:${OLDSERVICE_DIR}/${BIN} NOT FOUND! Exiting"
	exit 1
    fi
done


#replace
for BIN in ${POLKADOT_BINS}; do
    if [ -x "${NEWSERVICE_DIR}/${BIN}" ]; then
	send_log "Stripping and copying new binary: ${NEWSERVICE_DIR}/${BIN}"
	strip ${NEWSERVICE_DIR}/${BIN}
	cp -f ${NEWSERVICE_DIR}/${BIN} ${OLDSERVICE_DIR}/${BIN} >>${LOG} 2>&1
    else
	send_log "Binary:${NEWSERVICE_DIR}/${BIN} NOT FOUND! Exiting"
	exit 1
    fi
done

service ${SERVICE_NAME} start >>${LOG} 2>&1
if [ "$?" = "0" ]; then
    send_log "Starting new ${SERVICE_NAME} binary successful, update complete"
else
    send_log "Starting new ${SERVICE_NAME} binary ERROR! Exiting"
fi
