38ded8c3c19c9b08d5b587dd2715d44b90691787
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
68)             # Copy back some proprietary file types, if any.
69)             cp -r -n $backup_dir/.vim $HOME
70)         fi
David Blume Make setup.sh only backup c...

David Blume authored 8 years ago

71)     fi
David Blume Add BrightScript syntax, an...

David Blume authored 8 years ago

72)     if [ -d $backup_dir/.vim ]; then
David Blume Make the setup script bette...

David Blume authored 6 years ago

73)         echo "# diff -qr \"$backup_dir/.vim\" \"$HOME/.vim\""
David Blume Add BrightScript syntax, an...

David Blume authored 8 years ago

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

David Blume authored 6 years ago

75) else
76)     echo No change to the .vim directories.
David Blume Make setup.sh only backup c...

David Blume authored 8 years ago

77) fi
David Blume first commit

David Blume authored 8 years ago

78) 
David Blume Make setup.sh only backup c...

David Blume authored 8 years ago

79) echo Your old dotfiles are backed up to $backup_dir
David Blume first commit

David Blume authored 8 years ago

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

David Blume authored 8 years ago

83)     ((DRY_RUN==0)) && mkdir -p $HOME/.vim_undo