#!/bin/sh /etc/rc.common

START=99
STOP=15
USE_PROCD=1

SCUTCLIENT_PATH=/usr/bin/scutclient
SCUTCLIENT_CONFIG=scutclient

EXTRA_COMMANDS="logoff"
EXTRA_HELP=<<EOF
	logoff	Log off manually
EOF

scutclient_validate_drcom() {
	uci_validate_section scutclient drcom "${1}" \
		'dns:ip4addr:222.201.130.30' \
		'hostname:string:Lenovo-PC' \
		'server_auth_ip:ip4addr:202.38.210.131' \
		'version:string:4472434f4d0096022a' \
		'onlinehook:string' \
		'offlinehook:string' \
		'nettime:string' \
		'hash:string:2ec15ad258aee9604b18f2f8114da38db16efd00' || exit 1
}

scutclient_validate_scutclient() {
	uci_validate_section scutclient scutclient "${1}" \
		'username:string' \
		'password:string' \
		'interface:string:wan' || exit 1
}

scutclient_validate_option() {
	uci_validate_section scutclient option "${1}" \
		'enable:bool:0' \
		'debug:bool:0' || exit 1
}

scutclient_load_config() {
	config_load scutclient
	config_foreach scutclient_validate_option option
	config_foreach scutclient_validate_drcom drcom
}

scutclient_start_instance() {
	local cfg="$1"
	local username password interface device ifname
	scutclient_validate_scutclient "$cfg"
	uci_validate_section network interface "$interface" 'ifname:string' 'device:string'
	[ -z "$device" ] || ifname=$device
	procd_open_instance "scutclient_${cfg}_${interface}"
	procd_set_param stdout 1
	procd_set_param stderr 1
	procd_set_param netdev $ifname
	procd_set_param command "$SCUTCLIENT_PATH"
	procd_append_param command --username "$username" \
		--password "$password" \
		--iface "$ifname" \
		--dns "$dns" \
		--hostname "$hostname" \
		--udp-server "$server_auth_ip" \
		--cli-version "$version" \
		--hash "$hash"
	[ "$debug" -ge 1 ] && procd_append_param command --debug
	[ -n "$onlinehook" ] && procd_append_param command --online-hook "$onlinehook"
	[ -n "$offlinehook" ] && procd_append_param command --offline-hook "$offlinehook"
	[ -n "$nettime" ] && procd_append_param command --net-time "$nettime"
	procd_close_instance
}

scutclient_add_triggers() {
	local cfg="$1"
	local username password interface
	scutclient_validate_scutclient "$cfg"
	procd_add_interface_trigger "interface.*" $interface /etc/init.d/scutclient restart
}

scutclient_logoff() {
	local cfg="$1"
	local username password interface ifname
	scutclient_validate_scutclient "$cfg"
	uci_validate_section network interface "$interface" 'ifname:string'
	$SCUTCLIENT_PATH --iface $ifname --logoff
}

service_triggers() {
	local enable debug
	config_load scutclient
	config_foreach scutclient_validate_option option
	config_foreach scutclient_add_triggers scutclient
	#wan force_link is enabled by default when configured as static IP, which won't trigger hotplug events.
	#Add wan6 and dhcp here as a backup.
	procd_add_interface_trigger "interface.*" wan6 /etc/init.d/scutclient restart
	procd_add_reload_trigger "dhcp"
	procd_add_reload_trigger "$SCUTCLIENT_CONFIG"
	procd_open_validate
	scutclient_validate_option
	scutclient_validate_scutclient
	scutclient_validate_drcom
	procd_close_validate
}

start_service() {
	local dns hostname server_auth_ip version hash username password enable debug ifname authexe nettime
	scutclient_load_config
	[ $enable -eq 0 ] && exit
	config_foreach scutclient_start_instance scutclient
}

reload_service() {
	restart
}

boot() {
	return
}
