-
Notifications
You must be signed in to change notification settings - Fork 0
/
suggested_install.sh
155 lines (135 loc) · 4.13 KB
/
suggested_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
#!/bin/env bash
print_usage() {
self_filename="${0##*/}"
echo "usage: \"${self_filename}\" [-h] [DIR...]"
echo \
'
Search set of folders until one is found in the systems $PATH with write
access. Use it to install the files as commands into the folder. Without DIR
arguments a list of default folders are searched.
positional arguments:
DIR folders to look for when searching for installation path
options:
-h print this help and exit'
}
# Default path to search with priority, if no arguments are given.
default_install_dir=""
install_cmd="$(/usr/bin/which install)"
while getopts ':h' OPTION
do
case "$OPTION" in
h) print_usage
exit 0
;;
?) continue ;;
esac
done
# This variable will be set by the following function if any usable path is
# found. Then later functions will use it as base folder to install into.
install_dir=""
# This one will also be set by the following function. Can be useful to print
# in case of a problem.
list_of_install_dirs=""
# Searches the list of system $PATH if any of the given directories are found
# in it. If so, then update the variable "install_dir" with it, when user has
# write permission. If no arguments to the function are given, then it defaults
# to some predefined set of directories to look for.
find_install_dir () {
if [ ${#} -eq 0 ]
then
list_of_install_dirs=(
"${default_install_dir}"
"$(systemd-path user-binaries)"
"$HOME/.local/bin"
"$HOME/bin"
"/usr/local/bin"
)
else
list_of_install_dirs=("${@}")
fi
# Convert the system $PATH variable into a Bash list.
IFS=: read -r -d '' -a list_of_path < <(printf '%s:\0' "$PATH")
for dir in "${list_of_install_dirs[@]}"
do
for path in "${list_of_path[@]}"
do
if [ "${path}" == "${dir}" ]
then
if [ -w "${path}" ]
then
install_dir="${path}"
else
echo "No write permission for install directory:"
echo " ${path}"
exit 1
fi
return
fi
done
done
}
# Install the given "filename" as a command into the previously set
# "install_dir". It will remove the file extension, so it works like any
# normal system command. Second argument can be an option with special
# actions, such as creating an additional symbolic link.
install_program () {
filename="${1}"
file_no_ext="${filename%.*}"
option="${2}"
"${install_cmd}" -m 755 -b -C -D -t "${install_dir}" "${filename}"
if ! [ "${?}" == 0 ]
then
exit 1
else
mv -f "${install_dir}/${filename}" "${install_dir}/${file_no_ext}"
if [ "${option}" == "--symlink" ]
then
ln --symbolic \
"${install_dir}/${file_no_ext}" \
"${install_dir}/${filename}"
fi
fi
if [ -f "${install_dir}/${file_no_ext}" ]
then
echo ""
echo "${filename} installed as:"
echo " ${install_dir}/${file_no_ext}"
if [ "${option}" == "--symlink" ]
then
echo " ${install_dir}/${filename}"
fi
else
echo "Error! Could not install ${filename} to ${install_dir}"
exit 1
fi
}
# Simple question to answer with "y" for proceeding. Any other key than "y"
# will cause to end the script.
ask_proceed () {
echo "Continue installallation programs to:"
echo " ${install_dir}"
echo ""
echo "(Y)es or (N)o:"
read -rt 10 answer
first_letter="${answer:0:1}"
if ! [[ "${first_letter}" == "y" || "${first_letter}" == "Y" ]]
then
exit 1
fi
}
if [ $# -eq 0 ]
then
find_install_dir
else
find_install_dir "${@}"
fi
if [ "${install_dir}" == "" ]
then
echo "Error! No usable install directory could be recognized from:"
echo " \"${list_of_install_dirs[*]}\" "
exit 1
else
ask_proceed
fi
# Find install dir and install following files by removing their extension.
install_program "fp"