about summary refs log tree commit diff
path: root/nixpkgs/pkgs/tools/security/oath-toolkit/update.sh
blob: 3502a541fa8cac12b013d52dd38b88e50699a27b (plain) (blame)
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
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p curl git gnugrep nix

set -euo pipefail

nixfile='default.nix'
release_url='https://download.savannah.nongnu.org/releases/oath-toolkit/'
attr='oathToolkit'
command='oathtool --version'

color() {
    printf '%s: \033[%sm%s\033[39m\n' "$0" "$1" "$2" >&2 || true
}

color 32 "downloading $release_url..."
if ! release_page=$(curl -Lf "$release_url"); then
    color 31 "cannot download release page"
    exit 1
fi

tarball_name=$(printf '%s\n' "$release_page" \
    | grep -Po '(?<=href=").*?\.tar\.gz(?=")' \
    | sort -n | tail -n1)
tarball_version="${tarball_name%.tar.*}"
tarball_version="${tarball_version##*-}"
tarball_url="mirror://savannah${release_url#https://*/releases}$tarball_name"

color 32 "nix-prefetch-url $tarball_url..."
if ! tarball_sha256=$(nix-prefetch-url --type sha256 "$tarball_url"); then
    color 31 "cannot prefetch $tarball_url"
    exit 1
fi

old_version=$(grep -Pom1 '(?<=version = ").*?(?=";)' "$nixfile")

version=$(printf 'version = "%s";\n' "$tarball_version")
sha256=$(printf 'sha256 = "%s";\n' "$tarball_sha256")
sed -e "s,version = .*,$version," -e "s,sha256 = .*,$sha256," -i "$nixfile"

if git diff --exit-code "$nixfile" > /dev/stderr; then
    printf '\n' >&2 || true
    color 32 "$tarball_version is up to date"
else
    color 32 "running '$command' with nix-shell..."
    nix-shell -p "callPackage ./$nixfile {}" --run "$command"
    msg="$attr: $old_version -> $tarball_version"
    printf '\n' >&2 || true
    color 31 "$msg"
    git commit -m "$msg" "$nixfile"
fi