1#!/usr/bin/env nix-shell
2#!nix-shell -I nixpkgs=./. -i bash -p bash curl nix-prefetch jq
3# shellcheck shell=bash
4
5set -euo pipefail
6
7dir="$(dirname "$0")"
8url="https://smlnj.cs.uchicago.edu/dist/working/"
9hashfile="$dir/hashes.json"
10nixfile="$dir/default.nix"
11
12version="$(curl --silent "$url" \
13 | sed -n 's:.*<b>\([0-9]\{3\}\.[0-9.-]\+\)</b>.*:\1:p' \
14 | head -n1)"
15
16echo "Latest SML/NJ release: $version"
17
18if [[ -e "$hashfile" ]]; then
19 old_version="$(jq -r .version "$hashfile")"
20 if [[ $old_version = "$version" ]]; then
21 echo "Package is already up-to-date, skipping"
22 exit 0
23 fi
24 echo "Upgrading from $old_version to $version"
25else
26 echo "Generating hashes for $version"
27fi
28
29files=(
30 boot.amd64-unix.tgz boot.x86-unix.tgz
31 config.tgz cm.tgz compiler.tgz runtime.tgz system.tgz MLRISC.tgz
32 smlnj-lib.tgz old-basis.tgz ckit.tgz nlffi.tgz cml.tgz eXene.tgz ml-lpt.tgz
33 ml-lex.tgz ml-yacc.tgz ml-burg.tgz pgraph.tgz trace-debug-profile.tgz
34 heap2asm.tgz smlnj-c.tgz doc.tgz asdl.tgz
35)
36
37tmpdir="$(mktemp --directory)"
38trap 'rm -rf -- "$tmpdir"' EXIT
39
40declare -a pids=()
41
42for file in "${files[@]}"; do
43 nix-prefetch --silent fetchurl --url "$url/$version/$file" > "$tmpdir/$file" &
44 pids+=($!)
45done
46
47for pid in "${pids[@]}"; do
48 wait "$pid"
49done
50
51printf '{\n' > "$hashfile"
52for file in "${files[@]}"; do
53 printf ' "%s": "%s",\n' "$file" "$(cat "$tmpdir/$file")" >> "$hashfile"
54done
55printf ' "version": "%s"\n' "$version" >> "$hashfile"
56printf '}\n' >> "$hashfile"
57
58sed --in-place 's:version = "[0-9.]\+";:version = "'"$version"'";:' "$nixfile"
59
60echo "Done"