1#!/usr/bin/env nix-shell
2#!nix-shell -i bash -p gitMinimal curl gojq gclient2nix
3
4set -eou pipefail
5package="livekit-libwebrtc"
6pkg_dir="$(dirname "$0")"
7nixpkgs="$(git rev-parse --show-toplevel)"
8
9gh-curl () {
10 curl --silent ${GITHUB_TOKEN:+-u ":$GITHUB_TOKEN"} "$1"
11}
12
13# Get the current version part before the "-unstable-" for the branch name.
14# To manually update to a new major version, you can also invoke the script
15# with the new major version, e.g., UPDATE_MAJOR_VERSION=137.
16old_version="${UPDATE_NIX_OLD_VERSION:-$(nix-instantiate --eval -E "(import \"$nixpkgs\" { }).$package.version" | tr -d '"')}"
17major_version="${UPDATE_MAJOR_VERSION:-${old_version%%-unstable-*}}"
18branch="m${major_version}_release"
19
20# Fetch the current HEAD commit of the release branch
21head="$(gh-curl "https://api.github.com/repos/webrtc-sdk/webrtc/git/refs/heads/$branch" | gojq '.object.sha' --raw-output)"
22if gojq -e ".src.args.rev == \"$head\"" "$pkg_dir/sources.json"; then
23 echo "$package is already up-to-date: $head"
24 exit 0
25fi
26
27# Get the commit's date for the version field
28date="$(gh-curl "https://api.github.com/repos/webrtc-sdk/webrtc/git/commits/$head" | gojq '.committer.date| split("T") | .[0]' --raw-output)"
29
30echo "Updating sources.json to $head"
31gclient2nix generate --root src "https://github.com/webrtc-sdk/webrtc@$head" > "$pkg_dir/sources.json"
32
33sed -i "s|$old_version|$major_version-unstable-$date|g" "$pkg_dir/package.nix"