SHELL=/bin/bash

EXTDIR=${DESTDIR}/etc
DEFAULTSDIR=${DESTDIR}/etc/default
TMPFILESDIR=${DESTDIR}/usr/lib/tmpfiles.d
UNITSDIR=${DESTDIR}/lib/systemd/system
MODE=755
DIRMODE=755
CONFMODE=644

all:
	@grep "^install" Makefile.systemd | cut -d ":" -f 1
	@echo "Select an appropriate install target from the above list"

create-dirs:
	install -d -m ${DIRMODE} ${DEFAULTSDIR}
	install -d -m ${DIRMODE} ${TMPFILESDIR}
	install -d -m ${DIRMODE} ${UNITSDIR}

install-sysd-service-setup-nutyx: create-dirs
	install -m ${MODE} units/setup-nutyx.service ${UNITSDIR}
	install -d ${DIRMODE} ${UNITSDIR}/sysinit.target.wants
	ln -sv ../setup-nutyx.service ${UNITSDIR}/sysinit.target.wants

install-sysd-acpid: create-dirs
	install -m ${CONFMODE} units/acpid.service ${UNITSDIR}/
	install -m ${CONFMODE} units/acpid.socket ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable acpid.socket

install-sysd-dhclient: create-dirs
	install -m ${CONFMODE} units/dhclientat.service ${UNITSDIR}/dhclient@.service

install-sysd-dhcpcd: create-dirs
	install -m ${CONFMODE} units/dhcpcdat.service ${UNITSDIR}/dhcpcd@.service

install-sysd-dhcpd: create-dirs
	install -m ${CONFMODE} default/dhcpd ${DEFAULTSDIR}/
	install -m ${CONFMODE} units/dhcpd.service ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable dhcpd.service

install-sysd-exim: create-dirs
	install -m ${CONFMODE} units/exim.service ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable exim.service

install-sysd-git: create-dirs
	install -m ${CONFMODE} units/git-daemonat.service ${UNITSDIR}/git-daemon@.service
	install -m ${CONFMODE} units/git-daemon.socket ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable git-daemon.socket

install-sysd-gpm: create-dirs
	install -m ${CONFMODE} units/gpm.service ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable gpm.service

install-sysd-haveged: create-dirs
	install -m ${CONFMODE} units/haveged.service ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable haveged.service

install-sysd-httpd: create-dirs
	install -m ${CONFMODE} tmpfiles/httpd.conf ${TMPFILESDIR}/
	install -m ${CONFMODE} units/httpd.service ${UNITSDIR}/
	test -n "${DESTDIR}" || /bin/systemd-tmpfiles --create httpd.conf
	test -n "${DESTDIR}" || systemctl enable httpd.service

install-sysd-nginx: create-dirs
	install -m ${CONFMODE} units/nginx.service ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable nginx.service

install-sysd-iptables: create-dirs
	install -m ${CONFMODE} units/iptables.service ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable iptables.service

install-sysd-kdm: create-dirs
	install -m ${CONFMODE} units/kdm.service ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable kdm.service

install-sysd-krb5: create-dirs
	install -m ${CONFMODE} units/krb5-kdc.service ${UNITSDIR}/
	install -m ${CONFMODE} units/krb5-kpropd.service ${UNITSDIR}/
	install -m ${CONFMODE} units/krb5-kadmind.service ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable krb5-kdc.service
	test -n "${DESTDIR}" || systemctl enable krb5-kpropd.service
	test -n "${DESTDIR}" || systemctl enable krb5-kadmind.service

install-sysd-lightdm: create-dirs
	install -m ${CONFMODE} units/lightdm.service ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable lightdm.service

install-sysd-mysqld: create-dirs
	install -m ${CONFMODE} tmpfiles/mysqld.conf ${TMPFILESDIR}/
	install -m ${CONFMODE} units/mysqld.service ${UNITSDIR}/
	test -n "${DESTDIR}" || systemd-tmpfiles --create mysqld.conf
	test -n "${DESTDIR}" || systemctl enable mysqld.service

