-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.sh
executable file
·624 lines (585 loc) · 14.5 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
#!/bin/sh -
# Bluesky in the shell
# A Bluesky CLI (Command Line Interface) implementation in shell script
# Author Bluesky:@bills-appworks.blue
#
# Copyright (c) 2024-2025 bills-appworks
# This software is released under the MIT License.
# http://opensource.org/licenses/mit-license.php
IFS='
'
FILE_DIR=`dirname "$0"`
FILE_DIR=`(cd "${FILE_DIR}" && pwd)`
BSKYSHCLI_INSTALLER_VERSION='0.2.0'
rcfile_name='.bsky_sh_cli_rc'
install_fail()
{
param_reason="$1"
error "Install failed : ${param_reason}"
}
verify_file()
{
param_filepath="$1"
if [ -r "${param_filepath}" ]
then
:
else
install_fail "${param_filepath} is not readable. please check file organization."
fi
}
verify_file_organization()
{
_p '... '
verify_file "${FILE_DIR}/bin/bsky"
verify_file "${FILE_DIR}/lib/bsky_core.sh"
_pn '[OK]'
}
verify_required_tool()
{
param_tool="$1"
_p "${param_tool} ... "
which "${param_tool}" > /dev/null
tool_exist=$?
if [ ${tool_exist} -eq 0 ]
then
_pn '[OK]'
else
_pn "[NG] Command ${param_tool} not found."
STATUS_TOOLS=1
fi
return $tool_exist
}
verify_required_tools()
{
STATUS_TOOLS=0
verify_required_tool 'curl'
verify_required_tool 'jq'
# GNU sed
_p 'GNU sed ... '
which sed > /dev/null
result_sed=$?
if [ $result_sed -eq 0 ]
then
# check GNU sed -z option
sed -z 's///' < /dev/null > /dev/null 2>&1
result_sed=$?
if [ $result_sed -eq 0 ]
then
_pn '[OK] : sed'
else
# try to gsed
#_pn '[NG] : Command GNU sed not found (sed -z option is disabled).'
#STATUS_TOOLS=1
:
fi
else
# try to gsed
#_pn '[NG] : Command "sed" not found.'
#STATUS_TOOLS=1
:
fi
# gsed
if [ $result_sed -ne 0 ]
then
which gsed > /dev/null
result_sed=$?
if [ $result_sed -eq 0 ]
then
gsed -z 's///' < /dev/null > /dev/null 2>&1
result_sed=$?
if [ $result_sed -eq 0 ]
then
_pn '[OK] : gsed'
else
_pn '[NG] : Command GNU sed not found (gsed -z option is disabled).'
STATUS_TOOLS=1
fi
else
_pn '[NG] : Command GNU sed not found.'
STATUS_TOOLS=1
fi
fi
# file (libmagic)
_p 'file (libmagic) ... '
which file > /dev/null
result_file=$?
if [ $result_file -eq 0 ]
then
_pn '[OK]'
else
_pn '[WARNING] : Command file (libmagic) not found. Images and link cards cannot be used.'
fi
# convert (imagemagick)
_p '/usr/bin/convert (imagemagick) ... '
which /usr/bin/convert > /dev/null
result_convert=$?
if [ $result_convert -eq 0 ]
then
_pn '[OK]'
else
_pn '[WARNING] : Command /usr/bin/convert (imagemagick) not found. Images and link card some functions cannot be used.'
fi
if [ $STATUS_TOOLS -ne 0 ]
then
install_fail 'Lack of required tool.'
fi
}
is_super_user()
{
user=`whoami`
if [ "${user}" = 'root' ]
then
status=0
else
status=1
fi
return $status
}
get_candidate_user_config()
{
config=''
case $SHELL in
/bin/bash)
if [ -f "${HOME}/.bashrc" ]
then
config="${HOME}/.bashrc"
elif [ -f "${HOME}/.bash_profile" ]
then
config="${HOME}/.bash_profile"
elif [ -f "${HOME}/.bash_login" ]
then
config="${HOME}/.bash_login"
fi
;;
/bin/zsh)
if [ -f "${HOME}/.zshrc" ]
then
config="${HOME}/.zshrc"
elif [ -f "${HOME}/.zlogin" ]
then
config="${HOME}/.zlogin"
fi
;;
*)
;;
esac
if [ -z "${config}" ]
then
case $SHELL in
/bin/zsh)
config="${HOME}/.zprofile"
;;
*)
config="${HOME}/.profile"
;;
esac
fi
echo "${config}"
}
is_already_set_path()
{
param_install_dir="$1"
detect=`echo "${PATH}" | grep -o -E '(^|:)'"${param_install_dir}"/bin'(:|$)'`
if [ -n "${detect}" ]
then
status=0
else
status=1
fi
return $status
}
add_path_config()
{
param_config_file="$1"
param_install_dir="$2"
{
echo "# Configuration add by bsky-sh-cli (Bluesky in the shell) installer (install.sh) version ${BSKYSHCLI_INSTALLER_VERSION}"
# '$PATH' use literally
# shellcheck disable=SC2016
echo 'PATH=$PATH:'"${param_install_dir}/bin"
echo 'export PATH'
} >> "${param_config_file}"
# refer to echo result
# shellcheck disable=SC2320
return $?
}
inputYn()
{
read prompt_input
if [ -n "${prompt_input}" ]
then
case $prompt_input in
Y|y|YES|yes|Yes)
status=0
;;
*)
status=1
;;
esac
else
status=0
fi
return $status
}
inputyn()
{
param_prompt="$1"
status=255
while [ $status -eq 255 ]
do
_p "${param_prompt}"
read prompt_input
if [ -n "${prompt_input}" ]
then
case $prompt_input in
Y|y|YES|yes|Yes)
status=0
;;
N|n|NO|no|No)
status=1
;;
*)
;;
esac
fi
done
return $status
}
# entry point
echo "bsky-sh-cli (Bluesky in the shell) installer version ${BSKYSHCLI_INSTALLER_VERSION}"
echo ''
echo 'If the message "(Question)? [Y/n]:" is displayed, enter "y" for yes, "n" for no, and press the enter key.'
echo 'If you press the enter key without entering anything, it is assumed that you have specified the uppercase option ("Y" (yes) in the above case).'
lib_util_path="${FILE_DIR}/lib/util.sh"
if [ -r "${lib_util_path}" ]
then
:
else
echo "ERROR: ${lib_util_path} is not readable. please check file organization."
exit 1
fi
# SC1090
# shellcheck source=SCRIPTDIR/lib/util.sh
. "${lib_util_path}"
# parameter parse & check
parse_parameters '--install-dir:1 --config-path-file:1 --skip-config-path:0 --skip-rcfile-copy:0 --config-langs:1 --skip-confirm:0' "$@"
# install source files verification
_pn '>>>> Verify file part of organization'
verify_file_organization
# required tools verification
_pn '>>>> Verify required tools'
verify_required_tools
# change behavior by executor is super user or not
if is_super_user
then
install_dir='/opt/bsky_sh_cli'
case $SHELL in
/bin/zsh)
config_path_file='/etc/zprofile'
;;
*)
config_path_file='/etc/profile.d/bsky_sh_cli.sh'
;;
esac
else
install_dir="${HOME}/.local/bsky_sh_cli"
config_path_file=`get_candidate_user_config`
fi
# override path by parameter
if [ -n "${PARSED_PARAM_KEYVALUE_install_dir}" ]
then
install_dir="${PARSED_PARAM_KEYVALUE_install_dir}"
fi
if [ -n "${PARSED_PARAM_KEYVALUE_config_path_file}" ]
then
config_path_file="${PARSED_PARAM_KEYVALUE_config_path_file}"
fi
# configure install destination directory
_pn '>>>> Configure install destination directory'
if [ -n "${PARSED_PARAM_KEYONLY_skip_confirm}" ] || [ -n "${PARSED_PARAM_KEYVALUE_install_dir}" ]
then
_pn "Install to '${install_dir}'"
else
_pn "Suggests '${install_dir}' as a install destination directory."
_p 'Are you sure you want to install to this directory? [Y/n]: '
if inputYn
then
_pn "Install to '${install_dir}'"
else
_pn 'Please specify the directory path of the path to install, and press the enter key.'
_p 'Install directory path: '
read install_dir
fi
fi
# remove tail '/'
# '$' is not variable use
# shellcheck disable=SC2016
install_dir=`_p "${install_dir}" | sed -E 's_^(.+)/$_\1_'`
if [ -f "${install_dir}" ]
then
install_fail "A file with the same name already exists in specified directory '${install_dir}'."
fi
if [ -d "${install_dir}" ]
then
_pn "A directory with the same name already exists in specified directory '${install_dir}'."
if [ -n "${PARSED_PARAM_KEYONLY_skip_confirm}" ]
then
_pn "Overwrite directory '${install_dir}'."
overwrite_directory=0
else
_p 'Do you want to overwrite and continue? [Y/n]: '
if inputYn
then
_pn "Overwrite directory '${install_dir}'."
overwrite_directory=0
else
install_fail 'Installation canceled.'
fi
fi
else
overwrite_directory=1
fi
# configure PATH environment variable
_pn '>>>> Configure PATH environment variable'
if [ -n "${PARSED_PARAM_KEYONLY_skip_config_path}" ]
then
_pn 'Skip configure the environment variable PATH.'
config_path=1
else
if is_already_set_path "${install_dir}"
then
_pn "Directory '${install_dir}/bin' is already set in the environment variable PATH. Skip configuring the environment variable PATH."
config_path=1
else
if [ -n "${PARSED_PARAM_KEYONLY_skip_confirm}" ]
then
_pn 'Configure the environment variable PATH.'
config_path=0
else
_p 'Do you want to configure the environment variable PATH? [Y/n]: '
if inputYn
then
_pn 'Configure the environment variable PATH.'
config_path=0
else
_pn 'Skip configure the environment variable PATH.'
config_path=1
fi
fi
fi
if [ $config_path -eq 0 ]
then
if [ -n "${PARSED_PARAM_KEYONLY_skip_confirm}" ]
then
_pn "Configure login script_file: ${config_path_file}"
else
if [ -n "${PARSED_PARAM_KEYVALUE_config_path_file}" ]
then
_pn "Login script file path is specified: '${PARSED_PARAM_KEYVALUE_config_path_file}'"
else
_pn "Suggests '${config_path_file}' as a login script file in the environment variable PATH."
_p 'Are you sure you want to create or make changes to this file? [Y/n]: '
if inputYn
then
_pn "Configure login script file: ${config_path_file}"
else
_pn 'Please specify the path of the file to set the environment variable PATH, and press the enter key.'
_p 'file path: '
read config_path_file
fi
fi
_pn "NOTE: Please confirm that the login script file is read and the PATH is set when login in again."
fi
fi
fi
# Run Commands file copy
_pn '>>>> Configure Run Commands file'
if [ -n "${PARSED_PARAM_KEYONLY_skip_rcfile_copy}" ]
then
_pn "Skip put the Run Commands file."
rcfile_copy=1
else
if [ -n "${PARSED_PARAM_KEYONLY_skip_confirm}" ]
then
if [ -f "${HOME}/${rcfile_name}" ]
then
_pn "'${HOME}/${rcfile_name}' is already exists. Skip to put the Run Commands file."
rcfile_copy=1
else
_pn "Put the Run Commands file: '${HOME}/${rcfile_name}'"
rcfile_copy=0
fi
else
_p "Do you want to put the Run Commands file in '${HOME}/${rcfile_name}'? [Y/n]: "
if inputYn
then
if [ -f "${HOME}/${rcfile_name}" ]
then
_pn "'${HOME}/${rcfile_name}' is already exists."
_p 'Do you want to skip and continue? [Y/n]: '
if inputYn
then
_pn "Skip to overwrite Run Commands file."
rcfile_copy=1
else
_pn "Overwrite '${HOME}/${rcfile_name}'"
rcfile_copy=0
fi
else
_pn "Put the Run Commands file in '${HOME}/${rcfile_name}'."
rcfile_copy=0
fi
else
_pn 'Skip put the Run Commands file.'
rcfile_copy=1
fi
fi
fi
# post default languages
_pn '>>>> Configure post default languages'
if [ -n "${PARSED_PARAM_KEYONLY_skip_confirm}" ]
then
if [ $rcfile_copy -eq 0 ]
then
if [ -n "${PARSED_PARAM_KEYVALUE_config_langs}" ]
then
_pn "Configure post default languages: ${PARSED_PARAM_KEYVALUE_config_langs}"
config_langs_value="${PARSED_PARAM_KEYVALUE_config_langs}"
config_langs=0
else
_pn "Skip to configure post default languages."
config_langs=1
fi
else
_pn "Skip to configure post default languages."
config_langs=1
fi
else
if [ $rcfile_copy -eq 0 ]
then
if [ -n "${PARSED_PARAM_KEYVALUE_config_langs}" ]
then
_pn "Configure post default languages: ${PARSED_PARAM_KEYVALUE_config_langs}"
config_langs_value="${PARSED_PARAM_KEYVALUE_config_langs}"
config_langs=0
else
_p "Configure post default languages? [Y/n]: "
if inputYn
then
_p "Input languages (comma separated: e.g. en,ja). : "
read config_langs_value
_pn "Configure post default languages: ${config_langs_value}"
config_langs=0
else
_pn "Skip to configure post default languages."
config_langs=1
fi
fi
else
_pn "Skip to configure post default languages."
config_langs=1
fi
fi
# output configuration of install
_pn ''
_pn '[Configuration of install]'
_pn "Install directory: ${install_dir}"
_p 'Directory overwrite: '
if [ "${overwrite_directory}" -eq 0 ]
then
_pn 'Yes'
else
_pn 'No'
fi
_p 'Configure the environment variable PATH: '
if [ $config_path -eq 0 ]
then
_pn 'Yes'
_pn " Path of the file to set the environment variable PATH: ${config_path_file}"
_pn ' Add following lines:'
_pn " # Configuration add by bsky-sh-cli (Bluesky in the shell) installer (install.sh) version ${BSKYSHCLI_INSTALLER_VERSION}"
# '$PATH' use literally
# shellcheck disable=SC2016
_pn ' PATH=$PATH:'"${install_dir}/bin"
_pn ' export PATH'
else
_pn 'No'
fi
_p "Put the Run Commands file in '${HOME}/${rcfile_name}: "
if [ $rcfile_copy -eq 0 ]
then
_pn 'Yes'
else
_pn 'No'
fi
_p "Configure post default languages: "
if [ $config_langs -eq 0 ]
then
_pn 'Yes'
_pn " Add following line in '${HOME}/${rcfile_name}'"
_pn " # Configuration add by installer (install.sh) version ${BSKYSHCLI_INSTALLER_VERSION}"
_pn " BSKYSHCLI_POST_DEFAULT_LANGUAGES='${config_langs_value}'"
else
_pn 'No'
fi
# final confirm
if [ -z "${PARSED_PARAM_KEYONLY_skip_confirm}" ]
then
if inputyn 'Are you sure you want to install with the above configuration? [y/n]: '
then
:
else
_pn 'Installation canceled.'
exit 1
fi
fi
# install execution
status=0
_p "Create installation directories if necessary '${install_dir}' ... "
if mkdir -p "${install_dir}"
then
_pn 'Complete'
else
_pn 'Failed'
exit 1
fi
_p "Copying files to '${install_dir}' ... "
if cp -rp "${FILE_DIR}/bin" "${FILE_DIR}/lib" "${install_dir}/"
then
_pn 'Complete'
else
_pn 'Failed'
exit 1
fi
if [ $config_path -eq 0 ]
then
_p "Add the environment variable PATH set lines in '${config_path_file}' ... "
if add_path_config "${config_path_file}" "${install_dir}"
then
_pn 'Complete'
else
_pn 'Failed'
exit 1
fi
fi
if [ $rcfile_copy -eq 0 ]
then
_p "Copying Run Commands file to '${HOME}/${rcfile_name}' ... "
if cp "${FILE_DIR}/${rcfile_name}.sample" "${HOME}/${rcfile_name}"
then
_pn 'Complete'
else
_pn 'Failed'
exit 1
fi
fi
if [ $config_langs -eq 0 ]
then
_p "Add post default languages in '${HOME}/${rcfile_name}' ... "
{
echo "# Configuration add by installer (install.sh) version ${BSKYSHCLI_INSTALLER_VERSION}"
echo "BSKYSHCLI_POST_DEFAULT_LANGUAGES='${config_langs_value}'"
} >> "${HOME}/${rcfile_name}"
echo 'Complete'
fi
_pn 'Installation complete.'