-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlink.sh
executable file
·80 lines (67 loc) · 2.2 KB
/
link.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
#!/bin/bash
# settings
gitdir="$HOME/dotfiles"
source "$gitdir/config.sh"
dryrun="${dryrun:-1}" # 0 for real run
#### MAIN ####
function is_real_file() {
local path="$1"
# check if it
[ -e "$path" ] && ! [ -L "$path" ] && return 1
return 0
}
function create_link() {
# creates a link for file $1
origfile="$1"
origfile_basename="${1##*/}"
# path relativ to git dir
relpath="$1"
absolutepath="$gitdir/$relpath"
targetpath="$relpath"
for i in ${!relpath2target[@]} ; do
#echo sed "${relpath2target[$i]}"
targetpath=$(echo "$targetpath"|sed "${relpath2target[$i]}")
done
if ! [ -e "$absolutepath" ] ; then
echo -e "${color_skip} SKIPPING ${color_source} $origfile" \
"${color_default}because it does not exist"
return 1
fi
if ! is_real_file "$targetpath" ; then
echo -e "${color_skip} SKIPPING ${color_source} $origfile" \
"${color_default}because ${color_target}$targetpath" \
"${color_default}is real file"
else
origfile_aligned=$(printf "%${origfile_width}s" "$origfile")
echo -e "$color_link LINKING $color_source $origfile_aligned" \
"$color_link=> $color_target$targetpath$color_default"
[ "$dryrun" = 0 ] && ln -n --relative -f -s "$absolutepath" "$targetpath" || return 1
fi
return 0
}
create_directories() {
for d in "${createdirectories[@]}" ; do
if [[ -d "$HOME/$d" ]] ; then
echo -e "$color_link EXISTS $color_source ~/$d$color_default"
elif [[ -e "$HOME/$d" ]] ; then
echo -e "$color_skip ERROR: $color_source ~/$d" \
"$color_skip not a directory!$color_default"
else
echo -e "$color_link MKDIR -p $color_source ~/$d$color_default"
[ "$dryrun" = 0 ] && mkdir -p "$HOME/$d" || return 1
fi
done
}
create_directories
if [ "$1" = "-" ] ; then
while read i ; do
i=$(echo "$i"|sed "s#/*\$##") # remove all slashes at the end
create_link "$i"
done
else
for i in $* ; do
i=$(echo "$i"|sed "s#/*\$##") # remove all slashes at the end
create_link "$i"
done
fi
exit 0