#!/bin/sh

################################################################################
##                                                                            ##
## Copyright (c) International Business Machines  Corp., 2005                 ##
##                                                                            ##
## This program is free software;  you can redistribute it and#or modify      ##
## it under the terms of the GNU General Public License as published by       ##
## the Free Software Foundation; either version 2 of the License, or          ##
## (at your option) any later version.                                        ##
##                                                                            ##
## This program is distributed in the hope that it will be useful, but        ##
## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
## for more details.                                                          ##
##                                                                            ##
## You should have received a copy of the GNU General Public License          ##
## along with this program;  if not, write to the Free Software               ##
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA    ##
##                                                                            ##
##                                                                            ##
################################################################################
#
# File:
#   ftp-upload-stress02-rmt
#
# Description:
#   This is the remote script for ftp-upload-ipv${IP_VER}-stress02
#
# Author:
#   Mitsuru Chinen <mitch@jp.ibm.com>
#
# Arguments:
#   $1: ip address of the server
#   $2: filename to upload
#   $3: duration
#   $4: connect quantity
#
# Exit Value:
#    0: Success
#   >0: Fail
#
# History:
#	Oct 19 2005 - Created (Mitsuru Chinen)
#
#-----------------------------------------------------------------------
# Uncomment line below for debug output.
#trace_logic=${trace_logic:-"set -x"}
$trace_logic

# Check the arguments
if [ $# -ne 6 ]; then
  echo "Usage: $0 server_addr urldir filename filesize duration client_num"
    exit 1
fi
server_ipaddr="$1"
urldir="$2"
filename="$3"
filesize="$4"
duration="$5"
client_num="$6"

echo $server_ipaddr | fgrep ':' >/dev/null 2>&1
if [ $? -eq 0 ]; then
    server_ipaddr='['$server_ipaddr']'
fi

# Unset the maximum number of processes
ulimit -u unlimited

# Create a file to upload
echo -n "A" > $filename
echo -n "Z" | dd of=$filename bs=1 seek=`expr $filesize - 1` >/dev/null 2>&1
if [ $? -ne 0 ]; then
    echo "Failed to create $filename"
    exit 1
fi

#
# Main loop
#
start_epoc=`date +%s`
while true ; do
    # Exit when the specified seconds have passed.
    current_epoc=`date +%s`
    elapse_epoc=`expr $current_epoc - $start_epoc`
    if [ $elapse_epoc -ge $duration ]; then
	break
    fi

    num=0
    while [ $num -lt $client_num ]; do
	ps auxw | grep -l -- "curl.*${filename}${num}" >/dev/null 2>&1
	if [ $? -eq 0 ]; then
	    num=`expr $num + 1`
	    continue
	fi
	curl -u anonymous:ftp@ltp-ns.org -T $filename -g "ftp://${server_ipaddr}/${urldir}/${filename}${num}" >/dev/null 2>&1
	num=`expr $num + 1`
    done
done

# kill the curl command
killall -SIGINT curl >/dev/null 2>&1
sleep 3
killall -SIGKILL curl >/dev/null 2>&1
wait

# Check the test file is abole to be uploaded
curl -u anonymous:ftp@ltp-ns.org -T $filename -g "ftp://${server_ipaddr}/${urldir}/" >/dev/null 2>&1
if [ $? -ne 0 ]; then
    echo "Failed to upload the file to ftp://${server_ipaddr}/${urldir}/"
    rm -f $filename
    exit 1
fi

exit 0
