#!/bin/sh
#
#  SLEEP 80 seconds before executing the scripts.
#
#####

SDIR="/usr/local/etc/solajithome/SettingsXML"

SYSFILE="${SDIR}/SystemSetting.xml"
DEFFILE="${SDIR}/DefaultSystemSetting.xml"
LASTFILE="${SDIR}/LastValidSystemSetting.xml"

IEDEN_TIME_LOG="/tmp/ieden_time.log"

Recover_SystemSetting() {
    if [ -e ${SYSFILE} ] ; then
        cp ${SYSFILE} /var/tmp/SystemSetting.bak
        rm -f ${SYSFILE}
    fi

    if [ -e ${LASTFILE} ] ; then
        # Back Upがある場合
        #Back up file size check 
        DEFSIZE=`wc -c ${DEFFILE} |awk '{print $1}'`
        LASTSIZE=`wc -c ${LASTFILE} |awk '{print $1}'`

        if [ $DEFSIZE -gt $LASTSIZE ] ; then
            # echo Size of ${LASTFILE} is smaller than ${DEFFILE}.
            rm -f ${LASTFILE}
        fi
    fi

    if [ -e ${LASTFILE} ] ; then
        # Back Upがある場合
        # echo ${LASTFILE} exists.
        # Back Upが壊れていないかを確認
        /usr/local/nerscripts/XmlChecker.py ${LASTFILE}
        if [ $? = 0 ] ; then
            # 壊れていなければBack Upを使用
            # echo ${LASTFILE} is not corrupted.
            # echo copy ${LASTFILE} to ${SYSFILE}.
            cp ${LASTFILE} ${SYSFILE}
        else
            # 壊れているときはBack UpをrmしてDefaultを使用
            # echo ${LASTFILE} is corrupted. Remove it.
            rm -f ${LASTFILE}
            # echo copy ${DEFFILE} to ${SYSFILE}.
            cp ${DEFFILE} ${SYSFILE}
        fi
    else
        # Back Upがない場合
        # Defaultを使用
        # echo ${LASTFILE} does not exist.
        # echo copy ${DEFFILE} to ${SYSFILE}.
        cp ${DEFFILE} ${SYSFILE}
    fi
}

# Begin of ieden-time
echo `date +"%Y/%m/%d %H:%M:%S.%3N"` Begin of ieden-time >> ${IEDEN_TIME_LOG}
# syslogにも出力
logger -i -t ieden-time "Begin of ieden-time"

###### SystemSetting.xmlのチェックに時間がかかる状況を模擬
#####  sleep 30

# Is SystemSetting.xml recovered?
file_recovered=0

#Is there SystemSetting.xml?
if [ ! -e ${SYSFILE} ] ; then
    echo `date +"%Y/%m/%d %H:%M:%S.%3N"` No ${SYSFILE}. >> ${IEDEN_TIME_LOG}
    Recover_SystemSetting
    file_recovered=1
fi

#SystemSetting.xml size check 

DEFSIZE=`wc -c ${DEFFILE} |awk '{print $1}'`
SYSSIZE=`wc -c ${SYSFILE} |awk '{print $1}'`

if [ ${file_recovered} -ne 1 ] ; then
    if [ $DEFSIZE -gt $SYSSIZE ] ; then
        echo `date +"%Y/%m/%d %H:%M:%S.%3N"` Size of ${SYSFILE} is smaller than ${DEFFILE}. >> ${IEDEN_TIME_LOG}
        Recover_SystemSetting
        file_recovered=1
    fi
fi

#SystemSetting.xml validity check 

if [ ${file_recovered} -ne 1 ] ; then
    /usr/local/nerscripts/XmlChecker.py ${SYSFILE}
    if [ ! $? = 0 ] ; then
        # SystemSetting.xmlが破損している
        echo `date +"%Y/%m/%d %H:%M:%S.%3N"` ${SYSFILE} is corrupted. Remove and recover. >> ${IEDEN_TIME_LOG}
        Recover_SystemSetting
        file_recovered=1
    fi
fi

if [ ${file_recovered} -ne 1 ] ; then
    # SystemSetting.xmlはValidなのでそのまま使う 
    # ValidなSystemSetting.xmlでBack upを更新
    echo `date +"%Y/%m/%d %H:%M:%S.%3N"` ${SYSFILE} is valid. Copy ${SYSFILE} to ${LASTFILE}. >> ${IEDEN_TIME_LOG}
    rm -f ${LASTFILE}
    cp ${SYSFILE} ${LASTFILE}
fi

echo `date +"%Y/%m/%d %H:%M:%S.%3N"` SystemSetting.xml has been checked. >> ${IEDEN_TIME_LOG}

sed -i -e 's/mqtt-broker.infiswift.tech/mqtt-broker-prod.pvsafety.jp/g' /usr/local/etc/nerics/mqtt

sleep 80

# Iedenchi Adjust Time
echo `date +"%Y/%m/%d %H:%M:%S.%3N"` Iedenchi Adjust Time. >> ${IEDEN_TIME_LOG}

/usr/local/nerscripts/ieden_adjusttime.sh > /tmp/ieden_adjusttime.log 2>&1 &

# End of ieden-time
echo `date +"%Y/%m/%d %H:%M:%S.%3N"` End of ieden-time >> ${IEDEN_TIME_LOG}
# syslogにも出力
logger -i -t ieden-time "End of ieden-time"

exit 0
