#!/bin/sh

DEVICE=$(uci -q get qmi.global.device)
PIN1CODE=$(uci -q get qmi.global.pin)
APN=$(uci -q get qmi.global.apn)
USERNAME=$(uci -q get qmi.global.username)
PASSWORD=$(uci -q get qmi.global.password)
APN_AUTH=$(uci -q get qmi.global.auth)

_log() {
	echo "$@"
}

_err() {
	echo "$@" >&2
}

_qmicli() {
	qmicli -d "$DEVICE" -p $@
}

is_sim_unlocked() {
	local appstate=
	if ! _qmicli --uim-get-card-status 2>/dev/null | grep -q "Card state: 'present'" ; then
		_err "No Simcard present!"
		exit 1
	fi

	for i in $(seq 1 5) ; do
		appstate=$(_qmicli --uim-get-card-status 2>/dev/null | grep "Application state: " | head -n1 | awk '{ print $3 }' | sed "s/'//g")
		# checking state means we need to wait
		if [ "$appstate" = "check-personalization-state" ] ; then
			sleep 1
			continue
		else
			break
		fi
	done
	# setting return code
	[ "$appstate" = "ready" ]
}

unlock_sim() {
	# dont try pin when this is the last try
	if ! _qmicli --uim-get-card-status 2>/dev/null | grep -q "Card state: 'present'" ; then
		_err "No Simcard present!"
		exit 1
	fi

	retries=$(_qmicli --uim-get-card-status 2>/dev/null | grep "PIN1 retries:" | head -n1 | awk '{ print $3 }' | sed "s/'//g")
	if [ "$retries" != 2 ] && [ "$retries" != 3 ] ; then
		_err "PIN required but this is the last try! Don't try to use the last try"
		exit 1
	fi

	if [ -z "$PIN1CODE" ] ; then
		_err "Pin is required but not configured! Missing qmi.global.pin! Edit /etc/config/qmi"
		exit 1
	fi

	if ! _qmicli --uim-verify-pin=PIN1,$PIN1CODE 2>/dev/null >/dev/null; then
		_err "Wrong PIN code"
		exit 1
	fi
}

enable_internet() {
	# need to change interface format?
	local connect=""
	linuxtype=$(_qmicli --get-expected-data-format)
	qmitype=$(_qmicli  --wda-get-data-format |grep 'Link layer protocol:' | awk -F"'" '{ print $2 }')
	if [ "$linuxtype" != "$qmitype" ] ; then
		_log "$DEVICE doesnt match. linux: $linuxtype qmi: $qmitype"
		iface=$(_qmicli --get-wwan-iface)
		if [ -z "$iface" ] ; then
			_err "Could not get the interface device"
			exit 1
		fi

		ip link set "$iface" down
		_qmicli "--set-expected-data-format=$qmitype"
		ip link set "$iface" up
	fi

	_qmicli --device-open-sync --wds-reset
	_qmicli --wds-set-autoconnect-settings=disabled
	_qmicli --wds-stop-network=disable-autoconnect

	# The connect might return error with NoEffect, which is still ok
	connect="apn=$APN,ip-type=4"
	[ -n "$USERNAME" ] && connect="$connect,username=$USERNAME"
	[ -n "$PASSWORD" ] && connect="$connect,password=$PASSWORD"
	[ -n "$APN_AUTH" ] && connect="$connect,auth=$APN_AUTH"
	_qmicli "--wds-start-network=$connect" --wds-follow-network
}

wait_for_device() {
	if [ ! -e "$DEVICE" ] ; then
		_log "Waiting for network device $DEVICE"
		while [ ! -e "$DEVICE" ] ; do
			sleep 1 || exit 1
		done
	fi
}

if [ -z "$DEVICE" ] ; then
	_err "Missing device/qmi.global.device! Edit /etc/config/qmi"
	exit 1
fi

if [ -z "$APN" ] ; then
	_err "Missing APN/qmi.global.apn! Edit /etc/config/qmi"
	exit 1
fi

# wait at least 60 seconds to ensure the device is fully booted
wait_for_device
sleep 10

if ! is_sim_unlocked ; then
	unlock_sim
	if ! is_sim_unlocked ; then
		_err "Simcard is still locked!"
		exit 1
	fi
fi

enable_internet
