#!/bin/sh /etc/rc.common
# Copyright (C) 2015 OpenWrt.org

START=90
STOP=10

USE_PROCD=1
PROG=/usr/lib/sysstat/sadc
SYSSTAT_CFG="/etc/sysstat/sysstat"

validate_sysstat_section() {
	uci_validate_section sysstat sysstat "${1}" \
		'log_history:uinteger' \
		'compressafter:uinteger' \
		'sadc_options:string' \
		'sa_dir:string' \
		'zip:string' \
		'enabled:string'
}

start_service() {

	local log_history compressafter sadc_options sa_dir zip enabled

	validate_sysstat_section sysstat || {
		echo "validation failed"
		return 1
	}

	[ -d /var/log/sysstat ] || mkdir -p /var/log/sysstat

	echo "HISTORY=$log_history" > $SYSSTAT_CFG
	echo "COMPRESSAFTER=$compressafter" >> $SYSSTAT_CFG
	echo "SADC_OPTIONS=\"$sadc_options\"" >> $SYSSTAT_CFG
	echo "SA_DIR=\"$sa_dir\"" >> $SYSSTAT_CFG
	echo "ZIP=\"$zip\"" >> $SYSSTAT_CFG
	echo "ENABLED=\"$enabled\"" >> $SYSSTAT_CFG

	procd_open_instance
	procd_set_param command $PROG -S DISK -F -L -
	#procd_set_param limits core="unlimited"
	procd_set_param file $SYSSTAT_CFG
	procd_close_instance
}

service_triggers()
{
	procd_add_reload_trigger "sysstat"
	procd_add_validation validate_sysstat_section
}
