#!/sbin/openrc-run

description="Nginx HTTP and reverse proxy server"
description_checkconfig="Check configuration"
description_reload="Reload configuration without downtime"
description_reopen="Reopen log files"
description_upgrade="Upgrade the binary without downtime"

extra_commands="checkconfig"
extra_started_commands="reload reopen upgrade"

: ${cfgfile:="/etc/nginx/nginx.conf"}
: ${command:="/usr/sbin/nginx"}

command_args="-c $cfgfile $command_args"
rundir="/run/nginx"
pidfile="$rundir/nginx.pid"

required_files="$cfgfile"

depend() {
	need net
	use dns logger netmount
}

start_pre() {
	# Must be owned by the user running the master process (root by default),
	# which writes the pid file here, so the nginx user could abuse it with
	# symlinks.
	checkpath -d -m 0755 -o "${command_user:-root:root}" "$rundir" || return 1

	# nginx creates log files world-readable and they may contain sensitive
	# data, so create the default ones with restricted access. Existing
	# files are left alone, so that local changes aren't overridden. Other
	# logs are handled by logrotate after the first rotation.
	local path; for path in /var/log/nginx/access.log /var/log/nginx/error.log; do
		[ -e "$path" ] || checkpath -f -m 0640 -o nginx:adm "$path"
	done

	# nginx doesn't read resolv.conf for runtime lookups, so generate a
	# resolver for the optional include in nginx.conf. Skipped if the
	# directory isn't owned by root (nginx could plant symlinks there).
	if [ "$(stat -c %u "$rundir")" -eq 0 ]; then
		local servers="$(_get_nameservers)"

		if [ "$servers" ]; then
			echo "resolver$servers;" > "$rundir"/resolver.conf
		else
			ewarn "No usable nameserver in /etc/resolv.conf, not updating $rundir/resolver.conf"
		fi
	fi

	"$command" $command_args -t -q
}

checkconfig() {
	ebegin "Checking $RC_SVCNAME configuration"
	start_pre
	eend $?
}

reload() {
	ebegin "Reloading $RC_SVCNAME configuration"
	start_pre && start-stop-daemon --signal HUP --pidfile "$pidfile"
	eend $?
}

reopen() {
	ebegin "Reopening $RC_SVCNAME log files"
	start-stop-daemon --signal USR1 --pidfile "$pidfile"
	eend $?
}

upgrade() {
	start_pre || return 1

	ebegin "Upgrading $RC_SVCNAME binary"

	einfo "Sending USR2 to old binary"
	start-stop-daemon --signal USR2 --pidfile "$pidfile"

	einfo "Sleeping 3 seconds before pid-files checking"
	sleep 3

	if [ ! -f "$pidfile.oldbin" ]; then
		eerror "File with old pid ($pidfile.oldbin) not found"
		return 1
	fi

	if [ ! -f "$pidfile" ]; then
		eerror "New binary failed to start"
		return 1
	fi

	einfo "Sleeping 3 seconds before WINCH"
	sleep 3 ; start-stop-daemon --signal 28 --pidfile "$pidfile.oldbin"

	einfo "Sending QUIT to old binary"
	start-stop-daemon --signal QUIT --pidfile "$pidfile.oldbin"

	einfo "Upgrade completed"

	eend $? "Upgrade failed"
}

# Print the nameservers from resolv.conf, each prefixed with a space, in the
# format of the resolver directive: IPv6 addresses are in brackets and
# link-local ones are skipped, because nginx can't use them (zone ID).
_get_nameservers() {
	awk '$1 == "nameserver" && $2 !~ /%/ && tolower($2) !~ /^fe80:/ {
		printf " %s", ($2 ~ /:/ ? "[" $2 "]" : $2)
	}' /etc/resolv.conf 2>/dev/null
}
