-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·66 lines (51 loc) · 1.19 KB
/
install.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
#!/bin/sh
die ()
{
echo $*
exit 1
}
readlink_r ()
{
test -z "$1" && die "readlink_r need an argument"
path="$1"
while test -h $path;
do
tmp=$(readlink -e $path) || { echo $path; return 1; }
path=$tmp
done
echo $path
}
realpath ()
{
readlink -f "$1"
}
which readlink>/dev/null || die need "readlink" program to run
machine=$1
test -z $machine && die "Usage: $0 <machine>"
top_dir=$(dirname $0)
machine_dir=$(realpath $top_dir/machines/$machine)
test -d $machine_dir || die "invalid machine:'$machine'"
install_conf()
{
test $# -eq 2 || die "invalid install_conf arguments:$*"
src=$machine_dir/$1
dst=$2
test -f $src || return 0
# two files are the same, do nothing
test $(readlink_r $src) = $(readlink_r $dst) && { echo $src and $dst are the same file;return 0; }
if test -f $dst;then
echo "back up old file:$dst"
cp -i $dst $dst.bak
fi
mkdir -p $(dirname $dst)
echo will link $dst to $src
ln -sfn $src $dst
}
for dot_file in $machine_dir/dotfiles/*
do
dot_file=$(basename $dot_file)
install_conf dotfiles/$dot_file ~/.$dot_file
done
if test -f $machine_dir/install.sh; then
. $machine_dir/install.sh
fi