Neale Pickett
·
2024-08-19
scripting.md
1---
2title: Scripting
3---
4
5A puzzle-generating script must write a puzzle to stdout,
6as described in [Puzzle Format](puzzle.md).
7
8Scripts can also generate attachments by writing the attachment to the current working directory.
9
10Naming Your Script
11==========
12
13Your script should be named `mkpuzzle.*`,
14with an extension corresponding to the language you're using.
15
16Currently (August 2024),
17Windows supports:
18
19* `mkpuzzle.py` (Python 3)
20* `mkpuzzle.sh` (Bash)
21
22
23Random Seed
24===========
25
26The MOTH convention is to provide a random seed
27in the environment variable `SEED`.
28Your program should be prepared for `SEED` to be any string,
29not necessarily an integer.
30
31
32Working With Other Files
33================
34
35When a script is run,
36the working directory will be set to wherever files should be written.
37
38Attachments
39----------
40
41Attachments can be created by simply writing to the attachment filename:
42
43```sh
44echo foo > attachment.txt
45```
46
47Source Files
48---------
49
50Most source files should be relative to the directory of the script.
51
52Most scripting languages are able to determine
53the directory containing the script being executed.
54Here are two examples:
55
56### Bourne Shell
57```sh
58srcdir=$(dirname $0)
59read answer < $srcdir/answer.txt
60```
61
62### Python
63```python
64import os
65import sys
66
67srcdir = os.path.dirname(sys.argv[0])
68answer = open(os.path.join(srcdir, "answer.txt")).read()
69```
70
71### Environment Variable
72
73As a fallback option,
74the environment variable `srcdir`
75contains the absolute path to the script directory.
76
77
78Example (Bourne Shell)
79==========
80
81```sh
82#!/bin/sh
83
84srcdir=$(dirname $0)
85read answer < $srcdir/answer.txt
86encoded=$(echo $answer | tr 'a-z' 'n-za-m')
87
88echo $encoded > encoded.txt
89
90cat <<EOF
91---
92title: Example Scripted Puzzle
93answers:
94 - $answer
95attachments:
96 - encoded.txt
97---
98
99rot-13 decode this:
100
101 $encoded
102EOF
103```
104