Skip to content

Commit f16cc0c

Browse files
committed
v0.3.4 - Loading info from /etc/openwrt_release; Printing extra info on TXT and HTML email (from openwrt_release); Prettier HTML table (font type, size, extra table, etc); Print warning before updating SNAPSHOT versions; code cleanup
1 parent f7ab2fc commit f16cc0c

File tree

1 file changed

+59
-12
lines changed

1 file changed

+59
-12
lines changed

opkg-upgrade.sh

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616

1717

1818
### Initialization
19-
OPKGUPVERSION="0.3.2"
19+
OPKGUPVERSION="0.3.4"
2020
OPKGBIN="$(which opkg 2>/dev/null)"
2121
SSMTPBIN="$(which ssmtp 2>/dev/null)"
2222
BANNERSTRING="Simple OPKG Updater v$OPKGUPVERSION"
2323
TIMESTAMP="$(date '+%Y/%m/%d %H:%M:%S' 2>/dev/null)"
2424
OPKGUP_INSTALL_DIR='/usr/sbin'
25+
OPENWRT_RELEASE="/etc/openwrt_release"
26+
ROUTER_NAME="$(uname -n)"
27+
HTML_FONT="font-family:'Trebuchet MS', Helvetica, sans-serif;"
2528

2629
### Silly SH
2730
TRUE=0
@@ -47,13 +50,19 @@ OPKGUP_LOCATION="$(readlink -f $0)"
4750
PACKS=""
4851
PACKS_NAMES=""
4952
PACKS_COUNT=0
50-
PACKS_COLS="0 0 0"
5153

5254

5355

5456

5557
########################### FUNCTIONS STARTS
5658

