66524eba0506615606cb07bfb5dd88a2e0745a49
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 10 months ago

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

dblume authored 10 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 11 months ago

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

dblume authored 10 months ago

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

dblume authored 10 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 10 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 10 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 10 months ago

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

dblume authored 10 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 10 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 10 months ago

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

dblume authored 10 months ago

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

dblume authored 10 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 10 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 10 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 shellcheck -s bash setup.sh

dblume authored 10 months ago

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

dblume authored 10 months ago

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

dblume authored 10 months ago

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

David Blume authored 6 years ago

68)         fi
dblume shellcheck -s bash setup.sh

dblume authored 10 months ago

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

David Blume authored 6 years ago

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

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

dblume authored 10 months ago

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

David Blume authored 6 years ago

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

David Blume authored 6 years ago

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

David Blume authored 8 years ago

76)     fi
dblume shellcheck -s bash setup.sh

dblume authored 10 months ago

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

David Blume authored 6 years ago

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

David Blume authored 6 years ago

84)     else
dblume shellcheck -s bash setup.sh

dblume authored 10 months ago

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

David Blume authored 8 years ago

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

David Blume authored 6 years ago

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

David Blume authored 6 years ago

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

David Blume authored 8 years ago

89) fi
David Blume first commit

David Blume authored 8 years ago

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

dblume authored 10 months ago

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

dblume authored 10 months ago

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

David Blume authored 8 years ago

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

David Blume authored 3 years ago

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

dblume authored 10 months ago

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

David Blume authored 3 years ago

100)         echo Consider copying $i from a similar acct. \(\'touch \"\$HOME/$i\"\' to hide this msg.\)
101)     fi
102) done
103) 
dblume shell script variables lowe...

dblume authored 10 months ago

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

dblume authored 10 months ago

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

David Blume authored 6 years ago

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

dblume authored 10 months ago

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