iso27diy-corp/Corpus/Standards/ISO-27001-OST/ISO27001-EN-2022/rename-iso.zsh

50 lines
1.1 KiB
Bash
Executable file

#!/usr/bin/env zsh
set -euo pipefail
execute=false
if [[ ${1:-} == '--execute' ]]; then
execute=true
shift
fi
if [[ $# -gt 0 ]]; then
print -u2 'Usage: rename-iso.zsh [--execute]'
exit 2
fi
# Requires Obsidian app running and CLI enabled.
# Adjust OBSIDIAN_CLI to the command you actually use (e.g. `obsidian`).
: ${OBSIDIAN_CLI:=obsidian}
files=(ISO_27001*.md(N))
if (( ${#files} == 0 )); then
print 'No matching files found.'
exit 0
fi
for src in "$files[@]"; do
base=${src:t}
# Match both ISO_27001_OT and ISO_27001_2022_OT patterns
if [[ $base =~ '^ISO_27001(_2022)?_OT ([0-9.]+) (.+)\.md$' ]]; then
version=${match[2]#_}
title=${match[3]}
target="c-${version}-${title}.md"
# Replace spaces with dashes
target=${target// /-}
# Remove commas
target=${target//,}
# Prevent double dashes
target=${target//--/-}
if [[ $src == $target ]]; then
print "SKIP $src"
continue
fi
print "SRC $src"
print "DEST $target"
if $execute; then
"$OBSIDIAN_CLI" rename file="$src" name="$target"
fi
else
print -u2 "WARN skipped (pattern mismatch): $src"
fi
done