#!/bin/bash
#
# 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>
# Copyright (c) 2005 Juan Manuel Palacios <jmpp@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.
#

echo ""
echo "Checking the PATH variable for $USER..."
echo ""

BINPATH=/opt/local/bin
SBINPATH=/opt/local/sbin
TMP=`/usr/bin/mktemp /tmp/dp.$$`
$SHELL -l <<EOF > $TMP
    /usr/bin/printenv PATH
    exit
EOF

if grep $BINPATH $TMP >/dev/null 2>&1; then
	echo "You already have the right PATH - l337!"
else
    USHELL=`basename $SHELL`
   echo "Setting the PATH of $USHELL for $USER in ${HOME:=/Users/$USER}..."
    case $USHELL in
      *csh)
        /bin/cp -fp $HOME/.cshrc $HOME/.cshrc.dpsaved	# we backup the original
        echo "#" >> $HOME/.cshrc
        echo "# Your previous .cshrc (if any) is saved as .cshrc.dpsaved" >> $HOME/.cshrc
        echo "# Setting the path for DarwinPorts." >> $HOME/.cshrc
        echo "set path=($BINPATH $SBINPATH" '$path'")" >> $HOME/.cshrc
        chown $USER $HOME/.cshrc
        echo "Finished modifying $HOME/.cshrc"
       ;;
      *sh)
        /bin/cp -fp $HOME/.profile $HOME/.profile.dpsaved # we backup the original
        echo "#" >> $HOME/.profile
        echo "# Your previous .profile  (if any) is saved as .profile.dpsaved" >> $HOME/.profile
        echo "# Setting the path for DarwinPorts." >> $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

export PATH=$BINPATH:$PATH

# run selfupdate

echo""
echo "Selfupdating The DarwinPorts system ..."
echo ""

port -d selfupdate
if [ $? != 0 ]; then
    echo "An attempt to synchronize your recent DarwinPorts installation with OpenDarwin servers failed, please run 'port -d selfupdate' manually to find out the cause of the error."
    exit
fi

# done !!
echo ""
echo "You have succesfully installed the DarwinPorts system."
echo ""
echo "Launch a terminal and try it out !!"
echo "Read the port manualpage for help."
echo ""
