moth/bin/once

56 lines
1.2 KiB
Bash
Executable File

#! /bin/sh
if [ -n "$1" ]; then
cd $1
else
cd $(dirname $0)/..
fi
basedir=$(pwd)
[ -n "$DEBUG" ] && printf "Updating moth instance in %s\n" $(pwd)
# Do nothing if `disabled` is present
if [ -f disabled ]; then
exit
fi
# Reset to initial state if `reset` is present
if [ -f reset ]; then
rm -f state/teams/* state/points.new/* state/points.tmp/*
: > state/points.log
rm -f reset
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
# 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
# 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
done
# Generate new puzzles.json
if bin/puzzles $basedir > state/puzzles.json.new; then
mv state/puzzles.json.new state/puzzles.json
fi
# Generate new points.json
if bin/points $basedir > state/points.json.new; then
mv state/points.json.new state/points.json
fi