#!/bin/sh
# Filesystem watchdog test script. The test checks to see if the filesystem 
# has been placed into read-only mode. If this is the case return a non-zero value 
# so that the watchdog resets the board.

RESULT_CODE=0
MMC_PARTITION=/dev/mmcblk01
# Perform the filesystem test to see if the filesystem has 
# been mounted in read-only mode. If this is the case we require 
# a reboot.
function test {
    #MOUNT=$(cat /root/fakemounts)
    MOUNT=$(cat /proc/mounts)    
    # Split by newline
    IFS=$'\n' 
    # Process the results for mounts
    for mount in $MOUNT
    do
        #echo "[$mount]"
        # Look for mmc entry
        if [[ $mount =~ .*$MMC_PARTITION*. ]] ; 
        then
            # Split by spaces to process mount entry
            IFS=$' '
            COUNT=0
            for opt in $mount 
            do
                # Looking for third entry that contains (ro|rw)
                if [ $COUNT -eq 3 ] ; 
                then
                    # Split by comma
                    IFS=$','
                    # Try to find 'rw' in mount options
                    for mo in $opt
                    do
                        # Update return code to show something is NOT right
                        if [ "$mo" == "ro" ] ; 
                        then
                            RESULT_CODE=1
                        fi				
                    done
                fi # Count -eq 3
                let COUNT=COUNT+1			
            done 
        fi # $mount =~
    done
}

# No repair functionality. Set to one as it wont be a success.
function repair {    
    RESULT_CODE=1
}

# First argument should be either (test|repair)
case "$1" in 
    test)	
        test
    ;;
    repair)
        repair        
    ;;
    *)
        RESULT_CODE=1
    ;;
esac
# If partition was 'rw' return code should be 0 else it is 1
exit $RESULT_CODE 