install-sysd-named: create-dirs
	install -m ${CONFMODE} tmpfiles/named.conf ${TMPFILESDIR}/
	install -m ${CONFMODE} units/named.service ${UNITSDIR}/
	test -n "${DESTDIR}" || /bin/systemd-tmpfiles --create named.conf
	test -n "${DESTDIR}" || systemctl enable named.service

install-sysd-nfs-client: create-dirs
	install -m ${CONFMODE} default/nfs-utils ${DEFAULTSDIR}/
	install -m ${CONFMODE} units/rpc-statd.service ${UNITSDIR}/
	install -m ${CONFMODE} units/rpc-statd-notify.service ${UNITSDIR}/
	install -m ${CONFMODE} units/nfs-client.service ${UNITSDIR}/
	install -m ${CONFMODE} units/nfs-client.target ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable nfs-client.target

install-sysd-nfs-server: install-sysd-nfs-client
	install -m ${CONFMODE} units/nfs-server.service ${UNITSDIR}/
	install -m ${CONFMODE} units/rpc-mountd.service ${UNITSDIR}/
	install -m ${CONFMODE} units/proc-fs-nfsd.mount ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable nfs-server.service

install-sysd-nfsv4-server: install-sysd-nfs-server
	install -m ${CONFMODE} units/rpc-idmapd.service ${UNITSDIR}/
	install -m ${CONFMODE} units/var-lib-nfs-rpc_pipefs.mount ${UNITSDIR}/

install-sysd-ntpd: create-dirs
	install -m ${CONFMODE} units/ntpd.service ${UNITSDIR}/
	install -d -m ${DIRMODE} ${DESTDIR}/usr/lib/systemd/ntp-units.d
	test -n "${DESTDIR}" || systemctl enable ntpd.service

install-sysd-php-fpm: create-dirs
	install -m ${CONFMODE} units/php-fpm.service ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable php-fpm.service

install-sysd-postfix: create-dirs
	install -m ${CONFMODE} units/postfix.service ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable postfix.service

install-sysd-postgresql: create-dirs
	install -m ${CONFMODE} tmpfiles/postgresql.conf ${TMPFILESDIR}/
	install -m ${CONFMODE} units/postgresql.service ${UNITSDIR}/
	test -n "${DESTDIR}" || systemd-tmpfiles --create postgresql.conf
	test -n "${DESTDIR}" || systemctl enable postgresql.service

install-sysd-proftpd: create-dirs
	install -m ${CONFMODE} units/proftpd.service ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable proftpd.service

install-sysd-rpcbind: create-dirs
	install -m ${CONFMODE} units/rpcbind.service ${UNITSDIR}/
	install -m ${CONFMODE} units/rpcbind.socket ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable rpcbind.socket

install-sysd-rsyncd: create-dirs
	install -m ${CONFMODE} units/rsyncd.service ${UNITSDIR}/
	install -m ${CONFMODE} units/rsyncdat.service ${UNITSDIR}/rsyncd@.service
	install -m ${CONFMODE} units/rsyncd.socket ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable rsyncd.service

install-sysd-samba: create-dirs
	install -m ${CONFMODE} default/samba ${DEFAULTSDIR}/
	install -m ${CONFMODE} tmpfiles/samba.conf ${TMPFILESDIR}/
	install -m ${CONFMODE} units/nmbd.service ${UNITSDIR}/
	install -m ${CONFMODE} units/samba.service ${UNITSDIR}/
	install -m ${CONFMODE} units/smbd.service ${UNITSDIR}/
	install -m ${CONFMODE} units/smbdat.service ${UNITSDIR}/smbd@.service
	install -m ${CONFMODE} units/smbd.socket ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable nmbd.service
	test -n "${DESTDIR}" || systemctl enable smbd.service

