#!/bin/bash
MESG="usage: $0 -w warnlvl -c critlvl -d <directory>"
MESG0="Count number of files in a directory"
MESG1="critlvl must exceed warning level"
if [ "$1" == "-h" ]
	then 
	echo $MESG0
	echo $MESG
	exit 3
fi	
if [ "$1" == "-w" ]
	then
	warn=$2
	else
	echo $MESG
	exit 3
fi
if [ "$3" == "-c" ]
	then
	crit=$4
	else
	echo $MESG
	exit 3
fi
if [ "$5" == "-d" ]
	then
	dir=$6
	else
	echo $MESG
	exit 3
fi
if [ "$crit" -le "$warn" ]
	then 
	echo $MESG1
	exit 3
fi
if [ -d $dir ]
	then
	echo -e -n
	else
	echo "Directory $dir cannot be found"
	exit 3
fi

num=`ls -l $dir|wc -l`
stat=UNKNOWN
if [ "$num" -ge "$warn" ]
	then
	stat=WARNING
	lvl=1
	else
	stat=OK
	lvl=0
fi
if [ "$num" -ge "$crit" ]
	then
	stat=CRITICAL
	lvl=2
fi
echo $stat" filecount is : "$num"| files="$num
exit $lvl
