#!/bin/sh
#  Copyright (c) 2019 Qualcomm Technologies, Inc.
#  All Rights Reserved.
#  Confidential and Proprietary - Qualcomm Technologies, Inc.


[ -z "$1" ] && echo "Error: should be run by rmnet" && exit 1
[ -z "$2" ] && echo "Error: should be run by rmnet" && exit 1

. /lib/functions.sh
. /lib/functions/network.sh
. /lib/netifd/netifd-proto.sh
setup_interface() {
	INTERFACE=$1
	CONFIG=/tmp/rmnet_$2_ipv4config
	logger "rmnet setup_interface $1 $2 here"
	#Fetch information from lower.
	[ -f ${CONFIG} ] || {
		proto_notify_error "$INTERFACE" "RMNET data call Not ready"
		proto_block_restart "$INTERFACE"
		return
	}
	. ${CONFIG} 
	ip=$PUBLIC_IP
	DNS=$DNSSERVERS
	router=$GATEWAY
	subnet=$NETMASK
	interface=$IFNAME
	#Send the information to the netifd
	proto_init_update "$interface" 1 1
	#ip and subnet
	proto_add_ipv4_address "$ip" "${subnet:-255.255.255.0}"

	#Any router? if not, remove below scripts
	#router format should be separated by space
	for i in $router; do
		proto_add_ipv4_route "$i" 32 "" "$ip"
		proto_add_ipv4_route 0.0.0.0 0 "$i" "$ip"
	done

	#dns information tell the netifd.
	for dns in $DNS; do
		proto_add_dns_server "$dns"
	done

	#Domain information tell the netifd
	for domain in $domain; do
		proto_add_dns_search "$domain"
	done

	#proto_add_data
	[ -n "$ZONE" ] && json_add_string zone "$ZONE"
	proto_close_data

	proto_send_update "$INTERFACE"

}

case "$1" in
	renew)
		setup_interface $2 $3
	;;
esac

exit 0
