#
# Dependencies of this sub-command
#
read_and_register_depends()
{
	echo ""
}

#
# Sub-command: systest::gfarm_v2::read_and_register
#
subcmd_read_and_register()
{
	log_debug "subcmd_read_and_register"

	check_argc $# 2 +
	check_hostid any $HOSTID
	FILE_PATH=$1
	GFMD=$2
	GFSD=$3

	TMP_FILE=/tmp/gfservice.$$
	trap "rm -f $TMP_FILE; exit 1" 1 2 3 15
	cat - > $TMP_FILE

	#
	# Get BACKEND_HOSTNAME of the gfmd host.
	#
	BACKEND_HOSTNAME=`exec_remote_host_agent \
		$GFMD root get-config-gfarm-param param=BACKEND_HOSTNAME`
	[ "X$BACKEND_HOSTNAME" = X ] \
		&& log_error "failed to get BACKEND_HOSTNAME of $HOSTID"

	#
	# Get GFMD_PORT of the gfmd host.
	#
	GFMD_PORT=`exec_remote_host_agent \
		$GFMD root get-config-gfarm-param param=GFMD_PORT`
	[ "X$GFMD_PORT" = X ] \
		&& log_error "failed to get GFMD_PORT of $HOSTID"

	#
	# Get GFSD_HOSTNAME of the gfsd host.
	#
	if [ X$GFSD != X ]; then
		GFSD_HOSTNAME=`exec_remote_host_agent $GFSD root \
			get-config-gfsd-param param=GFSD_HOSTNAME`
		[ "X$GFSD_HOSTNAME" = X ] \
			&& log_error "failed to get GFSD_HOSTNAME of $HOSTID"
	fi

	cat $TMP_FILE | exec_remote_host_agent $HOSTID - systest::gfarm_v2::read_and_register \
		"gfarm-url=gfarm://${BACKEND_HOSTNAME}:${GFMD_PORT}/${FILE_PATH}" \
		"filesystem-node=$GFSD_HOSTNAME"
	[ $? -ne 0 ] && log_error "gfservice systest::gfarm_v2::read_and_register failed"

	rm -f $TMP_FILE

	log_debug "end subcmd_read_and_register"
}

#
# Dependencies of this sub-command agent
#
read_and_register_agent_depends()
{
	echo ""
}

#
# Sub-command: systest::gfarm_v2::read_and_register
# read stdin and register to the Gfarm file system
#
subcmd_read_and_register_agent()
{
	log_debug "subcmd_read_and_register_agent SIZE=$1 PATH=$2 NODE=$3"

	GFARM_URL=`get_param gfarm-url`
	FILESYSTEM_NODE=`get_param filesystem-node`

	if [ X$FILESYSTEM_NODE != X ]; then
		GFREG_OPT="-h $FILESYSTEM_NODE"
	fi

	TMP_FILE=/tmp/gfservice.$$
	trap "rm -f $TMP_FILE; exit 1" 1 2 3 15
	cat > $TMP_FILE

	log_debug "execute: GFARM_CONFIG_FILE=$GFARM_CONF gfreg $GFREG_OPT" \
		"$TMP_FILE $GFARM_URL"
	GFARM_CONFIG_FILE=$GFARM_CONF gfreg $GFREG_OPT $TMP_FILE $GFARM_URL
	[ $? -ne 0 ] && log_error "gfreg command failed"

	rm -f $TMP_FILE

	log_debug "end subcmd_read_and_register_agent"
}
