#!/bin/bash
#
#  reiserfs probing script
#  Copyright (C) 2001, 2002 Yury Umanets <torque@ukrpost.net> see COPYING for 
#  licensing and copyright details.
#

reiserfs_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)
	magic=$(echo $magic | strings)
	
	if [ ! $? -eq 0 ]; then
	    echo "Couldn't open device $dev"
	    return 0
	fi
	
	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
	}
	
	magic=$(dd if=$dev bs=1 count=10 skip=$super_off 2> /dev/null)
	magic=$(echo $magic | strings)
	
	test x$magic = xR4Sb && {
	    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

reiserfs_probe $1

exit 0
