-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall
More file actions
executable file
·151 lines (128 loc) · 4.48 KB
/
Copy pathinstall
File metadata and controls
executable file
·151 lines (128 loc) · 4.48 KB
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
#!/bin/sh
# Copyright © 2009-12, 2014-15, 2017, 2021 Jonathan Rascher.
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
#
# This is an attempt at porting Alan Pinstein's and Ryan Bates's dotfile
# installation utility [1] from a Ruby Rakefile to a POSIX shell script. Usage
# is pretty simple. Symbolic links to any normal files in this directory that
# don't have a ".local" extension will be created in the current user's home
# directory. Additionally, any file that does have a ".local" extension will be
# copied to (not linked in) the current user's home directory, provided that
# there is not an existing file with the same name in the home directory. The
# idea is that ".local" files will be used to store per-host settings and things
# that should not be placed into version control (e.g., for privacy reasons),
# while non-".local" files will store universal, public settings. Finally, all
# new symbolic links or copied files will be created with a dot prefixed to
# their names, e.g., a file named `foobar` will be installed with the name
# `~/.foobar`.
#
# Additionally, dotfiles in arbitrary subdirectories are now supported. The
# rules regarding local and nonlocal files also apply to files in
# subdirectories. The dot prefixing behavior described above is usually applied
# to files in subdirectories as well; however, if the highest subdirectory in a
# file's path is named `fortunes` or `texmf`, then no prefix is applied.
# Therefore, a file named `cows/ducks` will be installed with the name
# `~/.cows/ducks`, whereas a file named `fortunes/hello` will be installed as
# `~/fortunes/hello`.
#
# Links:
# 1. Pinstein's original Rakefile is here <http://bit.ly/7WITQW>.
# XXX: Do not change this to a newer $(...) style command substitution; that
# inexplicably breaks old versions of bash 3.
script=`cat <<'EOT'
dont_replace () {
what=$1
file=$2
printf '%s\n' "$what $file already exists. Replace? [y/N]"
read answer
if [ x"$answer" = xy ] || [ x"$answer" = xY ]; then
rm -rf "$file"
false
else
true
fi
}
handle_dir () {
dest=$1
if [ -d "$dest" ]; then
[ x"$verbose" = x1 ] \
&& printf '%s\n' "Directory $dest already exists; skipping"
return
fi
[ -e "$dest" ] && dont_replace Directory "$dest" && return
printf '%s\n' "Creating directory $dest."
mkdir "$dest"
}
handle_file () {
dest=$1
src=$2
ext=$(printf %s "$src" | sed -e 's/.*\.\(.*\)/\1/' -e t -e d)
if [ x"$ext" = xlocal ]; then
dest=$(printf %s "$dest" | cut -b -$(( ${#dest} - ${#ext} - 1 )))
else
if [ "$(readlink "$dest")" = "$src" ]; then
[ x"$verbose" = x1 ] \
&& printf '%s\n' "Symbolic link $dest already exists; skipping"
return
fi
fi
[ -e "$dest" ] && dont_replace File "$dest" && return
if [ x"$ext" = xlocal ]; then
printf '%s\n' "Copying file $src to $dest"
cp "$src" "$dest"
chmod g-rwx,o-rwx "$dest"
else
printf '%s\n' "Symbolically linking $dest to $src"
ln -s "$src" "$dest"
fi
}
lbrace={
rbrace=}
check_symlinks=
verbose=
while getopts cv opt; do
case $opt in
c) check_symlinks=1;;
v) verbose=1;;
\?) exit 1;;
esac
done
shift $(expr $OPTIND - 1)
for src; do
src=$(printf %s "$src" | sed 's:^\./::')
if printf %s "$src" | grep -E '^(fortunes|texmf)($|/)' >/dev/null; then
dest=~/$src
else
dest=~/.$src
fi
if [ -d "$src" ]; then
handle_dir "$dest"
elif [ -r "$src" ]; then
handle_file "$dest" "$PWD/$src"
fi
done
if [ x"$check_symlinks" = x1 ]; then
broken_symlink_script=$(cat <<'EOU'
for symlink; do
printf '%s\n' "Broken symbolic link (deleted dotfile?) $symlink"
done
EOU
)
find -L ~ -type l \
-exec sh -c "$broken_symlink_script" "$0" "$lbrace$rbrace" +
fi
EOT
`
find . \! \( -path . -o -path ./README -o -path ./install \) -a \
\! \( -name '.git*' -prune -o -exec sh -c "$script" "$0" "$@" {} + \)