#!/bin/bash

reiserfs_probe() {
    dev=$1

    for block_off in 2 16; do
    
	super_off=$(expr $block_off \* 4096)
	magic_off=$(expr $super_off + 52)
	
	magic=$(dd if=$dev bs=1 count=10 skip=$magic_off 2> /dev/null)

	test x$magic = xReIsErFs && {
	    echo "reiserfs 3.5"
	    return 0
	}

	test x$magic = xReIsEr2Fs && {
	    echo "reiserfs 3.6 (standard journal)"
	    return 0
	}
	
	test x$magic = xReIsEr3Fs && {
	    echo "reiserfs 3.6 (relocated journal)"
	    return 0
	}
	
	test x$magic = xReIsEr4Fs && {
	    echo "reiserfs 4.0"
	    return 0
	}
	
    done
}

[ -z $1 ] && {
    echo "Usage: $0 DEVICE"
    exit 1
}

if [ ! -b $1 -a ! -r $1 ]; then
    echo "Specified device isn't a block device and not a file"
    exit 1
fi
    
if [ ! -x /bin/dd ]; then
    echo "Can't find \"dd\" program"
    exit 1
fi

echo $(reiserfs_probe $1)

exit 0
