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