Eric Bower
·
2026-01-18
dotfiles.sh
1#!/usr/bin/env bash
2
3TRACKED_FILES=(
4 bin/
5 dot_config/
6 dot/gitconfig
7 dot/tmux.conf
8 dot/mbsyncrc
9 dot_gnupg/gpg-agent.conf
10 dot_ssh/config
11 dot_mblaze/mblaze
12 dot/msmtprc
13 dot/npmrc
14 dot/zshrc
15 dot/editorconfig
16)
17
18TRACKED_LOCATIONS=(
19 ~/.local/bin/
20 ~/.config/
21 ~/.gitconfig
22 ~/.tmux.conf
23 ~/.mbsyncrc
24 ~/.gnupg/gpg-agent.conf
25 ~/.ssh/config
26 ~/.mblaze/profile
27 ~/.msmtprc
28 ~/.npmrc
29 ~/.zshrc
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