Skip to content

Instantly share code, notes, and snippets.

@vuolter
Last active October 7, 2016 09:33
Show Gist options
  • Save vuolter/9754360 to your computer and use it in GitHub Desktop.
Save vuolter/9754360 to your computer and use it in GitHub Desktop.
OPKG Package Updater
#!/bin/sh
#
# Opkg Updater v1.4 - Update OPKG packages flawlessly
# @author: Walter Purcaro <[email protected]>
#
###############################################################################
VERSION="1.4"
trap 'echo Exiting...' INT TERM
info() {
echo "
╔═╗╔═╗╦╔═╔═╗┬ ┬┌─┐┌┬┐┌─┐┌┬┐┌─┐┬─┐
║ ║╠═╝╠╩╗║ ╦│ │├─┘ ││├─┤ │ ├┤ ├┬┘ Update OPKG packages flawlessly
╚═╝╩ ╩ ╩╚═╝└─┘┴ ─┴┘┴ ┴ ┴ └─┘┴└─ by Walter Purcaro <[email protected]>
####### Opkg Updater v$VERSION #######
"
}
help() {
info
echo ' USAGE:
opkgupdater.sh [COMMAND]
COMMANDS:
s <"cron time format"> Schedule start and exit
s off Stop scheduled start and exit
s status Show current scheduling and exit
EXAMPLES:
# Check for updates every 24 hours
opkgupdater.sh schedule "0 */24 * * *"
'
}
check() {
if [ -z "$(which opkg)" ]
then echo "OPKG Package Manager not found!" && return 1
else
ver="$(opkg --version | cut -d' ' -f3 | tr -d '.')"
[ $ver -gt 018 ] && echo "Opkg Updater may not operate properly with OPKG Package Manager newer than v0.1.8 ."
fi
}
schedule() {
ABSPATH="$(pwd -P)/$(basename "$0")"
cru a opkgupdater "$1 '$ABSPATH' start" && echo "Schedule added"
}
status() {
CRUCMD="$(cru l | grep opkgupdater | cut -d"'" -f1)"
if [ -n "$CRUCMD" ]
then echo "Scheduled mode is ON by $CRUCMD"
else echo "Scheduled mode is OFF"
fi
}
stop() {
status
[ -n "$CRUCMD" ] && cru d opkgupdater && echo "Schedule deleted"
}
start() {
check && [ $? -ne 0 ] && exit $?
echo "Checking for Updates..."
opkg update --verbosity=0
PACKUPGLIST="$(opkg list-upgradable 2>/dev/null)"
if [ -n "$PACKUPGLIST" ]
then
echo "Upgradable packages:"
echo "$PACKUPGLIST"
CONFILELIST="$(opkg list-changed-conffiles 2>/dev/null)"
if [ -n "$CONFILELIST" ]
then
echo "Detected configuration files modified after installation:"
echo "$CONFILELIST"
echo "These files won't be overwritten!"
fi
echo "Upgrading packages..."
UPGERR="/tmp/opkgupdater-uperr"
UPGOUT="$(opkg upgrade 2>$UPGERR)"
echo "$UPGOUT" | grep -v "Configuring"
echo "Configuring packages..."
CONFOUT="$(echo "$UPGOUT" | grep "Configuring")"
if [ -n "$CONFOUT" ]
then echo "$CONFOUT"
else echo "No packages configured."
fi
if [ -s $UPGERR ]
then
echo "Detected installation errors:"
tail -n +2 $UPGERR
fi
err=$(grep -c "opkg_install_pkg" $UPGERR)
upg=$(echo "$UPGOUT" | grep -c "Upgrading")
rm -f $UPGERR
if [ "$err" -ge "$upg" -a -z "$CONFOUT" ]
then
echo "Upgrade Failed."
exit 1
else
[ "$err" -ne 0 ] && echo "Some Packages can not be updated."
echo "Upgrade Successfully Completed."
echo "Cleaning up..."
opkg remove lib* --autoremove 2>/dev/null
fi
else
echo "No Packages marked for Update."
fi
}
### MAIN ###
if [ $# -eq 0 ]
then
info
start | logger -s -t "OPKG[$$]"
else
for arg in "$@"
do
case $arg in
-h|--help|help)
help
exit 1
;;
info|version|--version)
info
exit 1
;;
esac
done
while [ $# -ne 0 ]
do
case $1 in
s|schedule)
shift
if [ -n "$1" ]
then
if [ "$1" = "off" -o "$1" = "stop" ]
then stop
elif [ "$1" = "status" ]
then status
elif echo "$1" | grep -qE "[0-9,*/]+ [0-9,*/]+ [0-9,*/]+ [0-9,*/]+ [0-9,*/]+"
then schedule "$1"
else echo "Wrong time format argument"
fi
exit
else
echo "Missing time format argument"
help
exit 1
fi
;;
*)
echo "Invalid argument"
help
exit 1
;;
esac
shift
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment