sts=0
tmp=/var/tmp
trap "rm -f $tmp.*; exit \$sts" 0 1 2 3 15

if [ ! -f /etc/pcp.env ]
then
    # PCP not installed ... seems odd
    #
    echo "Error: /etc/pcp.env not installed!"
    sts=1
    exit
fi

. /etc/pcp.env

# 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

# no need to touch $PCP_VAR_DIR/pmns/.NeedRebuild as this file
# is included in the tarball
#

for conf in \
    $PCP_PMCDCONF_PATH $PCP_PMCDOPTIONS_PATH $PCP_PMCDRCLOCAL_PATH \
    $PCP_PMIECONTROL_PATH $PCP_PMLOGGERCONTROL_PATH \
    $PCP_PMPROXYOPTIONS_PATH $PCP_PMWEBDOPTIONS_PATH
do
    if [ -f "$conf.pre" -a -f "$conf" ]
    then
	if diff "$conf" "$conf.pre" >/dev/null 2>&1
	then
	    rm -f "$conf.pre"
	else
	    mv "$conf" "$conf.dist"
	    mv "$conf.pre" "$conf"
	    echo "Warning: $conf: original restored, new version in $conf.dist"
	fi
    fi
done

_clean_tmpdirs $PCP_TMP_DIR /var/tmp mmv pmdabash pmie pmlogger

# set up pcp and pcpqa users
#
if [ -f /etc/passwd ]
then
    # things like useradd may be hiding in odd places ...
    #
    PATH=$PATH:/usr/sbin
    # common command-line options
    # -c comment
    # -g primary_group_name
    # -d home_directory
    # -m create home directory
    # -s shell
    # command-line variations
    # do not create home directory
    # 	-M for Linux, nothing for netbsd
    # system account (no aging, sys gid/uid range, no home dir creation)
    #   -r for Linux, not supported for netbsd
    #
    if [ $PCP_PLATFORM = netbsd ]
    then
	no_home_dir=''
	sys_acc=''
	nologin=/sbin/nologin
	bash=/usr/pkg/bin/bash
    elif [ $PCP_PLATFORM = solaris ]
    then
	no_home_dir=''
	sys_acc=''
	nologin=`which false`
	bash=`which bash`
    else
	no_home_dir='-M'
	sys_acc='-r'
	nologin=/usr/sbin/nologin
	bash=/bin/bash
    fi
    if grep '^pcp:' /etc/group >/dev/null
    then
	:
    else
	if which groupadd >/dev/null 2>&1
	then
	    groupadd $sys_acc pcp
	else
	    if [ $PCP_PLATFORM = freebsd ]
	    then
		: adduser does this for us ...
	    else
		echo "postinstall: Error: don't know how to add group \"pcp\""
		sts=1
		exit
	    fi
	fi
    fi
    if grep '^pcp:' /etc/passwd >/dev/null
    then
	:
    else
	if which useradd >/dev/null 2>&1
	then
	    useradd -c "Performance Co-Pilot" -g pcp -d /var/lib/pcp $no_home_dir $sys_acc -s $nologin pcp
	elif which adduser >/dev/null 2>&1
	then
	    echo "pcp::::::Performance Co-Pilot:/var/lib/pcp::" \
	    | adduser -f - -w no
	else
	    echo "postinstall: Error: don't know how to add user \"pcp\""
	    sts=1
	    exit
	fi
    fi
    if grep '^pcpqa:' /etc/group >/dev/null
    then
	:
    else
	if which groupadd >/dev/null 2>&1
	then
	    groupadd $sys_acc pcpqa
	else
	    if [ $PCP_PLATFORM = freebsd ]
	    then
		: adduser does this for us ...
	    else
		echo "postinstall: Error: don't know how to add group \"pcpqa\""
		sts=1
		exit
	    fi
	fi
    fi
    if grep '^pcpqa:' /etc/passwd >/dev/null
    then
	:
    else
	if which useradd >/dev/null 2>&1
	then
	    useradd -c "PCP Quality Assurance" -g pcpqa -d /var/lib/pcp/testsuite $no_home_dir $sys_acc -s $bash pcpqa
	elif which adduser >/dev/null 2>&1
	then
	    # no bash for default FreeBSD
	    bash=`which sh`
	    echo "pcpqa::::::PCP Quality Assurance:/var/lib/pcp/testsuite:$bash:" \
	    | adduser -f - -w no
	else
	    echo "postinstall: Error: don't know how to add user \"pcpqa\""
	    sts=1
	    exit
	fi
    fi
