67f87f0ab8629adac9819330e7191432a7c6c02c
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 Add Neovim configs

dblume authored 8 months ago

10)                      ".config/dunst/dunstrc" ".config/nvim/init.vim"
11)                      ".config/nvim/colors/nvim_desert.vim")
dblume shell script variables lowe...

dblume authored 11 months ago

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

David Blume authored 8 years ago

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

dblume authored 11 months ago

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

David Blume authored 8 years ago

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

dblume authored 11 months ago

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

David Blume authored 8 years ago

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

David Blume authored 8 years ago

33) 
dblume shellcheck -s bash setup.sh

dblume authored 11 months ago

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

dblume authored 11 months ago

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

David Blume authored 8 years ago

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

David Blume authored 8 years ago

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

dblume authored 11 months ago

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

David Blume authored 8 years ago

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

dblume authored 11 months ago

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

dblume authored 11 months ago

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

dblume authored 11 months ago

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

David Blume authored 8 years ago

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

dblume authored 11 months ago

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

David Blume authored 6 years ago

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

David Blume authored 8 years ago

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

David Blume authored 6 years ago

55)             echo
David Blume Replaced tabs with spaces.

David Blume authored 8 years ago

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

David Blume authored 8 years ago

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

dblume authored 11 months ago

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

David Blume authored 8 years ago

62)     fi
63) done
David Blume first commit

David Blume authored 8 years ago

64) 
dblume shellcheck -s bash setup.sh

dblume authored 11 months ago

65) if ! diff -qr "$HOME"/.vim .vim > /dev/null ; then
dblume shell script variables lowe...

dblume authored 11 months ago

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

dblume authored 11 months ago

67)         if [ -d "$HOME"/.vim ]; then
68)             mv "$HOME"/.vim "${backup_dir}"
David Blume Make the setup script bette...

David Blume authored 6 years ago

69)         fi
dblume shellcheck -s bash setup.sh

dblume authored 11 months ago

70)         cp -r .vim "$HOME"
71)         if [ -d "${backup_dir}"/.vim ]; then
David Blume List the changed .vim/ files.

David Blume authored 6 years ago

72)             # Copy back proprietary file types (ex. ftdetect/my.vim), if any.
David Blume Print the files that get co...

David Blume authored 6 years ago

73)             # Print only the files that got moved back into ~/.vim
dblume shellcheck -s bash setup.sh

dblume authored 11 months ago

74)             cp -rnv "${backup_dir}"/.vim "$HOME" | grep " -> " | cut -d " " -f3 | \
David Blume Print the files that get co...

David Blume authored 6 years ago

75)                 xargs -I{} sh -c "test -f {} && echo Restored {}" || true
David Blume Make the setup script bette...

David Blume authored 6 years ago

76)         fi
David Blume Make setup.sh only backup c...

David Blume authored 8 years ago

77)     fi
dblume shellcheck -s bash setup.sh

dblume authored 11 months ago

78)     if [[ -d "${backup_dir}"/.vim ]]; then
David Blume Better output for .vim/ han...

David Blume authored 6 years ago

79)         if ! diff -qr "$backup_dir/.vim" "$HOME/.vim" ; then
80)             echo "# diff -qr \"$backup_dir/.vim\" \"$HOME/.vim\""
81)             echo
82)         else
83)             echo No change to the .vim/ directories after restoring proprietary files.
84)         fi
David Blume Update some bash logic when...

David Blume authored 6 years ago

85)     else
dblume shellcheck -s bash setup.sh

dblume authored 11 months ago

86)         echo No "${backup_dir}"/.vim/ from which to restore proprietary files \(yet\).
David Blume Add BrightScript syntax, an...

David Blume authored 8 years ago

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

David Blume authored 6 years ago

88) else
David Blume List the changed .vim/ files.

David Blume authored 6 years ago

89)     echo No change to the .vim/ directories.
David Blume Make setup.sh only backup c...

David Blume authored 8 years ago

90) fi
David Blume first commit

David Blume authored 8 years ago

91) 
dblume Add Neovim configs

dblume authored 8 months ago

92) # TODO: Neovim's plugins go in ~/.local/share/nvim/site/plugin/
93) # https://neovim.io/doc/user/usr_05.html
94) if ! diff -qr "$HOME"/.local/share/nvim/site/plugin .local/share/nvim/site/plugin > /dev/null ; then
95)     if [ $dry_run -eq 0 ]; then
96)         if [ -d "$HOME"/.local/share/nvim/site/plugin/ ]; then
97)                 mkdir -p "${backup_dir}"/.local/share/nvim/site/plugin
98)                 mv "$HOME"/.local/share/nvim/site/plugin/* "${backup_dir}"/.local/share/nvim/site/plugin/
99)         else
100)             mkdir -p "$HOME"/.local/share/nvim/site/plugin
101)         fi
102)         cp -r .local/share/nvim/site/plugin/* "$HOME"/.local/share/nvim/site/plugin/
103)     fi
104)     echo Check the plugins in .local/share/nvim/site/plugin/
105) else
106)     echo No change to the .local/share/nvim/site/plugin/ directories.
107) fi
108) 
dblume First tweaks for neovim, tw...

dblume authored 8 months ago

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

dblume authored 11 months ago

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

dblume authored 11 months ago

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

David Blume authored 8 years ago

112) fi
113) 
David Blume Add a reminder to copy acct...

David Blume authored 3 years ago

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

dblume authored 11 months ago

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

David Blume authored 3 years ago

118)         echo Consider copying $i from a similar acct. \(\'touch \"\$HOME/$i\"\' to hide this msg.\)
119)     fi
120) done
121) 
dblume shell script variables lowe...

dblume authored 11 months ago

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

dblume authored 11 months ago

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

David Blume authored 6 years ago

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

dblume authored 11 months ago

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