summaryrefslogtreecommitdiff
path: root/share/rcm.sh.in
blob: a2b44922204f888ecc8ba1d59bf5064fc20d75bc (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
VERSION="@PACKAGE_VERSION@"

#set -x

DEBUG=:
DEST_DIR="$HOME"
PRINT=echo
PROMPT=echo_n
ERROR=echo_error
VERBOSE=:
DEFAULT_DOTFILES_DIR="$HOME/.dotfiles"
MKDIR=mkdir
INSTALL=rcup
ROOT_DIR="$HOME"

if [ -z "$LOGNAME" ]; then
  LOGNAME=$(whoami)
fi

ln_v() {
  $VERBOSE "'$1' -> '$2'"
  ln -s "$1" "$2"
}

cp_v() {
  $VERBOSE "'$1' -> '$2'"
  cp -R "$1" "$2"
}

rm_v() {
  $VERBOSE "removed '$2'"
  rm $1 "$2"
}

mv_v() {
  $VERBOSE "'$1' -> '$2'"
  mv "$1" "$2"
}

unset CDPATH

echo_n() {
  printf "%s " "$*"
}

echo_error() {
  local exit_status=$1
  shift
  echo "$*" >&2
  exit $exit_status
}

echo_stderr() {
  echo "$*" >&2
}

is_relative() {
  echo "$1" | grep -v '^/' >/dev/null
}

version() {
  cat << EOV
$1 (rcm) $VERSION
Copyright (C) 2013 Mike Burns
Copyright (C) 2014 thoughtbot
License BSD: BSD 3-clause license

Written by Mike Burns.
EOV
}

handle_common_flags() {
  local prog_name="$1"
  local version="$2"
  local verbosity=$3

  if [ $version -eq 1 ]; then
    version "$prog_name"
    exit 0
  elif [ $verbosity -ge 2 ]; then
    DEBUG=echo_stderr
    VERBOSE=echo
    PRINT=echo
    INSTALL="$INSTALL -vv"
  elif [ $verbosity -eq 1 ]; then
    DEBUG=:
    VERBOSE=echo
    PRINT=echo
    INSTALL="$INSTALL -v"
  elif [ $verbosity -eq 0 ]; then
    DEBUG=:
    VERBOSE=:
    PRINT=echo
  else
    DEBUG=:
    VERBOSE=:
    PRINT=:
    INSTALL="$INSTALL -q"
  fi
}

determine_hostname() {
  local name="$1"

  if [ -n "$name" ]; then
    echo "$name"
  elif [ -n "$HOSTNAME" ]; then
    echo "$HOSTNAME"
  else
    echo "$(hostname | sed -e 's/\..*//')"
  fi
}

run_hooks() {
  $DEBUG "run_hooks $1 $2"
  $DEBUG "  with DOTFILES_DIRS: $DOTFILES_DIRS"
  local when="$1"
  local direction="$2"
  local hook_file
  local find_opts=

  if [ $RUN_HOOKS -eq 1 ]; then
    for dotfiles_dir in $DOTFILES_DIRS; do
      dotfiles_dir=$(eval echo "$dotfiles_dir")

      hook_file="$dotfiles_dir/hooks/$when-$direction"

      if [ -e "$hook_file" ]; then
        $VERBOSE "running $when-$direction hooks for $dotfiles_dir"

        if [ x$DEBUG != x: ]; then
          find_opts=-print
        fi

        # Emulate the non-POSIX-compliant `-execdir` action with `-exec` and a shell one-liner.
        # The former is however a bit better when it comes to security. On the other hand
        # running these hooks imply some level of trust; surely one doesn't clone somebody
        # else's dotfiles repository without reviewing the hooks before doing an `rcup`?
        find "$hook_file" -type f \( \( -user $LOGNAME -perm -100 \) -o -perm -001 \) \
          | sort | while read file; do
            sh -c 'cd -- "`dirname $1`" && ./"`basename $1`"' arg0 "$file"
          done
      else
        $DEBUG "no $when-$direction hook present for $dotfiles_dir, skipping"
      fi
    done
  fi
}

de_dot() {
  $DEBUG "de_dot $1"
  $DEBUG "  with DEST_DIR: $DEST_DIR"
  echo "$1" | sed -e "s|$DEST_DIR/||" | sed -e 's/^\.//'
}

DELIMITER="\a"

encode() {
  local file="$1"

  echo "$file" | tr " " "$DELIMITER"
}

decode() {
  local file="$1"

  echo "$file" | tr "$DELIMITER" " "
}

: ${RCRC:=$HOME/.rcrc}

if [ -r "$RCRC" ]; then
  . "$RCRC"
fi