Files
nixos/clone_henrovnix_ok.sh
T

85 lines
2.1 KiB
Bash
Executable File

#!/usr/bin/env zsh
emulate -L zsh
set -euo pipefail
setopt null_glob
SRC="$HOME/Repos/nixos/henrovnix_ok"
DST="$HOME/Repos/nixos/henrovnix"
: "${YOUR_MACHINE:=YOUR_MACHINE}"
: "${YOUR_USER:=YOUR_USER}"
MV_BIN=$(command -v mv || echo "/run/current-system/sw/bin/mv")
RSYNC_BIN=$(command -v rsync || echo "/run/current-system/sw/bin/rsync")
PERL_BIN=$(command -v perl || echo "/run/current-system/sw/bin/perl")
FIND_BIN=$(command -v find || echo "/run/current-system/sw/bin/find")
GREP_BIN=$(command -v grep || echo "/run/current-system/sw/bin/grep")
if [[ ! -d "$SRC" ]]; then
print -u2 "ERROR: Source directory not found: $SRC"
exit 2
fi
print "==> Removing destination if it exists: $DST"
rm -rf -- "$DST"
print "==> Copying $SRC -> $DST"
"$RSYNC_BIN" -a -- "$SRC/" "$DST/"
is_hidden_path() {
[[ "$1" == */.* || "$1" == .* ]]
}
replace_in_contents() {
local from="$1" to="$2"
print "==> Replacing in contents: '$from' -> '$to'"
"$FIND_BIN" "$DST" -type f -print0 |
while IFS= read -r -d '' file; do
is_hidden_path "$file" && continue
if LC_ALL=C "$GREP_BIN" -Iq . "$file" && "$GREP_BIN" -q "$from" "$file"; then
"$PERL_BIN" -pi -e 's/\Q'"$from"'\E/'"$to"'/g' -- "$file"
fi
done
}
replace_in_names() {
local from="$1" to="$2"
print "==> Renaming paths: '$from' -> '$to'"
"$FIND_BIN" "$DST" -depth -print0 |
while IFS= read -r -d '' path; do
is_hidden_path "$path" && continue
local base="${path:t}"
local dir="${path:h}"
local newbase="${base//${from}/${to}}"
if [[ "$newbase" != "$base" ]]; then
local newpath="${dir}/${newbase}"
if [[ -e "$newpath" ]]; then
print -u2 "ERROR: Rename collision:"
print -u2 " from: $path"
print -u2 " to: $newpath"
exit 3
fi
"$MV_BIN" -n -- "$path" "$newpath"
fi
done
}
replace_in_contents "traveldroid" "$YOUR_MACHINE"
replace_in_names "traveldroid" "$YOUR_MACHINE"
replace_in_contents "machine1" "$YOUR_MACHINE"
replace_in_names "machine1" "$YOUR_MACHINE"
replace_in_contents "henrov" "$YOUR_USER"
replace_in_names "henrov" "$YOUR_USER"
print "==> Done."
print "Result: $DST"