-
Notifications
You must be signed in to change notification settings - Fork 1
/
banner.sh
executable file
·276 lines (251 loc) · 4.88 KB
/
banner.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
#!/bin/sh
#
# Please send bug reports to pzurowski@pld-linux.org or pld-devel-* lists
#
# 2004, GPL 2+
#
# >> PLACE STANDARD GPL DISCLAIMER H E R E . <<
#
CONFIG=/etc/sysconfig/banner
####################################################### CONFIG ########
# default parameters
##########################
BANNERDIR="/var/lib/banner/"
# egrep regexp
EXCLUDEFILES="(rpmnew$|rpmsave$|~$)"
STDOUT="1" # stdout by default
#STDOUT="2" # stderr by default
# config parameters
##########################
if [ -r $CONFIG ]; then
. $CONFIG
fi
# override parameters
##########################
ALL_BANNERS=0
BANNERS=""
NOBANNERS=""
BANNER_LIST=""
CHOOSE_NEWER="no"
CHOOSE_OLDER="no"
EXCLUDE_FLAG=0
NEED_BANNER_LIST=0
NEED_MTIME_CHECK=0
NEW_APPEND=0
NEW_BANNER=""
NEW_SHOW=0
case $STDOUT in
[1-9]) ;;
*) STDOUT="1" ;;
esac
#################################################### FUNCTIONS ########
Usage() {
cat << EOF
Usage: $(basename $0) [options] [banners]
EOF
}
Help() {
Usage
cat << EOF
-a, --all - all banners
-d, --delete - delete specified banners
-e, --exclude - exclude following banners (useful with -a)
-h, --help - show this help
-i, --include - cancel effect of -e (EXCLUDED banners will remain excluded)
-m, --make - make a brand-new banner named as following parameter [1] (from stdin)
-M - same as above, but append if file exists
-n, --names - show names of the banners
--newer - all choosen banners should be newer than following parameter in seconds
--older - all choosen banners should be older than following parameter in seconds
-s, --show - show specified banners
--stderr - send banner to stderr instead of stdout (or other)
--stdout - send banner to stdout instead of stderr (or other)
-u, --usage - show short help
[1] if there is no slash ('/') in the given name default dir ($BANNERDIR) is used,
otherwise the one that's specified
EOF
}
Unknown_para() {
cat << EOF
Unknown parameter $1
EOF
Help
}
check_banners_mtime() {
BANNERS="$1"
OLDER="$2"
NEWER="$3"
DATE=$(date +%s)
for BANNER in $BANNERS;do
STAT=$(stat -c %Y "$BANNERDIR/$BANNER")
if [ $OLDER != "no" -a $(( $DATE - $STAT )) -lt $OLDER ]; then
BANNER=""
fi
if [ $NEWER != "no" -a $(( $DATE - $STAT )) -gt $NEWER ]; then
BANNER=""
fi
echo $BANNER
done
}
delete_banners() {
BANNERS="$1"
rm -rf $(get_banner_location_list "$BANNER")
}
get_all_banner_list() {
ls "$BANNERDIR" | grep -E -v "$EXCLUDEFILES"
}
get_banner_list() {
BANNERS="$1"
NOBANNERS="$2"
for BANNER in $BANNERS; do
if [ -r "$BANNERDIR/$BANNER" ]; then
echo $NOBANNERS | grep -q $BANNER || echo $BANNER
fi
done
}
get_banner_location_list() {
BANNERS="$1"
for BANNER in $BANNERS; do
echo "$BANNERDIR/$BANNER"
done
}
make_banner() {
BANNER="$1"
SHOW="$2"
if [ ! -d "${BANNER%/*}" ]; then
mkdir -p "${BANNER%/*}"
fi
data=$(cat)
if [ $NEW_APPEND -eq 0 ]; then
echo "$data" > $BANNER
else
echo "$data" >> $BANNER
fi
if [ $SHOW -eq 1 ]; then
echo "$data"
fi
}
show_banner() {
cat "$BANNERDIR/$1" >&$STDOUT
}
show_banners() {
for BANNER in $*; do
show_banner $BANNER
done
}
######################################################### MAIN ########
while [ -n "$1" ]; do
case "$1" in
-a|--all)
ALL_BANNERS=1
;;
-d|--delete)
NEED_BANNER_LIST=1
ACTION="delete"
;;
-e|--exclude)
EXCLUDE_FLAG=1
;;
-h|--help)
Help
exit 0
;;
-i|--include)
EXCLUDE_FLAG=0
;;
-m|--make|-M)
NEED_BANNER_LIST=0
if [[ "$2" != */* ]]; then
NEW_BANNER="$BANNERDIR/${2##*/}"
else
NEW_BANNER="$2"
fi
ACTION="make"
if [ "$1" = "-M" ]; then
NEW_APPEND=1
else
NEW_APPEND=0
fi
if [ -z "$NEW_BANNER" ]; then
Help
exit 2
fi
shift
;;
-n|--names)
NEED_BANNER_LIST=1
ACTION="names"
;;
--newer)
NEED_MTIME_CHECK=1
CHOOSE_NEWER="$2"
if [ -z "$CHOOSE_NEWER" ]; then
Help
exit 2
fi
shift
;;
--older)
NEED_MTIME_CHECK=1
CHOOSE_OLDER="$2"
if [ -z "$CHOOSE_OLDER" ]; then
Help
exit 2
fi
shift
;;
-s|--show)
NEED_BANNER_LIST=1
NEW_SHOW=1
ACTION="show"
;;
--stdout)
STDOUT="1"
;;
--stderr)
STDOUT="2"
;;
-u|--usage)
Usage
exit 0
;;
-*)
Unknown_para "$1"
exit 1
;;
*)
if [ $EXCLUDE_FLAG -eq 0 ]; then
BANNERS="$BANNERS ${1##*/}"
else
NOBANNERS="$NOBANNERS ${1##*/}"
fi
;;
esac
shift
done
if [ $ALL_BANNERS -ne 0 ]; then
BANNERS=`get_all_banner_list`
fi
if [ $NEED_BANNER_LIST -ne 0 ]; then
BANNER_LIST=`get_banner_list "$BANNERS" "$NOBANNERS"`
fi
if [ $NEED_MTIME_CHECK -ne 0 ]; then
BANNER_LIST=`check_banners_mtime "$BANNER_LIST" "$CHOOSE_OLDER" "$CHOOSE_NEWER"`
fi
case $ACTION in
"delete")
delete_banners $BANNER_LIST
;;
"make")
make_banner $NEW_BANNER $NEW_SHOW
;;
"names")
echo $BANNER_LIST
;;
"show")
show_banners $BANNER_LIST
;;
"")
Help
;;
esac