-
Notifications
You must be signed in to change notification settings - Fork 0
/
link.sh
executable file
·134 lines (117 loc) · 2.32 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/bash
error() {
echo_red "ERROR: $1\n"
exit 1
}
echo_green() {
echo -e "\e[32m$1\e[39m"
}
echo_yellow() {
echo -e "\e[33m$1\e[39m"
}
echo_red() {
echo -ne "\e[31m$1\e[39m"
}
echo_blue() {
echo -e "\e[34m$1\e[39m"
}
case "$OSTYPE" in
"linux"*)
OS=linux
;;
"darwin"*)
OS=osx
;;
*)
error "Unknown operating system"
;;
esac
# if hash X; then
# XORG=yes
# else
# XORG=no
# fi
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-f|--force)
ARG_FORCE=1
shift # past argument
;;
--default)
shift # past argument
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
ask_remove() {
# if [ ${ARG_FORCE} ];then
# return
# fi
echo_red " - File/directory already exists, remove $1 (y/n)? "
local answer=0
read -r answer < /dev/tty
case ${answer:0:1} in
y|Y )
local target=${1%/}
if [ -L "$1" ];then
rm -rf "$target"
else
mv "$target" "$target".bak
fi
;;
* )
;;
esac
}
link_file() {
local source=$1
local target=${2%/}
if [ ! -e "$source" ];then
echo_red "$source does not exist"
return
fi
# if [ ! -e "$target" ];then
# rm -rf "$target"
# fi
if [ ! -e "$target" ];then
echo_red "$target\n"
ask_remove "$target"
fi
if [ -e "$target" ];then
if [ "$(readlink -f "$target")" != "$source" ];then
echo_red "$target\n"
ask_remove "$target"
fi
fi
if [ -s "$target" ];then
if [[ $(realpath "$target") =~ $HOME/.dotfiles ]];then
echo_yellow " Skipping ${source} -> ${target}, already linked."
return
fi
fi
if [ ! -e "$target" ];then
echo_green " $source -> $target"
if [ ! -d "$(dirname "$target")" ];then
mkdir -p "$(dirname "$target")"
fi
ln -s "$source" "$target"
fi
}
echo_blue "** Linking files"
while IFS=\; read -r from_path to_path; do
link_file "$DIR/$from_path" "$HOME/$to_path"
done < files
if [ -f files.$OS ];then
echo_blue "** Linking $OS specific files"
while IFS=\; read -r from_path to_path; do
link_file "$DIR/$from_path" "$HOME/$to_path"
done < files.$OS
fi