| •1 |
#!/bin/sh |
| 2 |
|
| 3 |
: ${RCM_LIB:=$(dirname "$0")/../share/rcm} |
| 4 |
. "$RCM_LIB/rcm.sh" |
| 5 |
|
| 6 |
destination() { |
| 7 |
local dotfiles_dir="$1" |
| 8 |
local dotless="$2" |
| 9 |
local in_host="$3" |
| 10 |
local tag="$4" |
| 11 |
|
| 12 |
$DEBUG "destination $dotfiles_dir $dotless $in_host $tag" |
| 13 |
|
| 14 |
if [ "x$tag" != "x" ]; then |
| 15 |
echo "$dotfiles_dir/tag-$tag" |
| 16 |
elif [ $in_host = 1 ]; then |
| 17 |
echo "$dotfiles_dir/host-$HOSTNAME" |
| 18 |
else |
| 19 |
echo "$dotfiles_dir" |
| 20 |
fi |
| 21 |
} |
| 22 |
|
| 23 |
exit_if_dangerous() { |
| 24 |
local file="$1" |
| 25 |
|
| 26 |
if [ -L "$file" ]; then |
| 27 |
$ERROR 1 "'$file' is a symlink. Cannot process file." |
| 28 |
elif is_nested "$file"; then |
| 29 |
# Remove DEST_DIR in case one folder of $HOME is a symlink |
| 30 |
saved_ifs="$IFS" |
| 31 |
IFS=/ |
| 32 |
set -- $(dirname "$file" | sed "s|$DEST_DIR/||") |
| 33 |
IFS="$saved_ifs" |
| 34 |
|
| 35 |
built_dir="$DEST_DIR" |
| 36 |
for dir in $@; do |
| 37 |
built_dir="$built_dir/$dir" |
| 38 |
if [ -L "$built_dir" ]; then |
| 39 |
$ERROR 1 "'$file' path contains a symlink ($dir). Cannot process file." |
| 40 |
fi |
| 41 |
done |
| 42 |
fi |
| 43 |
} |
| 44 |
|
| 45 |
only_one_dir() { |
| 46 |
local dotfiles="$1" |
| 47 |
|
| 48 |
if [ ! -z "$OPT_DOTFILES_DIR" ]; then |
| 49 |
echo_stderr "Only one '-d' option is allowed in mkrc." |
| 50 |
show_help 64 |
| 51 |
else |
| 52 |
OPT_DOTFILES_DIR="$dotfiles" |
| 53 |
fi |
| 54 |
} |
| 55 |
|
| 56 |
show_help() { |
| 57 |
local exit_code=${1:-0} |
| 58 |
|
| 59 |
$PRINT "Usage: mkrc [-ChSsUuVvqokK] [-t TAG] [-d DIR] [-B HOSTNAME] FILES ..." |
| 60 |
$PRINT "see mkrc(1) and rcm(7) for more details" |
| 61 |
|
| 62 |
exit $exit_code |
| 63 |
} |
| 64 |
|
| 65 |
if [ $# -eq 0 ]; then |
| 66 |
show_help 64 |
| 67 |
fi |
| 68 |
|
| 69 |
tag= |
| 70 |
hostname= |
| 71 |
verbosity=0 |
| 72 |
in_host=0 |
| 73 |
version=0 |
| 74 |
always_copy=0 |
| 75 |
force_symlink=50 |
| 76 |
undotted=50 |
| 77 |
install_args= |
| 78 |
|
| 79 |
while getopts :ChSsUuVvqot:d:B:kK opt; do |
| 80 |
case "$opt" in |
| 81 |
C) always_copy=1 ;; |
| 82 |
h) show_help ;; |
| 83 |
t) tag="$OPTARG" ;; |
| 84 |
v) verbosity=$(($verbosity + 1)) ;; |
| 85 |
q) verbosity=$(($verbosity - 1)) ;; |
| 86 |
o) in_host=1 ;; |
| 87 |
d) only_one_dir "$OPTARG" ;; |
| 88 |
V) version=1 ;; |
| 89 |
S) force_symlink=1 ;; |
| 90 |
s) force_symlink=0 ;; |
| 91 |
U) undotted=1 ;; |
| 92 |
u) undotted=0 ;; |
| 93 |
B) |
| 94 |
in_host=1 |
| 95 |
hostname="$OPTARG" |
| 96 |
install_args=$(append_variable "$install_args" "-B $hostname") |
| 97 |
;; |
| 98 |
k) |
| 99 |
install_args=$(append_variable "$install_args" "-k") |
| 100 |
;; |
| 101 |
K) |
| 102 |
install_args=$(append_variable "$install_args" "-K") |
| 103 |
;; |
| 104 |
?) show_help 64 ;; |
| 105 |
esac |
| 106 |
done |
| 107 |
shift $(($OPTIND-1)) |
| 108 |
|
| 109 |
handle_common_flags mkrc $version $verbosity |
| 110 |
HOSTNAME="$(determine_hostname "$hostname")" |
| 111 |
|
| 112 |
if [ $in_host -eq 1 -a "x$tag" != "x" ]; then |
| 113 |
$ERROR 1 "Cannot specify both -o and -t" |
| 114 |
fi |
| 115 |
|
| 116 |
if [ $always_copy -eq 1 ]; then |
| 117 |
INSTALL="$INSTALL -C" |
| 118 |
fi |
| 119 |
|
| 120 |
files="" |
| 121 |
for i; do |
| 122 |
exit_if_dangerous "$i" |
| 123 |
files="$(printf "$files\n$i")" |
| 124 |
done |
| 125 |
|
| 126 |
for DOTFILES_DIR in "$OPT_DOTFILES_DIR" $DOTFILES_DIRS "$DEFAULT_DOTFILES_DIR"; do |
| 127 |
if [ -d "$DOTFILES_DIR" ]; then |
| 128 |
break |
| 129 |
else |
| 130 |
unset DOTFILES_DIR |
| 131 |
fi |
| 132 |
done |
| 133 |
|
| 134 |
if [ -z "$DOTFILES_DIR" ]; then |
| 135 |
$ERROR 1 "No dotfiles directories found in $OPT_DOTFILES_DIR${OPT_DOTFILES_DIR:+ }$DEFAULT_DOTFILES_DIR" |
| 136 |
fi |
| 137 |
|
| 138 |
if [ $force_symlink -eq 1 ]; then |
| 139 |
for file in $files; do |
| 140 |
dedotted="$(de_dot "$file")" |
| 141 |
INSTALL="$INSTALL -S $dedotted" |
| 142 |
done |
| 143 |
elif [ $force_symlink -eq 0 ]; then |
| 144 |
for file in $files; do |
| 145 |
dedotted="$(de_dot "$file")" |
| 146 |
INSTALL="$INSTALL -s $dedotted" |
| 147 |
done |
| 148 |
fi |
| 149 |
|
| 150 |
if [ $undotted -eq 1 ]; then |
| 151 |
for file in $files; do |
| 152 |
dedotted="$(de_dot "$file")" |
| 153 |
INSTALL="$INSTALL -U $dedotted" |
| 154 |
done |
| 155 |
elif [ $undotted -eq 0 ]; then |
| 156 |
for file in $files; do |
| 157 |
dedotted="$(de_dot "$file")" |
| 158 |
INSTALL="$INSTALL -u $dedotted" |
| 159 |
done |
| 160 |
fi |
| 161 |
|
| 162 |
saved_IFS="$IFS" |
| 163 |
IFS=' |
| 164 |
' |
| 165 |
for file in $files; do |
| 166 |
IFS="$saved_IFS" |
| 167 |
case "$file" in |
| 168 |
/*) : ;; |
| 169 |
*) [ -e "$PWD/$file" ] && file="$PWD/$file" ;; |
| 170 |
esac |
| 171 |
|
| 172 |
dotless="$(de_dot "$file")" |
| 173 |
dest="$(destination "$DOTFILES_DIR" "$dotless" $in_host "$tag")" |
| 174 |
mkdir -p "$dest/$(dirname "$dotless")" |
| 175 |
$VERBOSE "Moving..." |
| 176 |
mv_v "$file" "$dest/$dotless" |
| 177 |
$VERBOSE "Linking..." |
| 178 |
$INSTALL -d "$DOTFILES_DIR" -t "${tag:--}" $install_args "$dotless" |
| 179 |
done |