moth/bin/once

85 lines
2.0 KiB
Plaintext
Raw Normal View History

2015-04-10 16:37:21 -06:00
#! /bin/sh
if [ -n "$1" ]; then
cd $1
else
cd $(dirname $0)/..
fi
basedir=$(pwd)
2015-04-10 16:37:21 -06:00
2017-09-10 21:08:18 -06:00
log () {
echo "moth: $@" 1>&2
}
2015-04-10 16:37:21 -06:00
# Do nothing if `disabled` is present
2017-10-30 10:20:51 -06:00
if [ -f state/disabled ]; then
2017-10-11 18:55:34 -06:00
log "Instance disabled; doing nothing"
2015-04-10 16:37:21 -06:00
exit
fi
2017-10-11 18:55:34 -06:00
# Reset to initial state?
if [ ! -f state/initialized ]; then
log "Resetting contest state"
rm -rf state/teams state/points.new state/points.tmp
mkdir -p state/teams state/points.new state/points.tmp
chown www:www state/teams state/points.new state/points.tmp # Needs root. Use Docker.
: > state/points.log
2017-10-11 18:55:34 -06:00
echo 'Remove this file to obliterate teams and points' > state/initialized
2015-04-10 16:37:21 -06:00
fi
2017-09-10 21:08:18 -06:00
# Create some team names if needed
if [ ! -f state/assigned.txt ]; then
log "Generating team names"
hd </dev/urandom | awk '{print $3 $4 $5 $6;}' | head -n 100 > state/assigned.txt
fi
2017-10-11 18:55:34 -06:00
# Install new categories
for pkg in puzzles/*; do
cat=$(basename $pkg .zip)
if [ ! -f packages/$cat/installed ] || [ $pkg -nt packages/$cat/installed ]; then
log "Installing $pkg"
bin/install-category $pkg
: >packages/$cat/installed
fi
done
2017-09-10 21:08:18 -06:00
# Helpful error message
if [ $(ls packages | wc -l) -eq 0 ]; then
log "error: No packages installed"
exit
fi
# Create a list of currently-active categories
: > state/categories.txt.new
for dn in packages/*; do
cat=${dn##packages/}
echo "$cat" >> state/categories.txt.new
done
mv state/categories.txt.new state/categories.txt
2015-04-10 16:37:21 -06:00
# Collect new points
find state/points.new -type f | while read fn; do
# Skip files opened by another process
lsof $fn | grep -q $fn && continue
# Skip partially written files
[ $(wc -l < $fn) -gt 0 ] || continue
2015-04-18 19:13:07 -06:00
# filter the file for unique awards
sort -k 4 $fn | uniq -f 1 | sort -n >> state/points.log
# Now kill the file
rm -f $fn
2015-04-10 16:37:21 -06:00
done
2016-09-21 16:10:29 -06:00
# Generate new puzzles.json
if bin/puzzles $basedir > state/puzzles.json.new; then
mv state/puzzles.json.new state/puzzles.json
2015-04-10 16:37:21 -06:00
fi
# Generate new points.json
2016-09-21 16:10:29 -06:00
if bin/points $basedir > state/points.json.new; then
mv state/points.json.new state/points.json
2015-04-10 16:37:21 -06:00
fi