forked from jdonaldson/dotfiles_legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotfiles
executable file
·141 lines (123 loc) · 3.39 KB
/
dotfiles
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
### if you'd like git to automatically 'push' on 'commit'
# echo 'git push' > .git/hooks/post-commit
# chmod 755 .git/hooks/post-commit
set -e
PROG=$(basename $0)
HOSTNAME=$(hostname -s)
cd ${HOME}
# link dotfiles script into ~/bin
if [ ! -d ${HOME}/bin ] ; then mkdir ${HOME}/bin ; fi
if [ ! -L ${HOME}/bin/dotfiles ] ; then ln -s ../.dotfiles/dotfiles ${HOME}/bin/dotfiles ; fi
die() {
echo >&2 $@
exit 1
}
do_file_list() {
find .dotfiles* -type f | grep -v -e '~$' -e '\/\.' -e '^\.dotfiles\/dotfiles$' -e '^\.dotfiles\/README.markdown$' -e ':'
}
do_list() {
find .dotfiles* -type f | cut -d / -f 2- | grep -v -e '~$' -e '^\.' -e '^dotfiles$' -e '^README.markdown$' -e ':'
}
do_status_of() {
local src="$1"
local src_hostname="$src:${HOSTNAME}"
local basename=$(echo "$src" | cut -d / -f 2-)
local dst="${HOME}/.$basename"
[[ "${basename}" == */* ]] && local prefix="../" || local prefix=
if ! test -e "$dst" ; then
echo "."
elif ! test -L "$dst" ; then
echo "file"
else
local cur="$(readlink $dst)"
if test "$cur" = "$prefix$src" ; then
[ -f "$src_hostname" ] && echo "WARNING: $src_hostname exists, but .$basename is not linked to it" || echo "OK"
elif test "$cur" = "$prefix$src_hostname" ; then
echo "OK (linked to ${src_hostname})"
else
echo "link $cur"
fi
fi
}
do_status() {
local name
do_file_list | while read name ; do
local status=$(do_status_of "$name")
printf "%-45s %s\n" "$name" "$status"
done
}
do_install() {
local name
local force
local verbose
local skip=false
local dry
local -a files
while test -n "$1" ; do
local cmd="$1" ; shift
case "$cmd" in
-f) force="-f" ;;
-v) verbose="-v" ;;
-s) skip=true ;;
-*) die "bad option '$cmd'" ;;
*)
[ -e $cmd ] || die "bad option '$cmd'"
files[${#files[@]}]=$cmd
;;
esac
done
[ "${#files[@]}" -gt 0 ] || files=( $( do_file_list ) )
for name in ${files[@]} ; do
local basename=$(echo "$name" | cut -d / -f 2-)
local dirname=$(dirname $basename)
printf "installing %-36s " ".$basename"
local status=$(do_status_of "$name")
if [ -z "$force" ] && [[ "$status" == OK* ]] ; then
echo "... skipped, $status"
else
echo
if [ ! -d ".${dirname}" ] ; then mkdir -p ".${dirname}" ; chmod 700 ".${dirname}" ; fi
[[ "${basename}" == */* ]] && local prefix="../" || local prefix=
[ -f "${name}:${HOSTNAME}" ] && local suffix=:${HOSTNAME} || local suffix=
if ! ( ln $force $verbose -s "${prefix}${name}${suffix}" "${HOME}/.$basename" ) ; then
local rc=$?
$skip || return $rc
fi
fi
done
}
do_help() {
cat <<END
${PROG} - manages symlink files from ~/.dotfiles* to ~/.*
Syntax: ${PROG} [ <command> ] [ <options> ]
Commands:
list - list all files in ~/.dotfiles*
status - status of available files
install [-v] [-f] [-s] - installs symlinks
Options:
-v - be verbose
-f - force updates
-s - skip over existing files
END
}
cmd=$1 ; shift || true
case "$cmd" in
list)
do_list $@
exit $?
;;
status)
do_status $@
exit $?
;;
install)
do_install $@
exit $?
;;
*)
do_help
exit $?
;;
esac
exit 1