#!/bin/sh
 
# This is the directory where the sha1 sums will be stored.
# It makes sense to put it somewhere in /root
SHADIR=/your/secure/directory
 
# These are the directories that will be checked, seperated by spaces.
CHECKDIR=( /etc /bin /sbin /boot /lib )
 
function makeSums {
	## Check for .sha1 file directory ##
 
	if [ -d $SHADIR ]; then
		echo "Found directory ${SHADIR}, skipping creation."
	else
		echo "Directory ${SHADIR} does not exist, creating..."
		mkdir $SHADIR
	fi
 
 
	## Create .sha1 files ##
 
	for CHKD in ${CHECKDIR[@]}
	do
		echo Creating sha1-sums for $CHKD ...
 
		for FILE in `find ${CHKD} -type f`
		do
			sha1sum $FILE > "${SHADIR}/`basename ${FILE}`.sha1"
		done
	done
	echo "Setting premissions..."
	chmod -R 600 $SHADIR
	echo Done.
}
 
function verifySums {
	for FILE in `ls -A ${SHADIR}`
	do
		RESULT=`sha1sum -c ${SHADIR}/${FILE} | sed 's/^.* //'`
		if [ "$RESULT" != "OK" ]; then
			if [ $1 == 0 ]; then
				echo "${SHADIR}/${FILE}" | sed 's/^.* //'
			else
				cat "${SHADIR}/${FILE}" | sed 's/^.* //' >> $2
			fi
		fi
	done
}
 
function listUpdate {
	exec<$1
	while read line
	do
		updateSum $line
	done
}
 
function updateSum {
	sha1sum $1 > "${SHADIR}/`basename ${1}`.sha1"
}
 
 
if [ "$1" == "-m" ]; then
	makeSums
elif [ "$1" == "-ul" ]; then 
	listUpdate $2
	chmod -R 600 $SHADIR
	echo Done.
elif [ "$1" == "-u" ]; then 
	updateSum $2
	chmod -R 600 $SHADIR
	echo Done.
elif [ "$1" == "-c" ]; then 
	verifySums 0 0
elif [ "$1" == "-cf" ]; then
	verifySums 1 $2
else
	echo "Options:"
	echo "   -m         Make checksums"
	echo "   -c         Verify checksums against files (No output is good)"
	echo "   -cf file   Same as -c, but write output to file"
	echo "   -u         Update a single checksum"
	echo "   -ul file   Update multiple checksums from generated file"
fi