| 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-2012 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$ |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | #set -x |
|---|
| 37 | |
|---|
| 38 | # Script identification ('cause more often than not the svn Id is not expanded): |
|---|
| 39 | VERSION=2.1.0 |
|---|
| 40 | |
|---|
| 41 | # Abstraction variables: |
|---|
| 42 | PREFIX=__PREFIX__ |
|---|
| 43 | BINPATH=${PREFIX}/bin |
|---|
| 44 | SBINPATH=${PREFIX}/sbin |
|---|
| 45 | CONFIGPATH=${PREFIX}/etc/macports |
|---|
| 46 | MANPAGES=${PREFIX}/share/man |
|---|
| 47 | MACPORTS_TCL_DIR=${PREFIX}/share/macports/Tcl |
|---|
| 48 | REG1DIR=${MACPORTS_TCL_DIR}/registry1.0 |
|---|
| 49 | DSCL=/usr/bin/dscl |
|---|
| 50 | RUNUSR=macports |
|---|
| 51 | TCLSH=/usr/bin/tclsh |
|---|
| 52 | TCL_PACKAGE_DIR=/Library/Tcl |
|---|
| 53 | TIMESTAMP=$(date +"%Y-%m-%d_at_%H:%M:%S") |
|---|
| 54 | BACKUP_SUFFIX=macports-saved_${TIMESTAMP} |
|---|
| 55 | OUR_STRING="MacPorts Installer addition on ${TIMESTAMP}" |
|---|
| 56 | |
|---|
| 57 | # Create config files from defaults if not present |
|---|
| 58 | function setup_configs { |
|---|
| 59 | for f in archive_sites.conf 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": |
|---|
| 69 | function update_macports { |
|---|
| 70 | SCRIPT_DIR=$(dirname $0) |
|---|
| 71 | # Add [default] tag to the central MacPorts repository, if it isn't already |
|---|
| 72 | echo "Adding [default] tag to sources.conf if needed..." |
|---|
| 73 | ${TCLSH} ${SCRIPT_DIR}/upgrade_sources_conf_default.tcl ${PREFIX} |
|---|
| 74 | # Convert image directories (and direct mode installs) to image archives |
|---|
| 75 | echo "Updating port image format..." |
|---|
| 76 | ${TCLSH} ${SCRIPT_DIR}/images_to_archives.tcl ${MACPORTS_TCL_DIR} |
|---|
| 77 | |
|---|
| 78 | echo "Synchronizing the MacPorts installation with the project's rsync server..." |
|---|
| 79 | if ! ${BINPATH}/port -v selfupdate; then |
|---|
| 80 | echo "An attempt to synchronize your recent MacPorts installation with the project's rsync server failed!" |
|---|
| 81 | echo "Please run 'sudo port -d selfupdate' manually to find out the cause of the error." |
|---|
| 82 | else |
|---|
| 83 | echo "Successful!" |
|---|
| 84 | fi |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | # Through this command we write an environment variable to an appropriate shell configuration file, |
|---|
| 88 | # backing up the original only if it exists and if it doesn't contain the ${OUR_STRING} identification string, |
|---|
| 89 | # which hints that we've already tweaked it and therefore already backed it up. |
|---|
| 90 | function write_setting () { |
|---|
| 91 | if [[ -f "${HOME}/.${CONF_FILE}" ]] && ! grep "${OUR_STRING}" "${HOME}/.${CONF_FILE}" > /dev/null; then |
|---|
| 92 | echo "Backing up your ${HOME}/.${CONF_FILE} shell confguration file as ${HOME}/.${CONF_FILE}.${BACKUP_SUFFIX} before adapting it for MacPorts." |
|---|
| 93 | /bin/cp -fp "${HOME}/.${CONF_FILE}" "${HOME}/.${CONF_FILE}.${BACKUP_SUFFIX}" || { |
|---|
| 94 | echo "An attempt to backup your original configuration file failed! Please set your MacPorts compatible environment manually." |
|---|
| 95 | update_macports |
|---|
| 96 | exit 1 |
|---|
| 97 | } |
|---|
| 98 | echo -e "\n##\n# Your previous ${HOME}/.${CONF_FILE} file was backed up as ${HOME}/.${CONF_FILE}.${BACKUP_SUFFIX}\n##" >> "${HOME}/.${CONF_FILE}" |
|---|
| 99 | fi |
|---|
| 100 | echo -e "\n# ${OUR_STRING}: adding an appropriate ${1} variable for use with MacPorts." >> "${HOME}/.${CONF_FILE}" |
|---|
| 101 | echo "${ENV_COMMAND} ${1}${ASSIGN}${2}" >> "${HOME}/.${CONF_FILE}" |
|---|
| 102 | echo -e "# Finished adapting your ${1} environment variable for use with MacPorts.\n" >> "${HOME}/.${CONF_FILE}" |
|---|
| 103 | chown ${USER} "${HOME}/.${CONF_FILE}" || echo "Warning: unable to adapt permissions on your ${HOME}/.${CONF_FILE} shell configuration file!" |
|---|
| 104 | echo "An appropriate ${1} variable has been added to your shell environment by the MacPorts installer." |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | function cleanup_man () { |
|---|
| 108 | # Remove old non-compressed man pages |
|---|
| 109 | echo -e "\nRemoving old man pages..." |
|---|
| 110 | MAN1="port.1" |
|---|
| 111 | MAN5="macports.conf.5" |
|---|
| 112 | MAN7="portfile.7 portstyle.7 porthier.7 portgroup.7" |
|---|
| 113 | for m in ${MAN1}; do rm -vf ${MANPAGES}/man1/$m ; done |
|---|
| 114 | for m in ${MAN5}; do rm -vf ${MANPAGES}/man5/$m ; done |
|---|
| 115 | for m in ${MAN7}; do rm -vf ${MANPAGES}/man7/$m ; done |
|---|
| 116 | echo -e "Done.\n" |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | # Delete the old registry1.0 directory |
|---|
| 120 | function delete_reg1 { |
|---|
| 121 | if [[ -d ${REG1DIR} ]]; then |
|---|
| 122 | rm -vrf ${REG1DIR} |
|---|
| 123 | fi |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | # link macports1.0 into tcl package dir if possible |
|---|
| 127 | function link_tcl_package { |
|---|
| 128 | # delete old directory if present |
|---|
| 129 | if [[ ! -L "${TCL_PACKAGE_DIR}/macports1.0" ]]; then |
|---|
| 130 | if [[ ! -e "${TCL_PACKAGE_DIR}" ]]; then |
|---|
| 131 | /usr/bin/install -d "${TCL_PACKAGE_DIR}" || true |
|---|
| 132 | fi |
|---|
| 133 | if [[ -d "${TCL_PACKAGE_DIR}/macports1.0" ]]; then |
|---|
| 134 | rm -vrf "${TCL_PACKAGE_DIR}/macports1.0" || true |
|---|
| 135 | fi |
|---|
| 136 | if [[ ! -e "${TCL_PACKAGE_DIR}/macports1.0" && -d "${TCL_PACKAGE_DIR}" ]]; then |
|---|
| 137 | ln -vs "${MACPORTS_TCL_DIR}/macports1.0" "${TCL_PACKAGE_DIR}/macports1.0" || true |
|---|
| 138 | fi |
|---|
| 139 | fi |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | # create the user to use for privilege dropping |
|---|
| 143 | function create_run_user { |
|---|
| 144 | DSEDITGROUP=/usr/sbin/dseditgroup |
|---|
| 145 | if ! ${DSCL} -q . -read /Groups/${RUNUSR} > /dev/null 2>&1 ; then |
|---|
| 146 | echo "Creating group \"${RUNUSR}\"" |
|---|
| 147 | ${DSEDITGROUP} -q -o create ${RUNUSR} |
|---|
| 148 | fi |
|---|
| 149 | if ! ${DSCL} -q . -list /Users/${RUNUSR} > /dev/null 2>&1 ; then |
|---|
| 150 | echo "Creating user \"${RUNUSR}\"" |
|---|
| 151 | NEXTUID=501 |
|---|
| 152 | while [[ -n "`${DSCL} -q /Search -search /Users UniqueID $NEXTUID`" ]]; do |
|---|
| 153 | let "NEXTUID=NEXTUID+1" |
|---|
| 154 | done |
|---|
| 155 | ${DSCL} -q . -create /Users/${RUNUSR} UniqueID $NEXTUID |
|---|
| 156 | |
|---|
| 157 | # These are implicitly added on Mac OSX Lion. AuthenticationAuthority |
|---|
| 158 | # causes the user to be visible in the Users & Groups Preference Pane, |
|---|
| 159 | # and the others are just noise, so delete them. |
|---|
| 160 | # https://trac.macports.org/ticket/30168 |
|---|
| 161 | ${DSCL} -q . -delete /Users/${RUNUSR} AuthenticationAuthority |
|---|
| 162 | ${DSCL} -q . -delete /Users/${RUNUSR} PasswordPolicyOptions |
|---|
| 163 | ${DSCL} -q . -delete /Users/${RUNUSR} dsAttrTypeNative:KerberosKeys |
|---|
| 164 | ${DSCL} -q . -delete /Users/${RUNUSR} dsAttrTypeNative:ShadowHashData |
|---|
| 165 | |
|---|
| 166 | ${DSCL} -q . -create /Users/${RUNUSR} RealName MacPorts |
|---|
| 167 | ${DSCL} -q . -create /Users/${RUNUSR} Password \* |
|---|
| 168 | ${DSCL} -q . -create /Users/${RUNUSR} PrimaryGroupID $(${DSCL} -q . -read /Groups/${RUNUSR} PrimaryGroupID | /usr/bin/awk '{print $2}') |
|---|
| 169 | ${DSCL} -q . -create /Users/${RUNUSR} NFSHomeDirectory ${PREFIX}/var/macports/home |
|---|
| 170 | ${DSCL} -q . -create /Users/${RUNUSR} UserShell /usr/bin/false |
|---|
| 171 | fi |
|---|
| 172 | if [[ $(sw_vers -productVersion | /usr/bin/awk -F . '{print $2}') -eq 4 ]]; then |
|---|
| 173 | GID=$(${DSCL} -q . -read /Groups/${RUNUSR} PrimaryGroupID | /usr/bin/awk '{print $2}') |
|---|
| 174 | if [[ "$(${DSCL} -q . -read /Users/${RUNUSR} PrimaryGroupID 2>/dev/null | /usr/bin/awk '{print $2}')" != "$GID" ]]; then |
|---|
| 175 | echo "Fixing PrimaryGroupID for user \"${RUNUSR}\"" |
|---|
| 176 | ${DSCL} -q . -create /Users/${RUNUSR} PrimaryGroupID $GID |
|---|
| 177 | ${DSCL} -q . -create /Users/${RUNUSR} RealName MacPorts |
|---|
| 178 | fi |
|---|
| 179 | fi |
|---|
| 180 | if [[ "$(${DSCL} -q . -read /Users/${RUNUSR} NFSHomeDirectory)" = "NFSHomeDirectory: /var/empty" ]]; then |
|---|
| 181 | echo "Updating home directory location for user \"${RUNUSR}\"" |
|---|
| 182 | ${DSCL} -q . -create /Users/${RUNUSR} NFSHomeDirectory ${PREFIX}/var/macports/home |
|---|
| 183 | fi |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | echo "The MacPorts Project, postflight script version ${VERSION}: checking the shell environment for user \"${USER}\"." |
|---|
| 187 | |
|---|
| 188 | # create macports user |
|---|
| 189 | create_run_user |
|---|
| 190 | # Set up config files |
|---|
| 191 | setup_configs |
|---|
| 192 | # link macports1.0 pkg |
|---|
| 193 | link_tcl_package |
|---|
| 194 | |
|---|
| 195 | # Remove old stuff |
|---|
| 196 | cleanup_man |
|---|
| 197 | delete_reg1 |
|---|
| 198 | |
|---|
| 199 | # Determine the user's shell, in order to choose an appropriate configuration file we'll be tweaking. |
|---|
| 200 | # Exit nicely if the shell is any other than bash or tcsh, as that's considered non-standard. |
|---|
| 201 | USHELL=$(${DSCL} . -read /Users/${USER} shell | awk -F'/' '{print $NF}') || { |
|---|
| 202 | echo "An attempt to determine your shell name failed! Please set your MacPorts compatible environment manually." |
|---|
| 203 | update_macports |
|---|
| 204 | exit 1 |
|---|
| 205 | } |
|---|
| 206 | case "${USHELL}" in |
|---|
| 207 | tcsh) |
|---|
| 208 | echo "Detected the tcsh shell." |
|---|
| 209 | LOGIN_FLAG="" |
|---|
| 210 | ENV_COMMAND="setenv" |
|---|
| 211 | ASSIGN=" " |
|---|
| 212 | if [[ -f "${HOME}/.tcshrc" ]]; then |
|---|
| 213 | CONF_FILE=tcshrc |
|---|
| 214 | elif [[ -f "${HOME}/.cshrc" ]]; then |
|---|
| 215 | CONF_FILE=cshrc |
|---|
| 216 | else |
|---|
| 217 | CONF_FILE=tcshrc |
|---|
| 218 | fi |
|---|
| 219 | ;; |
|---|
| 220 | bash) |
|---|
| 221 | echo "Detected the bash shell." |
|---|
| 222 | LOGIN_FLAG="-l" |
|---|
| 223 | ENV_COMMAND="export" |
|---|
| 224 | ASSIGN="=" |
|---|
| 225 | if [[ -f "${HOME}/.bash_profile" ]]; then |
|---|
| 226 | CONF_FILE=bash_profile |
|---|
| 227 | elif [[ -f "${HOME}/.bash_login" ]]; then |
|---|
| 228 | CONF_FILE=bash_login |
|---|
| 229 | else |
|---|
| 230 | CONF_FILE=profile |
|---|
| 231 | fi |
|---|
| 232 | ;; |
|---|
| 233 | *) |
|---|
| 234 | echo "Unknown shell ($USHELL)! Please set your MacPorts compatible environment manually." |
|---|
| 235 | update_macports |
|---|
| 236 | exit 0 |
|---|
| 237 | ;; |
|---|
| 238 | esac |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | # Adding our setting to the PATH variable if not already there: |
|---|
| 242 | if ${SHELL} ${LOGIN_FLAG} -c "/usr/bin/printenv PATH" | grep ${PREFIX} > /dev/null; then |
|---|
| 243 | echo "Your shell already has the right PATH environment variable for use with MacPorts!" |
|---|
| 244 | else |
|---|
| 245 | write_setting PATH "${BINPATH}:${SBINPATH}:\$PATH" |
|---|
| 246 | fi |
|---|
| 247 | |
|---|
| 248 | # We gather the path into a variable of our own for faster operation: |
|---|
| 249 | ORIGINAL_MANPATH="$(${SHELL} ${LOGIN_FLAG} -c "/usr/bin/printenv MANPATH")" |
|---|
| 250 | # Adding our setting to the MANPATH variable only if it exists: |
|---|
| 251 | if ! ${SHELL} ${LOGIN_FLAG} -c "/usr/bin/env | grep MANPATH" > /dev/null || \ |
|---|
| 252 | # and following that, if it's not empty: |
|---|
| 253 | [[ -z "${ORIGINAL_MANPATH}" ]] || \ |
|---|
| 254 | # or if it doesn't already contain our path: |
|---|
| 255 | echo "${ORIGINAL_MANPATH}" | grep ${MANPAGES} > /dev/null || \ |
|---|
| 256 | # or if there's no empty component somewhere in the middle of it: |
|---|
| 257 | echo "${ORIGINAL_MANPATH}" | grep :: > /dev/null || \ |
|---|
| 258 | # or at the start of it: |
|---|
| 259 | [[ -z "$(echo "${ORIGINAL_MANPATH}" | awk -F : '{print $1}')" ]] || \ |
|---|
| 260 | # or at the end of it: |
|---|
| 261 | [[ -z "$(echo "${ORIGINAL_MANPATH}" | awk -F : '{print $NF}')" ]]; then |
|---|
| 262 | echo "Your shell already has the right MANPATH environment variable for use with MacPorts!" |
|---|
| 263 | else |
|---|
| 264 | write_setting MANPATH "${MANPAGES}:\$MANPATH" |
|---|
| 265 | fi |
|---|
| 266 | |
|---|
| 267 | # Adding a DISPLAY variable only if we're running on Tiger or less and if it doesn't already exist: |
|---|
| 268 | if (($(sw_vers -productVersion | awk -F . '{print $2}') >= 5)) || ${SHELL} ${LOGIN_FLAG} -c "/usr/bin/env | grep DISPLAY" > /dev/null; then |
|---|
| 269 | echo "Your shell already has the right DISPLAY environment variable for use with MacPorts!" |
|---|
| 270 | else |
|---|
| 271 | write_setting DISPLAY ":0" |
|---|
| 272 | fi |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | # Postflight script is done with its job, update MacPorts and exit gracefully! |
|---|
| 276 | update_macports |
|---|
| 277 | echo "You have successfully installed the MacPorts system. Launch a terminal and try it out!" |
|---|
| 278 | echo "Read the port(1) manual page and http://guide.macports.org/ for help." |
|---|
| 279 | echo "See http://www.macports.org/contact.php if you need to get in touch with The MacPorts Project." |
|---|
| 280 | exit 0 |
|---|