#!/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.

# Automatic Polkadot payout reward version 1.1.3

LOG="/var/log/auto_claim_reward.log"
SERVICE_BIN="/usr/local/bin/yarn"

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

if [ -f /etc/rc.conf ]; then
    . /etc/rc.conf
else
    send_log "Including rc.conf error! Exiting..."
    exit 1
fi

if [ ! "${polkadot_claim}" = "Yes" ]; then
    send_log "Polkadot claim reward disabled. Exiting..."
    exit 1
fi

if [ ! -n "${polkadot_chain}" ]; then
    send_log "Variable polkadot_chain not found in rc.conf! Exiting..."
    exit 1
fi

SRC_DIR="/usr/local/polkadot_support/polkadot-k8s-payouts"

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

if [ ! -x "${SERVICE_BIN}" ]; then
    send_log "Service binary: ${SERVICE_BIN} NOT FOUND! Exiting"
    exit 1
fi

cd ${SRC_DIR}
${SERVICE_BIN} start -c ./config/main_${polkadot_chain}.yaml >>${LOG} 2>&1
