#!/bin/bash
#
#FilterMonitorCalib ver1.1.1
#
# usage: 
#	FilterMonitorCalib callfunc traceloglevel  
#
# Description: 
#	for calibration 
#Copyright (C) 2012 NEC Corporation.
#If you tamper with the source, the behavior of the FilterMonitor is not guaranteed.
############################################################################################################

. $PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorUsage
. $PATHPREFIX/usr/bin/FilterMonitor/FM_FileChk
. $PATHPREFIX/etc/FilterMonitor/MessageList

SettingPath=/etc/FilterMonitor
funcname=FilterMonitorCalib
FM_IPMI_GetAirFlow180Ref=3

callfunc=$1
tracelevel=$2

settingfile=$PATHPREFIX/$SettingPath/FilterMonitorSetting
settingtmpfile=/tmp/FilterMonitorSetting
cronfile=$PATHPREFIX/etc/cron.d/FilterMonitor
crontmpfile=/tmp/FilterMonitor
cronflagfile=$PATHPREFIX/$SettingPath/FilterMonitorCronFlag

umask 077

ShowAlert()
{
    echo -e "Internal error occurs. Please contact to customer service for the support."
    $PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorLog syslog $funcname ERR $tracelevel $callfunc "failed to access file"
    $PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorLog tracelog $funcname ERR $tracelevel $callfunc "$failed to access file"
}

Finalize()
{
    $PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorLog tracelog $funcname INFO $tracelevel $callfunc "$funcname end"

    exit $1
}

AskQuestion()
{
questionmsg=$1
exitmsg=$2
isY=$3
UsrReply=0

while :
do
echo -e "$questionmsg"
read Reply

	if [ "$Reply" == "y" -o "$Reply" == "Y" ]
	then 
		UsrReply=1

	else	
		if [ "$Reply" == "n" -o "$Reply" == "N" ]
		then 
			UsrReply=2

		else
			UsrReply=0
		fi
	fi

	if [ $UsrReply -ne 0 ]
	then
		if [ $isY -eq  $UsrReply ]
		then
			break

		else
			echo -e "$exitmsg"
			$PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorLog tracelog $funcname INFO $tracelevel $callfunc "$funcname canceled by user"
			Finalize 0		
		fi

	else
		echo -e "Please enter Y or N."
	fi
done
}

$PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorLog tracelog $funcname INFO $tracelevel $callfunc "$funcname start"

