
testcmd=lib/libgfarm/gfarm/gfm_inode_or_name_op_test/gfm_inode_or_name_op_test
inode_op="$testcmd -I"
inode_op_nf="$testcmd -J"
name_op="$testcmd -N"
name_op_parent="$testcmd -Np"
name2_op="$testcmd -P"
name2_op_ol="$testcmd -Q"
gfs_realpath="$testcmd -R"
get_svr="$testcmd -S"

error() {
	echo $*
	clean_test
	exit $exit_fail;
}

check_ok() {
	ret=$?
	if [ "$ret" != 0 ]; then
		error expected exit code 0 but $ret
	fi
}

check_fail() {
	ret=$?
	if [ "$ret" = 0 ]; then
		error expected exit code non 0 but $ret
	fi
}

exec_check_ok() {
	$*
	check_ok
}

reg_file() {
	echo $1 > $localtmp
	exec_check_ok gfreg $localtmp $2
}

setup_file_structure() {

	# file structure
	#
	#     $gftmp                             : $R_d
	#     $gftmp/A                           : $A_d
	#     $gftmp/A/a1                        : $a1_f
	#     $gftmp/A/a2                        : $a2_f
	#     $gftmp/A/AA                        : $AA_d
	#     $gftmp/A/AA/a3                     : $a3_f
	#     $gftmp/A/AA/a4                     : $a4_f
	#     $gftmp/A/AA/a5                     : $a5_f
	#     $gftmp/A/AA/toC -> $gftmp/C        : $toC_l
	#     $gftmp/A/AA/toBB -> ../../B/BB     : $toBB_l
	#     $gftmp/toa1 -> $gftmp/A/a1         : $toa1_l
	#     $gftmp/B                           : $B_d
	#     $gftmp/B/toA -> $gftmp/A           : $toA_l
	#     $gftmp/B/toa2 -> $gftmp/A/a2       : $toa2_l
	#     $gftmp/B/BB                        : $BB_d
	#     $gftmp/B/BB/toAA -> $gftmp/A/AA    : $toAA_l
	#     $gftmp/B/BB/toa3 -> $gftmp/A/AA/a3 : $toa3_l
	#     $gftmp/B/BB/toa5 -> ../../A/AA/a5  : $toa5_l
	#     $gftmp/C                           : $C_d
	#     $gftmp/C/toa4 -> $gftmp/A/AA/a4    : $toa4_l
	#     $gftmp/C/toRoot -> /               : $toRoot
	#     $gftmp/toNotExist -> /NotExist     : $toNotExist_l
	#     $gftmp/L -> $gftmp/L/LL            : $toLL_l

	R_d=$gftmp
	A_d=$gftmp/A
	AA_d=$A_d/AA
	B_d=$gftmp/B
	BB_d=$B_d/BB
	C_d=$R_d/C
	a1_f=$A_d/a1
	a2_f=$A_d/a2
	a3_f=$AA_d/a3
	a4_f=$AA_d/a4
	a5_f=$AA_d/a5
	NotExist_f=$gftmp/NotExist
	LL_d=$gftmp/L/LL
	toC_l=$AA_d/toC
	toBB_l=$AA_d/toBB
	toa1_l=$gftmp/toa1
	toA_l=$B_d/toA
	toa2_l=$B_d/toa2
	toAA_l=$BB_d/toAA
	toa3_l=$BB_d/toa3
	toa4_l=$C_d/toa4
	toa5_l=$BB_d/toa5
	toRoot_l=$C_d/toRoot
	toNotExist_l=$gftmp/toNotExist
	toLL_l=$gftmp/L

	exec_check_ok gfmkdir $R_d
	exec_check_ok gfmkdir $A_d
	exec_check_ok gfmkdir $AA_d
	exec_check_ok gfmkdir $B_d
	exec_check_ok gfmkdir $BB_d
	exec_check_ok gfmkdir $C_d
	reg_file a1 $a1_f
	reg_file a2 $a2_f
	reg_file a3 $a3_f
	reg_file a4 $a4_f
	reg_file a5 $a5_f
	exec_check_ok gfln -s $C_d        $toC_l
	exec_check_ok gfln -s $a1_f       $toa1_l
	exec_check_ok gfln -s $A_d        $toA_l
	exec_check_ok gfln -s $a2_f       $toa2_l
	exec_check_ok gfln -s $AA_d       $toAA_l
	exec_check_ok gfln -s $a3_f       $toa3_l
	exec_check_ok gfln -s $a4_f       $toa4_l
	exec_check_ok gfln -s /           $toRoot_l
	exec_check_ok gfln -s $NotExist_f $toNotExist_l
	exec_check_ok gfln -s $LL_d       $toLL_l
	exec_check_ok gfln -s ../../A/AA/a5 $toa5_l
	exec_check_ok gfln -s ../../B/BB  $toBB_l
}

