#!/bin/bash
#
#
#    Scalix Public License
#
#
#    The contents of this file are subject to the Scalix Public License
#    Version 1.1 ("License"); you may not use this file except in
#    compliance with the License. You may obtain a copy of the License at
#
# 
#    http://www.scalix.com/community/licensing 
#
# 
#    Software distributed under the License is distributed on an "AS IS"
#    basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
#    the License for the specific language governing rights and limitations
#    under the License.
#
# 
#    The Original Code is Scalix Server. 
#    The Initial Developer of the Original Code is Scalix Corporation. 
#    Copyright (C) 2006 Scalix Corporation. 
#    All Rights Reserved.
#    Additional Copyrights may apply - see the notice in each file
#
#
#
#
#
#
# Copyright (C) 2006 Scalix Corporation.  All Rights Reserved.
#
# Plugin to scan any log of your choice
#
# usage function to pring usage information. Note this must exist with non-zero status.
# preferably greater than 1.
#
function usage {
	echo "usage: $0 --type SYSLOG|SENDMAIL { {--head | --tail } n | --search string }"
	exit 2
			
}
#
# This MUST be part of all Plugins. The format for the output must be adhered to
# as specified in the development guidelines.
# VERSION for Equinox must be "1.0"
# All five tokens NAME:, VERSION, DESCRIPTION, PARAMETERS, and OUTPUT must be
# part of this help() funtion. 
# Subsitute appropriate values for your plugin here.
#
function help {
	echo ""
	echo ""
	echo "VERSION:"
	echo "1.0"
	echo ""
	echo "NAME:" 
	echo "Scan a log file of your choice"
	echo ""
	echo "DESCRIPTION:"
	echo ""
	echo "This script scan or print contents of a specified log file."
	echo ""
	echo "PARAMETERS:"
	echo "--type	Logtype	SYSLOG|SENDMAIL	string	single	Type of log file"
	echo "--head	From Top	25	number	single	Number of lines from the Top of the File"
	echo "--tail	From Bottom	25	number	single	Number of lines from the bottom of the file"
	echo "--search	Search	\"\"	string	single	search string"
	echo "--help	Help	\"\"	\"\"	\"\"	Usage Information"
	echo ""
	echo "OUTPUT:"
	echo "text/plain"
}

#
# Search the file for a particular pattern
#
function do_search {
    local t=$1
    local p=$2
    local f=""
    case "$t" in
	SYSLOG)
	    file="/var/log/messages"
	    ;;
	SENDMAIL)
	    file="/var/log/maillog"
	    ;;
	*)
	    echo "Unknown log type"
	    return 4
	    ;;
    esac
    echo `egrep -i "$pattern" "$file"`
}

#
# get the head of the file
#
function do_head {
    local t=$1
    local l=$2
    case "$t" in
	SYSLOG)
	    file="/var/log/messages"
	    ;;
	SENDMAIL)
	    file="/var/log/maillog"
	    ;;
	*)
	    echo "Unknown log type"
	    return 4
	    ;;
    esac
    `echo head "-$l" "$pattern" "$file"`
}
#
# get the tail of the file
#
function do_tail {
    local t=$1
    local l=$2
    case "$t" in
	SYSLOG)
	    file="/var/log/messages"
	    ;;
	SENDMAIL)
	    file="/var/log/maillog"
	    ;;
	*)
	    echo "Unknown log type"
	    return 4
	    ;;
    esac
    `echo tail "-$l" "$pattern" "$file"`
}

    
GETOPT_RESULTS=`getopt -l help,head:,tail:,type:,search:-n 'scanlog' -- "$@"`
if [ $? -ne 0 ]
then 
    usage
fi

eval set -- "$GETOPT_RESULTS"

#
# parse arguments
#
#
type="SYSLOG"
DO_SEARCH=""
DO_TAIL=""
DO_HEAD=""
for args in "$@" 
do
	case $args in
	    --help) 
		help
		exit 0
		;;
	    --head)
		DO_HEAD=$args
		lines="$2"
		shift 2
		;;
	    --tail)
		DO_TAIL=$args
		lines="$2"
		shift 2
		;;
	    --search)
		DO_SEARCH=$args
		pattern="$2"
		shift 2
		;;
	    --type)
		type="$2"
		shift 2
		;;
	    --)
		#echo "end parsing"
		shift 
		 ;;
	esac
done

#echo "type=$type"
#echo "Lines=$lines"
#echo "head=$DO_HEAD"
#echo "tail=$DO_TAIL"
#echo "search=$DO_SEARCH"
#echo "pattern=$pattern"
#
# do some argument checking
#
if [ "$DO_TAIL" != "" -a "$DO_HEAD" != "" ]
then
    usage
fi

if [ "$DO_SEARCH" != "" -a "$pattern" = "" ]
then 
    usage
fi
if [ "$lines" = "" -a \( "$DO_TAIL" != "" -a "$DO_HEAD" != "" \) ]
then
    usage
fi
if [ "$DO_TAIL" != "" ]
then
    do_tail "$type" "$lines"
    result=$?
elif [ "$DO_HEAD" != "" ]
then
    do_head "$type" "$lines"
    result=$?
elif [ "$DO_SEARCH" != "" ]
then
    do_search "$type" "$pattern"
    result=$?
else
    usage
fi
exit $result





