-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstall.bash
executable file
·231 lines (202 loc) · 6.39 KB
/
install.bash
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
#!/usr/bin/env bash
# -----------------------------------------------------------------
# ipset set listing wrapper script
#
# https://github.com/AllKind/ipset_list
# https://sourceforge.net/projects/ipset-list/
# -----------------------------------------------------------------
# Copyright (C) 2013-2019 Mart Frauenlob aka AllKind ([email protected])
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# -----------------------------------------------------------------
# installer for ipset_list
# -----------------------------------------------------------------
# bash check
if [ -z "$BASH" ]; then
echo "\`BASH' variable is not available. Not running bash?"
exit 1
fi
# shell options
set +f -o braceexpand
umask 0022
# variables
: ${PATH:=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin}
ME=ipset_list
BACKUP=
BASHCOMPDIR=
CACHEDIR=
GROUP=
BINDIR=
DATAROOTDIR=
DOCDIR=
MANDIR=
NOACT=
OWNER=
OWNSH=
PREFIX=
SYSCONFDIR=
VERBOSE=
arr_args=( "$@" )
# ------------------------------------------------------------------------- #
# FUNCTIONS
# ------------------------------------------------------------------------- #
usage() {
printf "
USAGE: ${0##*/} [[Option] Option-argument] [...]\n
Options:
-?, -h, --help Show this usage instructions
-b, --backup Create numbered backups (suffix=~)
-g, --group group_name Set group ownership. Default: root
-n, --no-act Perform a dry-run
-o, --owner owner_name Set ownership. Default: root
-v, --verbose Verbose output
--prefix /path Prefix directory. Default: /usr/local
--bindir /path Default: PREFIX/sbin
--datarootdir /path Default: PREFIX/share
--docdir /path Default: DATAROOTDIR/doc
--mandir /path Default: DATAROOTDIR/man
--sysconfdir /path Default: PREFIX/etc
--bashcompdir /path Default: Retrieved with pkg-config, or as fallback:
/etc/bash_completion.d, or ~/.bash_completion
--cachedir /path Default: /var/cache
\n"
}
no_act() { # just print the command string
printf "%s\n" "$*"
}
create_dirs() { # create all directory components
${NOACT}command install $VERBOSE $OWNSH -d "$@"
}
install_dir() { # install multiple source files to destination
local str_perm
case "$1" in
-m|--mode)
str_perm="-m $2"
shift 2
;;
esac
${NOACT}command install $VERBOSE $BACKUP $OWNSH $str_perm "$@"
}
install_file() { # install a single file
local str_perm
case "$1" in
-m|--mode)
str_perm="-m $2"
shift 2
;;
esac
${NOACT}command install $VERBOSE $BACKUP $OWNSH $str_perm -T "$@"
}
# ------------------------------------------------------------------------- #
# MAIN
# ------------------------------------------------------------------------- #
# parse arguments
while (($#)); do
option="$1" opt_arg="$2"
shift
case "$option" in
"-?"|-h|--help) usage
exit 0
;;
-b|--backup) BACKUP="--backup=t"
continue
;;
-g|--group) GROUP="$opt_arg" ;;
-n|--no-act) NOACT="no_act "
continue
;;
-o|--owner) OWNER="$opt_arg" ;;
-v|--verbose) VERBOSE="-v"
continue
;;
--bashcompdir) BASHCOMPDIR="$opt_arg" ;;
--bindir) BINDIR="$opt_arg" ;;
--cachedir) CACHEDIR="$opt_arg" ;;
--datarootdir) DATAROOTDIR="$opt_arg" ;;
--docdir) DOCDIR="$opt_arg" ;;
--mandir) MANDIR="$opt_arg" ;;
--prefix) PREFIX="$opt_arg" ;;
--sysconfdir) SYSCONFDIR="$opt_arg" ;;
*) usage
exit 3
esac
if [[ $opt_arg ]]; then shift; fi
done
# set defaults
: ${PREFIX:=/usr/local}
: ${BINDIR:=${PREFIX}/sbin}
: ${CACHEDIR:=/var/cache}
: ${DATAROOTDIR:=${PREFIX}/share}
: ${SYSCONFDIR:=${PREFIX}/etc}
: ${DOCDIR:=${DATAROOTDIR}/doc}
: ${MANDIR:=${DATAROOTDIR}/man}
: ${OWNER:=root}
: ${GROUP:=root}
OWNSH="--group=$GROUP --owner=$OWNER"
if [[ -z $BASHCOMPDIR ]]; then
if command -v pkg-config &>/dev/null; then
while :; do
BASHCOMPDIR=$(command pkg-config --variable=completionsdir bash-completion) && break
BASHCOMPDIR=$(command pkg-config --variable=compatdir bash-completion) && break
done
else
while :; do
[[ -e /etc/bash_completion.d ]] && BASHCOMPDIR=/etc/bash_completion.d && break
BASHCOMPDIR='~' && break
done
fi
fi
# warning message on dry-run
if [[ $NOACT ]]; then
printf "\nNO ACT MODE - NOT INSTALLING!\n\n"
fi
# check for install bin
command -v install &>/dev/null || {
printf "Unable to find the \`install' binary in \$PATH.\n" >&2
exit 1
}
# warn about ownership - if not root
if ((EUID != 0)); then
printf "Not running as root. Not changing ownership/groups.\n"
OWNSH=
fi
# create directories
create_dirs "${BINDIR}" "${SYSCONFDIR}/$ME" "${DOCDIR}/$ME" "${MANDIR}/man8" "${BASHCOMPDIR}" "${CACHEDIR}/$ME"
# copy files
install_dir -m 0664 doc/*.8 "${MANDIR}/man8"
install_dir -m 0664 doc/*.html "${DOCDIR}/${ME}"
install_file -m 0755 "${ME}" "${BINDIR}/$ME"
install_file -m 0644 "${ME}.conf" "${SYSCONFDIR}/${ME}/${ME}.conf"
if [[ $BASHCOMPDIR = ~ ]]; then
printf "bashcompdir is \`~', adding completion to \`%s'.\n\tRemember to manually remove it on uninstall, or re-install.\n" "~/.bash_completion"
if ! [[ $NOACT ]]; then
command cat ip-array_bash_completion >> ~/.bash_completion
fi
else
install_file -m 0644 bash_completion/"$ME" "$BASHCOMPDIR/$ME"
fi
if ! [[ $NOACT ]]; then
# create versions file
printf "Creating version file: \`${SYSCONFDIR}/${ME}/${ME}_version'\n"
(set +C; "${BINDIR}/$ME" -v > "${SYSCONFDIR}/${ME}/${ME}_version")
# create log file, to be re-used by uninstall.bash
printf "Creating \`./${ME}-install.log' - Will need this for uninstallation!\n"
(set +C; printf '#!/usr/bin/env bash\n\n' > ./${ME}-install.log)
printf "# install arguments: %s\n\n" "${arr_args[*]}" >> ./${ME}-install.log
for var in PREFIX BASHCOMPDIR BINDIR CACHEDIR DATAROOTDIR DOCDIR MANDIR SYSCONFDIR; do
declare -p "$var" >> ./${ME}-install.log
done
fi
printf "Finished Install\n"
exit 0