-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_config.sh
executable file
·176 lines (145 loc) · 3.83 KB
/
setup_config.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/bash
usage()
{
echo "Usage: $0 [OPTIONS]"
echo
echo "-h: help"
echo "-v: verbose"
echo "-d: dryrun"
}
run_command()
{
local command_to_run=$1
if [ "$DRY_RUN" = "true" ]; then
echo "(DRY RUN) $command_to_run"
else
[ "$VERBOSE" = "true" ] && echo "$command_to_run"
(eval "$command_to_run")
fi
}
backup_file()
{
local file=$1
local backup_suffix="bkp_by_script"
[[ ! -f "$file" && ! -d "$file" ]] && return 0
[ -h "$file" ] && return 0
[[ -f "$file.$backup_suffix" || -d "$file.$backup_suffix" ]] && return 0
echo "Backup $file"
run_command "mv '$file' '$file.$backup_suffix'"
}
handle_user_policy()
{
local user_policy=$1
local file=$2
if [ "${UID}" != "0" ]; then
if [ "$user_policy" = "$POLICY_ROOT_ONLY" ]; then
[ "$VERBOSE" = "true" ] && echo "$INFO_PREFIX: The file/directory '$file', should only be setup as root"
return 1
fi
else
if [ "$user_policy" = "$POLICY_USER_ONLY" ]; then
[ "$VERBOSE" = "true" ] && echo "$INFO_PREFIX: The file/directory '${file//\/root/\$HOME}', should only be setup as normal user"
return 1
fi
fi
return 0
}
setup_config_dir()
{
local dir=$1
local user_policy=$2
handle_user_policy "$user_policy" "$dir" || return 0
run_command "mkdir -p '$dir'"
}
setup_setup_list()
{
local path=$1
local name=$2
local command_to_execute=$3
local user_policy=$4
handle_user_policy "$user_policy" "$path/$name" || return 0
if [ ! -d "$path/$name" ]; then
echo "Downloading $name plugin/theme into $path/$name"
run_command "cd '$path' && eval '$command_to_execute'"
else
echo "The plugin/theme '$path/$name' already exists"
fi
}
setup_config_file()
{
local source_file=$1
local linked_file=$2
local user_policy=$3
local backup_policy=$4
handle_user_policy "$user_policy" "$linked_file" || return 0
if [ "$backup_policy" = "true" ]; then
backup_file "$linked_file"
fi
if [[ ! -d "$linked_file" || -h "$linked_file" ]]; then
run_command "rm -f '$linked_file'"
else
run_command "rm -rf '$linked_file'"
fi
if [ "${UID}" != "0" ]; then
echo "$INFO_PREFIX: Linking '$linked_file' to '$source_file'"
run_command "ln -s '$source_file' '$linked_file'"
else
echo "$INFO_PREFIX: Copying '$source_file' to '$linked_file'"
if [ -d "$source_file" ]; then
[ "$VERBOSE" = "true" ] && echo "'$source_file' is a directory => 'rsync'"
run_command "rsync -rlptD '$source_file/' '$linked_file/'"
else
[ "$VERBOSE" = "true" ] && echo "'$source_file' is a file => 'cp'"
run_command "cp -f '$source_file' '$linked_file'"
fi
# If we only run this as root, we should make it readable for everyone
[ "$user_policy" = "$POLICY_ROOT_ONLY" ] && run_command "chmod -R go+rX '$linked_file'"
fi
}
DRY_RUN="false"
VERBOSE="false"
while getopts dvh name; do
case $name in
d)
DRY_RUN="true"
;;
v)
VERBOSE="true"
;;
h)
usage
exit 0
;;
?) ;;
esac
done
SCRIPT_ROOT_PATH="$(dirname "$(realpath -s "$0")")"
SCRIPT_HOME_DIR=$(eval echo "~$(stat -c %U "$0")")
INFO_PREFIX="Running as ${USER}"
POLICY_BOTH=0
POLICY_USER_ONLY=1
POLICY_ROOT_ONLY=2
export SCRIPT_ROOT_PATH SCRIPT_HOME_DIR POLICY_BOTH POLICY_USER_ONLY POLICY_ROOT_ONLY
while IFS= read -r line; do
user_policy="${line%|*}"
dir="$(eval echo "${line#*|}")"
setup_config_dir "$dir" "$user_policy"
done < config_dir_list
while IFS= read -r line; do
user_policy="${line%%|*}"
line="${line#*|}"
path="$(eval echo "${line%%|*}")"
line="${line#*|}"
name="${line%|*}"
command_to_execute="${line#*|}"
setup_setup_list "$path" "$name" "$command_to_execute" "$user_policy"
done < config_setup_list
while IFS= read -r line; do
user_policy="${line%%|*}"
line="${line#*|}"
backup_policy="${line%%|*}"
line="${line#*|}"
source_file="$(eval echo "${line%|*}")"
linked_file="$(eval echo "${line#*|}")"
setup_config_file "$source_file" "$linked_file" "$user_policy" "$backup_policy"
done < config_file_list