aboutsummaryrefslogtreecommitdiff
path: root/utils/buildsite.sh
blob: 7297dbb254891aa4f89586a9483ad785068b84ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/bin/sh -e
#
# Build the Alice Linux static website into public/.
# Run from the project root.
#
# Dependencies: cmark, curl

cd "$(dirname "$0")/.."

for dep in cmark curl; do
	command -v "$dep" >/dev/null 2>&1 || {
		printf 'Error: "%s" is required but not found in PATH.\n' "$dep" >&2
		exit 1
	}
done

rm -rf public
mkdir -p public

html_escape() {
	# takes a single string argument, prints escaped to stdout
	printf '%s' "$1" | sed 's/\&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g;s/"/\&quot;/g'
}

port_version() {
	grep '^version=' "$1/abuild" | cut -d = -f2- || true
}

port_release() {
	grep '^release=' "$1/abuild" | cut -d = -f2- || true
}

port_depends() {
	[ -f "$1/depends" ] || return 0
	grep -Ev '^(#|$)' "$1/depends" | tr '\n' ' ' | sed 's/[[:space:]]*$//' || true
}

generate_ports_page() {
	{
		sed "s/@TITLE@/ports/g" files/header
		echo "<p>Package ports generated from <code>repos/core</code>, <code>repos/extra</code>, and <code>repos/community</code>.</p>"
		echo "<div class=\"ports-toolbar\">"
		echo "<button type=\"button\" class=\"active\" data-repo=\"all\">all</button>"
		echo "<button type=\"button\" data-repo=\"core\">core</button>"
		echo "<button type=\"button\" data-repo=\"extra\">extra</button>"
		echo "<button type=\"button\" data-repo=\"community\">community</button>"
		echo "<input id=\"ports-search\" type=\"search\" placeholder=\"filter ports\" autocomplete=\"off\">"
		echo "</div>"
		echo "<div class=\"ports-count\"><span id=\"ports-visible\">0</span> / <span id=\"ports-total\">0</span> ports</div>"
		echo "<table id=\"ports-table\">"
		echo "<thead><tr><th>repo</th><th>name</th><th>version</th><th>dependencies</th></tr></thead>"
		echo "<tbody>"

		for repo in core extra community; do
			for port in repos/$repo/*; do
				[ -f "$port/abuild" ] || continue
				name=${port##*/}
				version=$(port_version "$port")
				release=$(port_release "$port")
				depends=$(port_depends "$port")
				[ "$release" ] && version=$version-$release

				e_repo=$(html_escape "$repo")
				e_name=$(html_escape "$name")
				e_version=$(html_escape "$version")
				e_depends=$(html_escape "$depends")

				printf '<tr data-repo="%s"><td>%s</td><td><a href="https://codeberg.org/emmett1/alicelinux/src/branch/main/repos/%s/%s">%s</a></td><td>%s</td><td>%s</td></tr>\n' \
					"$e_repo" \
					"$e_repo" \
					"$e_repo" \
					"$e_name" \
					"$e_name" \
					"$e_version" \
					"$e_depends"
			done
		done

		echo "</tbody>"
		echo "</table>"
		cat << 'EOF'
<script>
(function () {
	var table = document.getElementById('ports-table');
	var rows = Array.prototype.slice.call(table.querySelectorAll('tbody tr'));
	var search = document.getElementById('ports-search');
	var visible = document.getElementById('ports-visible');
	var total = document.getElementById('ports-total');
	var buttons = Array.prototype.slice.call(document.querySelectorAll('.ports-toolbar button'));
	var repo = 'all';

	total.textContent = rows.length;

	function filterPorts() {
		var query = search.value.trim().toLowerCase();
		var count = 0;

		rows.forEach(function (row) {
			var repoMatch = repo === 'all' || row.dataset.repo === repo;
			var textMatch = !query || row.textContent.toLowerCase().indexOf(query) !== -1;
			var show = repoMatch && textMatch;
			row.style.display = show ? '' : 'none';
			if (show) count++;
		});

		visible.textContent = count;
	}

	buttons.forEach(function (button) {
		button.addEventListener('click', function () {
			repo = button.dataset.repo;
			buttons.forEach(function (b) { b.classList.remove('active'); });
			button.classList.add('active');
			filterPorts();
		});
	});

	search.addEventListener('input', filterPorts);
	filterPorts();
}());
</script>
EOF
		cat files/footer
	} > public/ports.html
}

