#!/bin/sh
#
# Dieses Skript wird nur ein einziges Mal nach einem Upgrade oder der Erstinstallation ausgefuehrt:
#   http://wiki.openwrt.org/doc/uci#defaults
#


# shellcheck source=opennet/packages/on-core/files/usr/lib/opennet/on-helper.sh
. "${IPKG_INSTROOT:-}/usr/lib/opennet/on-helper.sh"


# packages that should be enabled by default (applied only during the first initialization)
DEFAULT_ENABLED_PACKAGES="on-olsr2"


# Die Einstellungen "use_olsrd_dns" und "use_olsrd_ntp" sind mit v0.5 hinzugekommen.
add_default_settings() {
	prepare_on_uci_settings
	# erzeuge die services-Node, falls noetig
	for setting in use_olsrd_dns use_olsrd_ntp; do
		[ -n "$(uci_get "on-core.settings.$setting")" ] && continue
		uci set "on-core.settings.$setting=1"
	done
	apply_changes on-core
}


# cron-Logging abschalten (bis auf Fehlermeldungen)
# siehe http://wiki.openwrt.org/doc/uci/system#system
disable_cron_logging() {
	uci set "system.@system[0].cronloglevel=9"
	apply_changes system
}


# verschiedene dnsmasq-Einstellungen
configure_dnsmasq() {
	# die Namensaufloesung im Opennet generiert auch 192.168er-Adressen - diese werden durch "rebind_protection" blockiert
	uci set "dhcp.@dnsmasq[0].rebind_protection=0"
	# keine Speicherung von DHCP leases
	uci set "dhcp.@dnsmasq[0].quietdhcp=1"
	# erlaube reverse lookup von 192er und 10er privaten IP Adressen
	uci set "dhcp.@dnsmasq[0].boguspriv=0"
	apply_changes dhcp
}


add_crontab_entries() {
	local crontab_file=/etc/crontabs/root
	local cron_prefix="[ -x /usr/bin/on-function ] && /usr/bin/on-function schedule_parts"
	local cron_suffix="2>&1 | logger -t cron-error"
	local random_hourly_minute=
	local random_daily_minute=
	# Erzeuge unterschiedliche Minuten-Werte (die nicht auf 0 oder 5 enden) für stündliche und
	# tägliche Cron-Jobs.
	while [ -z "$random_hourly_minute" ] \
			|| echo "$random_hourly_minute" | grep -q "[05]$"; do
		random_hourly_minute=$(get_random 60)
	done
	while [ -z "$random_daily_minute" ] \
			|| [ "$random_hourly_minute" = "$random_daily_minute" ] \
			|| echo "$random_daily_minute" | grep -q "[05]$"; do
		random_daily_minute=$(get_random 60)
	done
	line_in_file "$crontab_file" '^[^#].*\(schedule\|run\)_parts.*/etc/cron\.minutely' \
		"* * * * *		$cron_prefix /etc/cron.minutely '$cron_suffix' && /usr/bin/on-function run_with_cron_lock run_scheduled_tasks"
	line_in_file "$crontab_file" '^[^#].*\(schedule\|run\)_parts.*/etc/cron\.5mins' \
		"*/5 * * * *		$cron_prefix /etc/cron.5mins '$cron_suffix'"
	line_in_file "$crontab_file" '^[^#].*\(schedule\|run\)_parts.*/etc/cron\.hourly' \
		"$random_hourly_minute * * * *		$cron_prefix /etc/cron.hourly '$cron_suffix'"
	line_in_file "$crontab_file" '^[^#].*\(schedule\|run\)_parts.*/etc/cron\.daily' \
		"$random_daily_minute $(( $(get_random 3) + 3)) * * *		$cron_prefix /etc/cron.daily '$cron_suffix'"
	# es ist schwer zu pruefen, ob die Datei sich geaendert hat - also einfach neustarten
	/etc/init.d/cron restart
}


set_timezone_berlin() {
	# "zonename" ist bereits gesetzt? Wert beibehalten ...
	[ -n "$(uci_get "system.@system[0].zonename")" ] && return 0
	# Zone und Verschiebung setzen
	uci set "system.@system[0].zonename=Europe/Berlin"
	uci set "system.@system[0].timezone=CET-1CEST,M3.5.0,M10.5.0/3"
	uci commit system
}


enable_firewall_reload_trigger() {
	local script_path="/usr/lib/opennet/events/on-firewall-reload"
	create_uci_section_if_missing "firewall" "include" "path=$script_path" || return 0
	apply_changes firewall
}


enable_default_modules() {
	local module
	# prepare the uci settings for modules
	save_on_modules_list
	for module in $DEFAULT_ENABLED_PACKAGES; do
		enable_on_module "$module"
	done
}


reduce_uhttpd_requests() {
	if uci show uhttpd >/dev/null 2>&1 && [ "$(get_memory_size)" -le 32 ]; then
		uci set "uhttpd.main.max_requests=1"
		uci commit uhttpd
		reload_config
	fi
}


add_default_settings
disable_cron_logging
configure_dnsmasq
add_crontab_entries
set_timezone_berlin
enable_firewall_reload_trigger
enable_default_modules
reduce_uhttpd_requests
