summaryrefslogtreecommitdiff
path: root/mac/.local/bin/stw
diff options
context:
space:
mode:
Diffstat (limited to 'mac/.local/bin/stw')
-rwxr-xr-xmac/.local/bin/stw78
1 files changed, 78 insertions, 0 deletions
diff --git a/mac/.local/bin/stw b/mac/.local/bin/stw
new file mode 100755
index 0000000..f6a5c85
--- /dev/null
+++ b/mac/.local/bin/stw
@@ -0,0 +1,78 @@
+#!/bin/sh
+
+# Directory where your stow packages are located, adjust as necessary
+stowdir="${XDG_DOTFILES_DIR:-${HOME}/.dotfiles}"
+
+# Function to list and select stow packages using dmenu
+select_stow_package() {
+ find "$stowdir" -mindepth 1 -maxdepth 1 -type d -not -name ".*" -not -name "global" | while read -r dir; do
+ if [ -n "$(find "$dir" -mindepth 1 -maxdepth 1)" ]; then
+ basename "$dir"
+ fi
+ done | dmenu -i -p "Select package to stow: "
+}
+
+# Function to ask user for resolution strategy using dmenu
+ask_resolution_strategy() {
+ printf "delete\nmove" | dmenu -i -p "Choose resolution strategy: "
+}
+
+# Function to stow a package and resolve conflicts
+stow_package() {
+ target="$1"
+ resolve_strategy="$2"
+ # Attempt to stow the package
+ output=$(stow --no-folding -S "$target" 2>&1)
+ status=$?
+
+ # Handle conflicts based on resolution strategy
+ if [ $status -ne 0 ]; then
+ echo "$output" | grep "over existing target is stowed to a different package" | while IFS= read -r line; do
+ conflict_path=$(echo "$line" | sed -E 's/.*\: (.*) =>.*/\1/')
+ full_path="$HOME/$conflict_path"
+ if [ "$resolve_strategy" = "delete" ]; then
+ rm -rf "$full_path"
+ elif [ "$resolve_strategy" = "move" ]; then
+ mv "$full_path" "${full_path}.dotbak"
+ fi
+ done
+ echo "$output" | grep "over existing target" | while IFS= read -r line; do
+ conflict_path=$(echo "$line" | sed -E 's/.*over existing target\s(.*)\ssince.*/\1/')
+ full_path="$HOME/$conflict_path"
+ if [ "$resolve_strategy" = "delete" ]; then
+ rm -rf "$full_path"
+ elif [ "$resolve_strategy" = "move" ]; then
+ mv "$full_path" "${full_path}.dotbak"
+ fi
+ done
+
+ # Retry stowing after conflict resolution
+ output=$(stow --no-folding -S "$target" 2>&1)
+ status=$?
+ fi
+}
+
+# Ensure running from the correct directory
+cd "$stowdir" || exit 1
+
+# Select a stow package
+targetdir=$(select_stow_package) || exit 1
+
+# Ask the user for the resolution strategy
+resolve=$(ask_resolution_strategy) || exit 1
+
+# Stow
+stow_package "$targetdir" "$resolve" && stow_package "global" "$resolve" || exit
+
+# Link for profile
+ln -sf "$stowdir/$targetdir/.config/shell/profile" "$HOME/.zprofile"
+
+# Link for bash
+ln -sf "$stowdir/$targetdir/.config/bash/bash_profile" "$HOME/.bash_profile"
+ln -sf "$stowdir/$targetdir/.config/bash/bashrc" "$HOME/.bashrc"
+
+# Reload shortcuts (assumes this functionality is defined elsewhere and works as expected)
+shortcuts >/dev/null 2>&1 || exit 1
+zsh -c "source '${XDG_CONFIG_HOME:-${HOME}/.config}/shell/shortcutrc'" 2>/dev/null || exit 1
+zsh -c "source '${XDG_CONFIG_HOME:-${HOME}/.config}/shell/zshnameddirrc'" 2>/dev/null || exit 1
+notify-send "✅ Updated shortcuts"