#!/bin/bash

BIN_SCHEDULE_TASK="/usr/syno/bin/synoschedtask"
CONFIG_FILES="/usr/syno/etc/securityscanSetting.json"
LOG_PATH="/var/log/selfcheck"
ERR_RULE=""

if [ -e $CONFIG_FILES ];then 
    BIN_PYTHON="/usr/bin/python2"
    RESULT=$($BIN_PYTHON -c "import json; data = json.loads(open('$CONFIG_FILES').read()); print data['defaultGroup']")
    if [ "$RESULT" == "home" ]; then
        LEVEL="HOME"
    elif [ "$RESULT" == "company" ]; then
        LEVEL="COMPANY"
    else 
        LEVEL="CUSTOM"
    fi
    TASK_ID=$($BIN_PYTHON -c "import json; data = json.loads(open('$CONFIG_FILES').read()); print data['schedule']['scheduleTaskId']")
    RESULT=$($BIN_SCHEDULE_TASK --get id="$TASK_ID" | grep "State" | grep "enabled")
    if [ $? -eq 0 ]; then
        SCHEDULE_SCAN="yes"
    else
        SCHEDULE_SCAN="no"
    fi
    DAYS=$($BIN_SCHEDULE_TASK --get id="$TASK_ID" | grep "Days of week" | sed 's/^.*Days of week: \[/"/g' | sed 's/\].*$/"/g' | sed 's/,/", "/g')
fi

while read s f; do
	if [ "x${s}" != "x0" ]; then
		while read err_line; do
			if [ "x${ERR_RULE}" == "x" ]; then
				ERR_RULE="\"${err_line}\""
			else
				ERR_RULE="${ERR_RULE},\"${err_line}\""
			fi
		done < ${f}
	fi
done < <(find ${LOG_PATH} -type f ! -regex .*xz$ -print0 | xargs -0 ls -s)

echo -n "{"

echo -n \"security_level\":\"$LEVEL\",
echo -n \"schedule_scan\":\"$SCHEDULE_SCAN\",
echo -n \"schedule_date\":["$DAYS"],
echo -n \"failed_result\":[${ERR_RULE}],

# let version be the last one msg
echo -n \"collector_version\":2

echo "}"
