#!/bin/sh

#  Description:
#
# natfree server tool to configure /etc/hosts with server configuration

# check environment
[ "$NATFREE_HOOK" ] || exit 0

# hosts file to write 
HOSTS_BASE_FILE="natfree-server"

# load common functions
. /usr/share/natfree-common/natfree-common

case "$NATFREE_HOOK" in
	post-up)
		[ "$NATFREE_ADDRESS" ] || exit 0
		# List of servers aliases to write (comma separated)
		# TODO: revise list ...
		SRV_ALIASES="$(get_srv_aliases)"
		SEARCH_DOMAIN="$(get_domain)"
		# remove network mask to get server IP
		SRV_IP="${NATFREE_ADDRESS%%/*}"
		[ -z "$SEARCH_DOMAIN" ] || SEARCH_DOMAIN=":${SEARCH_DOMAIN}"

		# generate .hosts file
		write_hosts_file "$HOSTS_BASE_FILE" "${SRV_IP}:${SRV_ALIASES}${SEARCH_DOMAIN}"
		;;

	post-down)
		delete_hosts_file "$HOSTS_BASE_FILE"
		;;
	*)
		exit 0
		;;
esac

# update /etc/hosts
update-hosts

exit 0

