Subject: mailing MESSAGE (was: is the original username recorded)
To: None <tech-pkg@netbsd.org>
From: Jeremy C. Reed <reed@reedmedia.net>
List: tech-pkg
Date: 07/31/2003 23:15:23
On Thu, 31 Jul 2003, Jeremy C. Reed wrote:

> Does make or pkgsrc have the original username recorded in any variables?
>
> I didn't see it, but I am not sure if it is done in make itself.

Anyways, I was trying to figure out how to record the original username
before the do-su-install. Because I wanted the message emailed to give the
username installing the package.

Please see below and share comments. Notice that it does the MESSAGE
output outside of the SU_CMD. (I don't think superuser privilege is
needed; and it makes it so the email is sent by the user doing the install
too.)

I have more comments below patch.

Index: mk/bsd.pkg.mk
===================================================================
RCS file: /cvsroot/pkgsrc/mk/bsd.pkg.mk,v
retrieving revision 1.1227
diff -b -u -r1.1227 bsd.pkg.mk
--- mk/bsd.pkg.mk	2003/07/31 13:50:11	1.1227
+++ mk/bsd.pkg.mk	2003/08/01 06:12:44
@@ -2488,12 +2488,6 @@
 	${_PKG_SILENT}${_PKG_DEBUG}\
 	${MAKE} ${MAKEFLAGS} do-shlib-handling SHLIB_PLIST_MODE=0
 .endif
-.ifdef MESSAGE
-	@${ECHO_MSG} "${_PKGSRC_IN}> Please note the following:"
-	@${ECHO_MSG} ""
-	@${CAT} ${MESSAGE}
-	@${ECHO_MSG} ""
-.endif
 .if !defined(NO_PKG_REGISTER)
 	${_PKG_SILENT}${_PKG_DEBUG}cd ${.CURDIR} && ${MAKE} ${MAKEFLAGS} fake-pkg
 .endif # !NO_PKG_REGISTER
@@ -3004,6 +2998,17 @@
 		${SU_CMD} "cd ${.CURDIR}; ${SETENV} PATH=$${PATH}:${SU_CMD_PATH_APPEND} ${MAKE} $$args ${MAKEFLAGS} $$realtarget $$realflags"; \
 	fi

+.if target (real-install)
+.  ifdef MAIL_MESSAGE
+     _USER!=	${ID} -un
+.    if ${MAIL_MESSAGE} == "user"
+       _USER_MAIL= ${_USER}
+.    else
+       _USER_MAIL=
+.    endif
+.  endif
+.endif
+
 .PHONY: do-su-install
 do-su-install:
 	${_PKG_SILENT}${_PKG_DEBUG}					\
@@ -3021,6 +3026,22 @@
 	realtarget="real-su-install";					\
 	action="install";						\
 	${_SU_TARGET}
+.ifdef MESSAGE
+	@${ECHO_MSG} "${_PKGSRC_IN}> Please note the following:"
+	@${ECHO_MSG} ""
+	@${CAT} ${MESSAGE}
+	@${ECHO_MSG} ""
+.ifdef MAIL_MESSAGE
+	@(${ECHO} "The ${PKGNAME} package was installed by user ${_USER}" ; \
+	${ECHO} "using pkgsrc/${PKGPATH}." ; \
+	${ECHO} "" ; \
+	${ECHO} "Please note the following:" ; \
+	${ECHO} "" ; \
+	${CAT} ${MESSAGE} ; \
+	${ECHO} "" ) | \
+	mail -s "Package ${PKGNAME} installed" ${ROOT_USER} ${_USER_MAIL}
+.endif
+.endif

 .PHONY: do-su-package
 do-su-package:


I was getting a lot of "Unassociated shell command" errors when I was
trying to code this.

I use the .if target (real-install)
because I didn't want it to be set over and over again. Please teach me if
there is a better way.

To test, simply define MAIL_MESSAGE; if you set MAIL_MESSAGE=user then
both root and the user should get an email.

I use ROOT_USER, because if a normal user is doing unprivileged installs,
it doesn't make sense to email the "root". (I'll assume anyone doing
unprivileged installs would set ROOT_USER to something else.)

   Jeremy C. Reed
   http://bsd.reedmedia.net/

p.s. A posting on current-users reminded me of "pkg_info -D perl" which
reminded me that I needed to send-pr this: pkg/22328.