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

Last change on this file since 81558 was 81558, checked in by jeremyhu@…, 9 years ago

Lion: Hide new users from Users & Groups preference pane. #30168

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 11.7 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) 2008-2011 The MacPorts Project
5# Copyright (c) 2002-2007 Juan Manuel Palacios <jmpp@macports.org>, The MacPorts Project.
6# All rights reserved.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16# 3. Neither the name of The MacPorts Project nor the
17#    names of its contributors may be used to endorse or promote products
18#    derived from this software without specific prior written permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
24# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30# SUCH DAMAGE.
31#
32# postflight
33# $Id: postflight 81558 2011-08-01 22:05:00Z jeremyhu@macports.org $
34
35
36#set -x
37
38# Script identification ('cause more often than not the svn Id is not expanded):
39VERSION=2.0.0
40
41# Abstraction variables:
42PREFIX=__PREFIX__
43BINPATH=${PREFIX}/bin
44SBINPATH=${PREFIX}/sbin
45CONFIGPATH=${PREFIX}/etc/macports
46MANPAGES=${PREFIX}/share/man
47MACPORTS_TCL_DIR=${PREFIX}/share/macports/Tcl
48REG1DIR=${MACPORTS_TCL_DIR}/registry1.0
49DSCL=/usr/bin/dscl
50RUNUSR=macports
51TCLSH=/usr/bin/tclsh
52TCL_PACKAGE_DIR=/Library/Tcl
53TIMESTAMP=$(date +"%Y-%m-%d_at_%H:%M:%S")
54BACKUP_SUFFIX=macports-saved_${TIMESTAMP}
55OUR_STRING="MacPorts Installer addition on ${TIMESTAMP}"
56
57# Create config files from defaults if not present
58function setup_configs {
59    for f in macports.conf pubkeys.conf sources.conf variants.conf ; do
60        if [[ ! -f ${CONFIGPATH}/${f} ]]; then
61            echo "Copying ${f}.default to ${f}"
62            /bin/cp ${CONFIGPATH}/${f}.default ${CONFIGPATH}/${f}
63            /bin/chmod 644 ${CONFIGPATH}/${f}
64        fi
65    done
66}
67
68# Command to update the MacPorts installation through "selfupdate":
69function update_macports {
70    # Add [default] tag to the central MacPorts repository, if it isn't already
71    echo "Adding [default] tag to sources.conf if needed..."
72    ${TCLSH} ${PACKAGE_PATH}/Contents/Resources/upgrade_sources_conf_default.tcl ${PREFIX}
73    # Convert image directories (and direct mode installs) to image archives
74    echo "Updating port image format..."
75    ${TCLSH} ${PACKAGE_PATH}/Contents/Resources/images_to_archives.tcl ${MACPORTS_TCL_DIR}
76
77    echo "Synchronizing the MacPorts installation with the project's rsync server..."
78    if ! ${BINPATH}/port -v selfupdate; then 
79        echo "An attempt to synchronize your recent MacPorts installation with the project's rsync server failed!"
80        echo "Please run 'sudo port -d selfupdate' manually to find out the cause of the error."
81    else
82        echo "Successful!"
83    fi
84}
85
86# Through this command we write an environment variable to an appropriate shell configuration file,
87# backing up the original only if it exists and if it doesn't contain the ${OUR_STRING} identification string,
88# which hints that we've already tweaked it and therefore already backed it up.
89function write_setting () {
90    if [[ -f ${HOME}/.${CONF_FILE} ]] && ! grep "${OUR_STRING}" ${HOME}/.${CONF_FILE} > /dev/null; then
91        echo "Backing up your ${HOME}/.${CONF_FILE} shell confguration file as ${HOME}/.${CONF_FILE}.${BACKUP_SUFFIX} before adapting it for MacPorts."
92        /bin/cp -fp ${HOME}/.${CONF_FILE} "${HOME}/.${CONF_FILE}.${BACKUP_SUFFIX}" || {
93            echo "An attempt to backup your original configuration file failed! Please set your MacPorts compatible environment manually."
94            update_macports
95            exit 1
96        }
97        echo -e "\n##\n# Your previous ${HOME}/.${CONF_FILE} file was backed up as ${HOME}/.${CONF_FILE}.${BACKUP_SUFFIX}\n##" >> ${HOME}/.${CONF_FILE}
98    fi
99    echo -e "\n# ${OUR_STRING}: adding an appropriate ${1} variable for use with MacPorts." >> ${HOME}/.${CONF_FILE}
100    echo "${ENV_COMMAND} ${1}${ASSIGN}${2}" >> ${HOME}/.${CONF_FILE}
101    echo -e "# Finished adapting your ${1} environment variable for use with MacPorts.\n" >> ${HOME}/.${CONF_FILE}
102    chown ${USER} ${HOME}/.${CONF_FILE} || echo "Warning: unable to adapt permissions on your ${HOME}/.${CONF_FILE} shell configuration file!"
103    echo "An appropriate ${1} variable has been added to your shell environment by the MacPorts installer."
104}
105
106function cleanup_man () {
107    # Remove old non-compressed man pages
108    echo -e "\nRemoving old man pages..."
109    MAN1="port.1"
110    MAN5="macports.conf.5"
111    MAN7="portfile.7 portstyle.7 porthier.7 portgroup.7"
112    for m in ${MAN1}; do rm -vf ${MANPAGES}/man1/$m ; done
113    for m in ${MAN5}; do rm -vf ${MANPAGES}/man5/$m ; done
114    for m in ${MAN7}; do rm -vf ${MANPAGES}/man7/$m ; done
115    echo -e "Done.\n"
116}
117
118# Delete the old registry1.0 directory
119function delete_reg1 {
120    if [[ -d ${REG1DIR} ]]; then
121        rm -vrf ${REG1DIR}
122    fi
123}
124
125# link macports1.0 into tcl package dir if possible
126function link_tcl_package {
127    # delete old directory if present
128    if [[ ! -L "${TCL_PACKAGE_DIR}/macports1.0" ]] && [[ -d "${TCL_PACKAGE_DIR}/macports1.0" ]]; then
129        rm -vrf "${TCL_PACKAGE_DIR}/macports1.0" || true
130    fi
131    if [[ ! -e "${TCL_PACKAGE_DIR}/macports1.0" ]] && [[ -w "${TCL_PACKAGE_DIR}" ]]; then
132                ln -vs "${MACPORTS_TCL_DIR}/macports1.0" "${TCL_PACKAGE_DIR}/macports1.0" || true
133        fi
134}
135
136# create the user to use for privilege dropping
137function create_run_user {
138    DSEDITGROUP=/usr/sbin/dseditgroup
139    if ! ${DSCL} -q . -read /Groups/${RUNUSR} > /dev/null 2>&1 ; then
140        echo "Creating group \"${RUNUSR}\""
141        ${DSEDITGROUP} -q -o create ${RUNUSR}
142    fi
143    if ! ${DSCL} -q . -list /Users/${RUNUSR} > /dev/null 2>&1 ; then
144        echo "Creating user \"${RUNUSR}\""
145        NEXTUID=501
146        while [[ -n "`${DSCL} -q /Search -search /Users UniqueID $NEXTUID`" ]]; do
147            let "NEXTUID=NEXTUID+1"
148        done
149        ${DSCL} -q . -create /Users/${RUNUSR} UniqueID $NEXTUID
150
151        # These are implicitly added on Mac OSX Lion.  AuthenticationAuthority
152        # causes the user to be visible in the Users & Groups Preference Pane,
153        # and the others are just noise, so delete them.
154        # https://trac.macports.org/ticket/30168
155        ${DSCL} -q . -delete /Users/${RUNUSR} AuthenticationAuthority
156        ${DSCL} -q . -delete /Users/${RUNUSR} PasswordPolicyOptions
157        ${DSCL} -q . -delete /Users/${RUNUSR} dsAttrTypeNative:KerberosKeys
158        ${DSCL} -q . -delete /Users/${RUNUSR} dsAttrTypeNative:ShadowHashData
159
160        ${DSCL} -q . -create /Users/${RUNUSR} RealName MacPorts
161        ${DSCL} -q . -create /Users/${RUNUSR} Password \*
162        ${DSCL} -q . -create /Users/${RUNUSR} PrimaryGroupID $(${DSCL} -q . -read /Groups/${RUNUSR} PrimaryGroupID | /usr/bin/awk '{print $2}')
163        ${DSCL} -q . -create /Users/${RUNUSR} NFSHomeDirectory /var/empty
164        ${DSCL} -q . -create /Users/${RUNUSR} UserShell /usr/bin/false
165    fi
166    if [[ $(sw_vers -productVersion | /usr/bin/awk -F . '{print $2}') -eq 4 ]]; then
167        GID=$(${DSCL} -q . -read /Groups/${RUNUSR} PrimaryGroupID | /usr/bin/awk '{print $2}')
168        if [[ "$(${DSCL} -q . -read /Users/${RUNUSR} PrimaryGroupID 2>/dev/null | /usr/bin/awk '{print $2}')" != "$GID" ]]; then
169            echo "Fixing PrimaryGroupID for user \"${RUNUSR}\""
170            ${DSCL} -q . -create /Users/${RUNUSR} PrimaryGroupID $GID
171            ${DSCL} -q . -create /Users/${RUNUSR} RealName MacPorts
172        fi
173    fi
174}
175
176echo "The MacPorts Project, postflight script version ${VERSION}: checking the shell environment for user \"${USER}\"."
177
178# create macports user
179create_run_user
180# Set up config files
181setup_configs
182# link macports1.0 pkg
183link_tcl_package
184
185# Remove old stuff
186cleanup_man
187delete_reg1
188
189# Determine the user's shell, in order to choose an appropriate configuration file we'll be tweaking.
190# Exit nicely if the shell is any other than bash or tcsh, as that's considered non-standard.
191USHELL=$(${DSCL} . -read /Users/${USER} shell | awk -F'/' '{print $NF}') || {
192    echo "An attempt to determine your shell name failed! Please set your MacPorts compatible environment manually."
193    update_macports
194    exit 1
195}
196case "${USHELL}" in
197    tcsh)
198        echo "Detected the tcsh shell."
199        LOGIN_FLAG=""
200        ENV_COMMAND="setenv"
201        ASSIGN=" "
202        if [[ -f ${HOME}/.tcshrc ]]; then
203            CONF_FILE=tcshrc
204        elif [[ -f ${HOME}/.cshrc ]]; then
205            CONF_FILE=cshrc
206        else
207            CONF_FILE=tcshrc
208        fi
209        ;;
210    bash)
211        echo "Detected the bash shell."
212        LOGIN_FLAG="-l"
213        ENV_COMMAND="export"
214        ASSIGN="="
215        if [[ -f ${HOME}/.bash_profile ]]; then
216            CONF_FILE=bash_profile
217        elif [[ -f ${HOME}/.bash_login ]]; then
218            CONF_FILE=bash_login
219        else
220            CONF_FILE=profile
221        fi
222        ;;
223    *)
224        echo "Unknown shell ($USHELL)! Please set your MacPorts compatible environment manually."
225        update_macports
226        exit 0
227        ;;
228esac
229
230
231# Adding our setting to the PATH variable if not already there:
232if ${SHELL} ${LOGIN_FLAG} -c "/usr/bin/printenv PATH" | grep ${PREFIX} > /dev/null; then
233    echo "Your shell already has the right PATH environment variable for use with MacPorts!"
234else
235    write_setting PATH "${BINPATH}:${SBINPATH}:\$PATH"
236fi
237
238# We gather the path into a variable of our own for faster operation:
239ORIGINAL_MANPATH="$(${SHELL} ${LOGIN_FLAG} -c "/usr/bin/printenv MANPATH")"
240# Adding our setting to the MANPATH variable only if it exists:
241if ! ${SHELL} ${LOGIN_FLAG} -c "/usr/bin/env | grep MANPATH" > /dev/null || \
242# and following that, if it's not empty:
243  [[ -z "${ORIGINAL_MANPATH}" ]] || \
244# or if it doesn't already contain our path:
245  echo "${ORIGINAL_MANPATH}" | grep ${MANPAGES} > /dev/null || \
246# or if there's no empty component somewhere in the middle of it:
247  echo "${ORIGINAL_MANPATH}" | grep :: > /dev/null || \
248# or at the start of it:
249  [[ -z "$(echo "${ORIGINAL_MANPATH}" | awk -F : '{print $1}')" ]] || \
250# or at the end of it:
251  [[ -z "$(echo "${ORIGINAL_MANPATH}" | awk -F : '{print $NF}')" ]]; then
252    echo "Your shell already has the right MANPATH environment variable for use with MacPorts!"
253else
254    write_setting MANPATH "${MANPAGES}:\$MANPATH"
255fi
256
257# Adding a DISPLAY variable only if we're running on Tiger or less and if it doesn't already exist:
258if (($(sw_vers -productVersion | awk -F . '{print $2}') >= 5)) || ${SHELL} ${LOGIN_FLAG} -c "/usr/bin/env | grep DISPLAY" > /dev/null; then
259    echo "Your shell already has the right DISPLAY environment variable for use with MacPorts!"
260else
261    write_setting DISPLAY ":0"
262fi
263
264
265# Postflight script is done with its job, update MacPorts and exit gracefully!
266update_macports
267echo "You have succesfully installed the MacPorts system, launch a terminal and try it out!"
268echo "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."
269exit 0
Note: See TracBrowser for help on using the repository browser.