install-sysd-saslauthd: create-dirs
	install -m ${CONFMODE} default/saslauthd ${DEFAULTSDIR}/
	install -m ${CONFMODE} tmpfiles/saslauthd.conf ${TMPFILESDIR}/
	install -m ${CONFMODE} units/saslauthd.service ${UNITSDIR}/
	test -n "${DESTDIR}" || systemd-tmpfiles --create saslauthd.conf
	test -n "${DESTDIR}" || systemctl enable saslauthd.service

install-sysd-sendmail: create-dirs
	install -m ${CONFMODE} default/sendmail ${DEFAULTSDIR}/
	install -m ${CONFMODE} units/sm-client.service ${UNITSDIR}/
	install -m ${CONFMODE} units/sendmail.service ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable sendmail.service

install-sysd-slapd: create-dirs
	install -m ${CONFMODE} default/slapd ${DEFAULTSDIR}/
	install -m ${CONFMODE} tmpfiles/slapd.conf ${TMPFILESDIR}/
	install -m ${CONFMODE} units/slapd.service ${UNITSDIR}/
	test -n "${DESTDIR}" || systemd-tmpfiles --create slapd.conf
	test -n "${DESTDIR}" || systemctl enable slapd.service

install-sysd-sshd: create-dirs
	install -m ${CONFMODE} units/sshd.service ${UNITSDIR}/
	install -m ${CONFMODE} units/sshdgenkeys.service ${UNITSDIR}/
	install -m ${CONFMODE} units/sshdat.service ${UNITSDIR}/sshd@.service
	install -m ${CONFMODE} units/sshd.socket ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable sshd.service

install-sysd-svnserve: create-dirs
	install -m ${CONFMODE} default/svnserve ${DEFAULTSDIR}/
	install -m ${CONFMODE} tmpfiles/svnserve.conf ${TMPFILESDIR}/
	install -m ${CONFMODE} units/svnserve.service ${UNITSDIR}/
	test -n "${DESTDIR}" || systemd-tmpfiles --create svnserve.conf
	test -n "${DESTDIR}" || systemctl enable svnserve.service

install-sysd-unbound: create-dirs
	install -m ${CONFMODE} units/unbound.service ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable unbound.service

install-sysd-vsftpd: create-dirs
	install -m ${CONFMODE} units/vsftpd.service ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable vsftpd.service

install-sysd-winbindd: install-samba
	install -m ${CONFMODE} units/winbindd.service ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable winbindd.service

install-sysd-xinetd: create-dirs
	install -m ${CONFMODE} units/xinetd.service ${UNITSDIR}/
	test -n "${DESTDIR}" || systemctl enable xinetd.service

uninstall-sysd-acpid:
	test -n "${DESTDIR}" || systemctl stop acpid.service
	test -n "${DESTDIR}" || systemctl disable acpid.socket
	rm -f ${UNITSDIR}/acpid.service ${UNITSDIR}/acpid.socket

uninstall-sysd-dhclient:
	rm -f ${UNITSDIR}/dhclient@.service

uninstall-sysd-dhcpcd:
	rm -f ${UNITSDIR}/dhcpcd@.service

uninstall-sysd-dhcpd:
	test -n "${DESTDIR}" || systemctl stop dhcpd.service
	test -n "${DESTDIR}" || systemctl disable dhcpd.service
	rm -f ${DEFAULTSDIR}/dhcpd ${UNITSDIR}/dhcpd.service

uninstall-sysd-exim:
	test -n "${DESTDIR}" || systemctl stop exim.service
	test -n "${DESTDIR}" || systemctl disable exim.service
	rm -f ${UNITSDIR}/exim.service

uninstall-sysd-git:
	test -n "${DESTDIR}" || systemctl stop git-daemon.socket
	test -n "${DESTDIR}" || systemctl disable git-daemon.socket
	rm -f ${UNITSDIR}/git-daemon@.service ${UNITSDIR}/git-daemon.socket

