#!/bin/bash
source $HOME/.gamerc

IFS=_
CAUSE_ID=184

for host in $HOST_NAME; do
        IFS=
        if [[ $host == *"MS"* ]]; then
                set_id=$(echo $host | sed 's/MS//')
                [ "$1" == "" ] && start_date=$(date --date 'yesterday' '+%Y%m%d') || start_date=$1
                [ "$2" == "" ] && end_date=$(date --date 'yesterday' '+%Y%m%d') || end_date=$1
                if [ "${start_date}" == "${end_date}" ]; then
                        output_date="${start_date}"
                else
                        output_date="FROM ${start_date} TO ${end_date}"
                fi

                output_file="/tmp/s${set_id}_money_2_get_${start_date}_${end_date}.log"
                check_file="/tmp/s${set_id}_money_2_check_${start_date}_${end_date}.log"
                error_file="/tmp/money_2_error_monitor.log"
                [ -f ${output_file} ] && rm -rf ${output_file}
                [ -f ${check_file} ] && rm -rf ${check_file}
                [ -f ${error_file} ] && rm -rf ${error_file}

                #echo "get_log_filter ${start_date} ${end_date} Mission twolevel money | sed 's/\\x01/,/g' | perl -ne 'print \"\$1\n\" if /(log,(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})(,\d{3,4}),\S+,money,(\S+),(\d{3}),(\w+),(\d+),(\d+),(\d+),(\d+),(\d+),\d+,2,\d+,\d+,1,\d+)/' > ${output_file}"
                get_log_filter ${start_date} ${end_date} Mission twolevel money | sed 's/\x01/,/g' | perl -ne 'print "$1\n" if /(log,(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})(,\d{3,4}),\S+,money,(\S+),(\d{3}),(\w+),(\d+),(\d+),(\d+),(\d+),(\d+),\d+,2,\d+,\d+,1,\d+)/' > ${output_file}
                cat ${output_file} | awk -F"," '{print $12","$13}' | perl -F"," -lane '$k{"$F[0]"}+=$F[1]; END{print "$_,$k{$_}" for keys(%k) }' > ${check_file}
                cat ${check_file}
                cat ${check_file} | egrep -v "^${CAUSE_ID}," > ${error_file}
                send_messages ERROR money_2_error "正式機 S${set_id}\n${output_date}\n出現非儲值管道獲得付費鑽"
        fi
done
