forked from mathiasbynens/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.sh
executable file
·83 lines (75 loc) · 1.88 KB
/
sync.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env bash
# New folders have to be created manually
fileset=(
.aliases
.bashrc
.exports
.gitconfig
.gitignore
.hushlogin
.mocharc
.profile
.prompt
.vimrc
.vim/colors/
# Legacy files
.bash_profile
.bash_aliases
.gitignore_global
.npm-completion.bash
.node-completion.bash
.git-completion.bash
.mocharc.yml
)
function sync {
read -p "From '$1' to '$2'. Proceed? (y/n) " -r -n 1;
printf '\n'
if [[ $REPLY =~ ^[Yy]$ ]]; then
trash="/tmp/dotfiles-trash-$(date +%F-%s)"
mkdir -p "$trash" 2>/dev/null
for filename in "${fileset[@]}"; do
printf ' %s\n' "$filename"
mv "$2/$filename" "$trash" 2>/dev/null
cp -r "$1/$filename" "$2/$filename" 2>/dev/null
done
printf 'Done.\n'
else
printf 'Aborted.\n'
fi
}
function usage {
echo "Usage:
$ sync --install # Use the dotfiles from this repo.
$ sync --backup # Save the dotfiles from this machine.
$ sync --stash # Save a dated directory from this machine."
exit 0;
}
function main {
# help when no arguments or options
if [[ $# == 0 ]]; then usage; fi
# extract options from arguments
opts=(); while :; do if [[ $1 == -?* ]]; then opts+=("$1"); else break; fi; shift; done
# expects to be run from the dotfiles repo
dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
for opt in "${opts[@]}"; do
case "$opt" in
-i|--install)
printf 'This will overwrite dotfiles in your home directory.\n'
sync "$dir" "$HOME"
break;;
-b|--backup)
printf 'This will backup your dotfiles (without commit).\n'
sync "$HOME" "$dir"
break;;
-s|--stash)
printf 'This will stash your current dotfiles.\n'
stash="$dir/stash-$(date +%F-%s)"
mkdir -p "$stash"
sync "$HOME" "$stash"
break;;
*)
usage;;
esac
done
}
main "$@"