1bbe1e3ea9fcab29a9e861a88f323ea828249082
David Blume Improve setup.sh portability.

David Blume authored 8 years ago

1) #!/usr/bin/env bash
David Blume first commit

David Blume authored 8 years ago

2) set -eu -o pipefail # See: https://sipb.mit.edu/doc/safe-shell/
3) 
dblume shell script variables lowe...

dblume authored 11 months ago

4) declare -r script_name=$(basename "${BASH_SOURCE[0]}")
dblume shellcheck -s bash setup.sh

dblume authored 11 months ago

5) declare -r backup_dir="$HOME"/backup_of_dotfiles_$(date "+%Y-%m-%d_%H%M%S")
David Blume Add .tmux.conf to list of f...

David Blume authored 4 years ago

6) declare -a dotfiles=(".bashrc" ".bash_profile" ".vimrc" ".editrc" ".gitconfig"
dblume Add my .gdbinit

dblume authored 2 years ago

7)                      ".gitignore" ".inputrc" ".tmux.conf" ".ssh/config" ".ripgreprc"
dblume Add i3 configs

dblume authored 1 year ago

8)                      ".gdbinit" ".config/gitui/key_bindings.ron" ".visidatarc"
dblume Add dunst config from Frame...

dblume authored 11 months ago

9)                      ".config/i3/config" ".config/i3status/config"
dblume Create an update_dir functi...

dblume authored 8 months ago

10)                      ".config/dunst/dunstrc")
dblume shell script variables lowe...

dblume authored 11 months ago

11) declare -i dry_run=0
David Blume Make setup.sh only backup c...

David Blume authored 8 years ago

12) 
13) ## exit the shell (with status 2) after printing the message
14) usage() {
15)     echo "\
dblume shell script variables lowe...

dblume authored 11 months ago

16) $script_name -hn
David Blume Make setup.sh only backup c...

David Blume authored 8 years ago

17)     -h      Print this help text
18)     -n      Perform a dry run, to see what'll change
19) "
20)     exit 2;
21) }
22) 
23) ## Process the options
24) while getopts "hn" OPTION
25) do
26)   case $OPTION in
27)     h) usage;;
dblume shell script variables lowe...

dblume authored 11 months ago

28)     n) dry_run=1;;
David Blume Make setup.sh only backup c...

David Blume authored 8 years ago

29)     \?) usage;;
30)   esac
31) done
David Blume first commit

David Blume authored 8 years ago

32) 
dblume shellcheck -s bash setup.sh

dblume authored 11 months ago

33) if [ ! -d "${backup_dir}"/.ssh ]; then
dblume shell script variables lowe...

dblume authored 11 months ago

34)     ((dry_run==0)) && mkdir -p "${backup_dir}"/.ssh
David Blume first commit

David Blume authored 8 years ago

35) fi
36) 
37) # Move original dot files to backup
David Blume Add a .gitignore file

David Blume authored 8 years ago

38) for i in "${dotfiles[@]}"
39) do
dblume shellcheck -s bash setup.sh

dblume authored 11 months ago

40)     if [ -e "$HOME"/"$i" ]; then
41)         if ! cmp --silent "$HOME"/"$i" "$i" ; then
David Blume Replaced tabs with spaces.

David Blume authored 8 years ago

42)             echo "$i" will be changed as follows:
dblume shellcheck -s bash setup.sh

dblume authored 11 months ago

43)             diff "$HOME"/"$i" "$i" || true
dblume shell script variables lowe...

dblume authored 11 months ago

44)             if [ $dry_run -eq 0 ]; then
dblume shellcheck -s bash setup.sh

dblume authored 11 months ago

45)                 mv "$HOME"/"$i" "${backup_dir}"/"$i"
David Blume Make setup.sh only backup c...

David Blume authored 8 years ago

46)                 # Consider using symbolic links instead
47)                 # so pulling updates automatically apply
dblume shellcheck -s bash setup.sh

dblume authored 11 months ago

48)                 cp "$i" "$HOME"/"$i"
49)                 echo "# vimdiff \"$HOME/$i\" \"${backup_dir}/$i\""
David Blume Make the setup script bette...

David Blume authored 6 years ago

50)             else
51)                 # Provide a diff that can still be used
52)                 echo "# vimdiff \"$i\" \"$HOME/$i\""
David Blume Make setup.sh only backup c...

David Blume authored 8 years ago

53)             fi
David Blume Make the setup script bette...

David Blume authored 6 years ago

54)             echo
David Blume Replaced tabs with spaces.

David Blume authored 8 years ago

55)         else
56)             echo No change to "$i".
57)         fi
David Blume Make setup.sh only backup c...

David Blume authored 8 years ago

58)     else
59)         echo "$i" will be added to HOME.
dblume shell script variables lowe...

dblume authored 11 months ago

