#!/bin/sh

LNXALL_PLATSPEC=/opt/lnxall_app/platspec
export PATH=/opt/lnxall_app/bin:/opt/lnxall_app/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin

# intermediate variable for function `get_md5sum
MD5SUM_VAL=''
get_md5sum() {
	local file="$1"
	if [ ! -f "${file}" ] ; then
		MD5SUM_VAL=''
		return 1
	fi

	local md5val="$(md5sum ${file} 2>/dev/null | awk '{print $1}')"
	if [ -z "${md5val}" ] ; then
		MD5SUM_VAL=''
		return 2
	fi
	MD5SUM_VAL="${md5val}"
	return 0
}

lnxdf_systemd_inst() {
	local svrname="$1"
	local disabled="$2"
	local destdir="/usr/lib/systemd/system"

	get_md5sum "${LNXALL_PLATSPEC}/${svrname}"
	local md5_0="${MD5SUM_VAL}"
	if [ -z "${md5_0}" ] ; then
		echo "Installing '${svrname}'..."
		cp -v -f "${LNXALL_PLATSPEC}/${svrname}" "${destdir}/${svrname}" && \
			systemctl enable "${svrname}" && systemctl start "${svrname}"
		return $?
	fi

	get_md5sum "${destdir}/${svrname}"
	local md5_1="${MD5SUM_VAL}"
	if [ "${md5_0}" != "${md5_1}" ] ; then
		echo "Installing '${svrname}'..."
		if cp -v -f "${LNXALL_PLATSPEC}/${svrname}" "${destdir}/" ; then
			systemctl daemon-reload
			if [ -z "${disabled}" ]; then
				systemctl enable "${svrname}"
				systemctl start "${svrname}"
			fi
		fi
	fi
	return 0
}

lnxdf_systemd_disable() {
	while [ -n "$1" ] ; do
		echo "INFO: try to disable service with systemctl: $1"
        systemctl stop "$1"     >/dev/null 2>&1
        systemctl disable "$1"  >/dev/null 2>&1
		shift 1
	done
    return 0
}

# install bdhcpd.service, but do not enable it
lnxdf_bdhcpd_inst() {
	local APPDIR='/opt/lnxall_app'

	diff -q "${APPDIR}/platspec/bdhcpd.service" '/usr/lib/systemd/system/bdhcpd.service' 2>/dev/null
	if [ $? -ne 0 ] ; then
		cp -v -f "${APPDIR}/platspec/bdhcpd.service" /usr/lib/systemd/system/
		systemctl daemon-reload
	fi
}

# install lnxhostapd.service, but do not enable it:
lnxdf_hostapd_inst() {
	local APPDIR='/opt/lnxall_app'

	diff -q "${APPDIR}/platspec/lnxhostapd.service" '/usr/lib/systemd/system/lnxhostapd.service' 2>/dev/null
	if [ $? -ne 0 ] ; then
		cp -v -f "${APPDIR}/platspec/lnxhostapd.service" /usr/lib/systemd/system/
		systemctl daemon-reload
	fi
}

# initialize network and wireless configuration
lnxdf_network_inst() {
	local WAN_MACADDR="$(uci get network.wan.macaddr 2>/dev/null)"
	local APPDIR='/opt/lnxall_app'

	if [ ! -s '/etc/config/network' ] ; then
		if [ -e "${APPDIR}/platspec/etc-config-network" ] ; then
			cp -v -f "${APPDIR}/platspec/etc-config-network" '/etc/config/network'
			if [ -n "${WAN_MACADDR}" ] ; then
				uci set "network.wan.macaddr=${WAN_MACADDR}"
				uci commit network
			fi
			nothup /bin/sh -c 'sleep 5 && exec netuci'
		fi
	fi
}

lnxdf_wifi_inst() {
	local APPDIR='/opt/lnxall_app'

	if [ ! -s '/etc/config/wireless' ] ; then
		[ -e "${APPDIR}/platspec/etc-config-wireless" ] && \
			cp -v "${APPDIR}/platspec/etc-config-wireless" '/etc/config/wireless'
	fi
}

# symlink for factory script
lnxdf_factory_inst() {
	local APPDIR='/opt/lnxall_app'

	if [ ! -x '/usr/bin/factory' ] ; then
		[ -e "${APPDIR}/bin/factory" ] && rm -rf \
			'/usr/bin/factory' && ln -sv "${APPDIR}/bin/factory" '/usr/bin/'
	fi
}

# symlink for ssd_format.sh on RK3568
lnxdf_ssdformat_inst() {
	local APPDIR='/opt/lnxall_app'

	if [ ! -x '/usr/bin/ssd_format.sh' ] ; then
		[ -e "${APPDIR}/bin/ssd_format.sh" ] && rm -rf  \
			'/usr/bin/ssd_format.sh' && ln -sv "${APPDIR}/bin/ssd_format.sh" '/usr/bin/'
	fi
}

# symlink /etc/config for compatibility with openwrt
lnxdf_etcconfig_inst() {
	local APPDIR='/opt/lnxall_app'

	[ ! -e "${APPDIR}/etc/config" ] && mkdir -p "${APPDIR}/etc/config"
	[ ! -e '/etc/config' ] && ln -sv "${APPDIR}/etc/config" '/etc/config'
}