clean_file_structure() {
	gfrm -f $a1_f $a2_f $a3_f $a4_f $a5_f $toC_l $toa1_l $toA_l \
		$toa2_l $toAA_l $toa3_l $toa4_l $toRoot_l \
		$toNotExist_l $toLL_l $toa5_l $toBB_l 2>/dev/null
	gfrmdir $C_d $BB_d $B_d $AA_d $A_d $R_d 2> /dev/null
}

check_env_mds2() {
	if [ "$GFARM_TEST_MDS2" = "" ]; then
		echo GFARM_TEST_MDS2 is not set
		exit $exit_unsupported
	fi
}

setup_file_structure_mds2() {
	# file structure
	#
	#   MDS1 (default)
	#
	#     $gftmp                                    : $R_d
	#     $gftmp/A                                  : $A_d
	#     $gftmp/A/toU -> gfarm://mds2/$gftmp/U     : $toU_l
	#     $gftmp/A/tou1 -> gfarm://mds2/$gftmp/U/u1 : $tou1_l
	#     $gftmp/A/toR1 -> $gftmp                   : $toR1_l
	#     $gftmp/toV -> gfarm://mds2/$gftmp/V       : $toV_l
	#
	#   MDS2 (GFARM_TEST_MDS2)
	#
	#     $gftmp                                    : $R_d
	#     $gftmp/U                                  : $U_d
	#     $gftmp/U/u1                               : $u1_f
	#     $gftmp/U/toR2 -> $gftmp                   : $toR2_l
	#     $gftmp/V                                  : $V_d
	#     $gftmp/V/v1                               : $v1_f
	#     $gftmp/V/toA -> gfarm://mds1/$gftmp/A     : $toA_l
	#     $gftmp/L -> $gftmp/L/LL                   : $toLL_l

	R_d=$gftmp
	A_d=$gftmp/A
	toU_l=$A_d/toU
	tou1_l=$A_d/tou1
	toV_l=$gftmp/toV
	toR1_l=$A_d/toR1

	U_d=$gftmp/U
	V_d=$gftmp/V
	LL_d=$gftmp/L/LL
	v1_f=$V_d/v1
	u1_f=$U_d/u1
	toA_l=$V_d/toA
	toR2_l=$U_d/toR2
	toLL_l=$gftmp/L

	mds1=gfarm://`$get_svr`
	echo check $mds1
	exec_check_ok gfls $mds1 > /dev/null

	mds2=gfarm://$GFARM_TEST_MDS2
	echo check $mds2
	exec_check_ok gfls $mds2 > /dev/null

	exec_check_ok gfmkdir $mds1$R_d
	exec_check_ok gfmkdir $mds1$A_d
	gfmkdir $mds2/tmp > /dev/null
	gfchmod 777 $mds2/tmp > /dev/null
	exec_check_ok gfmkdir $mds2$R_d
	exec_check_ok gfmkdir $mds2$U_d
	exec_check_ok gfmkdir $mds2$V_d
	reg_file u1 $mds2$u1_f
	reg_file v1 $mds2$v1_f
	exec_check_ok gfln -s $mds2$U_d $mds1$toU_l
	exec_check_ok gfln -s $mds2$u1_f $mds1$tou1_l
	exec_check_ok gfln -s $mds2$V_d $mds1$toV_l
	exec_check_ok gfln -s $mds1$A_d $mds2$toA_l
	exec_check_ok gfln -s $gftmp $toR1_l
	exec_check_ok gfln -s $gftmp $mds2$toR2_l
	exec_check_ok gfln -s $LL_d $mds2$toLL_l
}

