Changeset 31774


Ignore:
Timestamp:
Dec 7, 2007, 2:15:15 AM (10 years ago)
Author:
jmpp@…
Message:

Simply amazing the amount of bugs the simple addition of a MANPATH check and setting introduced into this script,
it was simply not scalable. Therefore I rewrote it from scratch in a modular fashion (and tested it *very* thoroughly,
since this is going to be tweaking shell configuration files) to allow for an arbitrary number of environment checks
and settings additions, so that it can serve us in the future to check/add anything else we might need, if need be
(that is, if a better approach to tweaking shell configuration files is not settled on).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/release_1_6/base/portmgr/dmg/postflight

    r31688 r31774  
    11#!/bin/bash
    22#
    3 # Copyright (c) 2007 Juan Manuel Palacios <jmpp@macports.org>
    4 # Copyright (c) 2005 Ole Guldberg Jensen <olegb@opendarwin.org>
    5 # Copyright (c) 2005 Dr. Ernie Prabhakar <drernir@opendarwin.org>
    6 # Copyright (c) 2005 Matt Anton <matt@opendarwin.org>
     3# Copyright (c) 2002-2007 Juan Manuel Palacios <jmpp@macports.org>, The MacPorts Project.
    74# All rights reserved.
    85#
     
    1512#    notice, this list of conditions and the following disclaimer in the
    1613#    documentation and/or other materials provided with the distribution.
     14# 3. Neither the name of Apple, Inc., The MacPorts Project nor the
     15#    names of its contributors may be used to endorse or promote products
     16#    derived from this software without specific prior written permission.
    1717#
    18 # THIS SOFTWARE IS PROVIDED BY Eric Melville AND CONTRIBUTORS ``AS IS'' AND
     18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    1919# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    2020# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    21 # ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
    2222# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    2323# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     
    3232
    3333
     34# Abstraction variables:
    3435PREFIX=/opt/local
    3536BINPATH=$PREFIX/bin
    3637SBINPATH=$PREFIX/sbin
     38MANPAGES=$PREFIX/share/man
     39USHELL=$(basename $SHELL)
     40if [ $? != 0 ]; then
     41    echo "An attempt to determine your shell name failed! Please set your MacPorts compatible environment manually."
     42    exit 1
     43fi
     44case $USHELL in
     45    *csh)
     46        CONF_FILE=.cshrc
     47        ;;
     48    *sh)
     49        CONF_FILE=.profile
     50        ;;
     51    *)
     52        echo "Unknown shell! Please set your MacPorts compatible environment manually."
     53        ;;
     54esac
     55BACKUP_SUFFIX=mpsaved_$(date +"%Y-%m-%d_at_%H:%M:%S")
     56OUR_STRING="MacPorts setting on $(date +"%Y-%m-%d at %H:%M:%S"):"
    3757
    3858
    39 echo -e "\nChecking the PATH and MANPATH variables for user $USER...\n"
    40 
    41 if $SHELL -c "/usr/bin/printenv PATH" | grep -c $BINPATH > /dev/null; then
    42     echo "You already have the right PATH variable!"
    43 else
    44     USHELL=`basename $SHELL`
    45     echo "Setting the PATH of $USHELL for $USER in ${HOME:=/Users/$USER}..."
    46     case $USHELL in
    47         *csh)
    48             # we backup the original
    49             /bin/cp -fp $HOME/.cshrc $HOME/.cshrc.mpsaved
    50             echo "#" >> $HOME/.cshrc
    51             echo "# Your previous .cshrc (if any) is saved as .cshrc.mpsaved" >> $HOME/.cshrc
    52             echo "# Setting the path for MacPorts." >> $HOME/.cshrc
    53             echo "set path=($BINPATH $SBINPATH" '$path'")" >> $HOME/.cshrc
    54             chown $USER $HOME/.cshrc
    55             echo "Finished modifying $HOME/.cshrc"
    56         ;;
    57         *sh)
    58             # we backup the original
    59             /bin/cp -fp $HOME/.profile $HOME/.profile.mpsaved
    60             echo "#" >> $HOME/.profile
    61             echo "# Your previous .profile  (if any) is saved as .profile.mpsaved" >> $HOME/.profile
    62             echo "# Setting the path for MacPorts." >> $HOME/.profile
    63             echo "export PATH=$BINPATH:$SBINPATH:\$PATH" >> $HOME/.profile
    64             chown $USER $HOME/.profile
    65             echo "Finished modifying $HOME/.profile"
    66         ;;
    67         *)
    68             echo "Unknown shell! Please set your own PATH manually."
     59# Through this command we write an environment variable to an appropriate shell configuration file,
     60# backing up the original only if it exists and if it doesn't contain the $OUR_STRING identification string,
     61# which hints that we've already tweaked it and therefore already baked it up.
     62function write_setting () {
     63    if [ -f $HOME/$CONF_FILE ] && ! grep "$OUR_STRING" $HOME/$CONF_FILE; then
     64        /bin/cp -fp $HOME/$CONF_FILE $HOME/$CONF_FILE.$BACKUP_SUFFIX
     65        if [ $? != 0 ]; then
     66            echo "An attempt to backup your original configuration file failed! Please set your MacPorts compatible environment manually."
     67            exit 1
     68        fi
     69        echo -e "\n#\n# Your previous $HOME/$CONF_FILE file was backed up as $HOME/$CONF_FILE.$BACKUP_SUFFIX\n#" >> $HOME/$CONF_FILE
     70    fi
     71    echo -e "\n# $OUR_STRING adding an appropriate $1 variable for use with MacPorts." >> $HOME/$CONF_FILE
     72    case $1 in
     73        PATH)
     74            if [ "$CONF_FILE" == ".profile" ]; then
     75                echo "export PATH=$BINPATH:$SBINPATH:\$PATH" >> $HOME/$CONF_FILE
     76            elif [ "$CONF_FILE" == ".cshrc" ]; then
     77                echo "set path=($BINPATH $SBINPATH" '$path'")" >> $HOME/$CONF_FILE
     78            fi
     79            ;;
     80        MANPATH)
     81            if [ "$CONF_FILE" == ".profile" ]; then
     82                echo "export MANPATH=$MANPAGES:\$MANPATH" >> $HOME/$CONF_FILE
     83            elif [ "$CONF_FILE" == ".cshrc" ]; then
     84                echo "set manpath=($MANPAGES" '$manpath'")" >> $HOME/$CONF_FILE
     85            fi
    6986        ;;
    7087    esac
     88    chown $USER $HOME/$CONF_FILE
     89    echo -e "# Finished adapting your $1 environment variable for use with MacPorts.\n" >> $HOME/$CONF_FILE
     90}
     91
     92
     93echo -e "\nChecking the shell environment for user \"$USER\"...\n"
     94
     95# Adding our setting to the PATH variable if not already there:
     96if $SHELL -lc "/usr/bin/printenv PATH" | grep $PREFIX > /dev/null 2>&1; then
     97    echo "Your shell already has the right PATH environment variable for use with MacPorts!"
     98else
     99    write_setting PATH
     100fi
     101
     102# Adding out setting to the MANPATH variable only if it exists and, following that, if it doesn't already contain our path:
     103if ! $SHELL -lc "/usr/bin/env | grep MANPATH >/dev/null 2>&1" || $SHELL -lc "/usr/bin/printenv MANPATH" | grep $MANPAGES > /dev/null 2>&1; then
     104    echo "Your shell already has the right MANPATH environment variable for use with MacPorts!"
     105else
     106    write_setting MANPATH
    71107fi
    72108
    73109
    74 # Run selfupdate:
     110# Update the MacPorts installation through "selfupdate":
     111echo -e "\nSynchronizing the MacPorts installation with the project's rsync server...\n"
    75112export PATH=$BINPATH:$PATH
    76 echo -e "\nSelfupdating The MacPorts system...\n"
    77113port -d selfupdate
    78114if [ $? != 0 ]; then
    79     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."
     115    echo "An attempt to synchronize your recent MacPorts installation with the project's rsync server failed!"
     116    echo "Please run 'port -d selfupdate' manually to find out the cause of the error."
    80117    exit 1
    81118fi
     
    85122echo -e "\nYou have succesfully installed the MacPorts system.\n"
    86123echo "Launch a terminal and try it out!"
    87 echo -e "Read the port manual page for help.\n"
     124echo -e "Read the port(1) manual page for help.\n"
     125exit 0
Note: See TracChangeset for help on using the changeset viewer.