#!/bin/sh
# Copyright (C) 2013 OpenWrt.org
reasonpath=/tmp/ra_crash_time_log.txt
ramoopslogpath=/sys/fs/pstore/dmesg-ramoops-0
mtdid=$(cat /proc/mtd | grep "LOG" | awk -F : '{print $1;}' | sed s/mtd//)

compare_crashlog() {
	if [ -f "$ramoopslogpath" ]; then
		dd if=/dev/mtd$mtdid of=/tmp/mtdcrashlog bs=1024 count=10
		dd if=$ramoopslogpath of=/tmp/tempcrashlog bs=1024 count=10
		mtd_md5=$(md5sum /tmp/mtdcrashlog | cut -d ' ' -f1)
		temp_md5=$(md5sum /tmp/tempcrashlog | cut -d ' ' -f1)
		if [ "$mtd_md5" != "$temp_md5" ]; then
			echo reason=1 > $reasonpath
			mtd erase /dev/mtd$mtdid
			mtd write $ramoopslogpath /dev/mtd$mtdid
		else
			echo reason=0 > $reasonpath
		fi
		rm /tmp/mtdcrashlog
		rm /tmp/tempcrashlog
	else
		echo reason=0 > $reasonpath
	fi

}

compare_crashlog