if [ $# -lt 2 ]
then
    FilterMonitorUsage
    Finalize 1
fi

IFS="," read -a Setting < $settingfile
if [ ${#Setting[*]} -ne 17 ]
then
    ShowAlert
    Finalize 2
fi

AskQuestion "Do you want to start the initialization?(Y/n)" "The initialization is not completed." 1

#message[1]
AskQuestion "Is the dust filter new?(Y/n)" "Please replace the dust filter before executing the initialization." 1

#message[2]
AskQuestion "Do you close the cover of the machine?(e.g. the front covers of the machine or the side-panel of the machine)(Y/n)" "Please close the cover of the machine before the initialization." 1

#message[3]
AskQuestion "Are you going to change the location of the machine after executing the initialization?(y/N)" "If you have moved the machine, please execute the initialization again." 2

#message[4]
AskQuestion "Do you change the device configuration after the initialization?(y/N)" "If you change the device configuration, please execute the initialization again." 2

#message[5]
AskQuestion "Did you check that there is nothing to block AirFlow at the front of the machine?(Y/n)" "Please move anything to block AirFlow at the front of the machine." 1

#message[6]
AskQuestion "Did you check that there is nothing to affect the AirFlow around the machine?(Y/n)" "Please move anything that affect the AirFlow around the machine." 1

$PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorIPMI 1 $tracelevel $FM_IPMI_GetAirFlow180Ref 0 0 0 0 0 0
Result=$?

if [ $Result == 1 ]
then
	echo -e "Getting the information of the machine failed. The installation of Filter Replacement Sensor has troubles or the sensor may be broken. Please contact to customer service for the support."
	Finalize 3
fi

if [ $Result == 2 ]
then
	echo -e "The initialization failed. The installation of Filter Replacement Sensor has troubles or the sensor may be broken. Please contact to customer service for the support."
	Finalize 4
fi

# HighBall-S
#HWID=0
#HWIDCount=0
SKU=0
SKUCount=0
# HighBall-E
filenames=(`ls $PATHPREFIX/$SettingPath/*_Setting 2>/dev/null`)
filename="`basename ${filenames[0]}`"
# HighBall-S
#HWID=(`echo $filename | cut -c 1-4`)
#HWIDCount="`grep -c "$HWID" $PATHPREFIX/$SettingPath/"SKUDefine"`"
#SKU=(`echo $filename | cut -c 1-4`)
#SKU=(`echo $filename | cut -d "_" -f1`)
SKU="`echo $filename | cut -d "_" -f1`"
SKUCount="`grep -c "$SKU" $PATHPREFIX/$SettingPath/"SKUDefine"`"
# HighBall-E


# HighBall-S
#if [ $HWIDCount -eq 0 ]
if [ $SKUCount -eq 0 ]
# HighBall-E
then
    echo -e "The initialization failed. The installation of Filter Replacement Sensor has troubles or the sensor may be broken. Please contact to customer service for the support."
    $PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorLog syslog $funcname INFO $tracelevel $callfunc "${msg[16]}"
# HighBall-S
#    $PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorLog tracelog $funcname INFO $tracelevel $callfunc "$funcname Can not find $HWID in SKUDefine file."
    $PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorLog tracelog $funcname INFO $tracelevel $callfunc "$funcname Can not find $SKU in SKUDefine file."
# HighBall-E
    Finalize 7

fi
# HighBall-S
#Setting[0]=$HWID
Setting[0]=$SKU
# HighBall-E

for (( i = 0 ; i < ${#filenames[*]} ; i++ ))
{
filename="`basename ${filenames[i]}`"

# HighBall-S
#sensornum=(`echo $filename | cut -c 6-7`)
#sensornum=(`echo $filename | cut -d "_" -f2`)
sensornum="`echo $filename | cut -d "_" -f2`"
# HighBall-E

export IFS=","
read -a Sensorinfo < $PATHPREFIX/$SettingPath/$filename
CalibValue=${Sensorinfo[3]}
echo "$CalibValue"> $PATHPREFIX/$SettingPath/$sensornum
}

CalibDate=`date '+%Y%m%d'`
Setting[4]=1
Setting[13]=$CalibDate

echo -e -n ${Setting[0]} > $settingtmpfile
for (( i = 1 ; i < ${#Setting[*]} ; i++ ))
{
    echo -e -n ",${Setting[i]}" >> $settingtmpfile
}
echo -e "" >> $settingtmpfile
mv $settingtmpfile $settingfile
if [ $? -ne 0 ]
then
    echo -e "The initialization failed. The installation of Filter Replacement Sensor has troubles or the sensor may be broken. Please contact to customer service for the support."
    $PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorLog syslog $funcname INFO $tracelevel $callfunc "${msg[16]}"
    $PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorLog tracelog $funcname INFO $tracelevel $callfunc "$funcname Writing Reference time failed to common setting file"
    Finalize 5
fi

#if [ ! -f $cronfile ]
#then
#    rm -f $cronfile
#fi

#make crontab file
HourVal=${Setting[8]}
MinuteVal=${Setting[9]}
IntervalVal=${Setting[10]}
DayofWeek1Val=${Setting[11]}
DayofWeek2Val=${Setting[12]}

$PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorSetCron $HourVal $MinuteVal $IntervalVal $DayofWeek1Val $DayofWeek2Val

if [ $? -ne 0 ]
then
    echo -e "The initialization failed. The installation of Filter Replacement Sensor has troubles or the sensor may be broken. Please contact to customer service for the support."
    $PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorLog syslog $funcname INFO $tracelevel $callfunc "${msg[16]}"
    $PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorLog tracelog $funcname INFO $tracelevel $callfunc "$funcname Writing Reference time failed to common setting file"
    Finalize 6
fi

echo -e "The initialization is completed."


$PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorLog tracelog $funcname INFO $tracelevel $callfunc "$funcname end"

Finalize $Result
