summaryrefslogtreecommitdiff
path: root/pgm6/boiler.sh
blob: 7f8b403a5de5ffd79cc28392006087bf65a3ff74 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/bin/sh
#
# boiler.sh
# Adam Carpenter - 2017 - acarpent - acarpenter@email.wm.edu
# Utilizes code from P. Kearns - 2004
#


# **** Initializations ****
tempFile="./beetle.temp"
dataFile="./beetle.data"
plotfile=""
lifetimeAwk='BEGIN { FS = " "; } NF == 11 {print $1 " " $11}'
usage="boiler: usage: ./boiler -n num -i min max step {-p plotfile.png} | -r statefile"
trap 'cleanExit' INT
trap 'giveCurrent' USR1
trap 'dumpState' USR2


# **** Function declarations ****
cleanExit() {
    kill -9 "$pid" 2>/dev/null
    rm -f $tempFile
    echo
    exit 0
}

giveCurrent() {
    echo $min
}

dumpState() {
    kill -9 "$pid" 2>/dev/null
    stateFile="./beetle.state"
    echo $num > $stateFile
    echo $min >> $stateFile
    echo $max >> $stateFile
    echo $step >> $stateFile
    echo $plotFile >> $stateFile
    exec 3<$tempFile

    while read -u 3 line; do
        echo "$line" >> $stateFile
    done

    exec 3<&-
    rm -f $tempFile
    echo
    exit 0
}

beetlejuice() {
    # beetlejuice
    # beetlejuice
    if [ -f ./beetle ]; then
        ./beetle $min $num >> $tempFile &
    else
        /home/f85/kearns/public/415/p6/beetle $min $num >> $tempFile &
    fi

    pid=$!

    while ps -p $pid --format pid= > /dev/null; do
        : # Just waiting for traps.
    done

    min=`expr $min + $step`
}

# **** Main script ****
# Remove any residual temp file (should be a nonissue).
rm -f $tempFile
rm -f $dataFile

# Make sure there are arguments.
if [ -z $1 ]; then
    echo $usage
    exit 1
fi

if [ $1 = "-r" ]; then # Operating in restore mode.
    shift

    if [ ! -e $1 ]; then
        echo boiler: state file not found
        exit 1
    fi

    # Get state file and read in beetle arguments.
    if [ ! -z $1 ]; then
        stateFile=$1
        exec 3<$stateFile
        read -u 3 line
        num=$line
        read -u 3 line
        min=$line
        read -u 3 line
        max=$line
        read -u 3 line
        step=$line
        read -u 3 line
        plotFile=$line

        while read -u 3 line; do
            echo "$line" >> $tempFile
        done

        exec 3<&-
    else
        echo $usage
        exit 1
    fi

else # Operating in normal mode.
    while [ "$1" != "" ]; do

        case $1 in

        # Get num.
        "-n")
        shift

        if [ ! -z $1 ]; then
            num=$1
        else
            echo $usage
            exit 1
        fi
        ;;

        # Get min, max, and step.
        "-i")
        shift

        if [ ! -z $1 ]; then
            min=$1
        else
            echo $usage
            exit 1
        fi

        shift

        if [ ! -z $1 ]; then
            max=$1
        else
            echo $usage
            exit 1
        fi

        shift

        if [ ! -z $1 ]; then
            step=$1
        else
            echo $usage
            exit 1
        fi
        ;;

        # Get plotfile if requested.
        "-p")
        shift

        if [ ! -z $1 ]; then
            plotFile=$1
        else
            echo $usage
            exit 1
        fi
        ;;

        # Catch extraneous args.
        *)
        echo $usage
        exit 1
        ;;

        esac

        shift
    done

fi

# Make sure required vars were set correctly.
if [ -z $num ] || [ -z $min ] || [ -z $max ] || [ -z $step ]; then
    echo $usage
    exit 1
fi

# I researched methods of checking whether a variable is an integer online
# for shell scripts (where variables aren't declared or strongly typed) and I
# found a method of comparing strings to regular expressions, where any
# character that isn't a numeric is incorrect.
case $num in
    ''|*[!0-9]*) echo $usage; exit 1;;
    *);;
esac

case $min in
    ''|*[!0-9]*) echo $usage; exit 1;;
    *);;
esac

case $max in
    ''|*[!0-9]*) echo $usage; exit 1;;
    *);;
esac

case $step in
    ''|*[!0-9]*) echo $usage; exit 1;;
    *);;
esac

# Run the first simulaton to ensure it gets done no matter the size of max.
beetlejuice

# Run the remaining beetle simulations.
while [ $min -lt $max ] || [ $min -eq $max ]; do
    beetlejuice
done

# Use awk to pull data from tempFile and print it out.
awk "$lifetimeAwk" "$tempFile"

# If a plot was requested, plot the data into the given filename.
if [ ! -z $plotFile ]; then
    awk "$lifetimeAwk" "$tempFile" >> $dataFile

    gnuplot <<- EOF
    set xlabel "Square Side (inches)"
    set ylabel "Mean Beetle Lifetime (seconds)"
    set term png
    set output "$plotFile"
    plot "$dataFile" using 1:2 title ""
EOF

    rm -f $dataFile
fi

rm -f $tempFile