#!/bin/bash

. /usr/local/lib/7dtd/common.sh
. /usr/local/lib/7dtd/playerlog.sh

if [ $(isValidInstance $1) -eq 0 ]; then
	echo "No instance given or not a valid instance!"
	return
fi

INSTANCE=$1
LOG=$2
#LOG=$(getInstancePath $INSTANCE)/logs/$(date '+%Y-%m-%d_%H-%M-%S')_output_log.txt
CHATLOG=$(getInstancePath $INSTANCE)/logs/$(date '+%Y-%m-%d_%H-%M-%S')_chat.log
COMMANDLOG=$(getInstancePath $INSTANCE)/logs/$(date '+%Y-%m-%d_%H-%M-%S')_commandExecution.log

timestamp() {
	date '+%Y.%m.%d %H:%M:%S'
}

handleConnect() {
	local entityId="$1"
	local name="$2"
	local steamId="$3"
	local ip="$4"
	
	logPlayerConnect $INSTANCE "$entityId" "$name" "$steamId" "$ip"

	for H in $(getHooksFor playerConnect); do
		$H $INSTANCE "$entityId" "$name" "$steamId" "$ip"
	done
}

handleDisconnect() {
	local playerId="$1"
	local entityId="$2"

	logPlayerDisconnect $INSTANCE "$entityId"

	for H in $(getHooksFor playerDisconnect); do
		$H $INSTANCE "$playerId" "$entityId" "$NICKNAME" "$STEAMID"
	done
}

handleChat() {
	echo "$(timestamp): $1" >> $CHATLOG
	
	for H in $(getHooksFor chat); do
		$H $INSTANCE "$1"
	done
}

handleRemoteCommand() {
	local cmd="$1"
	local name="$2"
	
	echo "$(timestamp): Player \"$name\" executed \"$cmd\"" >> $COMMANDLOG

	for H in $(getHooksFor remoteCommand); do
		$H $INSTANCE "$cmd" "$name"
	done
}

handleTelnetCommand() {
	local cmd="$1"
	local ip="$2"

	echo "$(timestamp): Telnet from \"$ip\" executed \"$cmd\"" >> $COMMANDLOG

	for H in $(getHooksFor telnetCommand); do
		$H $INSTANCE "$cmd" "$ip"
	done
}


if [ ! -d "$(getInstancePath $INSTANCE)/logs" ]; then
	mkdir "$(getInstancePath $INSTANCE)/logs"
fi

setAllPlayersOffline

rm $(getInstancePath $INSTANCE)/logs/current_output_log.txt
rm $(getInstancePath $INSTANCE)/logs/current_chat.log
rm $(getInstancePath $INSTANCE)/logs/current_commandExecution.log
ln -s $LOG $(getInstancePath $INSTANCE)/logs/current_output_log.txt
ln -s $CHATLOG $(getInstancePath $INSTANCE)/logs/current_chat.log
ln -s $COMMANDLOG $(getInstancePath $INSTANCE)/logs/current_commandExecution.log

sleep 5

NOBUF="stdbuf -e0 -o0"

$NOBUF tail -n 5000 -F $LOG |
$NOBUF tr '\\' '/' |
$NOBUF tr -d '\r' |
$NOBUF grep -v "^(Filename: " |
$NOBUF sed -r 's/^[0-9]+-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+ [0-9]+[.,][0-9]+ [A-Z]+ (.*)$/\1/' |
while read line ; do
	if [ -n "$line" ]; then
		#Player connected, entityid=1278, name=termo2, steamid=76561197997439820, ip=178.203.27.140 
		#Player connected, entityid=[0-9]*, name=.*, steamid=[0-9]*, ip=[0-9.]*$ 
		if [ -n "$(echo "$line" | grep '^Player connected,')" ]; then
			entityId=$(expr "$line" : 'Player connected, entityid=\([0-9]*\), name=.*, steamid=[0-9]*, ip=[0-9.]*$')
			playerName=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=\(.*\), steamid=[0-9]*, ip=[0-9.]*$')
			steamId=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=.*, steamid=\([0-9]*\), ip=[0-9.]*$')
			ip=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=.*, steamid=[0-9]*, ip=\([0-9.]*\)$')
			sleep 1
			handleConnect "$entityId" "$playerName" "$steamId" "$ip"
			unset entityId playerName steamId ip
		else
		#Player disconnected: EntityID=[0-9]*, PlayerID='[0-9]*', OwnerID='[0-9]*', PlayerName='.*'$ 
		if [ -n "$(echo "$line" | grep '^Player disconnected: ')" ]; then 
			playerId=$(expr "$line" : "Player disconnected: EntityID=[0-9]*, PlayerID='\([0-9]*\)', OwnerID='[0-9]*', PlayerName='.*'$") 
			entityId=$(expr "$line" : "Player disconnected: EntityID=\([0-9]*\), PlayerID='[0-9]*', OwnerID='[0-9]*', PlayerName='.*'$") 
			handleDisconnect "$playerId" "$entityId"
			unset playerId entityId
		else
		#GMSG: .*$
		if [ -n "$(echo "$line" | grep -E '^GMSG: .+')" ]; then
			msg=$(expr "$line" : 'GMSG: \(.*\)$')
			handleChat "$msg"
			unset msg
		else
		#Executing command ".*" from client ".*"$ 
		if [ -n "$(echo "$line" | grep '^Executing command '.*' from client')" ]; then 
			cmd=$(expr "$line" : "Executing command '\(.*\)' from client .*$") 
			nick=$(expr "$line" : "Executing command '.*' from client \(.*\)$") 
			handleRemoteCommand "$cmd" "$nick"
			unset cmd nick
		else
		#Executing command ".*" by Telnet from .*$ 
		if [ -n "$(echo "$line" | grep '^Executing command '.*' by Telnet from ')" ]; then 
			cmd=$(expr "$line" : "Executing command '\(.*\)' by Telnet from .*$") 
			ip=$(expr "$line" : "Executing command '.*' by Telnet from \(.*\)$") 
			handleTelnetCommand "$cmd" "$ip"
			unset cmd ip
		fi
		fi
		fi
		fi
		fi
	fi
done
