#!/bin/sh

# Created by jiaqiang.ye@lnxall.com
# Simple IPK files installer script
# 2025/08/18

LINO_END=53
INST_SCRIPT="./ipkg-inst-list.sh"
export PATH=/opt/lnxall_app/bin:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin

# Create working directory
WORKDIR="$(mktemp -d /tmp/ipkg_inst_XXXXXX)"
if [ ! -d "${WORKDIR}" ] ; then
	echo "Error, failed to create temporary directory."
	exit 1
fi

opkg_do_inst() {
	echo "INFO: installing package file: $1 ..."
	opkg install --force-reinstall --force-overwrite "./$1"
	if [ $? -ne 0 ] ; then
		echo "ERROR: failed to install package: $1"
		rm -rf "${WORKDIR}"
		exit 2
	fi
	echo "IPK file installed successfully: $1"
	return 0
}

tail -n "+${LINO_END}" "$0" | bzip2 -d -c | tar -x -f - -C "${WORKDIR}"
if [ $? -ne 0 ] ; then
	echo "Error, failed to extract packed IPK files."
	rm -rf "${WORKDIR}"
	exit 3
fi

cd "${WORKDIR}"
if [ $? -ne 0 ] ; then
	echo "Error, failed to change into directory: ${WORKDIR}"
	rm -rf "${WORKDIR}"
	exit 4
fi

if [ ! -f ${INST_SCRIPT} ] ; then
	echo "Error, file not found: ${INST_SCRIPT}"
	exit 5
fi

. "${INST_SCRIPT}"
rm -rf "${WORKDIR}"
exit 0
#################################################### BEGIN-OF-RAWDATA
