#!/bin/bash
#
# Copyright (c) 2007 Juan Manuel Palacios <jmpp@macports.org>
# Copyright (c) 2005 Ole Guldberg Jensen <olegb@opendarwin.org>
# Copyright (c) 2005 Dr. Ernie Prabhakar <drernir@opendarwin.org>
# Copyright (c) 2005 Matt Anton <matt@opendarwin.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY Eric Melville AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# postflight
# $Id: postflight 31681 2007-12-03 01:57:53Z jmpp@macports.org $


BINPATH=/opt/local/bin
SBINPATH=/opt/local/sbin


echo -e "\nChecking the PATH variable for $USER...\n"

# No need to do any path adjustments if we're on Leopard, as we already install our path configs into /etc/paths.d/ and /etc/manpaths.d/
if [ $(sw_vers -productVersion | awk -F . '{print $2}') -eq 5 ]; then
    echo "No need to adjust the PATH and MANPATH environment variables on Leopard!"
    exit 0
fi

if $SHELL -c "/usr/bin/printenv PATH" | grep -c $BINPATH > /dev/null; then
    echo "You already have the right PATH!"
else
    USHELL=`basename $SHELL`
    echo "Setting the PATH of $USHELL for $USER in ${HOME:=/Users/$USER}..."
    case $USHELL in
        *csh)
	    # we backup the original
            /bin/cp -fp $HOME/.cshrc $HOME/.cshrc.mpsaved
            echo "#" >> $HOME/.cshrc
            echo "# Your previous .cshrc (if any) is saved as .cshrc.mpsaved" >> $HOME/.cshrc
            echo "# Setting the path for MacPorts." >> $HOME/.cshrc
            echo "set path=($BINPATH $SBINPATH" '$path'")" >> $HOME/.cshrc
            chown $USER $HOME/.cshrc
            echo "Finished modifying $HOME/.cshrc"
        ;;
	*sh)
	    # we backup the original
            /bin/cp -fp $HOME/.profile $HOME/.profile.mpsaved
            echo "#" >> $HOME/.profile
            echo "# Your previous .profile  (if any) is saved as .profile.mpsaved" >> $HOME/.profile
            echo "# Setting the path for MacPorts." >> $HOME/.profile
            echo "export PATH=$BINPATH:$SBINPATH:\$PATH" >> $HOME/.profile
            chown $USER $HOME/.profile
            echo "Finished modifying $HOME/.profile"
        ;;
	*)
            echo "Unknown shell! Please set your own PATH manually."
        ;;
    esac
fi


# Run selfupdate:
export PATH=$BINPATH:$PATH
echo -e "\nSelfupdating The MacPorts system...\n"
port -d selfupdate
if [ $? != 0 ]; then
    echo "An attempt to synchronize your recent MacPorts installation with the rsync server failed, please run 'port -d selfupdate' manually to find out the cause of the error."
    exit 1
fi


# Postflight script is done with its job!
echo -e "\nYou have succesfully installed the MacPorts system.\n"
echo "Launch a terminal and try it out!"
echo -e "Read the port manual page for help.\n"
