#!/bin/bash

. ${TOOLKIT}-functions.sh

#----------------------------------------------------------------
# required users
#----------------------------------------------------------------
[ "X$(getent passwd prjadmin)" = "X" ] && {
	putwarning "USER prjadmin" \
		"need to create the default user 'prjadmin', please" \
		"launch 'base-users' modules to fix his settings\n\n" \
		"RUN: kusa-reconf -s base-users base-users"
	useradd -u 2901 -g users prjadmin
	sleep 2
}

#----------------------------------------------------------------
# records services definitions to /etc/default/kusa-services
#----------------------------------------------------------------
echo -en "  services: "
base="kusa-services"
outfile="kusa-services.tmp"
cp $base $outfile || exit $?
for service in `jtconf --listvars service`
do
	echo -n "$service "
	value=`jtconf service.$service 2>/dev/null` || :	# can be 'undef' -> ""
	# varname, upcase all, minus as undescores
	var=$(echo "KUSA_SERVICE_$service" | tr '[a-z]' '[A-Z]' | tr '-' '_')
	echo "$var=\"$value\"" >>$outfile
done
echo ""



#------------------------------------------------------------------------
# records netservices ports definitions to /etc/default/kusa-netservices
#------------------------------------------------------------------------
echo -en "  netservices: "
base="kusa-netservices"
outfile="kusa-netservices.tmp"
cp $base $outfile || exit $?
for service in `jtconf --listvars netservice`
do
	value=`jtconf netservice.$service`
	echo -n "$service($value) "
	# varname, upcase all, minus as undescores
	var=$(echo "KUSA_SERVICE_$service" | tr '[a-z]' '[A-Z]' | tr '-' '_')
	echo "$var=\"$value\"" >>$outfile
done
echo ""

#------------------------------------------------------------------------
# records paths definitions to /etc/default/kusa-paths
#------------------------------------------------------------------------
echo -en "  paths: "
base="kusa-paths"
outfile="kusa-paths.tmp"
cp $base $outfile || exit $?
for path in `jtconf --listvars path`
do
	value=`jtconf path.$path`
	echo -n "$path "
	# varname, upcase all, minus as undescores
	var=$(echo "KUSA_PATH_$path" | tr '[a-z]' '[A-Z]' | tr '-' '_')
	echo "$var=\"$value\"" >>$outfile
done
echo ""

exit 0