clean_file_structure_mds2() {
	gfrm -f $mds1$toU_l $mds1$tou1_l $mds1$toV_l $mds1$toR1_l 2> /dev/null
	gfrmdir $mds1$R_d $mds1$A_d $mds1$gftmp 2> /dev/null

	gfrm -f $mds2$u1_f $mds2$v1_f $mds2$toA_l $mds2$toR2_l $mds2$toLL_l \
		2> /dev/null
	gfrmdir $mds2$U_d $mds2$V_d $mds2$gftmp 2> /dev/null
}

check_env_mds4() {
	if [ "$GFARM_TEST_MDS2" = "" ]; then
		echo GFARM_TEST_MDS2 is not set
		exit $exit_unsupported
	fi
	if [ "$GFARM_TEST_MDS3" = "" ]; then
		echo GFARM_TEST_MDS3 is not set
		exit $exit_unsupported
	fi
	if [ "$GFARM_TEST_MDS4" = "" ]; then
		echo GFARM_TEST_MDS4 is not set
		exit $exit_unsupported
	fi
}

setup_file_structure_mds4() {
	# file structure
	#
	#   MDS1 (default)
	#
	#     $gftmp                                    : $R_d
	#     $gftmp/toM2 -> gfarm://mds2/$gftmp        : $toM2_l
	#
	#   MDS2 (GFARM_TEST_MDS2)
	#
	#     $gftmp                                    : $R_d
	#     $gftmp/toM3 -> gfarm://mds3/$gftmp        : $toM3_l
	#
	#   MDS3 (GFARM_TEST_MDS3)
	#
	#     $gftmp                                    : $R_d
	#     $gftmp/toM4 -> gfarm://mds4/$gftmp        : $toM4_l
	#
	#   MDS4 (GFARM_TEST_MDS3)
	#
	#     $gftmp                                    : $R_d
	#     $gftmp/A                                  : $A_d

	R_d=$gftmp
	A_d=$R_d/A
	toM2_l=$R_d/toM2
	toM3_l=$R_d/toM3
	toM4_l=$R_d/toM4

	mds1=gfarm://`$get_svr`
	echo check $mds1
	exec_check_ok gfls $mds1 > /dev/null

	mds2=gfarm://$GFARM_TEST_MDS2
	echo check $mds2
	exec_check_ok gfls $mds2 > /dev/null
	gfmkdir $mds2/tmp 2> /dev/null
	gfchmod 777 $mds2/tmp 2> /dev/null

	mds3=gfarm://$GFARM_TEST_MDS3
	echo check $mds3
	exec_check_ok gfls $mds3 > /dev/null
	gfmkdir $mds3/tmp 2> /dev/null
	gfchmod 777 $mds3/tmp 2> /dev/null

	mds4=gfarm://$GFARM_TEST_MDS4
	echo check $mds4
	exec_check_ok gfls $mds4 > /dev/null
	gfmkdir $mds4/tmp 2> /dev/null
	gfchmod 777 $mds4/tmp > /dev/null

	exec_check_ok gfmkdir $mds1$R_d
	exec_check_ok gfmkdir $mds2$R_d
	exec_check_ok gfmkdir $mds3$R_d
	exec_check_ok gfmkdir $mds4$R_d
	exec_check_ok gfmkdir $mds4$A_d
	exec_check_ok gfln -s $mds2 $toM2_l
	exec_check_ok gfln -s $mds3 $mds2$toM3_l
	exec_check_ok gfln -s $mds4 $mds3$toM4_l
}

clean_file_structure_mds4() {
	gfrm -f $mds1$toM2_l $mds2$toM3_l $mds3$toM4_l 2> /dev/null
	gfrmdir $mds1$R_d $mds2$R_d $mds3$R_d $mds4$A_d $mds4$R_d \
		2> /dev/null
}

