-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_site.sh
executable file
·234 lines (200 loc) · 6.27 KB
/
build_site.sh
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
#!/usr/bin/env bash
# build_site.sh by Amory Meltzer
# Parse data, build index, piece together html
function get_help {
cat <<END_HELP
Usage: $(basename $0) -i <current_season.xlsx> [-u]
-i Specify input XLS/XLSX data file. Required.
-u Pass -u to makeMLSTable.pl (updates 'current as of' date)
-h this help
END_HELP
}
while getopts 'i:ulhH?' opt; do
case $opt in
i) input=$OPTARG;;
u) upDate='-u';;
h) get_help $0
exit 0;;
esac
done
# Coming from perl, bash's flexibility with variables makes me uncomfortable
function print() {
cat $top > $index
cat $news >> $index
cat $chart >> $index
cat $table >> $index
cat $arc >> $index
cat $bottom >> $index
cat templates/site_footer >> $index
# Indent html
emacs -batch $index --eval '(indent-region (point-min) (point-max) nil)' -f save-buffer 2>/dev/null
}
# Simple error handling
function dienice() {
echo "$1"
exit 1
}
# some basic checks before getting started
if [ ! "$input" ]; then
dienice "Please specify an XLS/XLSX data file"
elif [[ ! -a "$input" ]]; then
dienice "$input does not exist"
elif [ ! $(echo "$input" | grep -oE "\.xlsx?$") ]; then
dienice "$input is not an XLS/XLSX file"
else
# Main process: parses master excel and produces/calculates all data
perl multipleWorksheets.pl "$input"
# Die angrily if xlsx processing fails
if [ $? != 0 ]; then
echo
echo "Aborting. Cleanup likely needed."
exit
else
echo
fi
# Get all the games and seasons and tournaments that need linking to
SUBS=$(find -E . -regex "./mls_.*_.*.csv" -o -regex "./mls_t....csv" | grep -v _site)
# Generate archive index lists via
perl makeArchiveIndex.pl $SUBS
# Grab everything...
FILES=$(find -E . -maxdepth 1 -regex "./.*_t?[sfu][0-9][0-9].*csv" | grep -v _site)
FILES="$FILES ./mls_master.csv" # Add lifetime totals
FILES="$FILES ./mls_running.csv" # Add running totals
# Die if no proper files can be found
if [ -z "$FILES" ]; then
dienice "No valid files found!!!"
fi
# Build pages
echo "Creating html..."
for csv in $FILES
do
chartnav=0 # Used later to identify new season navs that need mofidying
# find insists on a leading ./ - I won't be providing such things when
# running this but it's a good thing to watch out for when sanitizing
csv=$(echo $csv | perl -pe 's/^.\///;')
# Prune file format
file=$(echo $csv | perl -pe 's/\.csv$//;')
# Generate names of subfolders
season=$(echo $file | grep -oE "t?[sfu][0-9][0-9]")
game=$(echo $file | grep -oE "[0-9][0-9]\.[0-9][0-9]")
# Set default values ahead of time
index=index.html
news=/dev/null
chart=templates/chart
arc=/dev/null
# Build tables
table=$(echo $file.table)
# Tournaments are halfway between seasons and games
# Should be able to make this more efficient FIXME TODO
if [ $(echo $season | grep -oE "t[sfu][0-9][0-9]") ]; then
perl makeMLSTable.pl -ag $csv $table # Game index
chart=/dev/null
elif [ $(echo $file | grep -oE "mls_[sfu][0-9][0-9]") ]; then
if [ $(echo $file | grep -oE "mls_[sfu][0-9][0-9]_") ]; then
perl makeMLSTable.pl -ag $csv $table # Game index
else
perl makeMLSTable.pl -a $csv $table # Season index
# Only show season chart if at least 3 games have been played
if [ $(grep -c ^ $season/data/AB.csv) -lt 5 ]; then
chartnav=1
chart=/dev/null
fi
# Set here to avoid tourny errors FIXME TODO
arc=templates/$season.list
fi
elif [ $(echo $csv | grep -oE "mls_[mr][a-z]{5,6}.csv") ]; then
perl makeMLSTable.pl $upDate $csv $table
fi
# Check each folder individually, avoid overwriting any data
if [[ -n $game ]]; then # Individual game data
if [[ ! -d $season/$game ]]; then
mkdir -p $season/$game/
fi
index=$season/$game/$index
chart=/dev/null
top=templates/game.index.top
arc=/dev/null
bottom=templates/game.index.bottom
print
mv $csv $table $season/$game
elif [[ -n $season ]]; then # Season-total
if [[ ! -d $season ]]; then
mkdir -p $season/
fi
# Only generate if season total
if [ $(echo $file | grep -oE "mls_t?[sfu][0-9][0-9]") ]; then
index=$season/$index
top=templates/season.index.top
bottom=templates/season.index.bottom
# Include TOC on season index, not tournaments
# Cleanup, this is all so awkward FIXME TODO
if [ ! $(echo $file | grep -oE "mls_t[sfu][0-9][0-9]") ]; then
news=templates/season.news
fi
print
echo "Generated $index"
if [ $chartnav == 1 ]; then
sed -i '' 's/^.*<li>.*statsgraph.*<.li>.*$//' $index
fi
chartnav=0
elif [ ! $(echo $file | grep -oE "mls_t?[sfu][0-9][0-9]") ]; then
# Rename and be done with season-based stats
# Stash in data directory
if [[ ! -d $season/data/ ]]; then
mkdir -p $season/data/
fi
mv $csv $season/data/$(echo $csv | sed -E 's/_[sfu][0-9][0-9]//')
continue
fi
# Organize data appropriately for seasons, not tournaments
if [ ! $(echo $file | grep -oE "mls_t[sfu][0-9][0-9]") ]; then
mv $csv $table $season/data/
else
mv $csv $table $season/
fi
elif [ $(echo $csv | grep -oE "mls_master.csv") ]; then
if [[ ! -d life/data/ ]]; then
mkdir -p life/data/
fi
index=life/$index
top=templates/season.index.top
news=templates/season.news
arc=templates/arc.list
bottom=templates/season.index.bottom
print
echo "Generated $index"
# Cleanup TOC
sed -i '' 's/Individual game /Season /' $index
# Cleanup arc
sed -i '' 's/<p>.*Lifetime stats<\/a> \• /<p>/' $index
mv $csv $table life/data/
elif [ $(echo $csv | grep -oE "mls_running.csv") ]; then
top=templates/top
news=templates/news
arc=templates/arc.list
bottom=templates/bottom
print
echo "Generated $index"
if [[ ! -d data/ ]]; then
mkdir -p data/
fi
mv $csv $table data/
else
echo "Warning: unable to properly file $csv"
fi
done
# Move lifetime stats as well
FILES=$(find -E . -maxdepth 1 -regex "./.{1,4}\.csv" | grep -v _site)
for csv in $FILES
do
mv $csv life/data/$csv
done
# Move running stats
FILES=$(find -E . -maxdepth 1 -regex "./running_.{1,4}\.csv" | grep -v _site)
for csv in $FILES
do
mv $csv "data/${csv/running_/}"
done
echo
echo "Site ready!"
fi