60)         ((dry_run==0)) && mkdir -p "$HOME"/"$(dirname "$i")" && cp "$i" "$HOME"/"$i"
David Blume Add a .gitignore file

David Blume authored 8 years ago

61)     fi
62) done
David Blume first commit

David Blume authored 8 years ago

63) 
dblume Create an update_dir functi...

dblume authored 8 months ago

64) update_dir () {
65)     if ! diff -qr "$HOME"/"$1" "$1" > /dev/null ; then
66)         local parentdir="$(dirname "$1")"
67) 
68)         if [ $dry_run -eq 0 ]; then
69)             if [ -d "$HOME"/"$1" ]; then
70)                 mkdir -p "${backup_dir}"/"$parentdir"
71)                 mv "$HOME"/"$1" "${backup_dir}"/"$parentdir"
72)             else
73)                 mkdir -p "$HOME"/"$parentdir"
74)             fi
75)             cp -RL "$1" "$HOME"/"$parentdir"
76)             if [ -d "${backup_dir}"/"$1" ]; then
77)                 # Copy back proprietary file types (ex. ftdetect/my.vim), if any.
78)                 # Print only the files that got moved back into ~/"$1"
79)                 cp -RLnv "${backup_dir}"/"$1" "$HOME"/"$parentdir" | grep " -> " | cut -d " " -f3 | \
80)                     xargs -I{} sh -c "test -f {} && echo Restored {}" || true
81)             fi
David Blume Make the setup script bette...

David Blume authored 6 years ago

82)         fi
dblume Create an update_dir functi...

dblume authored 8 months ago

83)         if [[ -d "${backup_dir}"/"$1" ]]; then
84)             if ! diff -qr "$backup_dir"/"$1" "$HOME"/"$1" ; then
85)                 echo "# diff -qr \"$backup_dir/$1\" \"$HOME/$1\""
86)                 echo
87)             else
88)                 echo No change to the "$1"/ directories after restoring proprietary files.
89)             fi
David Blume Better output for .vim/ han...

David Blume authored 6 years ago

90)         else
dblume Create an update_dir functi...

dblume authored 8 months ago

91)             echo No "${backup_dir}"/"$1"/ from which to restore proprietary files \(yet\).
David Blume Better output for .vim/ han...

David Blume authored 6 years ago

92)         fi
David Blume Update some bash logic when...

David Blume authored 6 years ago

93)     else
dblume Create an update_dir functi...

dblume authored 8 months ago

94)         echo No change to the "$1"/ directories.
David Blume Add BrightScript syntax, an...

David Blume authored 8 years ago

95)     fi
dblume Create an update_dir functi...

dblume authored 8 months ago

96) }
David Blume first commit

David Blume authored 8 years ago

97) 
dblume Create an update_dir functi...

dblume authored 8 months ago

98) update_dir ".vim"
99) update_dir ".config/nvim"
100) # Neovim's plugins go in ~/.local/share/nvim/site/plugin/
dblume Add Neovim configs

dblume authored 8 months ago

101) # https://neovim.io/doc/user/usr_05.html
dblume Create an update_dir functi...

dblume authored 8 months ago

102) update_dir ".local/share/nvim/site/plugin"
dblume Add Neovim configs

dblume authored 8 months ago

103) 
dblume First tweaks for neovim, tw...

dblume authored 8 months ago

104) # Make a directory for vim undo
dblume shellcheck -s bash setup.sh

dblume authored 11 months ago

105) if [ ! -d "$HOME"/.vim_undo ]; then
dblume shell script variables lowe...

dblume authored 11 months ago

106)     ((dry_run==0)) && mkdir -p "$HOME"/.vim_undo
David Blume first commit

David Blume authored 8 years ago

107) fi
108) 
David Blume Add a reminder to copy acct...

David Blume authored 3 years ago

109) # I have device local secrets in .localrc and a github secret in .gitconfig.local
110) for i in ".gitconfig.local" ".localrc"
111) do
dblume shellcheck -s bash setup.sh

dblume authored 11 months ago

112)     if [ ! -f "$HOME"/"$i" ]; then
David Blume Add a reminder to copy acct...

David Blume authored 3 years ago

113)         echo Consider copying $i from a similar acct. \(\'touch \"\$HOME/$i\"\' to hide this msg.\)
114)     fi
115) done
116) 
dblume shell script variables lowe...

dblume authored 11 months ago

117) if [ $dry_run -eq 0 ]; then
dblume shellcheck -s bash setup.sh

dblume authored 11 months ago

118)     echo Your old dotfiles are backed up to "${backup_dir}"
David Blume Make the dry run better dis...

David Blume authored 6 years ago

119)     echo Done. Check http://config.dlma.com for more.
120) else
dblume shellcheck -s bash setup.sh

dblume authored 11 months ago

121)     echo Your old dotfiles would have been backed up to "${backup_dir}"