#!/bin/sh
export PATH=/opt/lnxall_app/bin:/opt/lnxall_app/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin

FILE_DIR=/opt/lnxall_app/smu_files
INST_DIR=/opt/elect_resources/resources/app

if [ ! -d "${FILE_DIR}" ] ; then
	echo "Warning, directory not found, skipped: ${FILE_DIR}"
	exit 1
fi
# 获取 FILE_DIR 目录下日期最新的zip文件
LATEST_ZIP=$(find "${FILE_DIR}" -type f -name "*.zip" -printf '%T+ %p\n' | sort -r | head -n 1 | cut -d' ' -f2-)

if [ -z "${LATEST_ZIP}" ] ; then
	echo "Warning, no zip file found, skipped: ${FILE_DIR}"
	exit 2
fi

if [ ! -d "${INST_DIR}" ] ; then
	mkdir -v -p "${INST_DIR}"
	[ $? -ne 0 ] && exit 2
fi

unzip -o -d "${INST_DIR}" "$LATEST_ZIP" && rm -rf "${FILE_DIR}"
[ $? -ne 0 ] && exit 3
#
if systemctl list-unit-files --type=service | grep -q "lightdm.service"; then
	systemctl restart lightdm
else
	systemctl restart nodejs
fi
exit $?
