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

Last change on this file since 65056 was 65056, checked in by jmr@…, 8 years ago

delete any old macports1.0 dir in postflight script

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 9.2 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 65056 2010-03-20 14:39:25Z jmr@macports.org $
33
34
35#set -x
36
37# Script identification ('cause more often than not the svn Id is not expanded):
38VERSION=1.8.0
39
40# Abstraction variables:
41PREFIX=/opt/local
42BINPATH=${PREFIX}/bin
43SBINPATH=${PREFIX}/sbin
44CONFIGPATH=${PREFIX}/etc/macports
45MANPAGES=${PREFIX}/share/man
46MACPORTS_TCL_DIR=${PREFIX}/share/macports/Tcl
47REG1DIR=${MACPORTS_TCL_DIR}/registry1.0
48TCLSH=/usr/bin/tclsh
49TCL_PACKAGE_DIR=/Library/Tcl
50TIMESTAMP=$(date +"%Y-%m-%d_at_%H:%M:%S")
51BACKUP_SUFFIX=macports-saved_${TIMESTAMP}
52OUR_STRING="MacPorts Installer addition on ${TIMESTAMP}"
53
54# Create config files from defaults if not present
55function setup_configs {
56    for f in macports.conf sources.conf variants.conf ; do
57        if [ ! -f ${CONFIGPATH}/${f} ]; then
58            /bin/cp ${CONFIGPATH}/${f}.default ${CONFIGPATH}/${f}
59        fi
60    done
61}
62
63# Command to update the MacPorts installation through "selfupdate":
64function update_macports {
65    echo "Synchronizing the MacPorts installation with the project's rsync server..."
66    if ! ${BINPATH}/port -v selfupdate; then 
67        echo "An attempt to synchronize your recent MacPorts installation with the project's rsync server failed!"
68        echo "Please run 'sudo port -d selfupdate' manually to find out the cause of the error."
69    else
70        echo "Successful!"
71    fi
72   
73    # Add [default] tag to the central MacPorts repository, if it isn't already
74    ${TCLSH} ${PACKAGE_PATH}/Contents/Resources/upgrade_sources_conf_default.tcl ${PREFIX}
75    # Remove any duplicates from the dep map (could occur with 1.6 and earlier)
76    ${TCLSH} ${PACKAGE_PATH}/Contents/Resources/dep_map_clean.tcl ${MACPORTS_TCL_DIR}
77}
78
79# Through this command we write an environment variable to an appropriate shell configuration file,
80# backing up the original only if it exists and if it doesn't contain the ${OUR_STRING} identification string,
81# which hints that we've already tweaked it and therefore already backed it up.
82function write_setting () {
83    if [ -f ${HOME}/.${CONF_FILE} ] && ! grep "${OUR_STRING}" ${HOME}/.${CONF_FILE} > /dev/null; then
84        echo "Backing up your ${HOME}/.${CONF_FILE} shell confguration file as ${HOME}/.${CONF_FILE}.${BACKUP_SUFFIX} before adapting it for MacPorts."
85        /bin/cp -fp ${HOME}/.${CONF_FILE} "${HOME}/.${CONF_FILE}.${BACKUP_SUFFIX}" || {
86            echo "An attempt to backup your original configuration file failed! Please set your MacPorts compatible environment manually."
87            update_macports
88            exit 1
89        }
90        echo -e "\n##\n# Your previous ${HOME}/.${CONF_FILE} file was backed up as ${HOME}/.${CONF_FILE}.${BACKUP_SUFFIX}\n##" >> ${HOME}/.${CONF_FILE}
91    fi
92    echo -e "\n# ${OUR_STRING}: adding an appropriate ${1} variable for use with MacPorts." >> ${HOME}/.${CONF_FILE}
93    echo "${ENV_COMMAND} ${1}${ASSIGN}${2}" >> ${HOME}/.${CONF_FILE}
94    echo -e "# Finished adapting your ${1} environment variable for use with MacPorts.\n" >> ${HOME}/.${CONF_FILE}
95    chown ${USER} ${HOME}/.${CONF_FILE} || echo "Warning: unable to adapt permissions on your ${HOME}/.${CONF_FILE} shell configuration file!"
96    echo "An appropriate ${1} variable has been added to your shell environment by the MacPorts installer."
97}
98
99function cleanup_man () {
100    # Remove old non-compressed man pages
101    echo -e "\nRemoving old man pages..."
102    MAN1="port.1"
103    MAN5="macports.conf.5"
104    MAN7="portfile.7 portstyle.7 porthier.7 portgroup.7"
105    for m in ${MAN1}; do rm -vf ${MANPAGES}/man1/$m ; done
106    for m in ${MAN5}; do rm -vf ${MANPAGES}/man5/$m ; done
107    for m in ${MAN7}; do rm -vf ${MANPAGES}/man7/$m ; done
108    echo -e "Done.\n"
109}
110
111# Delete the old registry1.0 directory
112function delete_reg1 {
113    if [ -d ${REG1DIR} ]; then
114        rm -vrf ${REG1DIR}
115    fi
116}
117
118# link macports1.0 into tcl package dir if possible
119function link_tcl_package {
120    # delete old directory if present
121    if test -d "${TCL_PACKAGE_DIR}/macports1.0"; then
122        rm -vrf "${TCL_PACKAGE_DIR}/macports1.0" || true
123    fi
124    if ! test -e "${TCL_PACKAGE_DIR}/macports1.0" && test -w "${TCL_PACKAGE_DIR}"; then
125                ln -vs "${MACPORTS_TCL_DIR}/macports1.0" "${TCL_PACKAGE_DIR}/macports1.0" || true
126        fi
127}
128
129echo "The MacPorts Project, postflight script version ${VERSION}: checking the shell environment for user \"${USER}\"."
130
131# Set up config files
132setup_configs
133# link macports1.0 pkg
134link_tcl_package
135
136# Remove old stuff
137cleanup_man
138delete_reg1
139
140# Determine the user's shell, in order to choose an appropriate configuration file we'll be tweaking.
141# Exit nicely if the shell is any other than bash or tcsh, as that's considered non-standard.
142USHELL=$(dscl . -read /users/${USER} shell | awk -F'/' '{print $NF}') || {
143    echo "An attempt to determine your shell name failed! Please set your MacPorts compatible environment manually."
144    update_macports
145    exit 1
146}
147case "${USHELL}" in
148    tcsh)
149        echo "Detected the tcsh shell."
150        LOGIN_FLAG=""
151        ENV_COMMAND="setenv"
152        ASSIGN=" "
153        if [ -f ${HOME}/.tcshrc ]; then
154            CONF_FILE=tcshrc
155        elif [ -f ${HOME}/.cshrc ]; then
156            CONF_FILE=cshrc
157        else
158            CONF_FILE=tcshrc
159        fi
160        ;;
161    bash)
162        echo "Detected the bash shell."
163        LOGIN_FLAG="-l"
164        ENV_COMMAND="export"
165        ASSIGN="="
166        if [ -f ${HOME}/.bash_profile ]; then
167            CONF_FILE=bash_profile
168        elif [ -f ${HOME}/.bash_login ]; then
169            CONF_FILE=bash_login
170        else
171            CONF_FILE=profile
172        fi
173        ;;
174    *)
175        echo "Unknown shell ($USHELL)! Please set your MacPorts compatible environment manually."
176        update_macports
177        exit 0
178        ;;
179esac
180
181
182# Adding our setting to the PATH variable if not already there:
183if ${SHELL} ${LOGIN_FLAG} -c "/usr/bin/printenv PATH" | grep ${PREFIX} > /dev/null; then
184    echo "Your shell already has the right PATH environment variable for use with MacPorts!"
185else
186    write_setting PATH "${BINPATH}:${SBINPATH}:\$PATH"
187fi
188
189# We gather the path into a variable of our own for faster operation:
190ORIGINAL_MANPATH="$(${SHELL} ${LOGIN_FLAG} -c "/usr/bin/printenv MANPATH")"
191# Adding our setting to the MANPATH variable only if it exists:
192if ! ${SHELL} ${LOGIN_FLAG} -c "/usr/bin/env | grep MANPATH" > /dev/null || \
193# and following that, if it's not empty:
194  [ -z "${ORIGINAL_MANPATH}" ] || \
195# or if it doesn't already contain our path:
196  echo "${ORIGINAL_MANPATH}" | grep ${MANPAGES} > /dev/null || \
197# or if there's no empty component somewhere in the middle of it:
198  echo "${ORIGINAL_MANPATH}" | grep :: > /dev/null || \
199# or at the start of it:
200  [ -z "$(echo "${ORIGINAL_MANPATH}" | awk -F : '{print $1}')" ] || \
201# or at the end of it:
202  [ -z "$(echo "${ORIGINAL_MANPATH}" | awk -F : '{print $NF}')" ]; then
203    echo "Your shell already has the right MANPATH environment variable for use with MacPorts!"
204else
205    write_setting MANPATH "${MANPAGES}:\$MANPATH"
206fi
207
208# Adding a DISPLAY variable only if we're running on Tiger or less and if it doesn't already exist:
209if (($(sw_vers -productVersion | awk -F . '{print $2}') >= 5)) || ${SHELL} ${LOGIN_FLAG} -c "/usr/bin/env | grep DISPLAY" > /dev/null; then
210    echo "Your shell already has the right DISPLAY environment variable for use with MacPorts!"
211else
212    write_setting DISPLAY ":0"
213fi
214
215
216# Postflight script is done with its job, update MacPorts and exit gracefully!
217update_macports
218echo "You have succesfully installed the MacPorts system, launch a terminal and try it out!"
219echo "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."
220exit 0
Note: See TracBrowser for help on using the repository browser.