1VERSION="1.3.4"
2
3#set -x
4
5DEBUG=:
6DEST_DIR="$HOME"
7PRINT=echo
8PROMPT=echo_n
9ERROR=echo_error
10VERBOSE=:
11DEFAULT_DOTFILES_DIR="$HOME/.dotfiles"
12MKDIR=mkdir
13INSTALL=rcup
14ROOT_DIR="$HOME"
15
16if [ -z "$LOGNAME" ]; then
17  LOGNAME=$(whoami)
18fi
19
20ln_v() {
21  $VERBOSE "'$1' -> '$2'"
22  ln -s "$1" "$2"
23}
24
25cp_v() {
26  $VERBOSE "'$1' -> '$2'"
27  cp -R "$1" "$2"
28}
29
30rm_v() {
31  $VERBOSE "removed '$2'"
32  rm $1 "$2"
33}
34
35mv_v() {
36  $VERBOSE "'$1' -> '$2'"
37  mv "$1" "$2"
38}
39
40unset CDPATH
41
42echo_n() {
43  printf "%s " "$*"
44}
45
46echo_error() {
47  local exit_status=$1
48  shift
49  echo "$*" >&2
50  exit $exit_status
51}
52
53echo_stderr() {
54  echo "$*" >&2
55}
56
57is_relative() {
58  echo "$1" | grep -v '^/' >/dev/null
59}
60
61is_nested() {
62  echo "$1" | sed "s|$DEST_DIR/||" | grep '/' >/dev/null
63}
64
65version() {
66  cat << EOV
67$1 (rcm) $VERSION
68Copyright (C) 2013 Mike Burns
69Copyright (C) 2014 thoughtbot
70License BSD: BSD 3-clause license
71
72Written by Mike Burns.
73EOV
74}
75
76handle_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
106determine_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
118run_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          done
147      else
148        $DEBUG "no $when-$direction hook present for $dotfiles_dir, skipping"
149      fi
150    done
151  fi
152}
153
154de_dot() {
155  $DEBUG "de_dot $1"
156  $DEBUG "  with DEST_DIR: $DEST_DIR"
157  echo "$1" | sed -e "s|$DEST_DIR/||" | sed -e 's/^\.//'
158}
159
160DELIMITER="\a"
161
162encode() {
163  local file="$1"
164
165  echo "$file" | tr " " "$DELIMITER"
166}
167
168decode() {
169  local file="$1"
170
171  echo "$file" | tr "$DELIMITER" " "
172}
173
174append_variable() {
175  if [ -z "$1" ]; then
176    echo "$2"
177  else
178    echo "$1 $2"
179  fi
180}
181
182: ${RCRC:=$HOME/.rcrc}
183
184if [ -r "$RCRC" ]; then
185  . "$RCRC"
186fi
187