summaryrefslogtreecommitdiff
path: root/mac/.local/bin/stw
blob: f6a5c8516293b5bd3ae154d9f794b21eb841ae5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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"