#!/bin/bash
#
# Copyright (C) 2017 MOXA Inc. - https://www.moxa.com/
#
# Author: Wes Huang (黃淵河) <wes.huang@moxa.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#

rfs_resize () {
	expand2fs
	mv /sbin/expand2fs /sbin/expand2fs.bak
	sync
	/sbin/reboot -d -f -i
}

get_model_name () {
	local model_name=$(kversion | awk '{print $1}')

	case ${model_name} in
		UC-8112A-ME-T-LX)
			model_name="UC-8100A-ME-T-LX"
		;;
		UC-8112A-ME-T-LX-US)
			model_name="UC-8100A-ME-T-LX-US"
		;;
		UC-8112A-ME-T-LX-EU)
			model_name="UC-8100A-ME-T-LX-EU"
		;;
		UC-8112A-ME-T-LX-AP)
			model_name="UC-8100A-ME-T-LX-AP"
		;;
	esac

	echo ${model_name}
	#echo "UC-8100A-ME-LX"
}

one_time_setting () {
	local model_name=${1}

	# fix the issue that mount storage fuse-related filesystem via udev might be failed
	cp -a /lib/systemd/system/systemd-udevd.service /etc/systemd/system/
	sed -i 's/MountFlags=slave/MountFlags=shared/' /etc/systemd/system/systemd-udevd.service

	depmod -a

	# Enable bootloader to pass the fsck.repair parameter to kernel

	fw_setenv robust 1

	systemctl disable apt-daily.service
	systemctl disable apt-daily-upgrade.service

	# fix kernel 4.4 swap memory issue.
	if ! grep "vm.min_free_kbytes" /etc/sysctl.conf > /dev/null; then
		echo "vm.min_free_kbytes=16384" >> /etc/sysctl.conf
	fi
}

initial_wifi () {
	if dmesg | grep "idProduct=9378" > /dev/null; then
		systemctl start moxa-wifi-signald.service
	fi
}

initial_cellular () {
		systemctl start moxa-cellular-utils.service
		systemctl start moxa-cellular-signald.service
}

initial_tpm2 () {
	local tpm2=$(fw_printenv|grep ^tpm2 | cut -d '=' -f 2)

	# check tpm2 is exist or not.
	if [ X"${tpm2}" == X"1" ]; then
		# initial tpm2 interrupt pin and reset pin.
		init_gpio "112" "in"
		init_gpio "113" "out" "1"
		init_gpio "113" "out" "0"
		modprobe tpm
		modprobe tpm_spi_tis
		systemctl start tpm2-resourcemgr.service
	fi
}

init_gpio () {
	local gpio=${1}
	local direction=${2}
	local value=${3}

	if [ ! -e "/sys/class/gpio/gpio${gpio}" ]; then
		echo ${gpio} > "/sys/class/gpio/export"
		echo ${direction} > "/sys/class/gpio/gpio${gpio}/direction"
	fi
	if [ X"${direction}" == X"out" ]; then
		echo ${value} > "/sys/class/gpio/gpio${gpio}/value"
	fi
}

power_on_mini_pcie () {
	# The GPIO pin here has an invert gate, hence
	# 1 => off
	# 0 => on
	# Power => GPIO2_22 => gpio86
	# Reset => GPIO2_24 => gpio88

	local PWR_PIN=86
	local RST_PIN=88

	# The defualt power status is high (OFF in module side)

	# The defualt reset status is low (ON in module side) due to
	# the hardware limitation

	# Turn RST_PIN to high (OFF in module side)
	init_gpio 88 out 1

	# Turn PWR_PIN to low (ON in module side)
	init_gpio 86 out 0

	# Wait 0.5 seconds to start up the module
	sleep "0.5"
	# Turn RST_PIN to low (ON in module side)
	init_gpio 88 out 0
}

uc8100a_me_initial () {
	local model_name=$(get_model_name)

	ln -sf /etc/moxa-configs/configs/* /etc/moxa-configs/
	ln -sf /etc/moxa-configs/udev/* /etc/udev/rules.d/

	# only needs to initial one time
	if [ -f /sbin/expand2fs ]; then
		one_time_setting ${model_name}
		rfs_resize
	fi

	power_on_mini_pcie
	case ${model_name} in
		UC-8100A-ME-T-LX)
			initial_cellular
			initial_wifi
			;;
		UC-8100A-ME-T-LX-US|\
		UC-8100A-ME-T-LX-EU|\
		UC-8100A-ME-T-LX-AP)
			initial_cellular
			;;
		*)
			echo "unknown model name."
			;;
	esac

	initial_tpm2
}

umount_rfs () {
	umount -f -l /tmp
	umount -f -l /home
	umount -f -l /media
	umount -f -l /mnt
	umount -f -l /etc
	umount -f -l /root
	umount -f -l /opt
	umount -f -l /usr
	umount -f -l /bin
	umount -f -l /sbin
	umount -f -l /lib
	umount -f -l /var
	umount -f -l /overlayfs
}

stop_wireless () {
	cell_mgmt stop
	wifi_mgmt stop
	if lsmod | grep "^wlan" -q; then
		rmmod wlan
        fi
	init_gpio 86 out 1
}

uc8100a_me_stop () {
	stop_wireless
	umount_rfs
}

case "$1" in
    start)
	uc8100a_me_initial
	;;
    restart|reload|force-reload)
	echo "Error: argument '$1' not supported" >&2
	exit 3
	;;
    stop)
	uc8100a_me_stop
	;;
    *)
	echo "Usage: $0 start|stop" >&2
	exit 3
	;;
esac
