#!/bin/sh /etc/rc.common
# ZeroBlock init script for OpenWrt

START=99
STOP=01

EXTRA_COMMANDS="update_lists status"
EXTRA_HELP="    update_lists  Update domain/subnet lists
	status        Show service status"

ZEROBLOCK=/usr/bin/zeroblock

start() {
	[ -x "$ZEROBLOCK" ] || return 1

	if ! uci show zeroblock 2>/dev/null | grep -q "=section"; then
		logger -t zeroblock "No sections configured, restoring DNS and skipping start"
		$ZEROBLOCK dns_restore 2>/dev/null
		return 0
	fi

	logger -t zeroblock "Starting ZeroBlock..."
	mkdir -p /tmp/zeroblock
	$ZEROBLOCK start
}

stop() {
	logger -t zeroblock "Stopping ZeroBlock..."
	[ -x "$ZEROBLOCK" ] && $ZEROBLOCK stop
}

reload() {
	stop
	start
}

restart() {
	stop
	start
}

update_lists() {
	logger -t zeroblock "Updating lists..."
	[ -x "$ZEROBLOCK" ] && $ZEROBLOCK update_lists
}

status() {
	if pgrep -x sing-box >/dev/null 2>&1; then
		echo "running"
	else
		echo "inactive"
	fi
}
