mirror of
https://github.com/samsonjs/config.git
synced 2026-03-25 09:15:47 +00:00
Make the init script idempotent
This commit is contained in:
parent
1db9de17e1
commit
aedd9edb9c
1 changed files with 46 additions and 14 deletions
60
init.sh
60
init.sh
|
|
@ -1,13 +1,17 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
BASENAME="${0##*/}"
|
# Get the directory where this script resides
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
|
||||||
if [ x"$1" != x ]; then
|
# Use provided path or default to script directory
|
||||||
CONFIG_PATH="$1"
|
if [ -n "$1" ]; then
|
||||||
elif [ -d "${HOME}/config" ]; then
|
CONFIG_PATH="$(cd "$1" && pwd)"
|
||||||
CONFIG_PATH="${HOME}/config"
|
|
||||||
else
|
else
|
||||||
echo "Error: no config dir found"
|
CONFIG_PATH="$SCRIPT_DIR"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -d "$CONFIG_PATH" ]; then
|
||||||
|
echo "Error: config directory not found: $CONFIG_PATH"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
@ -15,18 +19,46 @@ link_config() {
|
||||||
SRC="$1"
|
SRC="$1"
|
||||||
NAME=$(basename "$SRC")
|
NAME=$(basename "$SRC")
|
||||||
DEST="${HOME}/.${NAME}"
|
DEST="${HOME}/.${NAME}"
|
||||||
if [ -e "$DEST" ]; then
|
|
||||||
echo "Existing file found at ${DEST}, moving to ~/original-dot-files."
|
# Check if destination exists
|
||||||
mkdir "${HOME}/original-dot-files" >/dev/null 2>/dev/null
|
if [ -e "$DEST" ] || [ -L "$DEST" ]; then
|
||||||
mv "$DEST" original-dot-files/
|
# If it's already a symlink pointing to the right place, skip it
|
||||||
|
if [ -L "$DEST" ] && [ "$(readlink "$DEST")" = "$SRC" ]; then
|
||||||
|
echo "✓ ${DEST} already linked correctly"
|
||||||
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Otherwise, back it up
|
||||||
|
echo "Backing up existing ${DEST} to ~/original-dot-files/"
|
||||||
|
mkdir -p "${HOME}/original-dot-files"
|
||||||
|
mv "$DEST" "${HOME}/original-dot-files/${NAME}.$(date +%Y%m%d_%H%M%S)"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Create the symlink
|
||||||
ln -s "$SRC" "$DEST"
|
ln -s "$SRC" "$DEST"
|
||||||
|
echo "→ Linked ${DEST} to ${SRC}"
|
||||||
}
|
}
|
||||||
|
|
||||||
cd "$CONFIG_PATH"
|
# Files to exclude from symlinking
|
||||||
|
EXCLUDE_FILES=("init.sh" "zsh" "Brewfile", "CLAUDE.md")
|
||||||
|
|
||||||
for FILE in *; do
|
echo "Creating symlinks from ${CONFIG_PATH}..."
|
||||||
if [ "$FILE" != "init.sh" ] && [ "$FILE" != "zsh" ] && [ "$FILE" != "Brewfile" ]; then
|
|
||||||
link_config "${CONFIG_PATH}/$FILE"
|
for FILE in "$CONFIG_PATH"/*; do
|
||||||
|
BASENAME=$(basename "$FILE")
|
||||||
|
|
||||||
|
# Skip if file is in exclude list
|
||||||
|
SKIP=false
|
||||||
|
for EXCLUDE in "${EXCLUDE_FILES[@]}"; do
|
||||||
|
if [ "$BASENAME" = "$EXCLUDE" ]; then
|
||||||
|
SKIP=true
|
||||||
|
break
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
if [ "$SKIP" = false ] && [ -f "$FILE" ]; then
|
||||||
|
link_config "$FILE"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "Done!"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue