
# set up pcp user
#
if which systemd-sysusers >/dev/null 2>&1
then
    # use user pcp controls installed in /usr/lib/sysusers.d to
    # create user and group
    if ! systemd-sysusers
    then
	echo >&2 "pcp.postinstall: Error: systemd-sysusers failed to create pcp user"
	exit 1
    fi
elif which useradd >/dev/null 2>&1
then
    # old-style
    #
    if ! getent group pcp >/dev/null
    then
	if ! groupadd -r pcp
	then
	    echo >&2 "pcp.postinstall: Error: groupadd failed to create pcp group"
	    exit 1
	fi
    fi
    if ! getent passwd pcp >/dev/null
    then
	if ! useradd -c "Performance Co-Pilot" -g pcp -d /var/lib/pcp -M -r -s /usr/sbin/nologin pcp
	then
	    echo >&2 "pcp.postinstall: Error: useradd failed to create pcp user"
	    exit 1
	fi
    fi
else
    echo >&2 "pcp.postinstall: Error: no systemd-sysusers or useradd: cannot create pcp user"
    exit 1
fi

# must match pmns/GNUmakefile, owned by root:root so $PCP_SYSCONF_DIR/pmcd/rc
# can remove it once PMNS rebuild is done on first pmcd startup
#
touch /var/lib/pcp/pmns/.NeedRebuild
chmod 644 /var/lib/pcp/pmns/.NeedRebuild

# owned by pcp:pcp so pmlogger_daily can remove it once rewriting
# is done
#
touch /var/log/pcp/pmlogger/.NeedRewrite
chown pcp:pcp /var/log/pcp/pmlogger/.NeedRewrite
chmod 644 /var/log/pcp/pmlogger/.NeedRewrite

# must match GNUmakefile
#
chown pcp:pcp /var/lib/pcp/config/pmda
chmod 775 /var/lib/pcp/config/pmda
chown pcp:pcp /var/lib/pcp/tmp
chmod 775 /var/lib/pcp/tmp
chown pcp:pcp /var/log/pcp
chmod 775 /var/log/pcp
# we used to dink with /var/run/pcp here, but that is done more
# safely in reboot-init now

# must match src/pmcd/GNUmakefile
#
chmod 700 /var/lib/pcp/pmcd

# must match src/pmdas/{bash,mmv}/GNUmakefile
#
for PMDA in bash mmv; do
    # directory may be in optionally build component, so test first
    #
    if [ -d /var/lib/pcp/tmp/$PMDA ]
    then
	chown pcp:pcp /var/lib/pcp/tmp/$PMDA
	chmod 775 /var/lib/pcp/tmp/$PMDA
    fi
done

# must match src/pmdas/mmv/src/GNUmakefile
# (strange but true, the "pmproxy" pmda is built in the mmv dir)
#
chown pcp:pcp /var/lib/pcp/tmp/pmproxy
chmod 775 /var/lib/pcp/tmp/pmproxy

# must match src/pmie/GNUmakefile
#
chown pcp:pcp /var/lib/pcp/config/pmie
chmod 775 /var/lib/pcp/config/pmie
chown pcp:pcp /var/lib/pcp/tmp/pmie
chmod 775 /var/lib/pcp/tmp/pmie
chown pcp:pcp /var/log/pcp/pmie
chmod 775 /var/log/pcp/pmie

# must match src/pmlogger/GNUmakefile
#
chown pcp:pcp /var/lib/pcp/config/pmlogger
chmod 775 /var/lib/pcp/config/pmlogger
chown pcp:pcp /var/lib/pcp/tmp/pmlogger
chmod 775 /var/lib/pcp/tmp/pmlogger
chown pcp:pcp /var/log/pcp/pmlogger
chmod 775 /var/log/pcp/pmlogger
chown pcp:pcp /var/log/pcp/sa
chmod 775 /var/log/pcp/sa

# must match src/pmproxy/GNUmakefile
#
chown pcp:pcp /var/log/pcp/pmproxy
chmod 775 /var/log/pcp/pmproxy

# must match src/pmfind/GNUmakefile
#
chown pcp:pcp /var/log/pcp/pmfind
chmod 775 /var/log/pcp/pmfind

# and fixup the change in deployment plan for these ones in the case
# of an upgrade, rather than an install ... they used to be pcp:pcp
chown root:root /etc/pcp/pmie
chmod 755 /etc/pcp/pmie
chown root:root /etc/pcp/pmie/control
chmod 644 /etc/pcp/pmie/control
chown root:root /etc/pcp/pmlogger
chmod 755 /etc/pcp/pmlogger
chown root:root /etc/pcp/pmlogger/control
chmod 644 /etc/pcp/pmlogger/control

# figure out what sort if init|systemctl|... we're using to
# launch daemons and services
do_systemctl=false
if which systemctl >/dev/null 2>&1
then
    # we have a systemctl executable, but it might be disabled,
    # e.g. on MX Linux or in a container with no systemd running
    systemctl whoami >/dev/null 2>&1 && do_systemctl=true
fi
do_systemd_helper=false
if $do_systemctl
then
    which deb-systemd-helper >/dev/null 2>&1 && do_systemd_helper=true
fi
do_sysv=false
if which update-rc.d >/dev/null 2>&1
then
    if which invoke-rc.d >/dev/null 2>&1
    then
	if which runlevel >/dev/null 2>&1
	then
	    if runlevel >/dev/null 2>&1
	    then
		# System-V init installed and running
		do_sysv=true
	    fi
	fi
    fi
fi

# only need to check pmcd.service, if it is here they will all
# be here
if [ -f /lib/systemd/system/pmcd.service ]
then
    :
else
    do_systemd_helper=false
    do_systemctl=false
fi
# ditto for the System-V variant for pmlogger
if [ -f /etc/init.d/pmlogger ]
then
    :
else
    do_sysv=false
fi

if $do_systemd_helper
then
    deb-systemd-helper enable pcp-reboot-init.service >/dev/null
    deb-systemd-helper enable pmcd.service >/dev/null
    deb-systemd-helper enable pmlogger.service >/dev/null
    deb-systemd-helper enable pmie.service >/dev/null
    deb-systemd-helper enable pmproxy.service >/dev/null
elif $do_sysv
then
    for svc in pmcd pmlogger pmie pmproxy
    do
	if update-rc.d -f $svc remove >/dev/null
	then
	    :
	else
	    echo >&2 "pcp.postinstall: Warning: update-rc.d $svc remove failed"
	fi
	if update-rc.d $svc defaults >/dev/null
	then
	    :
	else
	    echo >&2 "pcp.postinstall: Warning: update-rc.d $svc defaults failed"
	fi
    done
fi

if $do_systemctl
then
    if systemctl daemon-reload >/dev/null
    then
	:
    else
	echo >&2 "pcp.postinstall: Warning: systemctl daemon-reload failed"
    fi
    for svc in pcp-reboot-init pmcd pmlogger
    do
	if systemctl start $svc.service >/dev/null
	then
	    :
	else
	    echo >&2 "pcp.postinstall: Warning: systemctl start $svc failed"
	fi
    done
elif $do_sysv
then
    for svc in pmcd pmlogger
    do
	if invoke-rc.d $svc start
	then
	    :
	else
	    echo >&2 "pcp.postinstall: Warning: invoke-rc.d $svc start failed"
	fi
    done
else
    for svc in pmcd pmlogger
    do
	if /etc/init.d/$svc start
	then
	    :
	else
	    echo >&2 "pcp.postinstall: Warning: /etc/init.d/$svc start failed"
	fi
    done
fi

exit 0
