#!/bin/sh

[ -n "$INCLUDE_ONLY" ] || {
	. /lib/functions.sh
	. ../netifd-proto.sh
	init_proto "$@"
}

proto_qcm_init_config() {
	available=1
	no_device=1
	proto_config_add_string "device:device"
	proto_config_add_string "ifname"
	proto_config_add_string "apn"
	proto_config_add_string "pincode"
	proto_config_add_string "auth"
	proto_config_add_string "username"
	proto_config_add_string "password"
	proto_config_add_string "node"
	proto_config_add_int "mtu"
	proto_config_add_defaults
}

proto_qcm_setup() {
	local interface="$1"
	local devpath=""
	local devname=""

	local device ifname  apn pincode ifname auth username password  node $PROTO_DEFAULT_OPTIONS mtu band_enable band_list
	json_get_vars device ifname apn pincode auth username password  node $PROTO_DEFAULT_OPTIONS mtu	band_enable band_list
	local ipv6=`uci get glipv6.globals.enabled 2>/dev/null`

	. /lib/functions/modem.sh
	modem_AT_set_roaming
	modem_AT_set_band

	case $auth in
	"PAP") auth=1 ;;
	"CHAP") auth=2 ;;
	"PAP or CHAP") auth=3 ;;
	"PAP/CHAP") auth=3 ;;
	*) auth=0 ;;
	esac

	if [ -n "$node" ];then
		devpath="$(find  /sys/devices/ -name "$node" 2>/dev/null)"
		devname="$(find "$devpath" -name  "cdc-wdm*" 2>/dev/null)"
		devname="$(basename "$devname")"
	else
 		devname="$(basename "$device")"

		if [ ${devname/mhi//} == $devname ];then
			devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
			bus="$(basename "$devpath"|cut -d ':' -f 1)"
		else
			devpath="$(readlink -f /sys/class/net/rmnet_mhi0/device)"
			bus="$(basename $(dirname "$devpath"))"
			echo 0x1 > ${devpath}/net/rmnet_mhi0/link_state
		fi
	fi
	
	if [ -n "$node" ];then
		#fix config
		[ "$devname" = "$(basename "$device")" ] || {
			[ -n "$devname" ] && uci set network."$interface".device="/dev/${devname}" && uci commit
		}
	fi

	device="$(readlink -f $device)"
	[ -c "$device" ] || {
		echo "The specified control device does not exist"
		proto_notify_error "$interface" NO_DEVICE
		proto_set_available "$interface" 0
		return 1
	}

	ifname="$( ls "$devpath"/net )"
	apn_use=1
	profile_id=$(uci -q get network.$interface.id)
	if [ -n "$profile_id" ]; then
		apn_use=$(uci -q get apnprofile.$profile_id.apn_use)
	fi
	pdp_type='-4'
	bus_str=$(echo $interface|cut -d '_' -f 2)
	if [ ${#bus_str} -eq 1 ]; then
		modem_bus=$(echo $INTERFACE | sed 's/modem_//g' | sed 's/_/-/' | sed 's/_/./')
	else
		modem_bus="$(get_modem_bus)"
	fi
	if [ "$ipv6" = "1" ]; then
		pdp_type='-4 -6'
		if [ "$apn_use" != "3" ]; then
			[ -n "$modem_bus" ] && gl_modem -B $modem_bus AT AT+CGDCONT=1,\"IPV4V6\"
		fi
	fi

	[ -z "$apn" ] && {
		username=""
		password=""
		auth=""
	}

	[ -z "$username" ] && {
		password=""
		auth=""
	}

	[ -n "$mtu" ] && {
		echo "Setting MTU to $mtu"
		/sbin/ip link set dev $ifname mtu $mtu
	}

	if [ "$apn_use" = "3" ]; then
		pdp_type='-4 -6'
		proto_run_command "$interface" qcm ${pdp_type:=-4 -6} \
			${cid:=-n 3} \
			${apn:+-s $apn} \
			${username:+ $username} \
			${password:+ $password} \
			${auth:+ $auth} \
			${pincode:+-p $pincode}
	else
		proto_run_command "$interface" qcm ${pdp_type:=-4 -6} \
			${apn:+-s $apn} \
			${username:+ $username} \
			${password:+ $password} \
			${auth:+ $auth} \
			${pincode:+-p $pincode}
	fi

	# proto_run_command "$interface" qcm ${apn:+-s $apn} \
	# 		${username:+ $username} \
	# 		${password:+ $password} \
	# 		${auth:+ $auth} \
	# 		${pincode:+-p $pincode}

        proto_init_update "$ifname" 1                                                                                     
        proto_set_keep 1                                                                                     
        proto_send_update "$interface"

	time=`date '+%s'`
	json_init
	json_add_string name "${interface}_4"
	json_add_string ifname "@$interface"
	json_add_string proto "dhcp"
	json_add_string date "$time"
	proto_add_dynamic_defaults
	ubus call network add_dynamic "$(json_dump)"

	return 0
}

proto_qcm_teardown() {
	local interface="$1"
	proto_kill_command "$interface"
}

[ -n "$INCLUDE_ONLY" ] || {
	add_protocol qcm
}
