···1+#!/usr/bin/env nix-shell
2+#! nix-shell -i bash -p curl jq git gnused gnugrep
3+4+5+# executing this script without arguments will
6+# - find the newest stable plex-htpc version avaiable on snapcraft (https://snapcraft.io/plex-htpc)
7+# - read the current plex-htpc version from the current nix expression
8+# - update the nix expression if the versions differ
9+# - try to build the updated version, exit if that fails
10+# - give instructions for upstreaming
11+12+# As an optional argument you can specify the snapcraft channel to update to.
13+# Default is `stable` and only stable updates should be pushed to nixpkgs. For
14+# testing you may specify `candidate` or `edge`.
15+16+17+channel="${1:-stable}" # stable/candidate/edge
18+nixpkgs="$(git rev-parse --show-toplevel)"
19+plex_nix="$nixpkgs/pkgs/by-name/pl/plex-htpc/package.nix"
20+21+22+#
23+# find the newest stable plex-htpc version avaiable on snapcraft
24+#
25+26+# create bash array from snap info
27+snap_info=($(
28+ curl -s -H 'X-Ubuntu-Series: 16' \
29+ "https://api.snapcraft.io/api/v1/snaps/details/plex-htpc?channel=$channel" \
30+ | jq --raw-output \
31+ '.revision,.download_sha512,.version,.last_updated'
32+))
33+34+# "revision" is the actual version identifier on snapcraft, the "version" is
35+# just for human consumption. Revision is just an integer that gets increased
36+# by one every (stable or unstable) release.
37+revision="${snap_info[0]}"
38+# We need to escape the slashes
39+hash="$(nix-hash --to-sri --type sha512 ${snap_info[1]} | sed 's|/|\\/|g')"
40+upstream_version="${snap_info[2]}"
41+last_updated="${snap_info[3]}"
42+echo "Latest $channel release is $upstream_version from $last_updated."
43+#
44+# read the current plex-htpc version from the currently *committed* nix expression
45+#
46+47+current_version=$(
48+ grep 'version\s*=' "$plex_nix" \
49+ | sed -Ene 's/.*"(.*)".*/\1/p'
50+)
51+52+echo "Current version: $current_version"
53+54+#
55+# update the nix expression if the versions differ
56+#
57+58+if [[ "$current_version" == "$upstream_version" ]]; then
59+ echo "Plex is already up-to-date"
60+ exit 0
61+fi
62+63+echo "Updating from ${current_version} to ${upstream_version}, released on ${last_updated}"
64+65+# search-and-replace revision, hash and version
66+sed --regexp-extended \
67+ -e 's/rev\s*=\s*"[0-9]+"\s*;/rev = "'"${revision}"'";/' \
68+ -e 's/hash\s*=\s*"[^"]*"\s*;/hash = "'"${hash}"'";/' \
69+ -e 's/version\s*=\s*".*"\s*;/version = "'"${upstream_version}"'";/' \
70+ -i "$plex_nix"
71+