#!/bin/sh set -eu # Permission bits of a file, or 600 if they cannot be read. GNU stat spells this # `-c %a` while BSD/macOS spells it `-f %Lp`, and each mis-parses the other's # form -- GNU treats `-f` as --file-system and prints a block of filesystem info # before failing -- so try one at a time and only accept an octal answer. file_mode() { _m=$(stat -c '%a' "$1" 2>/dev/null) || _m='' case "$_m" in [0-7][0-7][0-7] | [0-7][0-7][0-7][0-7]) printf '%s\n' "$_m"; return 0 ;; esac _m=$(stat -f '%Lp' "$1" 2>/dev/null) || _m='' case "$_m" in [0-7][0-7][0-7] | [0-7][0-7][0-7][0-7]) printf '%s\n' "$_m"; return 0 ;; esac printf '600\n' } config_dir=${1:-"$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"} for appearance in dark light no-preference; do config_file="$config_dir/$appearance-theme.auto.conf" [ -f "$config_file" ] || continue if awk ' $1 == "macos_titlebar_color" { count++ if ($2 == "background") correct++ } END { exit !(count == 1 && correct == 1) } ' "$config_file"; then continue fi tmp=$(mktemp "$config_file.tmp.XXXXXX") trap 'rm -f "$tmp"' EXIT HUP INT TERM awk '$1 != "macos_titlebar_color"' "$config_file" > "$tmp" printf '\nmacos_titlebar_color background\n' >> "$tmp" chmod "$(file_mode "$config_file")" "$tmp" mv -f "$tmp" "$config_file" trap - EXIT HUP INT TERM done