-
Notifications
You must be signed in to change notification settings - Fork 0
/
mklinks.sh
executable file
·94 lines (84 loc) · 1.99 KB
/
mklinks.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
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
#!/bin/bash
function loginfo() {
if [ -z "$errors_only" ]; then
echo "$1"
fi
}
function logerror() {
echo "$1"
ret=1
}
function linkdir() {
local base=$1
local rel=$2
local relprefix=$3
local home=$4
local pad=$5
local fullbase=$base$rel
local relpath=${fullbase##$home/}
local relhome=$home$rel
cd $home
loginfo "${pad}Checking symlinks in $relhome relative to $relpath..."
if [ ! -d $relhome ]; then
loginfo "${pad} $relhome doesn't exist, $creating"
mkdir $relhome
fi
cd $fullbase
local files=*
for file in $files; do
case $file in
. | .. )
;;
README | mklinks.sh | .git | .nolink | .gitignore )
;;
* )
cd $relhome
local target=$relprefix$relpath$file
pre="${pad}Checking $file for link to $target..."
if [ -d $target -a -f $target/.nolink ]; then
loginfo "$pre directory with .nolink, recursing."
linkdir $base $rel$file/ "../$relprefix" $home "$pad "
cd $relhome
elif [ -h $file ]; then
local existing=$(readlink $file)
if [ "$existing" == "$target" ]; then
loginfo "$pre OK (already existing)"
else
logerror "$pre ERROR: already links to $existing"
fi
elif [ -e $file ]; then
if [ -z "$force" ]; then
logerror "$pre ERROR: already exists"
else
loginfo "$pre already exists, -f supplied: deleting and $creating"
if [ -z "$dryrun" ]; then
rm $file
ln -s $target $file
fi
fi
else
logerror "$pre$creating"
if [ -z "$dryrun" ]; then
ln -s $target $file
fi
fi
esac
done
}
dryrun=''
creating='creating'
errors_only=''
force=''
ret=0
while getopts 'efn' flag; do
case "${flag}" in
e) errors_only='true' ;;
f) force='true' ;;
n) dryrun='true' creating='not creating (DRY-RUN)' ;;
esac
done
BASEPATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
shopt -s dotglob
linkdir $BASEPATH / "" $HOME ""
chmod 700 ~/.ssh/control
exit $ret