50f1fe2 dotfiles.sh
Eric Bower  ·  2026-04-27
 1#!/usr/bin/env bash
 2
 3TRACKED_FILES=(
 4    bin/
 5    dot_pi/agent/extensions/
 6    dot_pi/agent/settings.json
 7    dot_config/
 8    dot/gitconfig
 9    dot/tmux.conf
10    dot/mbsyncrc
11    dot_gnupg/gpg-agent.conf
12    dot_ssh/config
13    dot_mblaze/mblaze
14    dot/msmtprc
15    dot/npmrc
16    dot/zshrc
17    dot/editorconfig
18)
19
20TRACKED_LOCATIONS=(
21    ~/.local/bin/
22    ~/.pi/agent/extensions/
23    ~/.pi/agent/settings.json
24    ~/.config/
25    ~/.gitconfig
26    ~/.tmux.conf
27    ~/.mbsyncrc
28    ~/.gnupg/gpg-agent.conf
29    ~/.ssh/config
30    ~/.mblaze/profile
31    ~/.msmtprc
32    ~/.npmrc
33    ~/.zshrc
34    ~/.editorconfig
35)
36
37find_files() {
38    if [[ ${TRACKED_FILES[$1]} == */ ]]; then
39        find "$PWD/${TRACKED_FILES[$1]}" -type d -exec find {} -maxdepth 1 -type f \; | sed "s|$PWD/${TRACKED_FILES[$1]}||" | sed "s|^/||"
40    fi
41}
42
43link_file() {
44    if [[ ! -L $2 && ! -f $2 && ! -d $2 ]]; then
45        echo "Creating a link from $1 to $2"
46        mkdir -p $2
47        rmdir $2
48        ln -s $1 $2
49    fi
50}
51export -f link_file
52
53unlink_file() {
54    if [[ -L $1 ]]; then
55        echo "Unlinking $1"
56        unlink "$1"
57    fi
58}
59export -f unlink_file
60
61move_file() {
62    if [[ ! -L $1 ]] && [[ -f $1 || -d $1 ]]; then
63        echo "Moving $1 to backup/$1"
64        mkdir -p "backup/$1"
65        rmdir "backup/$1"
66        mv $1 "backup/$1"
67    fi
68}
69export -f move_file
70
71for ((i = 0; i < ${#TRACKED_FILES[@]}; ++i)); do
72    if [ "$1" == "cleanup" ]; then
73        unlink_file "${TRACKED_LOCATIONS[$i]}"
74        find_files "$i" | xargs -I {} bash -c "unlink_file ${TRACKED_LOCATIONS[$i]}{}"
75        continue
76    fi
77
78    if [ "$1" == "override" ]; then
79        move_file ${TRACKED_LOCATIONS[$i]}
80        find_files "$i" | xargs -I {} bash -c "move_file ${TRACKED_LOCATIONS[$i]}{}"
81    fi
82
83    if [ "$1" != "cleanup" ]; then
84        link_file "$PWD/${TRACKED_FILES[$i]}" "${TRACKED_LOCATIONS[$i]}"
85        find_files "$i" | xargs -I {} bash -c "link_file $PWD/${TRACKED_FILES[$i]}{} ${TRACKED_LOCATIONS[$i]}{}"
86    fi
87done