uninstall-sysd-gpm:
	test -n "${DESTDIR}" || systemctl stop gpm.service
	test -n "${DESTDIR}" || systemctl disable gpm.service
	rm -f ${UNITSDIR}/gpm.service

uninstall-sysd-haveged:
	test -n "${DESTDIR}" || systemctl stop haveged.service
	test -n "${DESTDIR}" || systemctl disable haveged.service
	rm -f ${UNITSDIR}/haveged.service

uninstall-sysd-httpd:
	test -n "${DESTDIR}" || systemctl stop httpd.service
	test -n "${DESTDIR}" || systemctl disable httpd.service
	rm -f ${TMPFILESDIR}/httpd.conf ${UNITSDIR}/httpd.service

uninstall-sysd-nginx:
	test -n "${DESTDIR}" || systemctl stop nginx.service
	test -n "${DESTDIR}" || systemctl disable nginx.service
	rm -f ${UNITSDIR}/nginx.service

uninstall-sysd-iptables:
	test -n "${DESTDIR}" || systemctl disable iptables.service
	rm -f ${UNITSDIR}/iptables.service

uninstall-sysd-kdm:
	test -n "${DESTDIR}" || systemctl stop kdm.service
	test -n "${DESTDIR}" || systemctl disable kdm.service
	rm -f ${UNITSDIR}/kdm.service

uninstall-sysd-krb5:
	test -n "${DESTDIR}" || systemctl stop krb5-kadmind.service
	test -n "${DESTDIR}" || systemctl stop krb5-kpropd.service
	test -n "${DESTDIR}" || systemctl stop krb5-kdc.service
	test -n "${DESTDIR}" || systemctl disable krb5-kadmind.service
	test -n "${DESTDIR}" || systemctl disable krb5-kpropd.service
	test -n "${DESTDIR}" || systemctl disable krb5-kdc.service
	rm -f ${UNITSDIR}/krb5-kadmind.service ${UNITSDIR}/krb5-kpropd.service ${UNITSDIR}/krb5-kdc.service

uninstall-sysd-lightdm:
	test -n "${DESTDIR}" || systemctl stop lightdm.service
	test -n "${DESTDIR}" || systemctl disable lightdm.service
	rm -f ${UNITSDIR}/lightdm.service

uninstall-sysd-mysqld:
	test -n "${DESTDIR}" || systemctl stop mysqld.service
	test -n "${DESTDIR}" || systemctl disable mysqld.service
	rm -f ${TMPFILESDIR}/mysqld.conf ${UNITSDIR}/mysqld.service

uninstall-sysd-named:
	test -n "${DESTDIR}" || systemctl stop named.service
	test -n "${DESTDIR}" || systemctl disable named.service
	rm -f ${TMPFILESDIR}/named.conf ${UNITSDIR}/named.service

uninstall-sysd-nfs-client: uninstall-sysd-nfs-server
	test -n "${DESTDIR}" || systemctl stop nfs-client.target
	test -n "${DESTDIR}" || systemctl disable nfs-client.target
	rm -f ${DEFAULTSDIR}/nfs-utils ${UNITSDIR}/nfs-client.target
	rm -f ${UNITSDIR}/nfs-client.service ${UNITSDIR}/rpc-statd.service ${UNITSDIR}/rpc-statd-notify.service

uninstall-sysd-nfs-server: uninstall-sysd-nfsv4-server
	if [[ -z "${DESTDIR}" ]]; then test -e "${UNITSDIR}/nfs-server.service" && systemctl stop nfs-server.service; fi
	if [[ -z "${DESTDIR}" ]]; then test -e "${UNITSDIR}/nfs-server.service" && systemctl disable nfs-server.service; fi
	rm -f ${UNITSDIR}/nfs-server.service ${UNITSDIR}/rpc-mountd.service
	rm -f ${UNITSDIR}/proc-fs-nfsd.mount

uninstall-sysd-nfsv4-server:
	rm -f ${UNITSDIR}/rpc-idmapd.service
	rm -f ${UNITSDIR}/var-lib-nfs-rpc_pipefs.mount

