#!/bin/sh
#
# Copyright (c) 2003-2004 Silicon Graphics, Inc.  All Rights Reserved.
# 
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
# 
# Post installation/upgrade script for PCP
#

prog=`basename $0`
status=1
trap "exit \$status" 0 1 2 15

echo "$prog: PCP for macOS Upgrade"

echo "$prog: Stopping PCP"

# Unload periodic maintenance tasks (may not exist on older installs)
for plist in \
    io.pcp.pmlogger_check \
    io.pcp.pmlogger_daily \
    io.pcp.pmlogger_farm_check \
    io.pcp.pmie_check \
    io.pcp.pmie_daily \
    io.pcp.pmie_farm_check
do
    launchctl bootout system /Library/LaunchDaemons/${plist}.plist 2>/dev/null
done

# Stop core services
launchctl bootout system /Library/LaunchDaemons/io.pcp.pmproxy.plist 2>/dev/null
launchctl bootout system /Library/LaunchDaemons/io.pcp.pmlogger.plist 2>/dev/null
launchctl bootout system /Library/LaunchDaemons/io.pcp.pmie.plist 2>/dev/null
launchctl bootout system /Library/LaunchDaemons/io.pcp.pmcd.plist 2>/dev/null
# Legacy fallback for very old installs
/Library/StartupItems/pcp/pmlogger stop 2>/dev/null
/Library/StartupItems/pcp/pmcd stop 2>/dev/null

echo "$prog: Writing install log to /var/log/pcp_inst.log"

if [ ! -d $2 ]
then
    echo "$prog: Unable to open directory of new files ($2)"
    exit
fi

cd $2

# need to work through all the possible idb locations here ...
# start with the old ones and end with the current one
# for each, remove any files/dirs in the old idb and not in the
# new idb
#
for oldidb in /usr/share/pcp/bin/idb /usr/libexec/pcp/bin/idb 
do
    if [ -f $oldidb ]
    then
	if ./install-pcp -u $oldidb -l /var/log/pcp_inst.log idb 
	then
	    :
	else
	    echo "$prog: $oldidb Update Removal Failed"
	    exit
	fi
    fi
done

if ./install-pcp -i -l /var/log/pcp_inst.log idb
then
    :
else
    echo "$prog: Install Failed"
    exit
fi

if [ -f /etc/pcp.conf ]
then
    # for all the configuration files we know about, try to keep 
    # any local modifications, and salt away file with a .orig
    # suffix for a subsequent upgrade
    #
    . /etc/pcp.conf
    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 cmp -s $conf.pre $conf
	    then
		cp $conf $conf.orig
		rm $conf.pre
	    else
		# restore local modifications ...
		cp $conf $conf.orig
		mv $conf.pre $conf
	    fi
	elif [ -f $conf ]
	then
	    cp $conf $conf.orig
	fi
    done
fi

# create the user/group account that PCP daemons will use
#
userid=432
groupid=432

if ! dscl . -list /Groups | grep -q '^pcp$' 
then
    dscl . -create /Groups/pcp
    dscl . -create /Groups/pcp PrimaryGroupID $groupid
    dscl . -create /Groups/pcp Password \*
fi

if ! dscl . -list /Users | grep -q '^pcp$' 
then
    dscl . -create /Users/pcp
    dscl . -create /Users/pcp UniqueID $userid
    dscl . -create /Users/pcp UserShell /usr/bin/false
    dscl . -create /Users/pcp RealName 'Performance Co-Pilot'
    dscl . -create /Users/pcp NFSHomeDirectory /var/lib/pcp
    dscl . -create /Users/pcp PrimaryGroupID $groupid
    dscl . -create /Users/pcp Password \*
    dscl . append /Groups/pcp GroupMembership pcp
fi

chown -R pcp:pcp /var/log/pcp 2>/dev/null
chown -R pcp:pcp /var/lib/pcp/config 2>/dev/null

# https://github.com/performancecopilot/pcp/issues/20
defaults write io.pcp.pmtime NSAppSleepDisabled -bool YES

status=0
