#!/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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
# 
# Contact information: Silicon Graphics, Inc., 1500 Crittenden Lane,
# Mountain View, CA 94043, USA, or: http://www.sgi.com
#

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

echo -n "Are you sure you want to uninstall pcp-gui? [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

if [ -x /usr/share/pcp/bin/install-pcp-gui -a -r /usr/share/pcp/bin/idb-pcp-gui ]
then
    echo "Removing Receipts"
    if [ -d /Library/Receipts/pcp-gui-*.pkg ]; then
	rm -rf /Library/Receipts/pcp-gui-*.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-gui /usr/share/pcp/bin/idb-pcp-gui /tmp
    # create tidy up script
    echo '#!/bin/sh' > /tmp/_pcp-gui_tidy.sh
    echo '/tmp/install-pcp-gui -v -r -l /var/log/pcp-gui-inst.log /tmp/idb-pcp-gui && /bin/rm -rf /Library/Receipts/pcp-gui-*' >> /tmp/_pcp-gui_tidy.sh
    echo '/bin/rm -f /tmp/install-pcp-gui /tmp/idb-pcp-gui /tmp/_pcp-gui_tidy.sh' >> /tmp/_pcp-gui_tidy.sh
    echo 'echo Done' >> /tmp/_pcp-gui_tidy.sh
    chmod u+x /tmp/_pcp-gui_tidy.sh
    # Run remove, overriding this shell
    exec /tmp/_pcp-gui_tidy.sh
else
    echo "Unable to find /usr/share/pcp/bin/install-pcp-gui. Remove failed"
    exit 1
fi

