← All repositories

crossmate

A collaborative crossword app for iPhone and iPad

git clone https://code.inqk.net/crossmate.git


Crossmake/Scripts/select_puzzles.sh (3914B)

1#!/usr/bin/env bash
2set -euo pipefail
3
4COUNT="${1:-10}"
5DEFAULT_CANDIDATE_DIR="Generated"
6PICKMAKE_EXECUTABLE=".build/release/Pickmake"
7CLUEMAKE_EXECUTABLE=".build/release/Cluemake"
8SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9CROSSMAKE_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
10CALLER_DIR="$(pwd)"
11OUTPUT_DIR="${CROSSMAKE_DIR}/Picked"
12CLEAN=false
13CLUE_AGENT="${CROSSMAKE_CLUE_AGENT:-codex}"
14CLUE_TIMEOUT="${CROSSMAKE_CLUE_TIMEOUT:-300}"
15CLUE_ATTEMPTS="${CROSSMAKE_CLUE_ATTEMPTS:-3}"
16CLUE_PUZZLES=true
17
18if ! [[ "$COUNT" =~ ^[0-9]+$ ]] || ((10#$COUNT < 1)); then
19  echo "Usage: $0 [positive-puzzle-count] [-o output-dir] [--clean] [--clue-agent codex|claude] [--clue-timeout seconds] [--skip-clues] [candidate-dir-or-xd-file ...]" >&2
20  exit 1
21fi
22
23resolve_path() {
24  if [[ "$1" = /* ]]; then
25    echo "$1"
26  else
27    echo "${CALLER_DIR}/$1"
28  fi
29}
30
31shift || true
32inputs=()
33while (($# > 0)); do
34  case "$1" in
35    -o|--into)
36      if (($# < 2)); then
37        echo "$1 requires a directory argument" >&2
38        exit 1
39      fi
40      OUTPUT_DIR="$(resolve_path "$2")"
41      shift 2
42      ;;
43    --clean)
44      CLEAN=true
45      shift
46      ;;
47    --clue-agent)
48      if (($# < 2)); then
49        echo "$1 requires codex or claude" >&2
50        exit 1
51      fi
52      CLUE_AGENT="$2"
53      shift 2
54      ;;
55    --clue-timeout)
56      if (($# < 2)); then
57        echo "$1 requires a timeout in seconds" >&2
58        exit 1
59      fi
60      CLUE_TIMEOUT="$2"
61      shift 2
62      ;;
63    --skip-clues)
64      CLUE_PUZZLES=false
65      shift
66      ;;
67    *)
68      inputs+=("$(resolve_path "$1")")
69      shift
70      ;;
71  esac
72done
73
74if [[ "$CLUE_AGENT" != "codex" && "$CLUE_AGENT" != "claude" ]]; then
75  echo "Clue agent must be codex or claude" >&2
76  exit 1
77fi
78
79if ! [[ "$CLUE_TIMEOUT" =~ ^[0-9]+([.][0-9]+)?$ ]]; then
80  echo "Clue timeout must be a positive number" >&2
81  exit 1
82fi
83
84if ! [[ "$CLUE_ATTEMPTS" =~ ^[0-9]+$ ]] || ((10#$CLUE_ATTEMPTS < 1)); then
85  echo "CROSSMAKE_CLUE_ATTEMPTS must be a positive integer" >&2
86  exit 1
87fi
88
89if ((${#inputs[@]} == 0)); then
90  inputs+=("${CROSSMAKE_DIR}/${DEFAULT_CANDIDATE_DIR}")
91fi
92
93cd "$CROSSMAKE_DIR"
94
95echo "Building Pickmake and Cluemake in release mode" >&2
96swift build -c release --product Pickmake --product Cluemake
97
98if [[ ! -x "$PICKMAKE_EXECUTABLE" ]]; then
99  echo "Pickmake executable not found after build: $PICKMAKE_EXECUTABLE" >&2
100  exit 1
101fi
102
103if [[ "$CLUE_PUZZLES" == true && ! -x "$CLUEMAKE_EXECUTABLE" ]]; then
104  echo "Cluemake executable not found after build: $CLUEMAKE_EXECUTABLE" >&2
105  exit 1
106fi
107
108selected_paths="$("$PICKMAKE_EXECUTABLE" --count "$COUNT" --verbose "${inputs[@]}")"
109
110if [[ -z "$selected_paths" ]]; then
111  echo "No puzzles selected; nothing copied." >&2
112  exit 1
113fi
114
115mkdir -p "$OUTPUT_DIR"
116
117if [[ "$CLEAN" == true ]]; then
118  rm -f "$OUTPUT_DIR"/*.xd
119  echo "Cleaned existing .xd files from ${OUTPUT_DIR}." >&2
120fi
121
122copied_count=0
123while IFS= read -r selected_path; do
124  [[ -n "$selected_path" ]] || continue
125  output_path="${OUTPUT_DIR}/${selected_path##*/}"
126  cp "$selected_path" "$output_path"
127  echo "Copied ${selected_path} -> ${output_path}"
128  if [[ "$CLUE_PUZZLES" == true ]]; then
129    echo "Generating clues for ${output_path} with ${CLUE_AGENT}" >&2
130    clue_attempt=1
131    until "$CLUEMAKE_EXECUTABLE" --agent "$CLUE_AGENT" --timeout "$CLUE_TIMEOUT" "$output_path"; do
132      if ((clue_attempt >= 10#$CLUE_ATTEMPTS)); then
133        echo "Failed to generate clues for ${output_path} after ${CLUE_ATTEMPTS} attempt(s)." >&2
134        exit 1
135      fi
136      clue_attempt=$((clue_attempt + 1))
137      echo "Retrying clues for ${output_path} (${clue_attempt}/${CLUE_ATTEMPTS})" >&2
138    done
139  fi
140  copied_count=$((copied_count + 1))
141done <<<"$selected_paths"
142
143if [[ "$CLUE_PUZZLES" == true ]]; then
144  echo "Copied and clued ${copied_count} puzzle(s) into ${OUTPUT_DIR}." >&2
145else
146  echo "Copied ${copied_count} puzzle(s) into ${OUTPUT_DIR}." >&2
147fi