1#!/bin/bash
2set -e
3
4############################################################
5#
6# This script generates emails in the BigMessages directory
7#
8# Usage:
9#   script/generate_big_message.sh FILE
10#
11# where
12#
13#   FILE is the name of the file to generate.
14#     Parent directories will not be generated
15#     The basename of FILE has to be a number (1-4) and
16#     selects the message to be stored in FILE
17#
18# Return value:
19#   0     upon success
20#   != 0  upon failure
21#
22#-----------------------------------------------------------
23
24#-----------------------------------------------------------
25# Prints the body text of an email to stdout
26#-----------------------------------------------------------
27# $TEXT is printed $REPEATS*1000 times to stdout
28generate_text () {
29    # TEXT with newline
30    TEXT="$TEXT
31"
32    # 10 times $TEXT
33    TEXT="${TEXT}${TEXT}${TEXT}${TEXT}${TEXT}${TEXT}${TEXT}${TEXT}${TEXT}${TEXT}"
34    # 100 times $TEXT
35    TEXT="${TEXT}${TEXT}${TEXT}${TEXT}${TEXT}${TEXT}${TEXT}${TEXT}${TEXT}${TEXT}"
36    # 1000 times $TEXT
37    TEXT="${TEXT}${TEXT}${TEXT}${TEXT}${TEXT}${TEXT}${TEXT}${TEXT}${TEXT}${TEXT}"
38    for i in `seq 1 $REPEATS`
39    do
40	echo -n "$TEXT"
41    done
42}
43
44#-----------------------------------------------------------
45# Prints the header of a message to stdout
46#-----------------------------------------------------------
47generate_header () {
48    cat <<EOF
49Date: Tue, 4 Jan 2011 16:39:49 +0100
50From: generator@bash.script
51To: mairix@test.suite
52Subject: message with $SIZE bytes ($SUBJECT)
53
54EOF
55}
56
57#-----------------------------------------------------------
58# Prints a valid message to stdout
59#-----------------------------------------------------------
60generate_message()
61{
62    generate_header
63    generate_text
64}
65#-----------------------------------------------------------
66
67
68
69#processing the parameters
70FILE="$1"
71BASENAME="$(basename "$FILE")"
72test -d "$(dirname "$FILE")"
73
74#selecting the message to generate
75case "$BASENAME" in
76    "1")
77	SUBJECT=285kb
78	REPEATS=5
79	TEXT="Some repeating text quite close to three hundred kilobytes."
80	SIZE=300131
81	;;
82
83    "2")
84	SUBJECT=530kb
85	REPEATS=10
86	TEXT="Some repeating text for fivehundred thirty kilobytes"
87	SIZE=530131
88	;;
89
90    "3")
91	SUBJECT=2MB
92	REPEATS=46
93	TEXT="Some repeating text yielding a bit more than 2 MB..."
94	SIZE=2438130
95	;;
96
97    "4")
98	SUBJECT=5MB
99	REPEATS=95
100	TEXT="Some text that amoung for roughly five megabytes.   "
101	SIZE=5035130
102	;;
103    *)
104	echo "Unknown kind of file to create" >&2
105	exit 1
106esac
107
108#generating the message and dumping it into FILE
109generate_message >"$FILE"
110
111#asserting the message has the correct size
112if test "$SIZE" != "$(stat -c%s "$FILE")"
113then
114    rm -f "$FILE"
115    exit 1
116fi
117