-
Notifications
You must be signed in to change notification settings - Fork 4
/
dot-files
executable file
·124 lines (109 loc) · 2.25 KB
/
dot-files
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
#!/bin/bash
set -e
prog=$0
path=$(dirname $prog)
base=$(basename $prog)
cd $path
die() {
echo >&2 $@
exit 1
}
do_file_list() {
find . -maxdepth 1 | cut -d / -f 2- | grep -v -e '~$' -e '^\.' -e '^dot-files$'
}
do_list() {
do_file_list
}
do_status_of() {
local name="$1"
local src="etc/$name"
local dst="$HOME/.$name"
if ! test -e "$dst" ; then
echo "."
elif ! test -L "$dst" ; then
echo "file"
else
local cur="$(readlink $dst)"
if test "$cur" = "$src" ; then
echo "OK"
else
echo "link $cur"
fi
fi
}
do_status() {
local name
do_file_list | while read name ; do
local status=$(do_status_of "$name")
printf "%-30s %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
printf "installing %-30s " ".$name"
local status=$(do_status_of "$name")
if [ -z "$force" -a "$status" == "OK" ] ; then
echo "... skipped, $status"
else
echo
if ! ( ln $force $verbose -s "etc/$name" "$HOME/.$name" ) ; then
local rc=$?
$skip || return $rc
fi
fi
done
}
do_help() {
cat <<END
dot-files - manages symlink files from ~/etc/* to ~/.*
Syntax: $base [ <command> ] [ <options> ]
Commands:
list - list all etc files
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