summaryrefslogtreecommitdiff
path: root/bin/mkrc.in
blob: 5628f14ba76b24e97c3d2bcb1b5161cb8c8ebcdc (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
#!@SHELL@

: ${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"

  $DEBUG "destination $dotfiles_dir $dotless $in_host $tag"

  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
}

exit_if_dangerous() {
  local file="$1"

  if [ -L "$file" ]; then
      $ERROR 1 "'$file' is a symlink. Cannot process file."
  elif is_nested "$file"; then
    # Remove DEST_DIR in case $HOME is under a symlink
    saved_ifs="$IFS"
    IFS=/
    set -- $(dirname "$file" | sed "s|$DEST_DIR/||")
    IFS="$saved_ifs"

    built_dir="$DEST_DIR"
    for dir in $@; do
      built_dir="$built_dir/$dir"
      if [ -L "$built_dir" ]; then
        $ERROR 1 "'$file' path contains a symlink ($dir). Cannot process file."
      fi
    done
  fi
}

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

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

  exit $exit_code
}

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

for DOTFILES_DIR in $DOTFILES_DIRS $DEFAULT_DOTFILES_DIR; do
  break
done

tag=
hostname=
verbosity=0
in_host=0
version=0
always_copy=0
force_symlink=50
undotted=50
install_args=

while getopts :ChSsUuVvqot:d:B: 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 ;;
    U) undotted=1 ;;
    u) undotted=0 ;;
    B)
      in_host=1
      hostname="$OPTARG"
      install_args=$(append_variable "$install_args" "-B $hostname")
      ;;
    ?) show_help 64 ;;
  esac
done
shift $(($OPTIND-1))

handle_common_flags mkrc $version $verbosity
HOSTNAME="$(determine_hostname "$hostname")"

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=""
for i; do
  exit_if_dangerous "$i" 
  files="$(printf "$files\n$i")"
done

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

if [ $undotted -eq 1 ]; then
  for file in $files; do
    dedotted="$(de_dot "$file")"
    INSTALL="$INSTALL -U $dedotted"
  done
elif [ $undotted -eq 0 ]; then
  for file in $files; do
    dedotted="$(de_dot "$file")"
    INSTALL="$INSTALL -u $dedotted"
  done
fi

saved_IFS="$IFS"
IFS='
'
for file in $files; do
  IFS="$saved_IFS"
  case "$file" in
    /*) : ;;
    *) [ -e "$PWD/$file" ] && file="$PWD/$file" ;;
  esac

  dotless="$(de_dot "$file")"
  dest="$(destination "$DOTFILES_DIR" "$dotless" $in_host "$tag")"
  mkdir -p "$dest/$(dirname "$dotless")"
  $VERBOSE "Moving..."
  mv_v "$file" "$dest/$dotless"
  $VERBOSE "Linking..."
  $INSTALL -d "$DOTFILES_DIR" -t "${tag:--}" $install_args "$dotless"
done