2025-04-26 03:52:08 +00:00
{{- if ne .chezmoi.os "windows" -}}
2025-02-21 03:24:38 +00:00
#! /usr/bin/env bash
set -e
2025-04-15 16:11:55 +00:00
[ -z " $ SET_X " ] || env | grep -E 'chezmoi|^CZ'
2025-02-21 03:24:38 +00:00
cd " $ CHEZMOI_SOURCE_DIR "
2025-03-11 12:08:29 +00:00
#CZ _MODEL = " $(ioreg -l | grep " product - name " | sed -Ee ' s/^.* \ < " ( . * ) \" \ >.*$/ \1 / ' ) "
2025-04-15 16:11:55 +00:00
${ SET_X : - : } | grep '^CZ_' || true
2025-02-21 03:24:38 +00:00
printf 'System detected as %s/%s, setting up .chezmoiroot and initializing...\n' " $ CZ_SYS " " $ CZ_CHASSIS "
2025-04-15 16:11:55 +00:00
${ SET_X : - : } -x
CZROOTLINK=chezmoi.roots/__root_links/base.chezmoiroot
if [ -r " ${ CZROOTLINK } . $ CZ_SYS . $ CZ_CHASSIS " ]; then
ln -s " ${ CZROOTLINK } . $ CZ_SYS . $ CZ_CHASSIS " .chezmoiroot
elif [ -r " ${ CZROOTLINK } . $ CZ_SYS " ]; then
ln -s " ${ CZROOTLINK } . $ CZ_SYS " .chezmoiroot
elif [ -r " ${ CZROOTLINK } . $ CZ_CHASSIS " ]; then
ln -s " ${ CZROOTLINK } . $ CZ_CHASSIS " .chezmoiroot
2025-02-21 03:24:38 +00:00
else
printf 'Failed to find a matching .chezmoiroot. Aborting!\n'
false
fi
2025-04-10 15:30:55 +00:00
# Function to get relative path without realpath or python
relpath() {
local target=$1
local base=$2
local target_abs=$(cd "$(dirname " $ target ")" && pwd)/$(basename " $ target ")
local base_abs=$(cd " $ base " && pwd)
local common_part=" $ base_abs "
local back=""
while [[ " ${ target_abs #$common_part } " == " $ target_abs " ]]; do
common_part=$(dirname " $ common_part ")
back="../ $ back "
done
echo " ${ back } ${ target_abs #$common_part/ } "
}
safe_backup() {
local BASE=" ${ BASE : - $ HOME / . cache / chezmoi / init_backup } "
while [ -n " ${ 1 } " ]; do
local relp="./$(relpath " ${ 1 } " ~)"
[ -r " ${ 1 } " ] && mkdir -p " ${ BASE } / ${ relp % / * } " && cp ${ CPOPTS : - } " ${ 1 } " " ${ BASE } / ${ relp } "
shift
done
}
if ! [ -d ~/.cache/chezmoi/init_backup ]; then
safe_backup ~/.zshrc ~/.zsh_history ~/.zprofile ~/.bashrc ~/.bash_history ~/.profile
fi
2025-04-15 20:13:14 +00:00
# Brute force remove chezmoistate lock and initialize chezmoi
# apply with CZ_EXTR=1 will run twice as some logic in chemzoiexternal
# requires tools that are downloaded with chezmoiexternal
exec sh -c 'rm ~/.config/chezmoi/chezmoistate.boltdb \
&& chezmoi init && chezmoi apply \
&& printf "apply with externals #1/2...\n" && CZ_EXTR=1 chezmoi apply \
&& printf "apply with externals #2/2...\n" && CZ_EXTR=1 chezmoi apply'
2025-04-26 03:52:08 +00:00
{{- else -}}
#! /usr/bin/env pwsh
$ local :CZROOTLINK=Join-Path "chezmoi.roots" (Join-Path "__root_links" "base.chezmoiroot")
# Get-ChildItem -LiteralPath "env:" | Where-Object Name -imatch 'cz|chezmoi'
$ local :CZ_SYS= $ env :CZ_SYS
$ local :CZ_CHASSIS= $ env :CZ_CHASSIS
$ local :CZ_ROOT_LINK=(Join-Path $ env :CHEZMOI_SOURCE_DIR " $ CZROOTLINK ")
Write-Host -ForegroundColor Green "System detected as $( $ env :CHEZMOI_OS)/$( $ env :CHEZMOI_ARCH), setting up .chezmoiroot and initializing..."
if (Test-Path -PathType Leaf " $ CZ_ROOT_LINK . $ CZ_SYS . $ CZ_CHASSIS ") {
$ CZ_ROOT_LINK = " $ CZ_ROOT_LINK . $ CZ_SYS . $ CZ_CHASSIS "
} elseif(Test-Path -PathType Leaf " $ CZ_ROOT_LINK . $ CZ_SYS ") {
$ CZ_ROOT_LINK = " $ CZ_ROOT_LINK . $ CZ_SYS "
} elseif(Test-Path -PathType Leaf " $ CZ_ROOT_LINK . $ CZ_CHASSIS ") {
$ CZ_ROOT_LINK = " $ CZ_ROOT_LINK . $ CZ_CHASSIS "
} else {
Write-Host -ForegroundColor Red "Failed to find a matching .chezmoiroot. Aborting!"
exit 1
}
$ local :newItem = [ordered]@{
Type = "File"
Path = $ env :CHEZMOI_SOURCE_DIR
Name = ".chezmoiroot"
Value = @((Get-Content -LiteralPath $ CZ_ROOT_LINK ).Replace('\', '/')) -join "`n"
}
$ null = New-Item @newItem -ErrorAction Stop
Write-Host -ForegroundColor Green ".chezmoiroot initialized. You will need to run chezmoi init --apply again (the command is already in your clipboard)."
"chezmoi init --apply" | Set-Clipboard
{{ end }}