Skip to content

Instantly share code, notes, and snippets.

@supersonictw
Last active November 20, 2025 06:41
Show Gist options
  • Select an option

  • Save supersonictw/8548d9abbd28b5c13a533dd11f445828 to your computer and use it in GitHub Desktop.

Select an option

Save supersonictw/8548d9abbd28b5c13a533dd11f445828 to your computer and use it in GitHub Desktop.
#!/usr/bin/env zsh
# to_use.zsh - Zsh script to link configuration files based on hostname
# SPDX-License-Identifier: MIT (https://ncurl.xyz/s/Kkn2DQsNR)
# Interrupt on errors
setopt ERR_EXIT
# Use custom hostname if provided, else default to system hostname
HOSTNAME="${HOSTNAME:-$(hostname)}"
# Set target directory, defaulting to home
TARGET_DIR="${TARGET_DIR:-$HOME}"
# Define installation directory
INSTALL_DIR="$TARGET_DIR/.syncconf"
# Get current script directory
CURRENT_DIR="${0:a:h}"
# Define profile directory based on hostname
PROFILE_DIR="$CURRENT_DIR/$HOSTNAME"
# Check source directory exists
[[ -d "$PROFILE_DIR" ]] || { print -u2 "Error: $PROFILE_DIR not found"; exit 1; }
# Ensure target directory exists
mkdir -p -- "$TARGET_DIR"
# Remove old symlink if exists
[[ -e "$INSTALL_DIR" ]] && rm -rf -- "$INSTALL_DIR"
# Create main symlink
ln -s -- "$PROFILE_DIR" "$INSTALL_DIR"
# Link configuration files
for file in "$INSTALL_DIR"/*; do
basefile="$(basename -- "$file")"
targetfile="$TARGET_DIR/$basefile"
# Remove existing file or symlink
[[ -e "$targetfile" || -L "$targetfile" ]] && \
rm -rf -- "$targetfile"
# Create symlink
ln -s -- "$file" "$targetfile"
done
# Log the operation
print "Configuration linked to $TARGET_DIR"
date +"%Y-%m-%d %T" >> "$TARGET_DIR/.syncconf.timestamp"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment