Creating a service for a daemon

I'm working on a specialized server daemon in Google's Go language, and have a makefile that I want to have an "install" directive for, that would pu the compiled binary, the document root, the logfile directory, etc, in /opt/gochan/*, and create a service that can be started by running

systemctl start gochan

in distros using systemd

service gochan

in distros using Upstart, etc.

How would I go about doing this? What is the best way to make a catch-all method of starting it as a service?

Re: Creating a service for a daemon

Why install to /opt? Why not use the distro's prefered $PREFIX and/or $BINDIR?

As for systemd service files, there is a wiki page or a man page for that - they are pretty simple. The upstart equivalent may be in the *buntoo documentation.

Is the real question here just how to detect which init is used from within a Makefile? Autotools would probably be the best way. But a check of /proc/1/comm would be an effective shortcut.

EDIT: from another perspective, though, distro specific things should be handled by the packaging for that distro. Your makefile should just allow for different options. So the systemd service file could be installed via a PKGBUILD or the *buntoo equivalent, or perhaps with different install directives like `make install.systemd` and `make install.upstart`.