#!/bin/bash

########################################################################
# Copyright (c) 2005 Trimble Navigation Limited.
# $Id: checkBadBlocks,v 1.1 2005/03/15 02:15:10 tom Exp $
# $Source: /home/CVS/fireballTop/fireballFS/root/usr/local/fireball/checkBadBlocks,v $
########################################################################

########################################################################
# checkBadBlocks
#
# Utility script used by the Browser GUI (et al.) to implement
# background searches for bad blocks on compact flash partitions.
#
# When run with the path of a partition, like part4,
# checks that partition.
# When run with no parameters, checks all four partitions sequentially.
#
# Creates a runtime file /var/run/checkBadBlocks.pid
#   First line is the pid of the script
#   Second line contains the name of the partition we are checking.
#   This can be used to detect if the script is already running,
#   and to determine what parition is being checked.
#
# As a partition is checked, the list of bad blocks will go into
# /tmp/badBlocksList.partition
#
# If /etc/sysinfo/badBlocksList.partion doesn't exist, then we'll
# copy the list to that location.
# The GUI will be responsible for updating that copy under user control.
#
########################################################################

if [[ $# = 1 ]]
then
  PartitionsToCheck=$1
else
  PartitionsToCheck='part1 part2 part3 part4'
fi

for partition in $PartitionsToCheck
do
  devPath=/dev/discs/disc0/$partition
  listfile=badblocks.$partition
  echo Checking $devPath
  (echo $$
   echo $devPath
  ) > /var/run/checkBadBlocks.pid
  /sbin/badblocks -o /tmp/$listfile $devPath 

  # If there isn't a saved copy of this file in the
  # nonvolatile info area, then make one.
  if [[ ! -e /etc/sysinfo/$listfile ]]
  then
    mount -oremount,rw /etc/sysinfo 
    cp /tmp/$listfile /etc/sysinfo
    mount -oremount,ro /etc/sysinfo 
  fi
done

rm /var/run/checkBadBlocks.pid
