#!/bin/sh

msg_output() {
	msg="$1"
	echo "$msg" > /tmp/out.txt
	cat /tmp/out.txt
	rm -rf /tmp/out.txt
}

is_openwrt() {
	if [ ! -f /etc/os-release ] ; then
		msg_output "Warning, /etc/os-release file missing, not openwrt device"
		return 1
	fi
	if [ -z "$(grep -i -e openwrt /etc/os-release)" ] ; then
		msg_output "Warning, not openwrt device, not found in os-release"
		return 2
	fi
	return 0
}

check_md5sum() {
	if [ ! -f "$1" ] ; then
		return 1
	fi
	local md5=$(md5sum "$1" | awk '{print $1}')
	if [ "${md5}" != "$2" ] ; then
		return 2
	fi
	return 0
}

actual_work() {
	check_md5sum /app/sysmonitor.lua 7a70f6ab32f25295c9640b65759a677a
	local res1=$?
	check_md5sum /app/diag_for_QIYANG_IMX6ULL_2.lua ca9009e5cda44ba5a5558656ebf2c856
	local res2=$?
	check_md5sum /app/diag.sh b67407d055195f74edb738e1e1fefc55
	local res3=$?
	lsmod | grep 8733bu 1>2 2>/dev/null
	local res4=$?
	opkg list | grep mmc-utils 1>2 2>/dev/null
	local res5=$?
	check_md5sum /app/moudleinfo c2f916039511dcfb3ae8b98266c63b5f
	local res6=$?

	if [ $res1 -eq 0 ] && [ $res2 -eq 0 ] && [ $res3 -eq 0 ] && [ $res4 -eq 0 ] && [ $res5 -eq 0 ] && [ $res6 -eq 0 ] ; then
		msg_output "wifi and 4g already update!"
		return 0
	fi

	if [ ! -f "/tmp/openwrt_files.tar.gz" ] ; then
		msg_output "wifi and 4g openwrt error!!"
		return 1
	fi

	tar -xzvf /tmp/openwrt_files.tar.gz -C /tmp
	if [ $? -ne 0 ] ; then
		msg_output "wifi and 4g openwrt error!!"
		return 2
	fi

	cd /tmp/openwrt_files
	if [ $res1 -ne 0 ] ; then
		chmod +x sysmonitor.lua && mv -f sysmonitor.lua /app/sysmonitor.lua 2>/dev/null
	fi
	if [ $res2 -ne 0 ] ; then
		chmod +x diag_for_QIYANG_IMX6ULL_2.lua && mv -f diag_for_QIYANG_IMX6ULL_2.lua /app/diag_for_QIYANG_IMX6ULL_2.lua 2>/dev/null
	fi
	if [ $res3 -ne 0 ] ; then
		chmod +x diag.sh && mv -f diag.sh /app/diag.sh 2>/dev/null
	fi
	if [ $res4 -ne 0 ] ; then
		chmod +x boot && mv -f boot /etc/init.d/ 2>/dev/null
		rmmod 8188eu.ko 2>/dev/null
		rmmod 8733bu.ko 2>/dev/null
		rmmod cfg80211.ko 2>/dev/null
		mv -f 8733bu.ko /lib/modules/4.1.0/ 2>/dev/null
		mv -f cfg80211.ko /lib/modules/4.1.0/ 2>/dev/null

		insmod /lib/modules/4.1.0/cfg80211.ko 2>/dev/null
		if [ $? -ne 0 ] ; then
			cd / && rm -f /tmp/openwrt_files /tmp/openwrt_files.tar.gz 2>/dev/null
			msg_output "wifi and 4g update error!!"
			return 4
		fi
		insmod /lib/modules/4.1.0/8733bu.ko 2>/dev/null
		if [ $? -ne 0 ] ; then
			cd / && rm -f /tmp/openwrt_files /tmp/openwrt_files.tar.gz 2>/dev/null
			msg_output "wifi and 4g update error!!"
			return 5
		fi
	fi
	if [ $res5 -ne 0 ] ; then
		opkg install mmc-utils_2024.03.05~e1281d4d-r1_arm_cortex-a7_neon-vfpv4.ipk 2>/dev/null
		if [ $? -ne 0 ] ; then
			cd / && rm -f /tmp/openwrt_files /tmp/openwrt_files.tar.gz 2>/dev/null
			msg_output "wifi and 4g update error!!"
			return 6
		fi
	fi
	if [ $res6 -ne 0 ] ; then
		chmod +x moudleinfo && mv -f moudleinfo /app/moudleinfo 2>/dev/null
	fi

	cd / && rm -f /tmp/openwrt_files /tmp/openwrt_files.tar.gz 2>/dev/null
	msg_output "wifi and 4g update okay!"
	return 0
}

if is_openwrt ; then
	actual_work
	exit $?
fi
exit 0
