···11+#!/usr/bin/env nix-shell
22+#! nix-shell -i bash -p curl jq git gnused gnugrep
33+44+55+# executing this script without arguments will
66+# - find the newest stable plex-htpc version avaiable on snapcraft (https://snapcraft.io/plex-htpc)
77+# - read the current plex-htpc version from the current nix expression
88+# - update the nix expression if the versions differ
99+# - try to build the updated version, exit if that fails
1010+# - give instructions for upstreaming
1111+1212+# As an optional argument you can specify the snapcraft channel to update to.
1313+# Default is `stable` and only stable updates should be pushed to nixpkgs. For
1414+# testing you may specify `candidate` or `edge`.
1515+1616+1717+channel="${1:-stable}" # stable/candidate/edge
1818+nixpkgs="$(git rev-parse --show-toplevel)"
1919+plex_nix="$nixpkgs/pkgs/by-name/pl/plex-htpc/package.nix"
2020+2121+2222+#
2323+# find the newest stable plex-htpc version avaiable on snapcraft
2424+#
2525+2626+# create bash array from snap info
2727+snap_info=($(
2828+ curl -s -H 'X-Ubuntu-Series: 16' \
2929+ "https://api.snapcraft.io/api/v1/snaps/details/plex-htpc?channel=$channel" \
3030+ | jq --raw-output \
3131+ '.revision,.download_sha512,.version,.last_updated'
3232+))
3333+3434+# "revision" is the actual version identifier on snapcraft, the "version" is
3535+# just for human consumption. Revision is just an integer that gets increased
3636+# by one every (stable or unstable) release.
3737+revision="${snap_info[0]}"
3838+# We need to escape the slashes
3939+hash="$(nix-hash --to-sri --type sha512 ${snap_info[1]} | sed 's|/|\\/|g')"
4040+upstream_version="${snap_info[2]}"
4141+last_updated="${snap_info[3]}"
4242+echo "Latest $channel release is $upstream_version from $last_updated."
4343+#
4444+# read the current plex-htpc version from the currently *committed* nix expression
4545+#
4646+4747+current_version=$(
4848+ grep 'version\s*=' "$plex_nix" \
4949+ | sed -Ene 's/.*"(.*)".*/\1/p'
5050+)
5151+5252+echo "Current version: $current_version"
5353+5454+#
5555+# update the nix expression if the versions differ
5656+#
5757+5858+if [[ "$current_version" == "$upstream_version" ]]; then
5959+ echo "Plex is already up-to-date"
6060+ exit 0
6161+fi
6262+6363+echo "Updating from ${current_version} to ${upstream_version}, released on ${last_updated}"
6464+6565+# search-and-replace revision, hash and version
6666+sed --regexp-extended \
6767+ -e 's/rev\s*=\s*"[0-9]+"\s*;/rev = "'"${revision}"'";/' \
6868+ -e 's/hash\s*=\s*"[^"]*"\s*;/hash = "'"${hash}"'";/' \
6969+ -e 's/version\s*=\s*".*"\s*;/version = "'"${upstream_version}"'";/' \
7070+ -i "$plex_nix"
7171+