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

#
# Sub-command: append file
#
subcmd_append_file()
{
	log_debug "subcmd_append_file"

	check_argc $# 2
	check_hostid any $HOSTID
	exec_remote_host_agent $HOSTID - systest::gfarm2fs::append_file "$@"
	[ $? -ne 0 ] && log_error "gfservice systest::gfarm2fs::append_file failed"

	log_debug "end subcmd_append_file"
}

#
# calculate numeric size from human friendly format
#
calc_numeric_size()
{
	case $1 in
	*K)
		NUMERIC_SIZE=`expr "$1" : "\([1-9][0-9]*\)K"`
		NUMERIC_SIZE=`expr $NUMERIC_SIZE \* 1024`
		;;
	*M)
		NUMERIC_SIZE=`expr "$1" : "\([1-9][0-9]*\)M"`
		NUMERIC_SIZE=`expr $NUMERIC_SIZE \* 1024 \* 1024`
		;;
	*G)
		NUMERIC_SIZE=`expr "$1" : "\([1-9][0-9]*\)G"`
		NUMERIC_SIZE=`expr $NUMERIC_SIZE \* 1024 \* 1024 \* 1024`
		;;
	*)
		NUMERIC_SIZE=$1
	esac

	echo $NUMERIC_SIZE

	return 0
}

append_file()
{
	FILE_PATH=$1
	FILE_SIZE=$2

	NUMERIC_SIZE=`calc_numeric_size $FILE_SIZE`
	BLOCK_SIZE=65536
	BLOCKS=`expr $NUMERIC_SIZE / $BLOCK_SIZE`
	REMAINDER=`expr $NUMERIC_SIZE % $BLOCK_SIZE`

	dd if=/dev/zero bs=$BLOCK_SIZE count=$BLOCKS | tr "\000" "a" \
		>> $FILE_PATH
	[ $? -ne 0 ] && log_error "failed to append file: $PROGHOST:$FILE_PATH"

	[ $REMAINDER -eq 0 ] && return 0

	dd if=/dev/zero bs=$REMAINDER count=1 | tr "\000" "a" >> $FILE_PATH
	[ $? -ne 0 ] && log_error "failed to append file: $PROGHOST:$FILE_PATH"

	log_debug "$PROGHOST appends $FILE_PATH done"
}

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

#
# Sub-command: systest::gfarm2fs::append_file
# Make file in given path.
#
subcmd_append_file_agent()
{
	log_debug "subcmd_append_file_agent FILE_PATH=$1 FILE_SIZE=$2"

	append_file "$@"

	log_debug "end subcmd_append_files_agent"

	return 0
}
