-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbootstrap.sh
executable file
·66 lines (56 loc) · 1.87 KB
/
bootstrap.sh
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
#!/bin/bash
set -eu
# Set up soft links from files to their destination (in home directory)
# Note: /bin/bash is required for ~/.* expansion in loop below
# Can't use something like 'readlink -e $0' because that doesn't work everywhere
# And HP doesn't define $PWD in a sudo environment, so we define our own
case $0 in
/*|~*)
SCRIPT_INDIRECT="$(dirname "$0")"
;;
*)
PWD="`pwd`"
SCRIPT_INDIRECT="$(dirname "$PWD/$0")"
;;
esac
BASEDIR="$(cd "$SCRIPT_INDIRECT" ; pwd -P)"
for i in "$BASEDIR"/*; do
[ ! -d "$i" ] && continue
for j in "$i"/*; do
FILEDIR="$(dirname "$j")"
FILE="$(basename "$j")"
BASEFILE="$HOME/.$FILE"
if [ -h "$BASEFILE" ]; then
echo "Updating link : $BASEFILE"
rm "$BASEFILE"
elif [ -d "$BASEFILE" ]; then
echo "Replacing directory: $BASEFILE (saving old version)"
SAVE_NAME="$BASEFILE.dotfiles.sav"
if [ -e "$SAVE_NAME" ]; then
SAVE_NAME="$SAVE_NAME.$(date +'%s')"
fi
mv "$BASEFILE" "$SAVE_NAME"
elif [ -e "$BASEFILE" ]; then
# if it exists but isn't a directory or a link,
# assume it is a file. Doesn't seem worth it
# to try to handle other things (e.g. unix sockets)
# specially.
echo "Replacing file: $BASEFILE"
rm "$BASEFILE"
else
echo "Creating link: $BASEFILE"
fi
ln -s "$j" "$BASEFILE"
done
done
# Make a pass deleting stale links, if any
for i in ~/.*; do
[ ! -h "$i" ] && continue
# We have a link: Is it stale? If so, delete it ...
# Since we can't use readlink, assume that if the link is
# not pointing to a file or a directory that it is stale.
if [ ! -f "$i" -a ! -d "$i" ]; then
echo "Deleting stale link: $i"
rm "$i"
fi
done