Converting this script to use apk "Identify packages to be re-installed after system upgrade"

I have taken this very helpful script and converted all of the opkg commands to apk BUT I am not a full blown programmer I just putz around sometimes.....

I have never seen awk and there is an important section that needs to be fixed.to use apk command.

I have not fully tested this yet wanted to fix this first.

I have put my initials next to any line I have changed and I know it messes up the text formatting which I will fix but wanted to at least show the lines that changed. I have determined that the apk info -R command will give the dependencies but of course the format is not the same as opkg.

Here is the part of script that I am struggling with especially since I never saw the output of the opkg command.

apk info -R "$PACKAGE" |                          #
#            awk "/^Depends: / {
#                                sub(\"Depends: \", \"\");   \
#                                gsub(\", \", \"\\n\");      \
#                                print >> \"$PREQLIST\";      \
#                              }"
        Fi

apk info -R block-mount
block-mount-2026.02.15~8d377aa6-r1 depends on:
libblobmsg-json20260213
libc
libjson-c5
libubox20260213
libuci20250120
ubox

Here is full pgm

#! /bin/sh
set -x

Write a list of packages currently installed or read that list,

presumably after a firmware upgrade, in order to reinstall all packages

on that list not currently installed

(c) 2013 Malte Forkel malte.forkel@berlin.de

Originally found on OpenWrt forums at:

https://forum.openwrt.org/viewtopic.php?pid=194478#p194478

Thanks, too, to hnyman for important comments on this script

Version history

0.2.3 - Changed opkg command to apk for OpenWrt 25.12.0 release and newer versions SJK

0.2.2 - editorial tweaks to help text -richb-hanvover

0.2.1 - fixed typo in awk script for dependency detection

0.2.0 - command interface

0.1.0 - Initial release

PCKGLIST=/etc/config/apk.installed # default package list #SJK added path and file name
SCRIPTNAME=$(basename $0) # name of this script
COMMAND="" # command to execute

INSTLIST=$(mktemp) # list of packages to install
PREQLIST=$(mktemp) # list of prerequisite packages

UPDATE=false # update the package database
APKOPT="" # options for apk calls #SJK changed opkg to apk
VERBOSE=false # be verbose

#SJK commented to test cleanup () {
#SJK commented to test rm -f $INSTLIST $PREQLIST
#SJK Commented to test }

echo_usage () {
echo
"Usage: $(basename $0) [options...] command [packagelist]

Available commands:
help print this help text
write write a list of currently installed packages
install install packages on list not currently installed
script output a script to install missing packages

Options:
-u update the package database
-t test only, execute apk commands with --simulate #SJK changed --noaction to --simulate
-v be verbose

$SCRIPTNAME can be used to re-install those packages that were installed
before a firmware upgrade but are not part of the new firmware image.

Before the firmware upgrade, execute

$SCRIPTNAME [options...] write [packagelist]

to save the list of currently installed packages. Save the package list in a
place that will not be wiped out by the firmware upgrade. The default package list
is '$PCKGLIST', which works well for normal sysupgrades. Or copy that file to
another computer before the upgrade if you are not preserving the settings.

After the firmware upgrade, execute

$SCRIPTNAME -u                #SJK to update catalogs then do install 
$SCRIPTNAME [options...] install [packagelist]

to re-install all packages that were not part of the firmware image.
By default, the script will use the previously-created '$PCKGLIST'.
Alternatively, you can execute

$SCRIPTNAME [options...] script [packagelist]

to output a shell script that will contain calls to apk to install those "SJK change opkg to apk"
missing packages. This might be useful if you want to check which packages
would be installed or if you want to edit that list. " SJK spellcheck or was of"

In order for this script to work after a firmware upgrade or reboot, the
apk database must have been updated. You can use the option -u to do this. "SJK change opkg to apk"

You can specify the option -t to test what $SCRIPTNAME would do. All calls
to apk will be made with the option --simulate. This does not influence "SJK change opkg to apk and noaction to simulate"
the call to apk to write the list of installed packages, though. "SJK change opkg to apk"
"
}

