|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -xeuo pipefail |
| 4 | + |
| 5 | +state_dir=${XDG_STATE_HOME:-$HOME/.local/state}/winblues/chezmoi |
| 6 | + |
| 7 | +user_chezmoi_ignore=${XDG_CONFIG_HOME:-$HOME/.config}/winblues/chezmoiignore |
| 8 | +user_env_file=$(mktemp -p /tmp winblues-chezmoi-env.XXXX) |
| 9 | + |
| 10 | +# Save current user environment so we can reload it in chezmoi run_*.sh scripts |
| 11 | +echo "export XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}" >"${user_env_file}" |
| 12 | +echo "export XDG_DATA_HOME=${XDG_DATA_HOME:-$HOME/.local/share}" >>"${user_env_file}" |
| 13 | +echo "export XDG_STATE_HOME=${XDG_STATE_HOME:-$HOME/.local/state}" >>"${user_env_file}" |
| 14 | +echo "export XDG_CACHE_HOME=${XDG_CACHE_HOME:-$HOME/.cache}" >>"${user_env_file}" |
| 15 | +export WINBLUES_CHEZMOI_ORIGINAL_ENV_FILE="${user_env_file}" |
| 16 | + |
| 17 | +# Use our own chezmoi directories so we don't interfere with the user's chezmoi |
| 18 | +export XDG_CONFIG_HOME="${state_dir}/.config" |
| 19 | +export XDG_DATA_HOME="${state_dir}/.local/share" |
| 20 | +export XDG_STATE_HOME="${state_dir}/.local/state" |
| 21 | +export XDG_CACHE_HOME="${state_dir}/.cache" |
| 22 | + |
| 23 | +if [ ! -d "$state_dir" ]; then |
| 24 | + echo "First run of $0" |
| 25 | + mkdir -p "$state_dir" |
| 26 | + chezmoi init |
| 27 | + chezmoi git -- checkout -b main |
| 28 | +fi |
| 29 | + |
| 30 | +# Copy the booted image's chezmoi source to the user's state |
| 31 | +winblues_chezmoi_data_home="${XDG_DATA_HOME}/chezmoi" |
| 32 | +winblues_chezmoi_source=/usr/share/winblues/chezmoi |
| 33 | + |
| 34 | +cd $winblues_chezmoi_data_home/.. |
| 35 | +rsync -aP $winblues_chezmoi_source . |
| 36 | +cd $winblues_chezmoi_data_home |
| 37 | + |
| 38 | +if [[ -z "$(git status --porcelain)" ]]; then |
| 39 | + echo "No changes needed" |
| 40 | + exit 0 |
| 41 | +fi |
| 42 | + |
| 43 | +# At this point, we know that we need to run chezmoi apply to update |
| 44 | +# our managed files |
| 45 | + |
| 46 | +git config --local user.name "Winblues User" |
| 47 | +git config --local user.email "user@blues.win" |
| 48 | +git add . |
| 49 | + |
| 50 | +booted_image_version=$(rpm-ostree status --json | jq '.deployments[] | select(.booted == true) | .version' | tr -d '"') |
| 51 | +booted_image=$(rpm-ostree status --json | jq '.deployments[] | select(.booted == true) | ."container-image-reference"' | tr -d '"') |
| 52 | + |
| 53 | +git commit -m "bump: $booted_image_version of $booted_image" |
| 54 | + |
| 55 | +# Check if user has changes to files we're managing. Save them in a branch and rollback |
| 56 | +# to a known-good state. |
| 57 | +if [[ ! -z "$(chezmoi diff --script-contents=false)" ]]; then |
| 58 | + echo "Dirty state of managed files. Creating a branch to preserve user's changes." |
| 59 | + if git rev-parse "rollback" >/dev/null 2>&1; then |
| 60 | + git tag -d rollback |
| 61 | + fi |
| 62 | + |
| 63 | + git checkout -b "rollback-$(date +%Y%m%d-%H%M%S)" |
| 64 | + chezmoi re-add |
| 65 | + git add . |
| 66 | + git commit --allow-empty -m "changes to managed files" |
| 67 | + git tag rollback |
| 68 | +else |
| 69 | + echo "No user-modified managed files" |
| 70 | +fi |
| 71 | + |
| 72 | +git checkout main |
| 73 | + |
| 74 | +# Allow users to ignore updates to certain files |
| 75 | +if [[ -f $user_chezmoi_ignore ]]; then |
| 76 | + cp $user_chezmoi_ignore $winblues_chezmoi_data_home/.chezmoiignore |
| 77 | +fi |
| 78 | + |
| 79 | +chezmoi apply --force |
| 80 | +rm -f $winblues_chezmoi_data_home/.chezmoiignore |
0 commit comments