1#!/usr/bin/env nix-shell
2#!nix-shell -i bash -p curl gawk gnused pup jq
3
4set -euo pipefail
5
6DIRNAME=$(dirname "$0")
7readonly DIRNAME
8readonly NIXPKGS_ROOT="../../../.."
9readonly NIX_FLAGS=(--extra-experimental-features 'nix-command flakes')
10
11# awk is used for parsing the RARLAB website to get the newest version
12readonly AWK_FIELD_SEPARATOR='[-.]'
13# shellcheck disable=SC2016
14readonly AWK_COMMAND='
15# We will get the following output from pup:
16# /rar/rarlinux-x64-700b3.tar.gz
17# /rar/rarmacos-x64-700b3.tar.gz
18# /rar/rarlinux-x64-624.tar.gz
19# /rar/rarbsd-x64-624.tar.gz
20# /rar/rarmacos-x64-624.tar.gz
21
22# Ignore anything that is flagged as beta (e.g.: `/rar/rarlinux-x64-700b3.tar.gz`)
23!/[0-9]+b[0-9]*.tar.gz$/ {
24 # /rar/rarlinux-x64-624.tar.gz -> 624
25 val = $3
26 # Only get the value if it is bigger than the current one
27 if (val > max) max = val
28}
29END {
30 # 624 -> 6.24
31 printf "%.2f\n", max/100
32}
33'
34
35updateHash() {
36 local -r version="${1//./}"
37 local -r arch="$2"
38 local -r os="$3"
39 local -r nix_arch="$4"
40
41 url="https://www.rarlab.com/rar/rar$os-$arch-$version.tar.gz"
42 hash=$(nix store prefetch-file --json "$url" | jq -r .hash)
43 currentHash=$(cd "$DIRNAME" && nix "${NIX_FLAGS[@]}" eval --raw "$NIXPKGS_ROOT#legacyPackages.$nix_arch.rar.src.outputHash")
44
45 sed -i "s|$currentHash|$hash|g" "$DIRNAME/default.nix"
46}
47
48updateVersion() {
49 local -r version="$1"
50 sed -i "s|version = \"[0-9.]*\";|version = \"$version\";|g" "$DIRNAME/default.nix"
51}
52
53latestVersion="${1:-}"
54if [[ -z "$latestVersion" ]]; then
55 latestVersion=$(
56 curl --silent --location --fail https://www.rarlab.com/download.htm | \
57 pup 'a[href*=".tar.gz"] attr{href}' | \
58 awk -F"$AWK_FIELD_SEPARATOR" "$AWK_COMMAND"
59 )
60fi
61readonly latestVersion
62echo "Latest version: $latestVersion"
63
64currentVersion=$(cd "$DIRNAME" && nix "${NIX_FLAGS[@]}" eval --raw "$NIXPKGS_ROOT#legacyPackages.x86_64-linux.rar.version")
65readonly currentVersion
66echo "Current version: $currentVersion"
67
68if [[ "$currentVersion" == "$latestVersion" ]]; then
69 echo "rar is up-to-date"
70 exit 0
71fi
72
73updateHash "$latestVersion" x32 linux i686-linux
74updateHash "$latestVersion" x64 linux x86_64-linux
75updateHash "$latestVersion" arm macos aarch64-darwin
76updateHash "$latestVersion" x64 macos x86_64-darwin
77
78updateVersion "$latestVersion"