#!/bin/bash
#===============================================================================
#
#          FILE: check_SGL_duper
#
#         USAGE: check_SGL_duper <gzip/non-gzip itemlog files>
#
#   DESCRIPTION: Detect SGL dupers
#
#  REQUIREMENTS: xcat, highlight
#
#         NOTES:
#
#          BUGS:  ---
#        AUTHOR: rickz (Rick Zhang), xlrickz@gmail.com
#       COMPANY: X-LEGEND Entertainment Corp.
#       CREATED: Sun Sep 18 21:32:23 EDT 2011
#      REVISION: 1.0
#
#          TODO:
#
#===============================================================================

#set -o nounset                              # Treat unset variables as an error
#set -m                                       # Enable job control

[ "$1" == "" ] && exec echo "Usage: $0 <gzipped or non-gzipped itemlog>"

for ITEMLOG in $*;do
   echo "Checking itemlog $ITEMLOG ... " 1>&2
   mkdir -p "$(dirname "/dev/shm/$ITEMLOG-text")"
   xcat "$ITEMLOG" > "/dev/shm/$ITEMLOG-text"

   for player_location in $(egrep ",MailS,|,Divide,|,SGL," "/dev/shm/$ITEMLOG-text"|awk -F, '{print $(NF-1)","$NF}'|sort|uniq -c|awk '$1>5 {print $2}');do
        grep "$player_location" "/dev/shm/$ITEMLOG-text" > "/dev/shm/$ITEMLOG-player-$player_location"

        tr -d '\n' < "/dev/shm/$ITEMLOG-player-$player_location" | grep -q "SGL.*Stall Start Price Log.*SGL.*Success.*MailS.*Divide.*Divide.*" && \
        DUPER=1 || DUPER=0

        if [ "$DUPER" == "1" ] ; then
                echo "Player: $player_location" | highlight - ".*"
                egrep -v ",Login,|,Logout," "/dev/shm/$ITEMLOG-player-$player_location"|highlight - Divide MailS SGL Success "Stall Start Price Log"
                echo "Player: $player_location has $(($(grep ",MailS," "/dev/shm/$ITEMLOG-player-$player_location"|wc -l)-$(grep -B1 ",MailS," "/dev/shm/$ITEMLOG-player-$player_location"|grep ",Drop"|wc -l))) times MailS without Drop."|highlight - ".* has [1-9]+ times MailS without Drop."
                echo
        fi
        DUPER=0
   done

   rm -f /dev/shm/$ITEMLOG-*
done
