Add the following lines to your ports Makefile:

USE_RC_SUBR=	yes

SED_SCRIPT=	-e 's,%%RC_SUBR%%,${RC_SUBR},g' \
		-e 's,%%PREFIX%%,${PREFIX},g'

post-build:
	@${SED} ${SED_SCRIPT} ${FILESDIR}/${PORTNAME}.sh \
		>${WRKDIR}/${PORTNAME}.sh

post-install:
	@${INSTALL_SCRIPT} ${WRKDIR}/${PORTNAME}.sh \
		${PREFIX}/etc/rc.d/${PORTNAME}.sh

put your customized sample script into the files directory of your
port and add it to the pkg-plist.

To customize your rc.subr script, do

sed -e 's,%%NAME%%,MYPORTNAME,g' example.sh > MYPORTDIR/files/MYPORTNAME.sh

and check the executable path, required files and whether the daemon
really creates a pid file.

Then add MYPORTNAME__enable="YES" to /etc/rc.conf and test the
script with start, stop, restart, status and reload when enabled.

--- Dos and Don'ts ---

* Don't try to interpret the arguments given to the script by using
  `case "$1"' or similar statements

rc.subr already supports start, faststart, forcestart and onestart,
and might suport additional prefixes in the future. The argument
given should be interpreted by rc.subr and not your script. Use
extra_commands if you have to.

* Don't write auxilliary stuff in the rc.subr script.

The script is for (automatically) starting/stopping daemons, not
configuring the local machine (like creating users or modifying
configuration files). Use a seperate script for that.

* Be careful how to set default values.

Especially don't do

[ -z "$%%NAME%%_enable" ] && %%NAME%%_enable="NO"
%%NAME%%_enable=${%%NAME%%_enable:-"NO"}
%%NAME%%_enable=${%%NAME%%_enable:="NO"}

Set the default for parameters using

: ${%%NAME%%_enable="NO"}

only. This is important because otherwise users are unable to set
a variable to be empty by default. Even when a user accidentially
sets MYPORTNAME_enable="" the error should be detected and not
silently substitued by possibly unwanted defaults. Besides, having
a uniform setting of defaults makes scripts easy to read.

To elaborate: A user might have a central setting for networked
daemons in rc.conf, like

MY_NETDAEMONS="YES"
...
MYPORTNAME_enable="$MY_NETDAEMONS"

and due to some unfortunate accident, the MY_NETDAEMONS line slips
below the MYPORTNAME_enable one (or is misspelled). You really want
to detect this error, instead of just not starting the service.
Therefore *never* use := for defaults, even when they should not
be empty. Check parameters in start_precmd if you have to, but don't
overdo it.

* Don't write anything after run_rc_command (effectively masking
  it's return value)

Also don't write anything before . "%%RC_SUBR%%" or after load_rc_config
(except the : ${xxx=""} lines needed for the default values)

* Don't add pipelines, redirections, or background commands to
  %%NAME%%_flags.

* Don't try to autodetect PREFIX or assume in any way that $0 has
  a reasonable value

* Do use debug and warn to output messages

* Don't touch variables designed to be set in rc.conf except setting
  their default values once.

Especially don't mess with `%%NAME%%_flags' by doing
%%NAME%%_flags="${%%NAME%%_flags} whatever". Use command_args or
modify rc_flags in start_precmd if you have to.