# copy static .html files (from repo root, readme, etc.) into public/
find . -type f -name "*.html" ! -path './public/*' | sed 's|^\./||' | while IFS= read -r i; do
	dir=${i%/*}
	file=${i##*/}
	title=${dir##*/}
	[ "$dir" = "$file" ] && {
		title=home; dir=
	}
	mkdir -p "public/$dir"
	printf 'copy html for %s...\n' "$i"
	{
		sed "s/@TITLE@/$title/g" files/header
		cat "$i"
		cat files/footer
	} > "public/$dir/$file"
done

generate_ports_page

generate_commits_page() {
	{
		sed "s/@TITLE@/commits/g" files/header
		cat << 'EOF'
<p>Recent commits from <a href="https://codeberg.org/emmett1/alicelinux">codeberg.org/emmett1/alicelinux</a>.</p>
<ul id="commits"></ul>
<div id="commits-loading">loading…</div>
<div id="commits-error"></div>
<script>
(function () {
	var list = document.getElementById('commits');
	var loading = document.getElementById('commits-loading');
	var errEl = document.getElementById('commits-error');
	var page = 1;
	var loadingMore = false;
	var hasMore = true;
	var PER_PAGE = 30;

	function fmtHash(hash) {
		return hash.substring(0, 7);
	}

	function fmtDate(iso) {
		var d = new Date(iso);
		var y = d.getFullYear();
		var m = String(d.getMonth() + 1).padStart(2, '0');
		var day = String(d.getDate()).padStart(2, '0');
		var h = String(d.getHours()).padStart(2, '0');
		var min = String(d.getMinutes()).padStart(2, '0');
		return y + '-' + m + '-' + day + ' ' + h + ':' + min;
	}

	function esc(s) {
		var d = document.createElement('div');
		d.textContent = s;
		return d.innerHTML;
	}

	function loadCommits() {
		if (!hasMore || loadingMore) return;
		loadingMore = true;
		loading.style.display = 'block';

		var url = 'https://codeberg.org/api/v1/repos/emmett1/alicelinux/commits?limit=' + PER_PAGE + '&page=' + page;

		fetch(url)
			.then(function (r) {
				if (!r.ok) throw new Error('HTTP ' + r.status);
				var link = r.headers.get('X-HasMore');
				if (link === 'false' || link === null) hasMore = false;
				return r.json();
			})
			.then(function (data) {
				if (!data.length || data.length < PER_PAGE) hasMore = false;

				data.forEach(function (c) {
					var li = document.createElement('li');
					var hash = c.sha || '';
					var msg = c.commit.message.split('\n')[0] || '';
					var author = c.commit.author.name || 'unknown';
					var date = c.commit.author.date || '';
					var url = c.html_url || '#';

					li.innerHTML =
						'<span class="commit-hash"><a href="' + esc(url) + '">' + esc(fmtHash(hash)) + '</a></span>' +
						'<span class="commit-msg"><a href="' + esc(url) + '">' + esc(msg) + '</a></span>' +
						'<span class="commit-meta">' + esc(author) + '<br>' + esc(fmtDate(date)) + '</span>';
					list.appendChild(li);
				});

				page++;
				loadingMore = false;
				loading.style.display = 'none';
			})
			.catch(function (err) {
				loadingMore = false;
				loading.style.display = 'none';
				hasMore = false;
				errEl.textContent = 'Failed to load commits: ' + err.message;
				errEl.style.display = 'block';
			});
	}

	window.addEventListener('scroll', function () {
		if (loadingMore || !hasMore) return;
		var scrollBottom = window.scrollY + window.innerHeight;
		var threshold = document.body.scrollHeight - 600;
		if (scrollBottom >= threshold) loadCommits();
	});

	loadCommits();
}());
</script>
EOF
		cat files/footer
	} > public/commits.html
}

generate_commits_page

generate_download_page() {
	{
		sed "s/@TITLE@/download/g" files/header

		listing=$(curl -sL --max-time 10 https://dl.alicelinux.org/ 2>/dev/null || true)

		cat << 'EOF'
<h1>Download</h1>
<p>Alice Linux installation images and rootfs tarballs. See the <a href="/docs/install.html">installation guide</a> for setup instructions.</p>
<div id="dl-status" class="dl-empty">loading…</div>
<table class="dl-table" id="dl-table"><tbody>
EOF

		if [ -n "$listing" ]; then
			echo "$listing" | sed -n '/<tbody>/,/<\/tbody>/p' | while IFS= read -r row; do
				case $row in
					*'<tr>'*)
						href=$(printf '%s' "$row" | sed 's/.*<a href="\([^"]*\)">.*/\1/')
						name=$(printf '%s' "$row" | sed 's/.*<a href="[^"]*">\([^<]*\)<.*/\1/')
						size=$(printf '%s' "$row" | sed 's/.*<td class="s"[^>]*>\([^<]*\)<.*/\1/')
						date=$(printf '%s' "$row" | sed 's/.*<td class="m">\([^<]*\)<.*/\1/')
						type=$(printf '%s' "$row" | sed 's/.*<td class="t">\([^<]*\)<.*/\1/')
						[ "$name" = "../" ] && continue
						url="https://dl.alicelinux.org/$href"
						e_url=$(html_escape "$url")
						e_name=$(html_escape "$name")
						e_size=$(html_escape "$size")
						e_date=$(html_escape "$date")
						if [ "$type" = "Directory" ]; then
							printf '<tr><td><a href="%s">%s/</a></td><td>%s</td><td>%s</td></tr>\n' \
								"$e_url" "$e_name" "$e_size" "$e_date"
						else
							printf '<tr><td><a href="%s">%s</a></td><td>%s</td><td>%s</td></tr>\n' \
								"$e_url" "$e_name" "$e_size" "$e_date"
						fi
						;;
				esac
			done
		fi

		cat << 'EOF'
</tbody></table>
<script>
(function () {
	var table = document.getElementById('dl-table');
	var tbody = table.querySelector('tbody');
	var status = document.getElementById('dl-status');

	function esc(s) {
		var d = document.createElement('div');
		d.textContent = s;
		return d.innerHTML;
	}

	function rowHTML(href, name, size, date, isDir) {
		var url = 'https://dl.alicelinux.org/' + href;
		var label = esc(name) + (isDir ? '/' : '');
		return '<tr><td><a href="' + esc(url) + '">' + label + '</a></td><td>' + esc(size) + '</td><td>' + esc(date) + '</td></tr>';
	}

	fetch('https://dl.alicelinux.org/')
		.then(function (r) {
			if (!r.ok) throw new Error('HTTP ' + r.status);
			return r.text();
		})
		.then(function (html) {
			var parser = new DOMParser();
			var doc = parser.parseFromString(html, 'text/html');
			var rows = doc.querySelectorAll('table tbody tr');
			var frag = document.createDocumentFragment();

			rows.forEach(function (row) {
				var cells = row.querySelectorAll('td');
				if (!cells.length) return;
				var link = cells[0].querySelector('a');
				if (!link) return;
				var href = link.getAttribute('href');
				var name = link.textContent;
				if (name === '../') return;
				var size = cells[2] ? cells[2].textContent.trim() : '';
				var date = cells[1] ? cells[1].textContent.trim() : '';
				var isDir = cells[3] && cells[3].textContent.trim() === 'Directory';
				var tr = document.createElement('tr');
				tr.innerHTML = rowHTML(href, name, size, date, isDir);
				frag.appendChild(tr);
			});

			if (frag.childNodes.length) {
				tbody.innerHTML = '';
				tbody.appendChild(frag);
			}
			status.textContent = '';
			status.style.display = 'none';
		})
		.catch(function () {
			var rows = tbody.querySelectorAll('tr');
			if (rows.length) {
				status.textContent = '';
				status.style.display = 'none';
			} else {
				status.innerHTML = 'Unable to load. Visit <a href="https://dl.alicelinux.org">dl.alicelinux.org</a> directly.';
			}
		});
}());
</script>
EOF
		cat files/footer
	} > public/download.html
}

generate_download_page

# build docs index
cat docs/readme.md > docs/index.md
for f in docs/*.md; do
	case $f in */readme.md|*/index.md) continue;; esac
	title=$(head -n1 "$f" | sed "s/^#* *//")
	file=${f##*/}
	file_no_num=$(printf '%s' "$file" | sed 's/^[0-9][0-9]-//')
	echo "- [$title](./${file_no_num%.md}.html)" >> docs/index.md
done
cat >> docs/index.md << 'EOF'

Documentation improvements are welcome. Visit the [docs directory](https://codeberg.org/emmett1/alicelinux/src/branch/main/docs) to contribute.
EOF

# convert markdown to html
doc_files=$(ls docs/*.md 2>/dev/null | sort | grep -v /readme.md | grep -v /index.md || true)
find . -type f -name "*.md" ! -path './public/*' | sed 's|^\./||' | while IFS= read -r i; do
	dir=${i%/*}
	file=${i##*/}
	outfile=$(printf '%s' "$file" | sed 's/^[0-9][0-9]-//')
	title=${dir##*/}
	[ "$dir" = "$file" ] && {
		title=home; dir=
	}
	mkdir -p "public/$dir"
	printf 'generating html for %s...\n' "$i"
	{
		sed "s/@TITLE@/$title/g" files/header
		case $i in docs/readme.md|docs/index.md) ;; *) case $i in docs/*) printf '<a href="/docs/">&lt;- back to docs</a>\n\n';; esac; esac
		cmark "$i"
		case $i in docs/readme.md|docs/index.md) ;; *)
			case $i in docs/*)
				next=$(printf '%s\n' "$doc_files" | grep -FA1 "$i" | tail -n1)
				if [ "$next" ] && [ "$next" != "$i" ]; then
					next_title=$(head -n1 "$next" | sed 's/^#* *//')
					next_html=$(printf '%s' "${next##*/}" | sed 's/^[0-9][0-9]-//')
					printf '\n<div style="display:flex;justify-content:space-between;margin-top:1.5em"><a href="/docs/">&lt;- back to docs</a><a href="/docs/%s">%s -&gt;</a></div>\n' "${next_html%.md}.html" "$next_title"
				else
					printf '\n<a href="/docs/">&lt;- back to docs</a>\n'
				fi
			;;
			esac
		;;
		esac
		cat files/footer
	} > "public/$dir/${outfile%.md}.html"
done

# move readme.html to index.html
find public -type f -name "readme.html" | while IFS= read -r i; do
	mv -n "$i" "${i%/*}/index.html"
done

rm -f docs/index.md

if [ -d files ]; then
	cp -ra files public/
fi

echo alicelinux.org > public/.domains
echo alicelinux.emmett1.my >> public/.domains

exit 0