2015-04-08 17:28:18 -06:00
|
|
|
#! /bin/sh -e
|
|
|
|
|
|
|
|
indir=$1; shift
|
|
|
|
|
|
|
|
if ! [ -d $indir ]; then
|
|
|
|
echo "Usage: $0 PUZZLEDIR"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
die () {
|
2015-04-12 11:11:15 -06:00
|
|
|
echo "$@" 1>&2
|
|
|
|
exit 1
|
2015-04-08 17:28:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
escape () {
|
2015-04-12 11:11:15 -06:00
|
|
|
sed 's/&/\&/g;s/</\</g;s/>/\>/g'
|
2015-04-08 17:28:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
template () {
|
2015-04-12 11:11:15 -06:00
|
|
|
cat="$1"; shift
|
|
|
|
points="$1"; shift
|
|
|
|
author=$(echo $1 | escape); shift
|
2015-04-08 17:28:18 -06:00
|
|
|
|
2015-04-12 11:11:15 -06:00
|
|
|
cat <<EOF
|
2015-04-08 17:28:18 -06:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<title>$cat $points</title>
|
2015-04-12 11:33:53 -06:00
|
|
|
<link rel="stylesheet" href="/../../css/style.css" type="text/css">
|
2015-04-08 17:28:18 -06:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>$cat for $points points</h1>
|
|
|
|
EOF
|
|
|
|
|
2015-04-12 11:11:15 -06:00
|
|
|
echo "<section id=\"readme\">"
|
|
|
|
cat
|
|
|
|
echo "</section>"
|
|
|
|
|
|
|
|
if [ $# -gt 0 ]; then
|
|
|
|
echo "<section id=\"files\">"
|
|
|
|
echo "<h2>Associated files:</h2>"
|
|
|
|
echo "<ul>"
|
|
|
|
while [ $# -gt 0 ]; do
|
|
|
|
fn="$1"; shift
|
|
|
|
efn="$(echo $fn | escape)"
|
|
|
|
echo "<li><a href=\"$fn\">$efn</a></li>"
|
|
|
|
done
|
|
|
|
echo "</ul>"
|
|
|
|
echo "</section>"
|
|
|
|
fi
|
|
|
|
|
|
|
|
cat <<EOF
|
2015-04-08 17:28:18 -06:00
|
|
|
<section id="form">
|
2015-04-12 11:33:53 -06:00
|
|
|
<form id="puzzler" action="/puzzler.cgi" method="post" accept-charset="utf-8" autocomplete="off">
|
2015-04-08 17:28:18 -06:00
|
|
|
<input type="hidden" name="c" value="$cat">
|
|
|
|
<input type="hidden" name="p" value="$points">
|
|
|
|
Team hash:<input name="t" size="8">
|
|
|
|
Answer:<input name="a" size="20">
|
|
|
|
<input type="submit" value="submit">
|
|
|
|
</form>
|
|
|
|
</section>
|
|
|
|
<address>Puzzle by <span class="author" data-handle="$author">$author</span></address>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
cat=$(basename $indir)
|
2015-04-12 11:11:15 -06:00
|
|
|
outdir=$(dirname $0)/../packages/$cat
|
2015-04-08 17:28:18 -06:00
|
|
|
uanswers=$outdir/answers.unsorted
|
|
|
|
usummary=$outdir/summary.unsorted
|
|
|
|
umap=$outdir/map.unsorted
|
|
|
|
|
2015-04-12 11:11:15 -06:00
|
|
|
mkdir -p $outdir
|
2015-04-12 11:33:53 -06:00
|
|
|
if ! [ -r $outdir/salt ]; then
|
2015-04-12 11:11:15 -06:00
|
|
|
dd if=/dev/urandom bs=1 count=16 2>/dev/null | md5sum | cut -c1-16 > $outdir/salt
|
2015-04-08 17:28:18 -06:00
|
|
|
fi
|
|
|
|
read salt < $outdir/salt
|
|
|
|
|
|
|
|
> $uanswers
|
|
|
|
rm -f $usummary
|
|
|
|
|
|
|
|
for dn in $indir/[0-9]*; do
|
2015-04-12 11:11:15 -06:00
|
|
|
[ -d $dn ] || continue
|
|
|
|
points=$(basename $dn)
|
|
|
|
|
|
|
|
echo $dn
|
|
|
|
|
|
|
|
odn=$(printf "%s/%s/%s" "$salt" "$cat" "$points" | md5sum | sed 's/\(....\)/\1./g' | cut -b 1-19)
|
|
|
|
tgt=$outdir/puzzles/$odn
|
|
|
|
mkdir -p $tgt
|
|
|
|
#touch $tgt/index.html
|
|
|
|
|
|
|
|
if [ -f $dn/Makefile ]; then
|
|
|
|
# If there's a Makefile, run make
|
|
|
|
make DESTDIR=$(pwd)/$tgt -C $dn || exit 1
|
|
|
|
files=$(ls -1 $tgt | grep -v index.html || true)
|
|
|
|
elif [ -f $dn/00manifest.txt ]; then
|
|
|
|
# If there's a manifest, use that
|
|
|
|
files=
|
|
|
|
while read fn; do
|
2015-04-12 11:33:53 -06:00
|
|
|
cp $dn/$fn $tgt/
|
2015-04-12 11:11:15 -06:00
|
|
|
case $fn in
|
|
|
|
,*)
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
files="$files $fn"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done < $dn/00manifest.txt
|
|
|
|
else
|
|
|
|
# Otherwise, look for special files and copy the rest
|
|
|
|
files=
|
|
|
|
for fn in $dn/*; do
|
|
|
|
case $(basename $fn) in
|
|
|
|
00*)
|
|
|
|
# Handle meta-information later
|
|
|
|
;;
|
|
|
|
*~|"#"*)
|
|
|
|
# Don't copy temporary or backup files
|
|
|
|
;;
|
|
|
|
,*)
|
|
|
|
# Copy but don't list
|
2015-04-12 11:33:53 -06:00
|
|
|
cp $fn $tgt/
|
2015-04-12 11:11:15 -06:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
#ext=$(echo $fn | sed -ne 's/.*\././p')
|
|
|
|
cfn=$(md5sum $fn | cut -b -8)$ext
|
2015-04-12 11:33:53 -06:00
|
|
|
cp $fn $tgt/$cfn
|
2015-04-12 11:11:15 -06:00
|
|
|
files="$files $cfn"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Append answers
|
|
|
|
if [ -f $dn/00answer.txt ]; then
|
|
|
|
awk -v P=$points '/./ { printf("%d %s\n", P, $0); }' < $dn/00answer.txt >> $uanswers
|
|
|
|
else
|
|
|
|
die "$dn/00answer.txt: No such file or directory"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Append summary
|
|
|
|
if [ -f $dn/00summary.txt ]; then
|
|
|
|
awk -v P=$points '/./ { printf("%d %s\n", P, $0); }' < $dn/00summary.txt >> $usummary
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Read author
|
|
|
|
if [ -f $dn/00author.txt ]; then
|
|
|
|
author=$(cat $dn/00author.txt)
|
|
|
|
else
|
|
|
|
die "$dn/00author.txt does not exist."
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Generate index now that we have a list of files
|
|
|
|
if [ -f $dn/00index.mdwn ]; then
|
|
|
|
markdown --html4tags $dn/00index.mdwn
|
|
|
|
fi | template $cat $points "$author" $files > $tgt/index.html
|
|
|
|
|
|
|
|
# Write to map
|
|
|
|
printf "%d %s\n" $points $odn >> $umap
|
2015-04-08 17:28:18 -06:00
|
|
|
done
|
|
|
|
|
2015-04-12 11:11:15 -06:00
|
|
|
echo "Generating URL map"
|
2015-04-08 17:28:18 -06:00
|
|
|
sort -n $umap > $outdir/map.txt
|
2015-04-12 11:11:15 -06:00
|
|
|
|
|
|
|
echo "Generating answers list"
|
2015-04-08 17:28:18 -06:00
|
|
|
sort -n $uanswers > $outdir/answers.txt
|
2015-04-12 11:11:15 -06:00
|
|
|
|
|
|
|
echo "Generating summary"
|
2015-04-08 17:28:18 -06:00
|
|
|
[ -f $usummary ] && sort -ns $usummary > $outdir/summary.txt
|
2015-04-12 11:11:15 -06:00
|
|
|
|
|
|
|
echo "Linking into web space"
|
2015-04-13 16:38:56 -06:00
|
|
|
ln -sf ../packages/$cat/puzzles $outdir/../../www/$cat
|
2015-04-12 11:11:15 -06:00
|
|
|
|
|
|
|
echo "Cleaning up"
|
2015-04-08 17:28:18 -06:00
|
|
|
rm -f $uanswers $usummary $umap
|