#!/bin/bash

export NOTHUP_OUT=/root/lnxall_inst.log
export PATH=/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin

INSTALLER=
EXTRA_INST='/root/extra-boot-install.sh'

cd /root

filecnt=0
for instfile in LNXALL_APP*.elf ; do
	if [ -f "${instfile}" ] ; then
		if [ ${filecnt} -eq 0 ] ; then
			INSTALLER="${instfile}"
		else
			rm -rf -v "${instfile}"
		fi
		filecnt=$((filecnt + 1))
	else
		rm -rf -v "${instfile}"
	fi
done

if [ -n "${INSTALLER}" ] ; then
	echo "Found ELF package installer: ${INSTALLER}" >> "${NOTHUP_OUT}"
	chmod +x ${INSTALLER}
	if [ -e /tmp/.lnxall_elf_n ] ; then
		nothup bash -c "./${INSTALLER} -n ; rm -f ${INSTALLER}"
		rm -rf /tmp/.lnxall_elf_n
	else
		nothup bash -c "./${INSTALLER} ; rm -f ${INSTALLER}"
	fi
	sleep 15 ; sync
fi

if [ -f "${EXTRA_INST}" ] ; then
	chmod +x ${EXTRA_INST}
	nothup ${EXTRA_INST}
	sleep 5 ; sync
fi
sleep 600 ; sync
exit 0