uninstall-sysd-ntpd:
	test -n "${DESTDIR}" || systemctl stop ntpd.service
	test -n "${DESTDIR}" || systemctl disable ntpd.service
	rm -f ${UNITSDIR}/ntpd.service

uninstall-sysd-php-fpm:
	test -n "${DESTDIR}" || systemctl stop php-fpm.service
	test -n "${DESTDIR}" || systemctl disable php-fpm.service
	rm -f ${UNITSDIR}/php-fpm.service

uninstall-sysd-postfix:
	test -n "${DESTDIR}" || systemctl stop postfix.service
	test -n "${DESTDIR}" || systemctl disable postfix.service
	rm -f ${UNITSDIR}/postfix.service

uninstall-sysd-postgresql:
	test -n "${DESTDIR}" || systemctl stop postgresql.service
	test -n "${DESTDIR}" || systemctl disable postgresql.service
	rm -f ${TMPFILESDIR}/postgresql.conf ${UNITSDIR}/postgresql.service

uninstall-sysd-proftpd:
	test -n "${DESTDIR}" || systemctl stop proftpd.service
	test -n "${DESTDIR}" || systemctl disable proftpd.service
	rm -f ${UNITSDIR}/proftpd.service

uninstall-sysd-rpcbind:
	test -n "${DESTDIR}" || systemctl stop rpcbind.socket
	test -n "${DESTDIR}" || systemctl disable rpcbind.socket
	rm -f ${UNITSDIR}/rpcbind.service ${UNITSDIR}/rpcbind.socket

uninstall-sysd-rsyncd:
	test -n "${DESTDIR}" || systemctl stop rsyncd.socket
	test -n "${DESTDIR}" || systemctl stop rsyncd.service
	rm -f ${UNITSDIR}/rsyncd.service ${UNITSDIR}/rsyncd@.service
	rm -f ${UNITSDIR}/rsyncd.socket

uninstall-sysd-samba: uninstall-winbindd
	test -n "${DESTDIR}" || systemctl stop smbd.socket
	test -n "${DESTDIR}" || systemctl stop smbd.service
	test -n "${DESTDIR}" || systemctl stop nmbd.service
	test -n "${DESTDIR}" || systemctl disable smbd.socket
	test -n "${DESTDIR}" || systemctl disable smbd.service
	test -n "${DESTDIR}" || systemctl disable nmbd.service
	rm -f ${DEFAULTSDIR}/samba ${TMPFILESDIR}/samba.conf ${UNITSDIR}/nmbd.service
	rm -f ${UNITSDIR}/smbd.service ${UNITSDIR}/smbd@.service ${UNITSDIR}/smbd.socket

uninstall-sysd-saslauthd:
	test -n "${DESTDIR}" || systemctl stop saslauthd.service
	test -n "${DESTDIR}" || systemctl disable saslauthd.service
	rm -f ${DEFAULTSDIR}/saslauthd ${TMPFILESDIR}/saslauthd.conf ${UNITSDIR}/saslauthd.service

uninstall-sysd-sendmail:
	test -n "${DESTDIR}" || systemctl stop sendmail.service
	test -n "${DESTDIR}" || systemctl disable sendmail.service
	rm -f ${DEFAULTSDIR}/sendmail ${UNITSDIR}/sm-client.service ${UNITSDIR}/sendmail.service

uninstall-sysd-slapd:
	test -n "${DESTDIR}" || systemctl stop slapd.service
	test -n "${DESTDIR}" || systemctl disable slapd.service
	rm -f ${DEFAULTSDIR}/slapd ${TMPFILESDIR}/slapd.conf ${UNITSDIR}/slapd.service

uninstall-sysd-sshd:
	test -n "${DESTDIR}" || systemctl stop sshd.socket
	test -n "${DESTDIR}" || systemctl stop sshd.service
	test -n "${DESTDIR}" || systemctl disable sshd.socket
	test -n "${DESTDIR}" || systemctl disable sshd.service
	rm -f ${UNITSDIR}/sshd.service ${UNITSDIR}/sshd@.service ${UNITSDIR}/sshd.socket

