nix-configs

Personal NixOS and home-manager configuration files
Log | Files | Refs

startpage-sort.nix (1500B)


      1 {pkgs, ...}: let
      2   startpage-sort = pkgs.writeShellScriptBin "startpage-sort" ''
      3 	set -euo pipefail
      4 
      5 	source_file="$HOME/vault/src/startpage/index.html"
      6 	tmp_file="$(mktemp)"
      7 	trap 'rm -f "$tmp_file"' EXIT
      8 
      9 	before_sum="$(sha256sum "$source_file" | cut -d" " -f1)"
     10 
     11 	awk '
     12 	function ci_cmp(i1, v1, i2, v2,    a, b) {
     13 	  a = tolower(v1)
     14 	  b = tolower(v2)
     15 	  return (a < b ? -1 : a > b ? 1 : 0)
     16 	}
     17 
     18 	BEGIN {
     19 	  PROCINFO["sorted_in"] = "ci_cmp"
     20 	  in_bm = 0
     21 	  in_cm = 0
     22 	  nb = 0
     23 	  nc = 0
     24 	}
     25 
     26 	# Start of bookmarks block
     27 	/^      const bookmarks = \[$/ {
     28 	  in_bm = 1
     29 	  print
     30 	  next
     31 	}
     32 
     33 	# End of bookmarks block
     34 	in_bm && /^\s+\];$/ {
     35 	  for (i in bookmarks) print bookmarks[i]
     36 	  print
     37 	  in_bm = 0
     38 	  delete bookmarks
     39 	  next
     40 	}
     41 
     42 	# Inside bookmarks block
     43 	in_bm {
     44 	  bookmarks[$0] = $0
     45 	  next
     46 	}
     47 
     48 	# Start of commandMap block
     49 	/^      const commandMap = {$/ {
     50 	  in_cm = 1
     51 	  print
     52 	  next
     53 	}
     54 
     55 	# End of commandMap block
     56 	in_cm && /^\s+\};$/ {
     57 	  for (i in commands) print commands[i]
     58 	  print
     59 	  in_cm = 0
     60 	  delete commands
     61 	  next
     62 	}
     63 
     64 	# Inside commandMap block
     65 	in_cm {
     66 	  commands[$0] = $0
     67 	  next
     68 	}
     69 
     70 	# Default
     71 	{
     72 	  print
     73 	}
     74 	' "$source_file" > "$tmp_file"
     75 
     76 	mv "$tmp_file" "$source_file"
     77 	trap - EXIT
     78 
     79 	after_sum="$(sha256sum "$source_file" | cut -d" " -f1)"
     80 
     81 	if [ "$before_sum" != "$after_sum" ]; then
     82 	  echo "startpage index.html was reordered"
     83 	else
     84 	  echo "startpage index.html already sorted, no changes made"
     85 	fi
     86   '';
     87 in {
     88   environment.systemPackages = [startpage-sort];
     89 }