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

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

delete any existing registry1.0 directory during install

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