#!/bin/sh

get_option() {
	local variable="$1"
	local option="$2"

	local value="$(sed -rne 's/^[ \t]*'"$option"'[ \t]+(([^ \t\\]|\\.)+)[ \t]*$/\1/p' "$config" | tail -n1 | sed -re 's/\\(.)/\1/g')"
	[ -n "$value" ] || value="$(sed -rne 's/^[ \t]*'"$option"'[ \t]+'"'([^']+)'"'[ \t]*$/\1/p' "$config" | tail -n1)"
	[ -n "$value" ] || value="$(sed -rne 's/^[ \t]*'"$option"'[ \t]+"(([^"\\]|\\.)+)"[ \t]*$/\1/p' "$config" | tail -n1 | sed -re 's/\\(.)/\1/g')"
	[ -n "$value" ] || return 1

	export -n "$variable=$value"
	return 0
}

[ -e "/etc/openvpn.user" ] && {
	env -i ACTION="$ACTION" INSTANCE="$INSTANCE" \
		/bin/sh \
		/etc/openvpn.user \
		$*
}

# Wrap user defined scripts on up/down events
case "$ACTION" in
	up|down)
		if get_option command "$ACTION"; then
			exec /bin/sh -c "$command $ACTION $INSTANCE $*"
		fi
	;;
esac

exit 0
