From f16cc0cebea609630480761d68e89daf22585fb1 Mon Sep 17 00:00:00 2001 From: tavinus Date: Mon, 10 Dec 2018 20:14:18 -0200 Subject: [PATCH] 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 --- opkg-upgrade.sh | 71 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 59 insertions(+), 12 deletions(-) diff --git a/opkg-upgrade.sh b/opkg-upgrade.sh index ca46e36..1765f6b 100755 --- a/opkg-upgrade.sh +++ b/opkg-upgrade.sh @@ -16,12 +16,15 @@ ### Initialization -OPKGUPVERSION="0.3.2" +OPKGUPVERSION="0.3.4" OPKGBIN="$(which opkg 2>/dev/null)" SSMTPBIN="$(which ssmtp 2>/dev/null)" BANNERSTRING="Simple OPKG Updater v$OPKGUPVERSION" TIMESTAMP="$(date '+%Y/%m/%d %H:%M:%S' 2>/dev/null)" OPKGUP_INSTALL_DIR='/usr/sbin' +OPENWRT_RELEASE="/etc/openwrt_release" +ROUTER_NAME="$(uname -n)" +HTML_FONT="font-family:'Trebuchet MS', Helvetica, sans-serif;" ### Silly SH TRUE=0 @@ -47,13 +50,19 @@ OPKGUP_LOCATION="$(readlink -f $0)" PACKS="" PACKS_NAMES="" PACKS_COUNT=0 -PACKS_COLS="0 0 0" ########################### FUNCTIONS STARTS +# Load info from /etc/openwrt_release into memory +source_release() { + if is_file "$OPENWRT_RELEASE"; then + . "$OPENWRT_RELEASE" + fi +} + # get opkg packages listings and upgradable info opkg_init() { check_for_opkg @@ -67,6 +76,7 @@ opkg_init() { # main function main() { + source_release print_banner opkg_init upgrade_check # may exit here @@ -81,7 +91,7 @@ main() { else email_data="$(print_txt_email "$uplist")" fi - #local email_data="$(email_subject)"$'\n\n'"$(print_banner)"$'\n\n'"Report for: $(uname -n)"$'\n\n'"$uplist"$'\n\n'"Generated on: $TIMESTAMP" + #local email_data="$(email_subject)"$'\n\n'"$(print_banner)"$'\n\n'"Report for: $ROUTER_NAME"$'\n\n'"$uplist"$'\n\n'"Generated on: $TIMESTAMP" if just_print_html; then echo -e "$email_data" exit 0 @@ -96,6 +106,8 @@ main() { fi just_print && exit 0 opkg_has_update || { echo '' ; exit 0 ; } + + openwrt_is_snapshot && print_snapshot_disclaimer if ! no_confirm; then if ! confirm_upgrade; then @@ -126,14 +138,24 @@ list_upgrades() { if opkg_has_update; then echo "Packages available for upgrade: $PACKS_COUNT"$'\n' #echo -e "$PACKS" - prettyPrintPacks + print_packs_txt return $TRUE fi echo $'No packages to install!\n\n' return $FALSE } -prettyPrintPacks() { +# Print router info in plain text +print_info_txt() { + printf "%s\n" "Router name.: $ROUTER_NAME" + is_not_empty "$DISTRIB_DESCRIPTION" && printf "%s\n" "Description.: $DISTRIB_DESCRIPTION" + is_not_empty "$DISTRIB_TARGET" && printf "%s\n" "Target......: $DISTRIB_TARGET" + is_not_empty "$DISTRIB_ARCH" && printf "%s\n" "Arch........: $DISTRIB_ARCH" + echo "" +} + +# Pretty print package lists in plain text +print_packs_txt() { echo -ne "$PACKS" | awk ' function rep(c, n){ s=sprintf("%" n "s",""); gsub(/ /,c,s); return s } BEGIN{ j=1; } NR>0{ @@ -205,6 +227,11 @@ print_error() { echo "$@" >&2 } +# Prints Warning about upgrading beta/trunk versions +print_snapshot_disclaimer() { + 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!" +} + # prints program name and version print_banner() { [[ "$1" = 'error' ]] && { print_error $'\n'"$BANNERSTRING"$'\n' ; return $TRUE ; } @@ -333,17 +360,18 @@ print_html_email() { # prints an email report in txt format print_txt_email() { - echo "$(email_subject)"$'\n\n'"$(print_banner)"$'\n\n'"Report for: $(uname -n)"$'\n\n'"$1"$'\n\n'"Generated on: $TIMESTAMP" + #echo "$(email_subject)"$'\n\n'"$(print_banner)"$'\n\n'"Report for: $ROUTER_NAME"$'\n\n'"$1"$'\n\n'"Generated on: $TIMESTAMP" + echo "$(email_subject)"$'\n\n'"$(print_banner)"$'\n\n'"$(print_info_txt)"$'\n\n'"$1"$'\n\n'"Generated on: $TIMESTAMP" } # prints the packages html table print_html_table() { echo '
' - is_empty "$PACKS" && { echo '

No packages to install.

' ; return $TRUE ; } + is_empty "$PACKS" && { echo '

No packages to install.

' ; return $TRUE ; } local td_padding='padding-left:8px;padding-right:10px;padding-top:12px;padding-bottom:12px;' local td_open='' local th_open='' - echo '' + echo '
' echo ''$'\n\t'"$th_open"'#'$'\n\t'"$th_open"'Pack'$'\n\t'"$th_open"'Current'$'\n\t'"$th_open"'Update'$'\n''' # most of the table is generated using awk echo -ne "$PACKS" | \ @@ -354,9 +382,15 @@ awk 'BEGIN{ i=1; l=""; } { if (i % 2) l=""; else l=" style=\"background-color:#d # prints html email info print_html_header() { - echo $'\n\n''

'"$(print_banner 'nopadding')"'

' - echo '

'"Report for: $(uname -n)"'

' - echo '

'"Packages available for upgrade: $PACKS_COUNT"'

' + echo $'\n\n''

'"$(print_banner 'nopadding')"'

' + echo '
' + + echo '' + is_not_empty "$DISTRIB_DESCRIPTION" && echo '' + is_not_empty "$DISTRIB_TARGET" && echo '' + is_not_empty "$DISTRIB_ARCH" && echo '' + echo '' + echo '
Router Name'"$ROUTER_NAME"'
Description'"$DISTRIB_DESCRIPTION"'
Target'"$DISTRIB_TARGET"'
Arch'"$DISTRIB_ARCH"'
Updates Count'"$PACKS_COUNT"'
' } # prints html email mime type and format @@ -366,7 +400,7 @@ print_html_mime() { # prints html email info footer print_html_timestamp() { - echo $'\n''

'"Generated on: $TIMESTAMP by "'opkg-upgrade

'$'\n' + echo $'\n''

'"Generated on: $TIMESTAMP by "'opkg-upgrade

'$'\n' } # prints the email subject @@ -441,6 +475,12 @@ is_valid_email() { return $TRUE } +# returns $TRUE if it is a valid file, $FALSE otherwise +is_file() { + [ -f "$1" ] && return $TRUE + return $FALSE +} + # returns $TRUE if it is a valid folder, $FALSE otherwise is_dir() { [ -d "$1" ] && return $TRUE @@ -471,6 +511,13 @@ opkg_has_update() { return $TRUE } +# returns $TRUE if $DISTRIB_RELEASE equals SNAPSHOT +openwrt_is_snapshot() { + [ "$DISTRIB_RELEASE" = "SNAPSHOT" ] && return $TRUE + return $FALSE +} + + ###### OPERATION FLAGS