#!/bin/bash
#
#FilterMonitorSaving ver1.1.1	
#
# usage: 
#	FilterMonitorSaving callfunc outfile tracelevel
#
# Description: 
#	save the setting for FilterMonitorDetect
#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=FilterMonitorSaving

callfunc=$1
outfile=$2
tracelevel=$3

newfile=/tmp/`basename ${outfile}`.new

umask 077

ShowAlert()
{
    echo "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 "The file already exists. Overwrite?(Y/n)"
	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

OLDIFS=$IFS

now=0
HourVal=0
MinuteVal=0
IntervalVal=0
DayOfWeek1Val=0
DayOfWeek2Val=0
# HighBall-S
#HWID=""
SKU=""
# HighBall-E
SensorNumber=""
RefVal=0

filenames=(`ls $PATHPREFIX/$SettingPath/*_Setting 2>/dev/null`)
if [ ${#filenames[*]} -eq 0 ]
then
    ShowAlert
    Finalize 2
fi

calibvalue=(`ls $PATHPREFIX/$SettingPath/C* 2>/dev/null`)
if [ ${#calibvalue[*]} -eq 0 ]
then
    ShowAlert
    Finalize 2
fi

export IFS=","
read -a Setting < $PATHPREFIX/$SettingPath/FilterMonitorSetting

if [ ${#Setting[*]} -lt 13 ]
then
    ShowAlert
    Finalize 3
fi

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

# DEBUG echo $HourVal $MinuteVal $IntervalVal $DayofWeek2Val $DayofWeek2Val

FM_FileChk $funcname $tracelevel $outfile

if [ $? -ne 1 ]
then
    touch $outfile 2> /dev/null
    if [ $? -ne 0 ]
    then
	echo "Saving file failed."
	Finalize 4
    fi
    if [ ! -w $outfile ]
    then
	ShowAlert
	Finalize 5
    fi
    rm $outfile
else
    AskYN
    if [ "$reply" == "n" -o "$reply" == "N" ]
    then
	$PATHPREFIX/usr/bin/FilterMonitor/FilterMonitorLog tracelog $funcname INFO $tracelevel $callfunc "Not overwrite saving file"
	Finalize 0
    fi
fi

now=`date '+%Y%m%d%H%M%S'`

echo "$now,$HourVal,$MinuteVal,$IntervalVal,$DayofWeek1Val,$DayofWeek2Val" > $newfile

#reading Refvalue
for (( i = 0 ; i < ${#filenames[*]} ; i++ ))
{
    fname=${filenames[i]}
    calibfile=${calibvalue[i]}

    if [ ! -r $fname ]
    then
	rm $newfile
        ShowAlert
        Finalize 6
    fi
    if [ ! -r $calibfile ]
    then
	rm $newfile
        ShowAlert
        Finalize 6
    fi
    read -a Array < $calibfile
    if [ ${#Array[*]} -lt 1 ]
    then
	rm $newfile
        ShowAlert
        Finalize 7
    fi
    RefVal=${Array[0]}

    basename="`basename $fname`"

    IFS="_ " read -a Ids <<<"$basename"

    if [ ${#Ids[*]} -lt 3 ]
    then
	rm $newfile
        ShowAlert
        Finalize 8
    fi

# HighBall-S
#    HWID=${Ids[0]}
    SKU=${Ids[0]}
# HighBall-E
    SensorNumber=${Ids[1]}

# HighBall-S
#    echo "$HWID,$SensorNumber,$RefVal" >> $newfile
    echo "$SKU,$SensorNumber,$RefVal" >> $newfile
# HighBall-E
}

mv -f $newfile $outfile
if [ $? -eq 0 ]
then
	echo -e "Setting was saved."
else
	echo -e "Saving file failed."
	Finalize 9
fi

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

exit 0
