-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathINSTALL
executable file
·208 lines (176 loc) · 4.92 KB
/
INSTALL
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env bash
#
# FILE: INSTALL
#
# ABSTRACT: Install QC files from working copy and update crontab & .bashrc
#
# AUTHOR: Ralf Schandl
#
script_dir="$(cd "$(dirname "$0")" && pwd)"
script_name="$(basename "$0")"
cd "$script_dir" || exit 1
QC_DIR="$HOME/.qc"
execute()
{
if [ -n "$NO_OP" ]; then
echo " $*"
else
"$@"
fi
}
do_install()
{
local fqfn="$1"
local tgt="$2"
if [ -e "$tgt" ]; then
if [ "$mode" = "symlink" ] && [ -L "$tgt" ]; then
tgtln="$(realpath "$tgt")"
if [ "$fqfn" = "$tgtln" ]; then
echo "Up-to-date: $tgt"
return 0
fi
fi
if [ -z "$FORCE" ]; then
echo >&2 "ERROR: File exists: $tgt"
echo >&2 " Use '-f' to force overwrite."
echo >&2 " Or try '-h' to get help."
echo >&2 ""
exit 1
fi
fi
if [ "$mode" = "copy" ]; then
echo "Copying $fqfn -> $tgt"
execute cp -f "$fqfn" "$tgt"
else
echo "Linking $tgt -> $fqfn"
execute ln -fs "$fqfn" "$tgt"
fi
}
install_rcfile()
{
typeset fn="$1"
typeset tgt_dir="$2"
typeset bn="${fn#_}"
# shellcheck disable=SC2155 # expect no error from realpath
typeset fqfn="$(realpath "$fn")"
typeset tgt="$tgt_dir/.$bn"
do_install "$fqfn" "$tgt"
}
install_file()
{
typeset fn="$1"
typeset tgt_dir="$2"
# shellcheck disable=SC2155 # expect no error from realpath
typeset fqfn="$(realpath "$fn")"
typeset tgt="$tgt_dir/$fn"
do_install "$fqfn" "$tgt"
}
add_to_init_file()
{
typeset file="$1"
if [ ! -e "$file" ]; then
echo "SKIPPED: File does not exist: $file"
echo
return
fi
if grep "quick_change_directory.sh" "$file" >/dev/null 2>&1; then
echo "SKIPPED: The file quick_change_directory.sh seems to be already sourced from $file:"
grep "quick_change_directory.sh" "$file" | sed "s/^/ /"
echo "Please check that the path to the file is correct."
echo
else
echo "Adding quick_change_directory.sh to $file..."
typeset dir="${QC_DIR/#$HOME/\$HOME}"
# to complex for execute function (because of redir)
if [ -n "$NO_OP" ]; then
if [ "$QC_DIR" != "$HOME/.qc" ]; then
echo " echo \"export QC_DIR=\\\"$dir\\\"\" >> $file"
fi
echo " echo \"[ -f \\\"$dir/quick_change_directory.sh\\\" ] && . \\\"$dir/.qc/quick_change_directory.sh\\\"\" >> $file"
else
if [ "$QC_DIR" != "$HOME/.qc" ]; then
echo "export QC_DIR=\"$dir\"" >> "$file"
fi
echo "[ -f \"$dir/quick_change_directory.sh\" ] && . \"$dir/quick_change_directory.sh\"" >> "$file"
fi
fi
}
usage()
{
echo >&2 "Usage: $script_name [-nfCSh] [-t target-dir] <copy|symlink>"
echo >&2 " -n Just print what would be done."
echo >&2 " -f force overwrite existing files"
echo >&2 " -C skip cron - don't configure cronjob"
echo >&2 " -S skip shell config - don't change bashrc"
echo >&2 " -h Show this help."
echo >&2 " -t target-dir install to target-dir instead of ~/.qc"
}
#---------[ MAIN ]-------------------------------------------------------------
if [ $# -eq 0 ]; then
usage
exit 0
fi
NO_OP=
FORCE=
skip_cron=
skip_shell_cfg=
while getopts "nfCSht:" o "$@"; do
case $o in
n) NO_OP="false" ;;
f) FORCE=true ;;
C) skip_cron="true" ;;
S) skip_shell_cfg="true" ;;
t) QC_DIR="$OPTARG" ;;
h) usage; exit 0 ;;
*) usage; exit 1 ;;
esac
done
shift $((OPTIND-1))
if [ $# -ne 1 ]; then
echo >&2 "ERROR: exactly one argument needed ('copy' or 'symlink')"
usage
exit 1
fi
case "$1" in
copy) mode="copy"
;;
symlink) mode="symlink"
;;
*) echo >&2 "Unknown mode: $1"
usage
exit 1
;;
esac
echo "Installing to $QC_DIR (mode $mode)"
if [ ! -d "$QC_DIR" ]; then
execute mkdir -p "$QC_DIR"
fi
for fn in quick_change_directory.sh qc-backend qc-build-index dstore; do
if [ ! -f "$fn" ]; then
echo >&2 "ERROR: Missing file: $fn"
exit 1
fi
install_file "$fn" "$QC_DIR"
done
if [ -e "$QC_DIR/qc-index.cfg" ]; then
echo "Copying qc-index.cfg -> $QC_DIR/qc-index.cfg.new"
execute cp -f qc-index.cfg "$QC_DIR/qc-index.cfg.new"
else
echo "Copying qc-index.cfg -> $QC_DIR/qc-index.cfg"
execute cp qc-index.cfg "$QC_DIR/qc-index.cfg"
fi
if [ -z "$skip_cron" ]; then
echo "Configuring cronjob"
execute "$QC_DIR"/qc-build-index --cron 10
echo ""
fi
if [ -z "$skip_shell_cfg" ]; then
add_to_init_file "$HOME/.bashrc"
add_to_init_file "$HOME/.zshrc"
add_to_init_file "$HOME/.kshrc"
fi
if [ -n "$NO_OP" ]; then
echo
echo "SIMULATION MODE - NOTHING DONE"
echo
fi