59+
# Load info from /etc/openwrt_release into memory
60+
source_release() {
61+
if is_file "$OPENWRT_RELEASE"; then
62+
. "$OPENWRT_RELEASE"
63+
fi
64+
}
65+
5766
# get opkg packages listings and upgradable info
5867
opkg_init() {
5968
check_for_opkg
@@ -67,6 +76,7 @@ opkg_init() {
6776

6877
# main function
6978
main() {
79+
source_release
7080
print_banner
7181
opkg_init
7282
upgrade_check # may exit here
@@ -81,7 +91,7 @@ main() {
8191
else
8292
email_data="$(print_txt_email "$uplist")"
8393
fi
84-
#local email_data="$(email_subject)"$'\n\n'"$(print_banner)"$'\n\n'"Report for: $(uname -n)"$'\n\n'"$uplist"$'\n\n'"Generated on: $TIMESTAMP"
94+
#local email_data="$(email_subject)"$'\n\n'"$(print_banner)"$'\n\n'"Report for: $ROUTER_NAME"$'\n\n'"$uplist"$'\n\n'"Generated on: $TIMESTAMP"
8595
if just_print_html; then
8696
echo -e "$email_data"
8797
exit 0
@@ -96,6 +106,8 @@ main() {
96106
fi
97107
just_print && exit 0
98108
opkg_has_update || { echo '' ; exit 0 ; }
109+
110+
openwrt_is_snapshot && print_snapshot_disclaimer
99111

100112
if ! no_confirm; then
101113
if ! confirm_upgrade; then
@@ -126,14 +138,24 @@ list_upgrades() {
126138
if opkg_has_update; then
127139
echo "Packages available for upgrade: $PACKS_COUNT"$'\n'
128140
#echo -e "$PACKS"
129-
prettyPrintPacks
141+
print_packs_txt
130142
return $TRUE
131143
fi
132144
echo $'No packages to install!\n\n'
133145
return $FALSE
134146
}
135147

136-
prettyPrintPacks() {
148+
# Print router info in plain text
149+
print_info_txt() {
150+
printf "%s\n" "Router name.: $ROUTER_NAME"
151+
is_not_empty "$DISTRIB_DESCRIPTION" && printf "%s\n" "Description.: $DISTRIB_DESCRIPTION"
152+
is_not_empty "$DISTRIB_TARGET" && printf "%s\n" "Target......: $DISTRIB_TARGET"
153+
is_not_empty "$DISTRIB_ARCH" && printf "%s\n" "Arch........: $DISTRIB_ARCH"
154+
echo ""
155+
}
156+
157+
# Pretty print package lists in plain text
158+
print_packs_txt() {
137159
echo -ne "$PACKS" | awk '
138160
function rep(c, n){ s=sprintf("%" n "s",""); gsub(/ /,c,s); return s }
139161
BEGIN{ j=1; } NR>0{
@@ -205,6 +227,11 @@ print_error() {
205227
echo "$@" >&2
206228
}
207229

230+
# Prints Warning about upgrading beta/trunk versions
231+
print_snapshot_disclaimer() {
232+
printf "\n%s\n%s\n%s\n%s\n" "WARNING! You are running a Beta / Snapshot / Trunk version!" "Upgrading snapshots MAY cause undesired results, including soft-bricks." "The current trunk head may not be compatible with your installed version!" "You have been warned! Proceed at your own risk!"
233+
}
234+
208235
# prints program name and version
209236
print_banner() {
210237
[[ "$1" = 'error' ]] && { print_error $'\n'"$BANNERSTRING"$'\n' ; return $TRUE ; }
@@ -333,17 +360,18 @@ print_html_email() {
333360

334361
# prints an email report in txt format
335362
print_txt_email() {
336-
echo "$(email_subject)"$'\n\n'"$(print_banner)"$'\n\n'"Report for: $(uname -n)"$'\n\n'"$1"$'\n\n'"Generated on: $TIMESTAMP"
363+
#echo "$(email_subject)"$'\n\n'"$(print_banner)"$'\n\n'"Report for: $ROUTER_NAME"$'\n\n'"$1"$'\n\n'"Generated on: $TIMESTAMP"
364+
echo "$(email_subject)"$'\n\n'"$(print_banner)"$'\n\n'"$(print_info_txt)"$'\n\n'"$1"$'\n\n'"Generated on: $TIMESTAMP"
337365
}
338366

339367
# prints the packages html table
340368
print_html_table() {
341369
echo '<br>'
342-
is_empty "$PACKS" && { echo '<br><br><h3>No packages to install.</h3>' ; return $TRUE ; }
370+
is_empty "$PACKS" && { echo '<br><br><h3 style="'"$HTML_FONT"' font-size:13pt; font-weight:bold">No packages to install.</h3>' ; return $TRUE ; }
343371
local td_padding='padding-left:8px;padding-right:10px;padding-top:12px;padding-bottom:12px;'
344372
local td_open='<td style=\"'"$td_padding"'\">'
345373
local th_open='<th style="'"$td_padding"'">'
346-
echo '<table border="1" width="600px" cellpadding="0" cellspacing="0" style="border-collapse:collapse;">'
374+
echo '<table border="1" width="600px" cellpadding="0" cellspacing="0" style="border-collapse:collapse; '"$HTML_FONT"' font-size:10pt">'
347375
echo '<tr style="background-color:#2f3263; color:#EEE;" align="left" margin=0 padding=0>'$'\n\t'"$th_open"'#</th>'$'\n\t'"$th_open"'Pack</th>'$'\n\t'"$th_open"'Current</th>'$'\n\t'"$th_open"'Update</th>'$'\n''</tr>'
348376
# most of the table is generated using awk
349377
echo -ne "$PACKS" | \
@@ -354,9 +382,15 @@ awk 'BEGIN{ i=1; l=""; } { if (i % 2) l=""; else l=" style=\"background-color:#d
354382

355383
# prints html email info
356384
print_html_header() {
357-
echo $'\n\n''<h2>'"$(print_banner 'nopadding')"'</h2>'
358-
echo '<h3>'"Report for: $(uname -n)"'</h3>'
359-
echo '<h3>'"Packages available for upgrade: $PACKS_COUNT"'</h3>'
385+
echo $'\n\n''<h2 style="'"$HTML_FONT"' font-size:14pt; margin-top:1.5em; font-weight:bold">'"$(print_banner 'nopadding')"'</h2>'
386+
echo '<table border="1" width="600px" cellpadding="6pt" cellspacing="0" style="border-collapse:collapse;'"$HTML_FONT"' font-size:11pt">'
387+
388+
echo '<tr><td style="font-weight:bold">Router Name</td><td>'"$ROUTER_NAME"'</td></tr>'
389+
is_not_empty "$DISTRIB_DESCRIPTION" && echo '<tr><td style="font-weight:bold">Description</td><td>'"$DISTRIB_DESCRIPTION"'</td></tr>'
390+
is_not_empty "$DISTRIB_TARGET" && echo '<tr><td style="font-weight:bold">Target</td><td>'"$DISTRIB_TARGET"'</td></tr>'
391+
is_not_empty "$DISTRIB_ARCH" && echo '<tr><td style="font-weight:bold">Arch</td><td>'"$DISTRIB_ARCH"'</td></tr>'
392+
echo '<tr><td style="font-weight:bold">Updates Count</td><td>'"$PACKS_COUNT"'</td></tr>'
393+
echo '</table>'
360394
}
361395

362396
# prints html email mime type and format
@@ -366,7 +400,7 @@ print_html_mime() {
366400

367401
# prints html email info footer
368402
print_html_timestamp() {
369-
echo $'\n''<h4>'"Generated on: $TIMESTAMP by "'<a href="https://github.com/tavinus/opkg-upgrade">opkg-upgrade</a></h4>'$'\n'
403+
echo $'\n''<h4 style="'"$HTML_FONT"'">'"Generated on: $TIMESTAMP by "'<a href="https://github.com/tavinus/opkg-upgrade">opkg-upgrade</a></h4>'$'\n'
370404
}
371405

372406
# prints the email subject
@@ -441,6 +475,12 @@ is_valid_email() {
441475
return $TRUE
442476
}
443477

478+
# returns $TRUE if it is a valid file, $FALSE otherwise
479+
is_file() {
480+
[ -f "$1" ] && return $TRUE
481+
return $FALSE
482+
}
483+
444484
# returns $TRUE if it is a valid folder, $FALSE otherwise
445485
is_dir() {
446486
[ -d "$1" ] && return $TRUE
@@ -471,6 +511,13 @@ opkg_has_update() {
471511
return $TRUE
472512
}
473513

514+
# returns $TRUE if $DISTRIB_RELEASE equals SNAPSHOT
515+
openwrt_is_snapshot() {
516+
[ "$DISTRIB_RELEASE" = "SNAPSHOT" ] && return $TRUE
517+
return $FALSE
518+
}
519+
520+
474521

475522

476523
###### OPERATION FLAGS

0 commit comments

Comments
 (0)