-
Notifications
You must be signed in to change notification settings - Fork 0
/
dw
executable file
·108 lines (99 loc) · 2.56 KB
/
dw
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
#!/usr/bin/env bash
#: Sets up, verifies, and maintains links for dotfiles.
#: For each file listed in ${CONFIG_FILE}, this script will:
#:
#: 1. If the file does not exist, link it to the corresponding file in ${DOTFILE_DIR}.
#: 2. If the file exists and is not a link, move it to ${DOTFILE_DIR}
#: and replace it with a symbolic link.
#: 3. If the file exists and is a link, make sure it's linked to the corresponding file
#: in ${DOTFILE_DIR}.
#:
#: This script is idempotent, so you can use it to
#: set up links for the first time on a new machine,
#: put additional files under dotfile management (add them to ${CONFIG_FILE} first),
#: or simply verify that existing links are correct.
DOTFILE_DIR=~/github/dotWombat
CONFIG_FILE=${DOTFILE_DIR}/config
function linkPath {
if [[ "$1" =~ ^/.* ]]; then
echo $1
else
echo ~/$1
fi
}
function targetPath {
if [[ "$1" =~ ^/.* ]]; then
# echo "DEBUG $1 is an absolute path"
echo ${DOTFILE_DIR}$1 # Don't add a second /
else
# echo "DEBUG $1 is a relative path"
echo ${DOTFILE_DIR}/$1
fi
}
function makeDirFor {
DIR=$(dirname "$1")
if [ -d "${DIR}" ]; then
: # Directory already exists, nothing to do
else
echo "Creating directory ${DIR}"
mkdir -p ${DIR}
fi
}
function moveAndLink {
echo "Moving $1 to $2 and creating link"
makeDirFor $2
if [[ "$1" =~ /.* ]]; then
sudo mv $1 $2 && sudo ln -s $2 $1
else
mv $1 $2 && ln -s $2 $1
fi
}
function createLink {
if [ -e "$2" ]; then
echo "Creating link from $1 to $2"
makeDirFor $1
ln -s $2 $1
else
echo "ERROR: $2 does not exist"
fi
}
function verifyLink {
if [ -e "$2" ]; then
TARGET=`readlink $1`
if [ "${TARGET}" = "$2" ]; then
echo "$1 is properly linked"
else
echo "ERROR: $1 links to ${TARGET} instead of $2"
fi
else
echo "$1 is a symbolic link, but $2 does not exist"
fi
}
function setupLink {
if [ -e "$1" ]; then
#echo "DEBUG $1 exists"
if [ -h "$1" ]; then
#echo "DEBUG $1 is a link"
verifyLink $1 $2
else
#echo "DEBUG $1 is not a link"
if [ -e $2 ]; then
echo "ERROR: $2 exists, but $1 is not a symbolic link"
else
#echo "DEBUG $2 exists"
moveAndLink $1 $2
fi
fi
else
#echo "DEBUG $1 does not exist"
createLink $1 $2
fi
}
while read DOTFILE
do
# echo "Processing ${DOTFILE}"
LINK_NAME=`linkPath ${DOTFILE}`
TARGET=`targetPath ${DOTFILE}`
# echo "DEBUG LINK_NAME=${LINK_NAME} TARGET=${TARGET}"
setupLink ${LINK_NAME} ${TARGET}
done < "${CONFIG_FILE}"