Debian Codegraph @

share/rcm.sh.in

raw
1 VERSION="@PACKAGE_VERSION@"
2
3 #set -x
4
5 DEBUG=:
6 DEST_DIR="$HOME"
7 PRINT=echo
8 PROMPT=echo_n
9 ERROR=echo_error
10 VERBOSE=:
11 DEFAULT_DOTFILES_DIR="$HOME/.dotfiles"
12 MKDIR=mkdir
13 INSTALL=rcup
14 ROOT_DIR="$HOME"
15
16 if [ -z "$LOGNAME" ]; then
17 LOGNAME=$(whoami)
18 fi
19
20 ln_v() {
21 $VERBOSE "'$1' -> '$2'"
22 ln -s "$1" "$2"
23 }
24
25 cp_v() {
26 $VERBOSE "'$1' -> '$2'"
27 cp -R "$1" "$2"
28 }
29
30 rm_v() {
31 $VERBOSE "removed '$2'"
32 rm $1 "$2"
33 }
34
35 mv_v() {
36 $VERBOSE "'$1' -> '$2'"
37 mv "$1" "$2"
38 }
39
40 unset CDPATH
41
42 echo_n() {
43 printf "%s " "$*"
44 }
45
46 echo_error() {
47 local exit_status=$1
48 shift
49 echo "$*" >&2
50 exit $exit_status
51 }
52
53 echo_stderr() {
54 echo "$*" >&2
55 }
56
57 is_relative() {
58 echo "$1" | grep -v '^/' >/dev/null
59 }
60
61 is_nested() {
62 echo "$1" | sed "s|$DEST_DIR/||" | grep '/' >/dev/null
63 }
64
65 version() {
66 cat << EOV
67 $1 (rcm) $VERSION
68 Copyright (C) 2013 Mike Burns
69 Copyright (C) 2014 thoughtbot
70 License BSD: BSD 3-clause license
71
72 Written by Mike Burns.
73 EOV
74 }
75
76 handle_common_flags() {
77 local prog_name="$1"
78 local version="$2"
79 local verbosity=$3
80
81 if [ $version -eq 1 ]; then
82 version "$prog_name"
83 exit 0
84 elif [ $verbosity -ge 2 ]; then
85 DEBUG=echo_stderr
86 VERBOSE=echo
87 PRINT=echo
88 INSTALL="$INSTALL -vv"
89 elif [ $verbosity -eq 1 ]; then
90 DEBUG=:
91 VERBOSE=echo
92 PRINT=echo
93 INSTALL="$INSTALL -v"
94 elif [ $verbosity -eq 0 ]; then
95 DEBUG=:
96 VERBOSE=:
97 PRINT=echo
98 else
99 DEBUG=:
100 VERBOSE=:
101 PRINT=:
102 INSTALL="$INSTALL -q"
103 fi
104 }
105
106 determine_hostname() {
107 local name="$1"
108
109 if [ -n "$name" ]; then
110 echo "$name"
111 elif [ -n "$HOSTNAME" ]; then
112 echo "$HOSTNAME"
113 else
114 echo "$(hostname | sed -e 's/\..*//')"
115 fi
116 }
117
118 run_hooks() {
119 $DEBUG "run_hooks $1 $2"
120 $DEBUG " with DOTFILES_DIRS: $DOTFILES_DIRS"
121 local when="$1"
122 local direction="$2"
123 local hook_file
124 local find_opts=
125
126 if [ $RUN_HOOKS -eq 1 ]; then
127 for dotfiles_dir in $DOTFILES_DIRS; do
128 dotfiles_dir=$(eval echo "$dotfiles_dir")
129
130 hook_file="$dotfiles_dir/hooks/$when-$direction"
131
132 if [ -e "$hook_file" ]; then
133 $VERBOSE "running $when-$direction hooks for $dotfiles_dir"
134
135 if [ x$DEBUG != x: ]; then
136 find_opts=-print
137 fi
138
139 # Emulate the non-POSIX-compliant `-execdir` action with `-exec` and a shell one-liner.
140 # The former is however a bit better when it comes to security. On the other hand
141 # running these hooks imply some level of trust; surely one doesn't clone somebody
142 # else's dotfiles repository without reviewing the hooks before doing an `rcup`?
143 find "$hook_file" -type f \( \( -user $LOGNAME -perm -100 \) -o -perm -001 \) \
144 | sort | while read file; do
145 sh -c 'cd -- "`dirname $1`" && ./"`basename $1`"' arg0 "$file"
146 hook_ret=$?
147
148 if [ "$hook_ret" -ne 0 ]; then
149 echo "$when-$direction hook $file exited non-zero ($hook_ret)" >&2
150 exit $hook_ret # NB. this only exits the while-read; caller must still check and exit
151 fi
152 done
153 else
154 $DEBUG "no $when-$direction hook present for $dotfiles_dir, skipping"
155 fi
156 done
157 fi
158 }
159
160 de_dot() {
161 $DEBUG "de_dot $1"
162 $DEBUG " with DEST_DIR: $DEST_DIR"
163 echo "$1" | sed -e "s|$DEST_DIR/||" | sed -e 's/^\.//'
164 }
165
166 DELIMITER="\a"
167
168 encode() {
169 local file="$1"
170
171 echo "$file" | tr " " "$DELIMITER"
172 }
173
174 decode() {
175 local file="$1"
176
177 echo "$file" | tr "$DELIMITER" " "
178 }
179
180 append_variable() {
181 if [ -z "$1" ]; then
182 echo "$2"
183 else
184 echo "$1 $2"
185 fi
186 }
187
188 : ${RCRC:=$HOME/.rcrc}
189
190 if [ -r "$RCRC" ]; then
191 . "$RCRC"
192 fi