trap cleanup SIGHUP SIGINT SIGTERM EXIT

parse command line options

while getopts "htuvw" OPTS; do
case $OPTS in
t )
OPKGOPT="$OPKGOPT --simulate";; #SJK changed --noaction to --simulate
u )
UPDATE=true;;
v )
VERBOSE=true;;
[h?*] )
echo_usage
exit 0;;
esac
done

fi #SJK corrected error needed done not fi

shift $(($OPTIND - 1))

Set the command

COMMAND=$1

Update

if $UPDATE; then
apk $APKOPT update #SJK change opkg to apk
#exit 0
fi

Set name of the package list

if [ "x$2" != "x" ]; then
PCKGLIST="$2"
fi

Help

if [ "x$COMMAND" == "x" ]; then
echo "No command specified."
echo ""
COMMAND="help"
fi

if [ $COMMAND == "help" ]; then
echo_usage
exit 0
fi

Update

#if $UPDATE; then

apk $APKOPT update #SJK change opkg to apk

#exit 0
#done
#fi

Write

if [ $COMMAND = "write" ] ; then
if $VERBOSE; then
echo "Saving package list to $PCKGLIST"
fi
# NOTE: option --simulate not valid for list-installed #SJK changed --noaction to --simulate
apk list --installed > "$PCKGLIST"
exit 0
fi

Update

#if $UPDATE; then

apk $APKOPT update #SJK change opkg to apk

#fi

Check

if [ $COMMAND == "install" ] || [ $COMMAND == "script" ]; then
# detect uninstalled packages
if $VERBOSE && [ $COMMAND != "script" ]; then
echo "Checking packages... "
fi

cat "$PCKGLIST" | while read PACKAGE SEP VERSION; do # SJK
# apk status is much faster than apk info #SJK not sure if this is true
# it only returns status of installed packages
if ! apk list $PACKAGE | grep -q "[installed]"; then #"SJK change opkg to apk
if [ "x$(apk list $PACKAGE)" == "x" ]; then #SJK change opkg to apk
# collect uninstalled packages
echo $PACKAGE >> $INSTLIST
# collect prerequisites
apk info -R "$PACKAGE" | #

awk "/^Depends: / {

sub("Depends: ", ""); \

gsub(", ", "\n"); \

print >> "$PREQLIST"; \

}"

    fi

done #SJK corrected error needed fi not done

fi

Install or script

if [ $COMMAND == "install" ]; then
# install packages
cat "$INSTLIST" | while read PACKAGE; do
if grep -q "^$PACKAGE$" "$PREQLIST"; then
# prerequisite package, will be installed automatically
if $VERBOSE; then
echo "$PACKAGE installed automatically"
fi
else
# install package
apk add $OPKGOPT $PACKAGE #SJK change opkg install to apk add
fi
done
elif [ $COMMAND == "script" ]; then
# output install script
echo "#! /bin/sh"
cat "$INSTLIST" | while read PACKAGE; do
if ! grep -q "^$PACKAGE$" "$PREQLIST"; then
echo "apk add $PACKAGE" #SJK change opkg install to apk add
fi
done
else
echo "Unknown command '$COMMAND'."
echo ""
echo_usage
exit 1
fi

clean up and exit

exit 0
done #SJK added done was getting error
fi

type or paste code here

....

There's an example of the output at https://openwrt.org/docs/guide-user/additional-software/opkg

Package: horst
Version: 2.0-rc1-2
Depends: libncurses
Provides:
Status: install user installed
Section: net
Architecture: ar71xx
Maintainer: Bruno Randolf <br1@einfach.org>
MD5Sum: 378cea9894ec971c419876e822666a6a
Size: 19224
Filename: horst_2.0-rc1-2_ar71xx.ipk
Source: feeds/packages/net/horst
Description: [horst] is a scanning and analysis tool for 802.11 wireless networks and
 especially IBSS (ad-hoc) mode and mesh networks (OLSR).

From the output of apk you posted, it seems each dependency is already in its own line, so you probably don't even need awk for that, just remove the first line and append the result to $PREQLIST.

Thanks this was helpful