--- /dev/null
+#!/bin/bash
+
+#
+# Generate R script for plot processing from campaign and scenario
+# configuration file
+#
+# Sample run:
+# ./rgen performance-experiments-3.01.cfg > out.R
+#
+
+if test $# -ne 1; then
+ echo "Usage: $0 scenario-config-file" 1>&2
+ exit 1
+fi
+
+scenario_file=$1
+
+scenario_name=$(basename ${scenario_file} .cfg)
+
+declare num_trackers
+declare num_seeders
+declare num_leechers
+declare description
+declare dl_max_limit
+declare ul_max_limit
+
+# parse comment zone in scenario file (description, leechers, seeders etc.)
+parse_comment()
+{
+ num_trackers=$(grep "^[ \t]*#[ \t]*Number of trackers:" ${scenario_file} | awk -F ":" '{print $2;}')
+ num_leechers=$(grep "^[ \t]*#[ \t]*Number of leechers:" ${scenario_file} | awk -F ":" '{print $2;}')
+ num_seeders=$(grep "^[ \t]*#[ \t]*Number of seeders:" ${scenario_file} | awk -F ":" '{print $2;}')
+ dl_max_limit=$(grep "^[ \t]*#[ \t]*Maximum download:" ${scenario_file} | awk -F ":" '{print $2;}')
+ dl_max_upload=$(grep "^[ \t]*#[ \t]*Maximum upload:" ${scenario_file} | awk -F ":" '{print $2;}')
+ description=$(grep "^[ \t]*#[ \t]*Description:" ${scenario_file} | awk -F ":" '{print $2;}')
+}
+
+# test client description for "leecher" string
+test_is_leecher()
+{
+ client_type=$1
+
+ grep "leecher" <<<"${client_type}" &> /dev/null
+}
+
+# test client description for "seeder" string
+test_is_seeder()
+{
+ client_type=$1
+
+ grep "seeder" <<<"${client_type}" &> /dev/null
+}
+
+parse_comment
+
+cat <<END
+# import ggplot2
+library(ggplot2)
+
+# Read transmitted command line arguments
+args <- commandArgs(trailingOnly = TRUE)
+
+# The data files are located in the target folder. Also, the graph will be saved in the target folder.
+target_folder <- args[2]
+campaign_name <- args[3]
+rm(args)
+
+# read data from the data file
+END
+
+index=1
+while read server port user remote_path remote_if dl_bw dl_burst ul_bw ul_burst num_conn pre_run_script post_run_script client_type torrent_file periods; do
+ new_port=${port%22}
+ if test -z "${new_port}"; then
+ base_log_name=${server%%.*}
+ else
+ base_log_name=${server%%.*}-${new_port}
+ fi
+ if test_is_leecher ${client_type}; then
+ base_client_name="leecher"
+ elif test_is_seeder ${client_type}; then
+ base_client_name="seeder"
+ else
+ base_client_name="tracker"
+ fi
+ if [ ${base_client_name} != "tracker" ]; then
+ cat <<END
+${base_client_name}${index}=read.table(paste(target_folder, "${base_log_name}.log.data", sep="/"), header=T, sep=" ")
+END
+ ((index++))
+ fi
+done < <(grep -v '^#\|^[ \t]*$' ${scenario_file})
+
+cat <<END
+
+# transform KB/s to Mbit/s
+END
+
+index=1
+while read server port user remote_path remote_if dl_bw dl_burst ul_bw ul_burst num_conn pre_run_script post_run_script client_type torrent_file periods; do
+ if ! test_is_leecher ${client_type}; then
+ ((index++))
+ continue
+ fi
+ cat <<END
+leecher${index}\$dlspeed <- leecher${index}\$dlspeed*8/1000
+END
+ ((index++))
+done < <(grep -v '^#\|^[ \t]*$' ${scenario_file})
+
+cat <<END
+
+# plot dlspeed-percent data
+p <- ggplot() +
+END
+
+index=1
+while read server port user remote_path remote_if dl_bw dl_burst ul_bw ul_burst num_conn pre_run_script post_run_script client_type torrent_file periods; do
+ if ! test_is_leecher ${client_type}; then
+ ((index++))
+ continue
+ fi
+ cat <<END
+geom_point(aes(x=leecher${index}\$percent, y=leecher${index}\$dlspeed, label="Leecher ${index}"), size=1) +
+END
+ ((index++))
+done < <(grep -v '^#\|^[ \t]*$' ${scenario_file})
+
+cat <<END
+theme_bw() +
+scale_x_continuous("Percent", limits=c(0, 100), breaks=seq(0, 100, 10)) +
+scale_y_continuous("Download speed (Mbit/s)", limits=c(0, ${dl_max_limit}), breaks=seq(0, ${dl_max_limit}, 0.5)) +
+coord_cartesian() +
+scale_colour_manual("Legend") +
+opts(title=paste(campaign_name, "${scenario_name}: a test swarm (${num_seeders} Seeders, ${num_leechers} Leechers, ${num_trackers} Trackers), ${description}", sep="\n"))
+
+# plot data as an eps file
+postscript(paste(target_folder, "${scenario_name}-dlspeed-percent.eps", sep="/"))
+print(p)
+dev.off()
+
+# plot data as an png file
+png(paste(target_folder, "${scenario_name}-dlspeed-percent.png", sep="/"), width = 1280, height = 800)
+print(p)
+dev.off()
+
+
+# plot dlspeed-time data
+p <- ggplot() +
+END
+
+index=1
+while read server port user remote_path remote_if dl_bw dl_burst ul_bw ul_burst num_conn pre_run_script post_run_script client_type torrent_file periods; do
+ if ! test_is_leecher ${client_type}; then
+ ((index++))
+ continue
+ fi
+ cat <<END
+geom_point(aes(x=leecher${index}\$time, y=leecher${index}\$dlspeed, label="Leecher ${index}"), size=1) +
+END
+ ((index++))
+done < <(grep -v '^#\|^[ \t]*$' ${scenario_file})
+
+cat <<END
+theme_bw() +
+scale_x_continuous("Time(s)") +
+scale_y_continuous("Download speed (Mbit/s)", limits=c(0, ${dl_max_limit}), breaks=seq(0, ${dl_max_limit}, 0.5)) +
+coord_cartesian() +
+scale_colour_manual("Legend") +
+opts(title=paste(campaign_name, "${scenario_name}: a test swarm (${num_seeders} Seeders, ${num_leechers} Leechers, ${num_trackers} Trackers), ${description}", sep="\n"))
+
+# plot data as an eps file
+postscript(paste(target_folder, "${scenario_name}-dlspeed-time.eps", sep="/"))
+print(p)
+dev.off()
+
+# plot data as an png file
+png(paste(target_folder, "${scenario_name}-dlspeed-time.png", sep="/"), width = 1280, height = 800)
+print(p)
+dev.off()
+END