source: trunk/base/portmgr/dmg/postflight @ 38037

Last change on this file since 38037 was 38037, checked in by raimue@…, 11 years ago

portmgr/dmg/postflight:
Copy the postflight script from the release_1_6 branch so the changes will not be lost or forgotten

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 7.1 KB
Line 
1#!/bin/bash
2# -*- coding: utf-8; mode: shell-script-mode; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:filetype=sh:et:sw=4:ts=4:sts=4
3#
4# Copyright (c) 2002-2007 Juan Manuel Palacios <jmpp@macports.org>, The MacPorts Project.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15# 3. Neither the name of Apple, Inc., The MacPorts Project nor the
16#    names of its contributors may be used to endorse or promote products
17#    derived from this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
23# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29# SUCH DAMAGE.
30#
31# postflight
32# $Id: postflight 38037 2008-07-04 02:58:10Z raimue@macports.org $
33
34
35#set -x
36
37# Script identification ('cause more often than not the svn Id is not expanded):
38VERSION=1.7.0
39
40# Abstraction variables:
41PREFIX=/opt/local
42BINPATH=${PREFIX}/bin
43SBINPATH=${PREFIX}/sbin
44MANPAGES=${PREFIX}/share/man
45TIMESTAMP=$(date +"%Y-%m-%d_at_%H:%M:%S")
46BACKUP_SUFFIX=macports-saved_${TIMESTAMP}
47OUR_STRING="MacPorts Installer addition on ${TIMESTAMP}"
48
49
50# Command to update the MacPorts installation through "selfupdate":
51function update_macports {
52    echo "Synchronizing the MacPorts installation with the project's rsync server..."
53    if ! ${BINPATH}/port -v selfupdate; then 
54        echo "An attempt to synchronize your recent MacPorts installation with the project's rsync server failed!"
55        echo "Please run 'sudo port -d selfupdate' manually to find out the cause of the error."
56    else
57        echo "Successful!"
58    fi
59}
60
61# Through this command we write an environment variable to an appropriate shell configuration file,
62# backing up the original only if it exists and if it doesn't contain the ${OUR_STRING} identification string,
63# which hints that we've already tweaked it and therefore already baked it up.
64function write_setting () {
65    if [ -f ${HOME}/.${CONF_FILE} ] && ! grep "${OUR_STRING}" ${HOME}/.${CONF_FILE} > /dev/null; then
66        echo "Backing up your ${HOME}/.${CONF_FILE} shell confguration file as ${HOME}/.${CONF_FILE}.${BACKUP_SUFFIX} before adapting it for MacPorts."
67        /bin/cp -fp ${HOME}/.${CONF_FILE} "${HOME}/.${CONF_FILE}.${BACKUP_SUFFIX}" || {
68            echo "An attempt to backup your original configuration file failed! Please set your MacPorts compatible environment manually."
69            update_macports
70            exit 1
71        }
72        echo -e "\n##\n# Your previous ${HOME}/.${CONF_FILE} file was backed up as ${HOME}/.${CONF_FILE}.${BACKUP_SUFFIX}\n##" >> ${HOME}/.${CONF_FILE}
73    fi
74    echo -e "\n# ${OUR_STRING}: adding an appropriate ${1} variable for use with MacPorts." >> ${HOME}/.${CONF_FILE}
75    echo "${ENV_COMMAND} ${1}${ASSIGN}${2}" >> ${HOME}/.${CONF_FILE}
76    echo -e "# Finished adapting your ${1} environment variable for use with MacPorts.\n" >> ${HOME}/.${CONF_FILE}
77    chown ${USER} ${HOME}/.${CONF_FILE} || echo "Warning: unable to adapt permissions on your ${HOME}/.${CONF_FILE} shell configuration file!"
78    echo "An appropriate ${1} variable has been added to your shell environment by the MacPorts installer."
79}
80
81
82echo "The MacPorts Project, postflight script version ${VERSION}: checking the shell environment for user \"${USER}\"."
83
84
85# Determine the user's shell, in order to choose an appropriate configuration file we'll be tweaking.
86# Exit nicely if the shell is any other than bash or tcsh, as that's considered non-standard.
87USHELL=$(dscl . -read /users/${USER} shell | awk -F'/' '{print $NF}') || {
88    echo "An attempt to determine your shell name failed! Please set your MacPorts compatible environment manually."
89    update_macports
90    exit 1
91}
92case "${USHELL}" in
93    tcsh)
94        echo "Detected the tcsh shell."
95        LOGIN_FLAG=""
96        ENV_COMMAND="setenv"
97        ASSIGN=" "
98        CONF_FILE=tcshrc
99        ;;
100    bash)
101        echo "Detected the bash shell."
102        LOGIN_FLAG="-l"
103        ENV_COMMAND="export"
104        ASSIGN="="
105        CONF_FILE=profile
106        ;;
107    *)
108        echo "Unknown shell ($USHELL)! Please set your MacPorts compatible environment manually."
109        update_macports
110        exit 0
111        ;;
112esac
113
114
115# Adding our setting to the PATH variable if not already there:
116if ${SHELL} ${LOGIN_FLAG} -c "/usr/bin/printenv PATH" | grep ${PREFIX} > /dev/null; then
117    echo "Your shell already has the right PATH environment variable for use with MacPorts!"
118else
119    write_setting PATH "${BINPATH}:${SBINPATH}:\$PATH"
120fi
121
122# We gather the path into a variable of our own for faster operation:
123ORIGINAL_MANPATH="$(${SHELL} ${LOGIN_FLAG} -c "/usr/bin/printenv MANPATH")"
124# Adding out setting to the MANPATH variable only if it exists:
125if ! ${SHELL} ${LOGIN_FLAG} -c "/usr/bin/env | grep MANPATH" > /dev/null || \
126# and following that, if it's not empty:
127  [ -z "${ORIGINAL_MANPATH}" ] || \
128# or if it doesn't already contain our path:
129  echo "${ORIGINAL_MANPATH}" | grep ${MANPAGES} > /dev/null || \
130# or if there's no empty component somewhere in the middle of it:
131  echo "${ORIGINAL_MANPATH}" | grep :: > /dev/null || \
132# or at the start of it:
133  [ -z "$(echo "${ORIGINAL_MANPATH}" | awk -F : '{print $1}')" ] || \
134# or at the end of it:
135  [ -z "$(echo "${ORIGINAL_MANPATH}" | awk -F : '{print $NF}')" ]; then
136    echo "Your shell already has the right MANPATH environment variable for use with MacPorts!"
137else
138    write_setting MANPATH "${MANPAGES}:\$MANPATH"
139fi
140
141# Adding a DISPLAY variable only if we're running on Tiger or less and if it doesn't already exist:
142if (($(sw_vers -productVersion | awk -F . '{print $2}') >= 5)) || ${SHELL} ${LOGIN_FLAG} -c "/usr/bin/env | grep DISPLAY" > /dev/null; then
143    echo "Your shell already has the right DISPLAY environment variable for use with MacPorts!"
144else
145    write_setting DISPLAY ":0"
146fi
147
148
149# Postflight script is done with its job, update MacPorts and exit gracefully!
150update_macports
151echo "You have succesfully installed the MacPorts system, launch a terminal and try it out!"
152echo "Read the port(1) manual page and http://guide.macports.org for help, http://www.macports.org/contact.php if you need to get in touch with The MacPorts Project."
153exit 0
Note: See TracBrowser for help on using the repository browser.