#!/bin/sh

# w ~/.gg/config:
# log_path na ~/.gg/history
# log 2

if test -z $1; then
		
	echo "Uycie: `basename $0` [nick/uin] [liczba linii (def. 20)]"
	exit 0
	
	else


#Kogo chcemy oglda 

		if [ -f $HOME/.gg/history/$1 ];
		then HISTFILE=$HOME/.gg/history/$1;  #jeli podamy numerek
		fi

	for i in `ls $HOME/.gg/history/`; do


		if [ $(tail -n 1 $HOME/.gg/history/$i | cut -d "," -f 3 | grep -i "$1") ];
		then HISTFILE=$HOME/.gg/history/$i;  #jeli podamy nick

		fi
	done
fi

if test -z $HISTFILE; then 
	echo "Nie ma takiego numeru/nick-a"
	exit 0
fi
	

#Ile linii historii

if test -z $2; then 
	ILELINII=20
else 
	ILELINII=$2
fi

#eby si cudzysowy, przecinki, oraz \r \n dobrze wywietlay...

tail -n $ILELINII $HISTFILE | sed -e 's/\\r/\\\\r/g; s/\\n/\\\\n/g; s/,"/,/; s/"$//; s/*/"*"/g' > /usr/tmp/gg_history 
				 
				 # czemu * wywietla zaw. katalogu?
				 # *uj j tam wie :>
							

#No to sru

cat /usr/tmp/gg_history | while read linia; do

DATA_S=$(echo $linia | cut -d "," -f4 )
DATA=$(date -d "1970-01-01 `echo $DATA_S` sec UTC" +"%Y-%m-%d %T")

#Moje bredzenie

if [ "$(echo $linia | cut -d "," -f1)" = "chatsend" ]; then
		
		WYPOWIEDZ=$(echo $linia | cut -d "," -f 5-)
		echo -e "$DATA Ja:\n $WYPOWIEDZ"
#A tu nie moje		

else
		WYPOWIEDZ=$(echo $linia | cut -d "," -f 6-)
		NICK=$(echo $linia | cut -d "," -f 3)
		echo -e "$DATA $NICK:\n $WYPOWIEDZ"
fi

done
