#!/bin/sh
#
# $FreeBSD$
#

# PROVIDE: ix-etc
# REQUIRE: earlykld ix-syncdisks
# BEFORE: fsck

. /etc/rc.subr

copy_templates()
{
	# Copy over FreeNAS specific /etc template files
	for file in $(find /etc | grep '.template$' 2>/dev/null)
	do
		nfile=$(echo $file | sed 's|.template||g')
		cp ${file} ${nfile}
		if [ $? -ne 0 ] ; then
			echo "WARNING: Failed to copy template file $file -> $nfile"
		fi

		# Special handling for login.conf file that needs cap_mkdb run
		if [ "$nfile" = "/etc/login.conf" ] ; then
			cap_mkdb -f /etc/login.conf.db /etc/login.conf
		fi
	done
}

generate_etc()
{
	copy_templates

	[ -s /data/dhparam.pem ] || openssl dhparam -rand /dev/random 2048 > /data/dhparam.pem

	LD_LIBRARY_PATH=/usr/local/lib /usr/local/bin/midclt call etc.generate_all > /dev/null

	# etc rc plugin will generate rc.conf.freenas which needs to be re-read by /etc/rc script
	killall -ALRM sh
}

name="ix-etc"
start_cmd='generate_etc'
stop_cmd=':'

load_rc_config $name
run_rc_command "$1"
