#!/bin/sh
#
# Check the firmware integrity
#

PATH=/bin:/usr/bin:/sbin:/usr/sbin

#
# Important files (or directories) that could be corrupted by
# unsynced power-off.
#
FILES="
/etc/inetd.conf
"
# Below extension is in sync with backup-config RPM.
BACKUP_EXT=_backup
#
# check_sys_files()
#	check for missing or corrupted files in the list 
#
check_sys_files()
{
	RET=0
	for i in $FILES; do
			grep "[[:alnum:]]" < $i > /dev/null 2>&1
			if [ $? -ne 0 ]; then
				# Check the secondary.
				grep "[[:alnum:]]" < /mnt/$i >/dev/null 2>&1
				if [ $? -ne 0 ]; then
					echo "--- File $i is corrupted on both partition."

					#
					# Recover the file from a backup file if available
					#
					if [ -f $i.0 ]; then
						echo "--- Recover from the backup file $i.0."
						echo "--- The file may not be up-to-date."
						echo "--- Please check it after system boots up."
						echo ""
						cp $i.0 $i 
					elif [ -f $i$BACKUP_EXT ]; then
						echo "--- Recover from the backup file $i$BACKUP_EXT"
						echo "--- The file may not be up-to-date."
						echo "--- Please check it after system boots up."
						echo ""
						cp $i$BACKUP_EXT $i
					else
						RET=1
					fi
				else
					echo "Recover file $i from the secondary partition."
					cp -a /mnt/$i $i	
				fi
			fi
	done
	return $RET
}

#
# main
#
check_sys_files
