e53a259bc6c2ae885c0bab1b820b511b885a64c4
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")
David Blume Add a .gitignore file

David Blume authored 8 years ago

5) declare -r backup_dir=$HOME/backup_of_dotfiles_`date "+%Y-%m-%d"`
David Blume New \h:\W$ with error dete...

David Blume authored 6 years ago

6) declare -a dotfiles=(".bashrc" ".bash_profile" ".vimrc" ".editrc"
David Blume Add workaround for OpenSSH...

David Blume authored 8 years ago

7)                      ".gitconfig" ".gitignore" ".inputrc" ".ssh/config")
David Blume Make setup.sh only backup c...

David Blume authored 8 years ago

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

David Blume authored 8 years ago

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

David Blume authored 6 years ago

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

David Blume authored 8 years ago

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

David Blume authored 8 years ago

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

David Blume authored 8 years ago

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

David Blume authored 8 years ago

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

David Blume authored 8 years ago

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

David Blume authored 8 years ago

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

David Blume authored 8 years ago

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

David Blume authored 8 years ago

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

David Blume authored 6 years ago

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

David Blume authored 8 years ago

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

David Blume authored 6 years ago

51)             echo
David Blume Replaced tabs with spaces.

David Blume authored 8 years ago

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

David Blume authored 8 years ago

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

David Blume authored 8 years ago

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

David Blume authored 8 years ago

58)     fi
59) done
David Blume first commit

David Blume authored 8 years ago

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

David Blume authored 6 years ago

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

David Blume authored 6 years ago

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

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

David Blume authored 6 years ago

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

David Blume authored 8 years ago

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

David Blume authored 6 years ago

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

David Blume authored 6 years ago

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

David Blume authored 6 years ago

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

David Blume authored 8 years ago

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

David Blume authored 6 years ago

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

David Blume authored 6 years ago

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

David Blume authored 8 years ago

86) fi
David Blume first commit

David Blume authored 8 years ago

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

David Blume authored 8 years ago

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

David Blume authored 8 years ago

91) fi
92)