summaryrefslogtreecommitdiff
path: root/share/rcm.sh.in
blob: 6d8a7e01e4dceb6ee552548b5b722025493d2787 (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
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"
HOSTNAME="$(hostname | sed -e 's/\..*//')"

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 -vq '^/'
}

version() {
  cat << EOV
$1 (rcm) $VERSION
Copyright (C) 2013 Mike Burns
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
}

handle_metadata_flags() {
  local arg_tags="$1"
  local dotfiles_dirs="$2"

  : ${TAGS:=$arg_tags}
  : ${DOTFILES_DIRS:=$dotfiles_dirs}

  $DEBUG "TAGS: $TAGS"
  $DEBUG "DOTFILES_DIRS: $DOTFILES_DIRS"
}

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
      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

        find "$hook_file" -type f -perm -111 $find_opts -exec {} \;
      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/^\.//'
}

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

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