Subject: Re: coding numeric UID in config file
To: Geert Hendrickx <ghen@telenet.be>
From: Johnny C. Lam <jlam@pkgsrc.org>
List: tech-pkg
Date: 08/03/2007 09:53:42
Geert Hendrickx wrote:
> 
> I'm creating a package for a program that should (preferably) run as a
> dedicated unprivileged user, so I've added PKG_USERS and all that.  However
> this program expects a numeric uid and gid in its config file, it won't
> accept a name like most programs do.  The numeric uid and gid have to be
> looked up in an INSTALL script (using ${ID}) because binary packages cannot
> know them in advance.  The problem is that the PRE-INSTALL target is too
> early: the example config file is not extracted yet, and POST-INSTALL is
> too late: the example config file has already been copied to PKG_SYSCONFDIR
> (via CONF_FILES), and I don't want the package to mess with live config
> files (e.g. renaming to .bak can be dangerous because folks may use that in
> their PKG_SYSCONFDIR).
> 
> I've been grepping through other INSTALL files and I can't find others
> doing similar things.  Is there a way to accomplish the above?

You want to do it at POST-INSTALL.  Don't manage that example config 
file via a CONF_FILES.  Instead, do your own management where you do sed 
replacement in the config file template and compare-and-copy to the 
final location.  For example, something like the following untested code:

# Use the package metadata directory as a scratch location to keep the
# example foo.conf, generated from the foo.conf template.
#
case ${STAGE} in
POST-INSTALL)
	case "$_PKG_CONFIG" in
	yes)
		file="${PKG_SYSCONFDIR}/foo.conf"
		f_eg="${PKG_PREFIX}/share/examples/foo/foo.conf"
		if [ -f $file ]; then
			${ECHO} "${PKGNAME}: $file already exists"
		else
			pkguid=`${ID} foouser`
			pkggid=`${ID} -g foogroup`
			${SED} -e "s,@pkguid@,$pkguid,g" \
			       -e "s,@pkggid@,$pkggid,g" \
			$f_eg > ./foo.conf
			${ECHO} "${PKGNAME}: creating $file from $f_eg"
			${CP} ./foo.conf $file
		fi
		;;
	esac
	;;

DEINSTALL)
	case "$_PKG_CONFIG" in
	yes)
		file="${PKG_SYSCONFDIR}/foo.conf"
		if [ -f ./foo.conf -a -f $file ] &&
		   ${CMP} -s "$file" ./foo.conf; then
			${RM} -f "$file" ./foo.conf
		fi
		;;
	esac
esac

	Cheers,

	-- Johnny Lam <jlam@pkgsrc.org>