#!/bin/bash

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

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

cd /root

filecnt=0
for instfile in LNXALL_APP_ARMHF_*.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 "./${INSTALLER}" -n
                rm -rf /tmp/.lnxall_elf_n
        else
                nothup "./${INSTALLER}"
        fi
        sleep 15 ; sync
fi

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