#!/bin/sh
# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# Copyright 2006 PLD Linux
# $Header$

. /etc/rc.d/init.d/functions || exit 1

if [ "$(id -u)" != 0 ]; then
	nls "%s: must be root." $0
	exit 1
fi

usage() {
echo "usage: env-update.sh

note:
	  This utility generates /etc/profile.env and /etc/csh.env
	  from the contents of /etc/env.d/
"
	exit 1
}

# bool is_older_than(reference, files/dirs to check)
#
#   return 0 if any of the files/dirs are newer than
#   the reference file
#
#   EXAMPLE: if is_older_than a.out *.o ; then ...
is_older_than() {
	local x=
	local ref="$1"
	shift

	for x in "$@" ; do
		[[ ${x} -nt ${ref} ]] && return 0

		if [[ -d ${x} ]] ; then
			is_older_than "${ref}" "${x}"/* && return 0
		fi
	done

	return 1
}

export SVCDIR="/var/cache"

# Only update if files have actually changed
if [ "$1" == "-u" ]; then
	is_older_than "${svcdir}/envcache" /etc/env.d && exit 0
	shift
fi

if [ "$#" -ne 0 ]; then
	usage
else
	umask 002
	gawk \
		-f /lib/functions.awk \
		-f /lib/genenviron.awk
fi

# vim:ts=4
