fc7bfa6d0aa1ec7df3210faf70ea266f1e48f55b
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) 
David Blume Make setup.sh only backup c...

David Blume authored 8 years ago

4) declare -r SCRIPT_NAME=$(basename "$BASH_SOURCE")
dblume Add time to backup director...

dblume authored 2 years 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 Whitespace.

dblume authored 2 years ago

8)                      ".gdbinit")
David Blume Make setup.sh only backup c...

David Blume authored 8 years ago

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

David Blume authored 8 years ago

30) 
David Blume Ensure the .ssh/config file...

David Blume authored 6 years ago

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

David Blume authored 8 years ago

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

David Blume authored 8 years ago

36) for i in "${dotfiles[@]}"
37) do
38)     if [ -e $HOME/"$i" ]; then
David Blume Make setup.sh only backup c...

David Blume authored 8 years ago

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

David Blume authored 8 years ago

40)             echo "$i" will be changed as follows:
41)             diff $HOME/"$i" "$i" || true
David Blume Make setup.sh only backup c...

David Blume authored 8 years ago

42)             if [ $DRY_RUN -eq 0 ]; then
David Blume Minor tweaks to config file...

David Blume authored 8 years ago

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

David Blume authored 8 years ago

44)                 # Consider using symbolic links instead
45)                 # so pulling updates automatically apply
David Blume Minor tweaks to config file...

David Blume authored 8 years ago

46)                 cp "$i" $HOME/"$i"
David Blume Make the setup script bette...

David Blume authored 6 years ago

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

David Blume authored 8 years ago

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

David Blume authored 6 years ago

52)             echo
David Blume Replaced tabs with spaces.

David Blume authored 8 years ago

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

David Blume authored 8 years ago

56)     else
57)         echo "$i" will be added to HOME.
David Blume Minor tweaks to config file...

David Blume authored 8 years ago

58)         ((DRY_RUN==0)) && cp "$i" $HOME/"$i"
David Blume Add a .gitignore file

David Blume authored 8 years ago

59)     fi
60) done
David Blume first commit

David Blume authored 8 years ago

61) 
David Blume Make the setup script bette...

David Blume authored 6 years ago

62) if ! diff -qr $HOME/.vim .vim > /dev/null ; then
63)     if [ $DRY_RUN -eq 0 ]; then
64)         if [ -d $HOME/.vim ]; then
65)             mv $HOME/.vim $backup_dir
66)         fi
67)         cp -r .vim $HOME
68)         if [ -d $backup_dir/.vim ]; then
David Blume List the changed .vim/ files.

David Blume authored 6 years ago

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

70)             # Print only the files that got moved back into ~/.vim
71)             cp -rnv $backup_dir/.vim $HOME | grep " -> " | cut -d " " -f3 | \
72)                 xargs -I{} sh -c "test -f {} && echo Restored {}" || true
David Blume Make the setup script bette...

David Blume authored 6 years ago

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

David Blume authored 8 years ago

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

David Blume authored 6 years ago

75)     if [[ -d $backup_dir/.vim ]]; then
David Blume Better output for .vim/ han...

David Blume authored 6 years ago

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

David Blume authored 6 years ago

82)     else
83)         echo No $backup_dir/.vim/ from which to restore proprietary files \(yet\).
David Blume Add BrightScript syntax, an...

David Blume authored 8 years ago

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

David Blume authored 6 years ago

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

David Blume authored 6 years ago

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

David Blume authored 8 years ago

87) fi
David Blume first commit

David Blume authored 8 years ago

88) 
89) # Make a directory for vim undo
90) if [ ! -d $HOME/.vim_undo ]; then
David Blume Make setup.sh only backup c...

David Blume authored 8 years ago

91)     ((DRY_RUN==0)) && mkdir -p $HOME/.vim_undo
David Blume first commit

David Blume authored 8 years ago

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

David Blume authored 3 years ago

94) # I have device local secrets in .localrc and a github secret in .gitconfig.local
95) for i in ".gitconfig.local" ".localrc"
96) do
97)     if [ ! -f $HOME/"$i" ]; then
98)         echo Consider copying $i from a similar acct. \(\'touch \"\$HOME/$i\"\' to hide this msg.\)
99)     fi
100) done
101)