a9dff907b4f0b00693f3a3977ca598688cfd8bab
David Blume Improve setup.sh portability.

David Blume authored 9 years ago

1) #!/usr/bin/env bash
David Blume first commit

David Blume authored 9 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 Add i3 configs

dblume authored 1 year 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 9 years ago

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

David Blume authored 6 years ago

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

David Blume authored 9 years ago

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

David Blume authored 9 years ago

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

David Blume authored 8 years ago

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:
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
David Blume Minor tweaks to config file...

David Blume authored 8 years 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
David Blume Minor tweaks to config file...

David Blume authored 8 years ago

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

David Blume authored 6 years ago

48)                 echo "# vimdiff \"$HOME/$i\" \"$backup_dir/$i\""
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 Add key bindings for gitui

dblume authored 2 years ago

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

David Blume authored 9 years ago

60)     fi
61) done
David Blume first commit

David Blume authored 9 years ago

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

David Blume authored 6 years ago

63) if ! diff -qr $HOME/.vim .vim > /dev/null ; then
64)     if [ $DRY_RUN -eq 0 ]; then
65)         if [ -d $HOME/.vim ]; then
66)             mv $HOME/.vim $backup_dir
67)         fi
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
72)             cp -rnv $backup_dir/.vim $HOME | grep " -> " | cut -d " " -f3 | \
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
David Blume Update some bash logic when...

David Blume authored 6 years 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
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 9 years ago

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

David Blume authored 8 years ago

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

David Blume authored 9 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
98)     if [ ! -f $HOME/"$i" ]; then
99)         echo Consider copying $i from a similar acct. \(\'touch \"\$HOME/$i\"\' to hide this msg.\)
100)     fi
101) done
102)