#!/bin/bash
#
#FilterMonitorStatus ver1.1.1
#
# usage: 
#	 FilterMonitorStatus callfunc tracelevel status
#
# Description: 
#	 Get status of Filter Monitor Sensor
#
#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/FilterMonitorCommonFunc

SettingPath=/etc/FilterMonitor
funcname=FilterMonitorStatus

callfunc=$1
tracelevel=$2
status=4

settingfile=$PATHPREFIX/$SettingPath/FilterMonitorSetting

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
}

AskYN()
{
    while true; do
	echo -e "$1"
	read Reply
	if [ "$Reply" == "n" -o "$Reply" == "N" -o "$Reply" == "y" -o "$Reply" == "Y" ]
	then
	    break
	fi;
	echo "Please enter Y or N."
    done
}

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

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

Chk_1=0
Chk_2=0
Chk_3=0
Chk_4=0
Chk_5=0
DoFLG=0

OLDIFS=$IFS

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

CurrentStatus=${Setting[4]}

case $CurrentStatus in
    0)
	echo -e "Initialization is not completed. Please execute the initialization."
	$PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorLog tracelog $funcname INFO $tracelevel $callfunc "Current status is '0'."
	;;

    1)
	echo -e "The dust filter is normal."
	$PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorLog tracelog $funcname INFO $tracelevel $callfunc "Current status is '1'."
	;;

    2)
	echo -e "The dust filter is clogged. Please replace the filter."
	$PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorLog tracelog $funcname INFO $tracelevel $callfunc "Current status is '2'."
	;;

    3)
	echo -e "Abnormal AirFlow has been detected. Please check the message in the following. If this message repeatedly appeared, please contact to the customer service for the support."

	AskYN "The cover of the machine is opened?(e.g. the front covers or the side-panel of the machine)(y/N)"
	if [ $Reply == "Y" -o $Reply == "y" ]
	then
	    Chk_1=1
	else
	    Chk_1=0
	fi

	AskYN "Are you detecting the AirFlow before the initialization?(y/N)"
	if [ $Reply == "Y" -o $Reply == "y" ]
	then
	    Chk_2=1
	else
	    Chk_2=0
	fi

	AskYN "Did you move the machine after the dust filter replaced?(y/N)"
	if [ $Reply == "Y" -o $Reply == "y" ]
	then
	    Chk_3=1
	else
	    Chk_3=0
	fi

	AskYN "Is there anything to block AirFlow at the front of the machine?(y/N)"
	if [ $Reply == "Y" -o $Reply == "y" ]
	then
	    Chk_4=1
	else
	    Chk_4=0
	fi

	AskYN "Did you change the device configuration after the initialization?(y/N)"
	if [ $Reply == "Y" -o $Reply == "y" ]
	then
	    Chk_5=1
	else
	    Chk_5=0
	fi

	if [ $Chk_1 == "1" -a $Chk_2 == "1" -a $Chk_3 == "1" -a $Chk_4 == "1" -a $Chk_5 == "1" ]
	then
	    echo -e "Please close the cover of the machine or move anything to block AirFlow around the machine. Then execute the initialization again after changing the device configuration or the machine location."
	    DoFLG=2
	    $PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorLog tracelog $funcname INFO $tracelevel $callfunc "All question is 'YES'."
	elif [ $Chk_1 == "0" -a $Chk_2 == "0" -a $Chk_3 == "0" -a $Chk_4 == "0" -a $Chk_5 == "0" ]
	then
	    echo -e "The failure of Filter Replacement Sensor or system is found. Please contact to customer service for the support."
	    $PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorLog tracelog $funcname INFO $tracelevel $callfunc "All question is 'NO'."
	elif [ $Chk_1 == "1" ]
	then
	    echo -e "Abnormal AirFlow detected because of the cover of the machine open. Please close the cover of the machine."
	    $PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorLog tracelog $funcname INFO $tracelevel $callfunc "Question of cover open is 'YES'."
	    DoFLG=1
	elif [ $Chk_4 == "1" ]
	then
	    echo -e "There is something to block the AirFlow around the machine. Please move anything to block AirFlow around the machine."
	    $PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorLog tracelog $funcname INFO $tracelevel $callfunc "Question of anything to block AirFlow is 'YES'."
	    DoFLG=1
	elif [ $Chk_2 == "1" -o $Chk_3 == "1" -o $Chk_5 == "1" ]
	then
	    echo -e "Abnormal AirFlow detected because of changing the device configuration or the machine location. Please execute the initialization again after changing the device configuration or the machine location."
	    $PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorLog tracelog $funcname INFO $tracelevel $callfunc "Question of Changing machine configuration or location is 'YES'."
	    DoFLG=2
	fi;;

    *)
	ShowAlert
	Finalize 2;;
esac

case $DoFLG in
    1)
	AskYN "Start measure the AirFlow. Execute?(y/N)"
	if [ $Reply == "Y" -o $Reply == "y" ]
	then
		 #Call detection
		$PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorMeasure $status $DoFLG $tracelevel
		funcret=`echo $?`
		if [ "$funcret" -eq 0 ] 
		then
			$PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorDetect $status $DoFLG $tracelevel 

		elif [  "$funcret" -eq 6 ]
		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 1;
		else
			$PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorLog tracelog $funcname DEBUG $tracelevel $callfunc \
	"FileterMoniorDetect Failed:$funcret."
			Finalize 1;
		fi
	fi
	;;

    2)
	# Call Calib
	${PATHPREFIX}/usr/bin/FilterMonitor/FilterMonitorCalib $status $tracelevel
	;;
esac

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

Finalize 0
