#!/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.
# 
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#

if [ $USER != "root" ]
then
    echo "Only the root user can uninstall PCP, try the following command instead:"
    echo "    sudo $0"
    exit 1
fi

echo -n "Are you sure you want to uninstall PCP? [y/n] "
read answer

if [ "X$answer" != "Xy" -a "X$answer" != "XY" ]
then
    echo "Uninstall cancelled."
    exit 1
fi

here=`pwd`

if [ "$here" = "/usr/share/pcp/bin" -o "$here" = "/usr/share/pcp" ]
then
    echo "Do not run $0 from /usr/share/pcp, uninstall cancelled."
    exit 1
fi

echo "Stopping PCP..."
/Library/StartupItems/pcp/pcp stop
sleep 5

if [ -x /usr/share/pcp/bin/install-pcp -a -r /usr/share/pcp/bin/idb ]
then
    echo "Removing Receipts"
    if [ -d /Library/Receipts/pcp-*.pkg ]; then
	rm -rf /Library/Receipts/pcp-*.pkg
    fi
    echo "Uninstall in background to remove all running scripts"
    # Copy to /tmp since inst will remove itself and this script
    cp /usr/share/pcp/bin/install-pcp /usr/share/pcp/bin/idb /tmp
    # create tidy up script
    echo '#!/bin/sh' > /tmp/_pcp_tidy.sh
    echo '/tmp/install-pcp -v -r -l /var/log/pcp_inst.log /tmp/idb && /bin/rm -rf /Library/Receipts/pcp-*' >> /tmp/_pcp_tidy.sh
    echo '/bin/rm -f /tmp/install-pcp /tmp/idb /tmp/_pcp_tidy.sh' >> /tmp/_pcp_tidy.sh
    echo 'echo Done' >> /tmp/_pcp_tidy.sh
    chmod u+x /tmp/_pcp_tidy.sh
    # Run remove, overriding this shell
    exec /tmp/_pcp_tidy.sh
else
    echo "Unable to find /usr/share/pcp/bin/install-pcp. Remove failed"
    exit 1
fi