fi

# permissions ...
#

# fix up some ones that used to be pcp:pcp and/or in a different place
#
grp=root
case $PCP_PLATFORM
in
    freebsd|netbsd)
	    grp=wheel
	    ;;
esac
for daemon in pmlogger pmie
do
    chown root:$grp $PCP_SYSCONF_DIR/$daemon
    chmod 755 $PCP_SYSCONF_DIR/$daemon
    if [ -f $PCP_SYSCONF_DIR/$daemon/config.default ]
    then
	if [ -f $PCP_VAR_DIR/config/$daemon/config.default
	then
	    mv $PCP_SYSCONF_DIR/$daemon/config.default $PCP_VAR_DIR/config/$daemon/config.default.old
	else
	    mv $PCP_SYSCONF_DIR/$daemon/config.default $PCP_VAR_DIR/config/$daemon/config.default
	fi
    fi
done
chown root:$grp $PCP_SYSCONF_DIR/pmcd/pmcd.conf
chmod 644 $PCP_SYSCONF_DIR/pmcd/pmcd.conf

# Expect only root owned things below $PCP_SYSCONF_DIR
#
find $PCP_SYSCONF_DIR ! -user root | xargs chown root:$grp

# pcp:pcp and mode 775 directories
#
for dir in $PCP_LOG_DIR \
	   $PCP_LOG_DIR/pmcd $PCP_LOG_DIR/pmie $PCP_LOG_DIR/pmwebd \
	   $PCP_LOG_DIR/pmproxy $PCP_LOG_DIR/pmlogger $PCP_LOG_DIR/pmmgr \
	   $PCP_RUN_DIR \
	   $PCP_VAR_DIR/tmp $PCP_VAR_DIR/tmp/pmie $PCP_VAR_DIR/tmp/pmlogger \
	   $PCP_VAR_DIR/config/pmda \
	   $PCP_VAR_DIR/config/pmie $PCP_VAR_DIR/config/pmlogger
do
    [ -d $dir ] || mkdir $dir
    chmod 775 $dir
    chown pcp:pcp $dir
done

# pcp:pcp and mode 644 files
#
for file in $PCP_VAR_DIR/config/pmie/config.default $PCP_VAR_DIR/config/pmlogger/config.default
do
    [ -f $file ] || continue
    chmod 644 $file
    chown pcp:pcp $file
done

# pcpqa:pcpqa and mode 755 dir and all below there
#
[ -d $PCP_VAR_DIR/testsuite ] || mkdir $PCP_VAR_DIR/testsuite
chmod 775 $PCP_VAR_DIR/testsuite
chown -R pcpqa:pcpqa $PCP_VAR_DIR/testsuite

# Note: do not start daemons other than: pmcd, pmlogger, pmie and pmproxy
#
if which svcs >/dev/null 2>&1
then
    # in Solaris land, we need an XML file to control all of this
    #
    if [ ! -f ../sun/pcp.xml ]
    then
	echo "Warning: Solaris SMF XML missing: ../sun/pcp.xml"
    else
	svccfg import ../sun/pcp.xml
    fi
else
    for svc in pcp pmcd pmie pmlogger pmproxy pmmgr pmwebd
    do
	[ -f /etc/init.d/$svc ] || continue
	if which rc-update >/dev/null 2>&1
	then
	    if rc-update show | grep " $svc " >/dev/null 2>&1
	    then
		# service is already installed ...
		#
		rc-update delete $svc
	    fi
	    rc-update add $svc
	fi

	# disable or enable and start as required by default policy
	case $svc
	in
	    pmcd|pmlogger|pmproxy|pmie|pmmgr|pmwebd)
		if which rc-update >/dev/null 2>&1
		then
		    : TODO?
		fi
		$PCP_RC_DIR/$svc start
		;;
	    *)
		if which rc-update >/dev/null 2>&1
		then
		    : TODO?
		fi
		;;
	esac

    done
fi

exit
