# set up $PATH to include all the optional and bizarre places that
# useful executables might be hiding
#
for xtra in /usr/sbin /sbin /usr/local/bin $PCP_BINADM_DIR \
	    /usr/local/git/bin /usr/perl5/bin /usr/gnu/bin
do
    [ -d "$xtra" ] || continue
    if echo ":${PATH}:" | grep ":$xtra:" >/dev/null 2>&1
    then
	:
    else
	PATH="$PATH:$xtra"
    fi
done
export PATH

# migrate and clean configs
$show_me || echo >>$PCP_LOG_DIR/install.log
$show_me || date >>$PCP_LOG_DIR/install.log
for base in pmcd pmie pmlogger pmproxy
do
    if $show_me
    then
	_clean_configs -n $PCP_SYSCONF_DIR/$base /var/lib/pcp/config/$base /etc/$base /etc/pcp/$base /etc/sysconfig/$base
    else
	_clean_configs -v 2>>$PCP_LOG_DIR/install.log $PCP_SYSCONF_DIR/$base /var/lib/pcp/config/$base /etc/$base /etc/pcp/$base /etc/sysconfig/$base
    fi
done

for svc in pmproxy pmie pmlogger pmmgr pmcd pcp 
do
    # Note:
    #   We need to stop all the PCP daemons before removing the
    #   services.  It is safe to ignore systemctl because it is not in
    #   play for platforms that are using the tarball install.
    #   But we cannot use $PCP_RC_DIR/$svc stop here because this relies
    #   on (at least) pmsignal which relies on libpcp.so ... the QA workflow
    #   may have clobbered libpcp.so during the earlier build on this host.
    #
    pids=`_get_pids_by_name -a $svc`
    [ -n "$pids" ] && $KILL -TERM $pids
    if which systemctl >/dev/null 2>&1
    then
	if systemctl list-unit-files $svc.service 2>&1 | grep "^$svc.service" >/dev/null
	then
	    $SYSTEMCTL stop $svc.service
	    $SYSTEMCTL disable $svc.service
	fi
    elif which rc-update >/dev/null 2>&1
    then
	if rc-update show | grep " $svc " >/dev/null 2>&1
	then
	    # service is installed ...
	    #
	    $RC_UPDATE delete $svc
	fi
    fi
done

for conf in \
    $PCP_PMCDCONF_PATH $PCP_PMCDOPTIONS_PATH $PCP_PMCDRCLOCAL_PATH \
    $PCP_PMIECONTROL_PATH $PCP_PMLOGGERCONTROL_PATH \
    $PCP_PMPROXYOPTIONS_PATH $PCP_PMWEBDOPTIONS_PATH \
    $PCP_ETC_DIR/pcp/indom.config \
# end
do
    if [ -f "$conf" ]
    then
	$RM -f "$conf.pre"
	$MV "$conf" "$conf.pre"
    fi
done

exit
