#!/bin/bash
# gunzip and bunzip2 script capabilities
# included tar options
# version 0.1
# TODO : add save path and save information for tar information
# 14 November 2005
# Duckz

E_NOARGS=65
OPTSTAIL=  # you can use xvf option for verbose or xf for clean layout.
OPTSDMESG=
OPTSPS=x
OPTSKILL=
OPTSLOGPATH=/var/log/messages
SAVETOPATH=/home/duckz
SAVEFILE=no


if [ -z "$2" ]

then
	if [ "$1" = "--help" ] ; then
	echo " Usage : `basename $0` -p|-d|-k|-t|--help argument"
	echo " Options : "
	echo " -p : for ps program, example `basename $0` -p processname"
	echo " -d : for dmesg program, example `basename $0` - d processname"
	echo " -k : for killall program, example `basename $0` -k processname"
	echo " -t : for tail program, example `basename $0` -t logentry/processname"
	echo " --help : for displaying this help message, example `basename $0` --help"
	exit 0

	else

        echo " Usage : `basename $0` -options argument"
        echo " Try --help for help"
        exit $E_NOARGS
	fi
fi

if [ -z "$1" ] ; then
               
#	if [ "$1" = "--help" ] 
#	then 
##	echo " Usage : `basename $0` -p|-d|-k|-t|--help argument"
#	echo " Options :"
#	echo " -p : for ps program, example `basename $0` -p gnome"
#	echo " -d : for dmesg program, example `basename $0` -d ACPI"
#	echo " -k : for killing program, example `basename $0` -k gnome"
#	echo " -t : for tail program, example `basename $0` -t eth1"
#	echo " --help : for displaying help messages"
#	exit 0
#	else
	
	echo " Usage : `basename $0` -p|-d|-k|-t|--help argument"
	echo " Try --help for help"
	exit 0
		fi
#fi
	
if [ "$1" = "-p" ]
	then
	
	p=1 

		if [ "$p" = "1" ] ; then
		
		ps $OPTSPS > $SAVETOPATH/$2.ps
			
		echo " Active process $2 found :"
		grep $2 $SAVETOPATH/$2.ps
		echo

		echo " Finish searching" 

			if [ "$SAVEFILE" = "no" ] ; then
			
			rm -f $SAVETOPATH/$2.ps
			else
			exit 0
			fi
		fi

	elif [ "$1" = "-d" ] ; then d=1
	
		if [ "$d" = "1" ] ; then 
		
		dmesg $OPTSDMESG > $SAVETOPATH/$2.dmesg
			                
	                grep $2 $SAVETOPATH/$2.dmesg &> /dev/null
                                                            
                   	if [ "$?" = "0" ] ; then
			echo " Process $2 found : "
			echo
                        
			grep $2 $SAVETOPATH/$2.dmesg

				echo " Finish searching"
                        	if [ "$SAVEFILE" = "no" ] ; then
                        
 
                        	rm -f $SAVETOPATH/$2.dmesg
				exit 0
                       		else
                        	exit 0
				fi
                        else
                        echo " No $2 process found, exiting"
                        rm -f $SAVETOPATH/$2.dmesg
                        exit 0
                 	fi
		fi
		elif [ "$1" = "-k" ] ; then k=1
	
			if [ "$k" = "1" ] ; then 
		
			killall $OPTSDMESG $2 &> /dev/null > $SAVETOPATH/$2.killed
		
			if [ "$?" = "0" ] ; then		
	                echo " Process $2 killed : "
	                
			grep $2 < $SAVETOPATH/$2.killed
                                                            
                   	echo
                        echo " Finish killing process"
                        	if [ "$SAVEFILE" = "no" ] ; then
                        
 
                        	rm -f $SAVETOPATH/$2.killed
                        	else
                        	exit 0
				fi
                        else
                        echo " No $2 process found, exiting"
                        rm -f $SAVETOPATH/$2.killed
                        exit 0
                 	fi
		fi
		elif [ "$1" = "-t" ] ; then t=1
	
			if [ "$OPTSLOGPATH" = "" ] ; then 
				echo " No log file found, check log path"
				exit 0
				else
			
				if [ "$t" = "1" ] ; then 
		
				tail $OPTSTAIL $OPTSLOGPATH > $SAVETOPATH/$2.tail		
	                		
				echo " Result searching $2 in log entry at $OPTSLOGPATH : "
	                
	                	grep $2 $SAVETOPATH/$2.tail &> /dev/null
				
				fi

			if [ "$?" = "0" ] ; then
		
			grep $2 $SAVETOPATH/$2.tail
                                                            
                   	echo
                        echo " Finish searching"
                        
					if [ "$SAVEFILE" = "no" ] ; then
                        
 
                        		rm -f $SAVETOPATH/$2.tail
                       			else
                        		exit 0
					fi
                        		
			else
                       	echo " No log entry for $2 found at $OPTSLOGPATH, exiting"
                        rm -f $SAVETOPATH/$2.tail
                        exit 0
                 	fi
fi
       elif [ "$1" = "--help" ] ; then
       echo " Usage : `basename $0` -p|-d|-k|-t|--help argument"
       echo " Try --help for help"
#       echo " -p : for ps program, example asename $0 -p gnome"
#       echo " -d : for dmesg program, example asename $0 -d ACPI"
#       echo " -k : for killing program, example asename $0 -k gnome"
#       echo " -t : for tail program, example asename $0 -t eth1"
#       echo " --help : for displaying help messages"
       exit 0
       

                
fi                 	
		
else 

echo " Wrong option, use -p for ps, -d for dmesg, -k for killall, -t for tail"
exit 0
fi                                                                                                                                               
