#!/bin/bash
 
#/etc/functions
#[ -f ${LOG_FILE} ] && cat ${LOG_FILE} || echo "No log info now."
#input parm: 
# if $1 is "last-rows", then it will show the last rows of log,then the $2 will be the number of lines
# if $1 is "date",then it will show the log at a particular date,the $2 will be the date.at this case,
# the format of date can be month.date .for example:10.10,10.01,10.1,01.01,1.1 1.10,01.1,
#/pica/config/pica.conf is the configure file of pica,we grep it to find the syslogd is 
# output to /var/log/message or /tmp/log/messages,the default is /tmp/log/nessages
# get the syslog local output file
# get the syslog output file from configure file,if the file doesn't exist,the use the default.
if [ -f /pica/config/pica.conf ]
then
		TMP=`grep "local-file:" /pica/config/pica.conf | awk -F \" '{print $2}'`
		if [ x"$TMP" = x"disk" ]
		then
       		 LOG_FILE="/var/log/messages"
		else
        	 LOG_FILE="/tmp/log/messages"
		fi
else
		LOG_FILE="/tmp/log/messages"
fi
# check whether the log file exists
if [ ! -f ${LOG_FILE} ] 
then
    echo "No log info now." 
    exit 0
fi
# the $1 is last-rows,then the $2 should be the number of last lines
if [ $1 = "last-rows" ]
then
    tmp=`tail -n $2 ${LOG_FILE}`
    if [ "$tmp" = "" ]
    then
        echo "There is no log info now!"
        exit 0
    fi
	tail -n $2 ${LOG_FILE}
	exit 0
fi
# the $1 is date, then the $2 should be the date. for example 10.10
# firstly ,check whether the $2 is legal,then change the date into the format of syslog output,
# and handle the 0 in $2,change it into a space
# at last ,grep the log files
if [ $1 = "date" ]
then
    tmp1=$2
    tmp=`echo $tmp1 | grep .`
    if [ "$tmp" = "" ]
    then
        echo "The date input is illegal!"
        exit 0
    fi
    if [ "${tmp1:0:1}" = "0" ]
    then
        tmp1=${tmp1:1}
    else
        tmp1=${tmp1}
    fi
    year=${tmp1%%.*}
    tmp1=${tmp1#*.}
    if [ "${tmp1:0:1}" = "0" ]
    then
        tmp1=${tmp1:1}
    else
        tmp1=${tmp1}
    fi
    tmp=${tmp1%%.*}
    case $tmp in
  
          1)   date="Jan";;
          2)   date="Feb";;
          3)   date="Mar";;
          4)   date="Apr";;
          5)   date="May";;
          6)   date="Jun";;
          7)   date="Jul";;
          8)   date="Aug";;
          9)   date="Sep";;
         10)   date="Oct";;
         11)   date="Nov";;
         12)   date="Dec";;
          *)   echo "The date input is illegal!"
	       exit 0 ;;
    esac
    tmp1=${tmp1#*.}
    if [ "${tmp1:0:1}" = "0" ]
    then
        day=${tmp1:1}
    else
        day=${tmp1}
    fi
    allline=`wc -l ${LOG_FILE}`
    allline=${allline// /}
    allline=${allline%%/*}
    case $day in
             [1-9])   tmp=`grep -n $date"  "$day" "$year ${LOG_FILE}`;;
        [1-3][0-9])   tmp=`grep -n $date" "$day" "$year ${LOG_FILE}`;;
                 *)   echo "The date input is illegal!"
                      exit 0 ;;
    esac
    if [ "$tmp" = "" ]         
    then                   
        echo "There is no log at that time!"
        exit 0                              
    fi
    line=${tmp%%:*}
    line=$(($allline-$line+1))
    tail -n ${line} ${LOG_FILE}
fi
