#!/bin/bash
trap "{ ps -u `whoami`|awk {'print $1'}|xargs kill;rm tl_3*; exit 0; }" SIGINT SIGTERM

name=$1
date=$2
what=$3

find /u[0,1]/archive_logs/{mpfs,webdav}|grep "$name"|grep -E "$date" > "$name"

let threads=`cat /proc/cpuinfo |grep processor -c`-2

let number=$(echo "`wc -l $name |awk {'print $1'}`/$threads")
if [ "$number" -lt 1 ];
then
    echo "No such files"
    exit 0
fi

split -l "$number" "$name" tl_3

function thread {
    file="$1"
    what="$2"
    while read i
    do
        host=$(basename $(dirname "$i"))
        if [ -s "$i" ]; then
            zcat "$i"|grep "$what"| awk -v host="$host" {'print host ":" $0'} >> "$file".log
        fi
    done < "$file"
}

for file in tl_3*
do
    thread "$file" "$what" &
done

gcount=1

while [ $gcount -gt 0 ]
do
    let gcount=$(ps -u `whoami`|grep -c gzip)+$(ps -u `whoami`|grep -c grep)-3
    sleep 1
done

cat tl_3*.log > result.log

rm tl_3*
#http://wiki.yandex-team.ru/users/triklozoid/poiskpologstore

trap "{ ps -u `whoami`|awk {'print $1'}|xargs kill;rm tl_3*; exit 0; }" SIGINT SIGTERM

name=$1
date=$2
what=$3


find /u[0,1]/archive_logs/{mpfs,webdav}|grep $name|grep -E $date > $name

let threads=`cat /proc/cpuinfo |grep processor -c`-2

let number=$(echo "`wc -l $name |awk {'print $1'}`/$threads")
if [ $number -lt 1 ];
then
    echo "No such files"
    exit 0
fi

split -l $number $name tl_3

function thread {
    file=$1
    what=$2
    for i in $(cat $file)
    do 
        host=$(basename $(dirname $i))
	if [ -s $i ]; then
		zcat $i|grep $what| awk -v host=$host {'print host ":" $0'} >> $file.log
	fi
    done
}

for file in tl_3*
do 
    thread $file "$what" &
done

gcount=1

while [ $gcount -gt 0 ]
do
    let gcount=$(ps -u `whoami`|grep -c gzip)+$(ps -u `whoami`|grep -c grep)-3
    sleep 1
done

cat tl_3*.log > result.log

rm tl_3*