uninstall-sysd-svnserve:
	test -n "${DESTDIR}" || systemctl stop svnserve.service
	test -n "${DESTDIR}" || systemctl disable svnserve.service
	rm -f ${DEFAULTSDIR}/svnserve ${TMPFILESDIR}/svnserve.conf ${UNITSDIR}/svnserve.service

uninstall-sysd-unbound:
	test -n "${DESTDIR}" || systemctl stop unbound.service
	test -n "${DESTDIR}" || systemctl disable unbound.service
	rm -f ${UNITSDIR}/unbound.service

uninstall-sysd-vsftpd:
	test -n "${DESTDIR}" || systemctl stop vsftpd.service
	test -n "${DESTDIR}" || systemctl disable vsftpd.service
	rm -f ${UNITSDIR}/vsftpd.service

uninstall-sysd-winbindd:
	test -n "${DESTDIR}" || systemctl stop winbindd.service
	test -n "${DESTDIR}" || systemctl disable winbindd.service
	rm -f ${UNITSDIR}/winbindd.service

uninstall-sysd-xinetd:
	test -n "${DESTDIR}" || systemctl stop xinetd.service
	test -n "${DESTDIR}" || systemctl disable xinetd.service
	rm -f ${UNITSDIR}/xinetd.service

.PHONY: all create-dirs \
	install-sysd-acpid \
	install-sysd-dhclient \
	install-sysd-dhcpcd \
	install-sysd-dhcpd \
	install-sysd-exim \
	install-sysd-git \
	install-sysd-gpm \
	install-sysd-httpd \
	install-sysd-nginx \
	install-sysd-iptables \
	install-sysd-kdm \
	install-sysd-krb5 \
	install-sysd-mysqld \
	install-sysd-named \
	install-sysd-nfs-client \
	install-sysd-nfs-server \
	install-sysd-nfsv4-server \
	install-sysd-ntp \
	install-sysd-php-fpm \
	install-sysd-postfix \
	install-sysd-postgresql \
	install-sysd-proftpd \
	install-sysd-rpcbind \
	install-sysd-rsyncd \
	install-sysd-samba \
	install-sysd-saslauthd \
	install-sysd-sendmail \
	install-sysd-slapd \
	install-sysd-sshd \
	install-sysd-svnserve \
	install-sysd-unbound \
	install-sysd-vsftpd \
	install-sysd-winbindd \
	install-sysd-xinetd \
	uninstall-sysd-acpid \
	uninstall-sysd-dhclient \
	uninstall-sysd-dhcpcd \
	uninstall-sysd-dhcpd \
	uninstall-sysd-exim \
	uninstall-sysd-git \
	uninstall-sysd-gpm \
	uninstall-sysd-httpd \
	uninstall-sysd-nginx \
	uninstall-sysd-iptables \
	uninstall-sysd-kdm \
	uninstall-sysd-krb5 \
	uninstall-sysd-mysqld \
	uninstall-sysd-named \
	uninstall-sysd-nfs-client \
	uninstall-sysd-nfs-server \
	uninstall-sysd-nfsv4-server \
	uninstall-sysd-ntpd \
	uninstall-sysd-php-fpm \
	uninstall-sysd-postfix \
	uninstall-sysd-postgresql \
	uninstall-sysd-proftpd \
	uninstall-sysd-rpcbind \
	uninstall-sysd-rsyncd \
	uninstall-sysd-samba \
	uninstall-sysd-saslauthd \
	uninstall-sysd-sendmail \
	uninstall-sysd-slapd \
	uninstall-sysd-sshd \
	uninstall-sysd-svnserve \
	uninstall-sysd-unbound \
	uninstall-sysd-vsftpd \
	uninstall-sysd-winbindd \
	uninstall-sysd-xinetd
