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