-
Notifications
You must be signed in to change notification settings - Fork 5
/
linkfiles
executable file
·83 lines (76 loc) · 1.83 KB
/
linkfiles
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
#!/bin/bash
set -e
linkdirs=( ".config/awesome" ".urxvt" ".config/i3" )
copydirs=( "etc/udev/rules.d" "etc/systemd/system" )
cleanuppath() {
if test -L "$1"; then
rm "$1"
elif test -e "$1"; then
rm "$1"
fi
}
islinkdir() {
for linkdir in "${linkdirs[@]}"; do
if [[ "$file" == $linkdir ]]; then
return 0
fi
done
return 1
}
join_by() {
local IFS="$1"; shift; echo "$*";
}
linkfile() {
cmd="ln -s"
sourcepath="$1"
target="$2"
file="$3"
fullpath="$1/$file"
targetpath="$target/$file"
cleanuppath "$targetpath"
for copydir in "${copydirs[@]}"; do
if [[ "$file" == $copydir* ]]; then
cmd="cp"
break
fi
done
$cmd "$fullpath" "$target/$file"
}
linkfiles() {
if ! test -e $1; then
echo "Source $1 does not exists"
return
fi
target="$2"
pushd "$1" > /dev/null
for file in $(find -type d -printf '%P\n'); do
if ! islinkdir $file; then
mkdir -p "$target/$file"
fi
done
skipme=$(join_by \| "${linkdirs[@]}")
for file in $(find -type f -printf '%P\n' | grep -E -v "($skipme)"); do
linkfile "$1" "$target" "$file"
done
for linkdir in "${linkdirs[@]}"; do
if ! test -e "$linkdir"; then
echo "Dir $linkdir does not exists skipping"
continue
fi
linkfile "$1" "$target" "$linkdir"
done
popd > /dev/null
}
ROOT=""
scripdir="$(dirname $(readlink -f "$0"))"
if [ "$1" == "--root" ]; then
if [ "$UID" != "0" ]; then
echo "--root requires you to be root"
exit 1
fi
linkfiles "${scripdir}/osconfig" "/"
linkfiles "${scripdir}/overlay/osconfig" "/"
else
linkfiles "${scripdir}/homeconfig" "$HOME"
linkfiles "${scripdir}/overlay/homeconfig" "$HOME"
fi