#!/bin/sh
# ReadLog ver 0.1 from pC-fREAK org.
# I'm lazy to read the logs cycling through the logs
# so i wrote a little stuff to show it for you.
# you can pause with p you can read the logs on pages
# or on lines you can also grep for specific string through the logs
# and then see the grepped stuff throw the line mode.
# So, "it's on your own kid" ;].
# All copyrights and copylefts for hipodilski
# This is Under The GNU (General Public License), you can
# redistribute it free but comes with ABSOLUTELY NO WARRANTIES!
# Use at your own risk.
# For bugs suggestions etc. hipo@ncbis.ue-varna.bg
# http://rootshell.be/~pcfreak/Archive/ -- Say NO to M$! 

# Defaults vars. You don't need to edit them.
a=0;
b=1;
i=0;

string0="$0";
string1="$1";
string2="$2";
string3="$3";
temp_file="/tmp/readlog.tmp";

######### Config #########

# Time to sleep between printing the lines
sleep_time="1";

# Time to if in page mode.
sleep_page_time="10";

# Uncomment the following for default file to look over
## string1="/var/log/messages";

# Our page has ??? lines?
page_lines="23";
######### End Config #########

# Banner function.
banner () {

echo "LogRead Vear 0.1 by hip0";
echo '-=-=-=-=-=-==-=-=-=-=-=-';
echo "Syntax is: `basename $string0` file_to_read";
echo "or `basename $string0` file_to_read (-l|-p)"; 

}

# Checks args.
chk_args () {

# If args 0 zero and and file didn't exists and is dir. exit.
if [ -z "$string1" ] || [ ! -e "$string1" ] || [ -d "$string1" ]; then
banner;
echo "Exiting ...";
fi

}

chk_args1 () {

# if string1 zero and is not dir and string2 is zero.
if [ -n "$string1" ] && [ ! -d "$string1" ] && [ -z "$string2" ] &&\
[ -z "$string3" ] || [ "$string2" == "-l" ] && [ -z "$string3" ]; then
echo "LogRead Started: Cycling threw the lines.";
echo '-----------------------------------------';
cycle_line;
fi

# if string1 is empty and and not dir and string2 empty.
if [ -n "$string1" ] && [ ! -d "$string1" ] && [ -n "$string2" ] &&\
[ "$string2" == "-p" ] && [ -z "$string3" ]; then
echo "LogRead Started: Cycling threw the pages.";
echo '-----------------------------------------';
cycle_pages;
fi

# if string1 and string2 not empty and  and string2 not -l || -p.
if [ -n "$string1" ] && [ -n "$string2" ] && [ "$string2" != "-l" ] &&\
[ "$string2" != "-p" ] && [ -z "$string3" ]; then
echo "Unknown argument: $string2";
echo "Argument2 may be == -l or -p";
echo "Argument3 could be the string we grep for";
echo "Exiting ...";
exit 0;
fi

# if string1 and string2 and string3 not empty.
if [ -n "$string1" ] && [ -n "$string2" ] &&\
[ -n "$string3" ] && [ "$string2" == "-l" ] ||
[ "$string2" == "-p" ]; then
cycle_and_grep;
fi

}


# Cycle line func.
cycle_line () {

fc=`wc -l $string1 | awk '{ print $1 }'`;

# cycle threw the lines
for i in `seq 1 $fc`; do

unset if_p;
read -t $sleep_time -n 1 -s if_p;

if [ $if_p ]; then
read;
fi

cat $string1 | head -n $i | tail -n 1;

done

echo 'Thanks for using ReadLog ver 0.1';

}

# Cycle pages func.
cycle_pages () {

fc=`wc -l $string1 | awk '{ print $1 }'`;
fc1=`expr $(($fc/$(($page_lines + 2))))`;

# Cycle threw the pages.
for i in `seq 1 $fc1`; do

b=$(( $b + $page_lines ));
unset if_p;
read -t $sleep_page_time -n 1 -s if_p;

if [ $if_p ]; then
read;
fi

cat $string1 | head -n $b;

done

echo 'Thanks for using ReadLog ver. 0.1';

}

# Cycle and grep func.
cycle_and_grep () {
cat $string1 | grep "$string3" >> $temp_file;
fc=`wc -l $temp_file | awk '{ print $1 }'`;
for ((i=0; i<=fc; i++)); do

unset if_p;
read -t 1 -n 1 -s if_p;

if [ "$if_p" == "p" ]; then
read; 
fi

if [ "$if_p" == "n" ]; then
i=$(( $i + $page_lines ));
clear
fi

cat $temp_file | head -n $i | tail -n 1;

done

echo 'Thanks for using ReadLog ver 0.1';
}

get_rid_of_temp () {
rm -f $temp_file;
}

main () {

chk_args;
chk_args1;
get_rid_of_temp;
}

main;

