7d8d94cbf89dc407eae7661f70c00d2366405187
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 shellcheck -s bash setup.sh

dblume authored 11 months ago

4) declare -r SCRIPT_NAME=$(basename "${BASH_SOURCE[0]}")
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 11 months ago

8)                      ".gdbinit" ".config/gitui/key_bindings.ron" ".visidatarc"
9)                      ".config/i3/config" ".config/i3status/config")
David Blume Make setup.sh only backup c...

David Blume authored 8 years ago

10) declare -i DRY_RUN=0
11) 
12) ## exit the shell (with status 2) after printing the message
13) usage() {
14)     echo "\
15) $SCRIPT_NAME -hn
16)     -h      Print this help text
17)     -n      Perform a dry run, to see what'll change
18) "
19)     exit 2;
20) }
21) 
22) ## Process the options
23) while getopts "hn" OPTION
24) do
25)   case $OPTION in
26)     h) usage;;
27)     n) DRY_RUN=1;;
28)     \?) usage;;
29)   esac
30) done
David Blume first commit

David Blume authored 8 years ago

31) 
dblume shellcheck -s bash setup.sh

dblume authored 11 months ago

32) if [ ! -d "${backup_dir}"/.ssh ]; then
33)     ((DRY_RUN==0)) && mkdir -p "${backup_dir}"/.ssh
David Blume first commit

David Blume authored 8 years ago

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

David Blume authored 8 years ago

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

dblume authored 11 months ago

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

David Blume authored 8 years ago

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

dblume authored 11 months ago

42)             diff "$HOME"/"$i" "$i" || true
David Blume Make setup.sh only backup c...

David Blume authored 8 years ago

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

dblume authored 11 months ago

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

David Blume authored 8 years ago

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

dblume authored 11 months ago

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

David Blume authored 6 years ago

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

David Blume authored 8 years ago

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

David Blume authored 6 years ago

53)             echo
David Blume Replaced tabs with spaces.

David Blume authored 8 years ago

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

David Blume authored 8 years ago

57)     else
58)         echo "$i" will be added to HOME.
dblume shellcheck -s bash setup.sh

dblume authored 11 months ago

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

David Blume authored 8 years ago

60)     fi
61) done
David Blume first commit

David Blume authored 8 years ago

62) 
dblume shellcheck -s bash setup.sh

dblume authored 11 months ago

63) if ! diff -qr "$HOME"/.vim .vim > /dev/null ; then
David Blume Make the setup script bette...

David Blume authored 6 years ago

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

dblume authored 11 months ago

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

David Blume authored 6 years ago

67)         fi
dblume shellcheck -s bash setup.sh

dblume authored 11 months ago

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

David Blume authored 6 years ago

70)             # 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

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

dblume authored 11 months ago

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

David Blume authored 6 years ago

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

David Blume authored 6 years ago

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

David Blume authored 8 years ago

75)     fi
dblume shellcheck -s bash setup.sh

dblume authored 11 months ago

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

David Blume authored 6 years ago

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

David Blume authored 6 years ago

83)     else
dblume shellcheck -s bash setup.sh

dblume authored 11 months ago

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

David Blume authored 8 years ago

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

David Blume authored 6 years ago

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

David Blume authored 6 years ago

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

David Blume authored 8 years ago

88) fi
David Blume first commit

David Blume authored 8 years ago

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

dblume authored 11 months ago

91) if [ ! -d "$HOME"/.vim_undo ]; then
92)     ((DRY_RUN==0)) && mkdir -p "$HOME"/.vim_undo
David Blume first commit

David Blume authored 8 years ago

93) fi
94) 
David Blume Add a reminder to copy acct...

David Blume authored 3 years ago

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

dblume authored 11 months ago

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

David Blume authored 3 years ago

99)         echo Consider copying $i from a similar acct. \(\'touch \"\$HOME/$i\"\' to hide this msg.\)
100)     fi
101) done
102) 
David Blume Make the dry run better dis...

David Blume authored 6 years ago

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

dblume authored 11 months ago

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

David Blume authored 6 years ago

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

dblume authored 11 months ago

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