#!/bin/bash
#
#  reiserfs probing script

#  Copyright (C) 2001, 2002, 2003 by Hans Reiser, licencing governed by
#  reiser4progs/COPYING.


reiserx_probe() {
	dev=$1

	for block_off in 16 2; 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)
		[ ! $? -eq 0 ] && {
		    echo "Can't read $1. Probably permissions denied."
		    return 1
		}

		magic=$(echo $magic | strings)

		if [ ! $? -eq 0 ]; then
		    echo "Couldn't open device $dev"
		    return 0
		fi

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

		test x$magic = xReIsEr2Fs && {
		    echo "reiser 3.6 (standard journal)"
		    return 0
		}

		test x$magic = xReIsEr3Fs && {
		    echo "reiser 3.6 (relocated journal)"
		    return 0
		}

		magic=$(dd if=$dev bs=1 count=10 skip=$super_off 2> /dev/null)
		magic=$(echo $magic | strings)

		test x$magic = xReIsEr4 && {
		    echo "reiser 4.0"
		    return 0
		}

	done
}

[ -z $1 ] && {
	echo "Usage: $0 FILE"
	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

reiserx_probe $1

exit 0
