summaryrefslogtreecommitdiff
path: root/bin/mkrc
blob: d99cf36280cdb399354452a6057fd802345d5ae0 (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
#!/bin/sh

: ${RCM_LIB:=$(dirname "$0")/../share/rcm}
. "$RCM_LIB/rcm.sh"

destination() {
  local dotfiles_dir="$1"
  local dotless=$2
  local in_host=$3
  local tag="$4"

  if [ "x$tag" != "x" ]; then
    echo "$dotfiles_dir/tag-$tag"
  elif [ $in_host = 1 ]; then
    echo "$dotfiles_dir/host-$HOSTNAME"
  else
    echo "$dotfiles_dir"
  fi
}

show_help() {
  local exit_code=${1:-0}

  $PRINT "Usage: mkrc [-hvqo] [-t TAG] [-d DIR] FILES ..."
  $PRINT "see mkrc(1) and rcm(5) for more details"

  exit $exit_code
}

if [ $# -eq 0 ]; then
  show_help 1
fi

for DOTFILES_DIR in $DOTFILES_DIRS $DEFAULT_DOTFILES_DIR; do
  break
done

tag=
verbosity=0
in_host=0
version=0
always_copy=0
force_symlink=0

while getopts ChSsVvqot:d: opt; do
  case "$opt" in
    C) always_copy=1 ;;
    h) show_help ;;
    t) tag="$OPTARG" ;;
    v) verbosity=$(($verbosity + 1)) ;;
    q) verbosity=$(($verbosity - 1)) ;;
    o) in_host=1 ;;
    d) DOTFILES_DIR="$OPTARG" ;;
    V) version=1 ;;
    S) force_symlink=1 ;;
    s) force_symlink=0 ;;
  esac
done
shift $(($OPTIND-1))

handle_common_flags mkrc $version $verbosity

if [ $in_host -eq 1 -a "x$tag" != "x" ]; then
  $ERROR 1 "Cannot specify both -o and -t"
fi

if [ $always_copy -eq 1 ]; then
  INSTALL="$INSTALL -C"
fi

files=$@

if [ $force_symlink -eq 1 ]; then
  for file in $files; do
    dedotted="$(de_dot "$file")"
    INSTALL="$INSTALL -S $dedotted"
  done
fi

for file in $files; do
  dotless="$(de_dot "$file")"
  dest="$(destination "$DOTFILES_DIR" "$dotless" $in_host "$tag")"
  mkdir -p "$dest/$(dirname "$dotless")"
  $PRINT "Moving..."
  $MV "$file" "$dest/$dotless"
  $PRINT "Linking..."
  $INSTALL -d "$DOTFILES_DIR" -t "${tag:--}" "$dotless"
done