-
-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathpostflight.in
More file actions
executable file
·342 lines (318 loc) · 14.1 KB
/
postflight.in
File metadata and controls
executable file
·342 lines (318 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/bin/bash
# -*- 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
#
# Copyright (c) 2008-2012 The MacPorts Project
# Copyright (c) 2002-2007 Juan Manuel Palacios <jmpp@macports.org>, The MacPorts Project.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of The MacPorts Project nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# postflight
#set -x
# Script identification:
VERSION=2.7
# Abstraction variables:
PREFIX="@prefix@"
BINPATH=${PREFIX}/bin
SBINPATH=${PREFIX}/sbin
CONFIGPATH=${PREFIX}/etc/macports
MANPAGES=${PREFIX}/share/man
DSCL=/usr/bin/dscl
OS_MAJOR=$(uname -r | cut -d . -f 1)
RUNUSR=macports
TCLSH="@TCLSH@"
TCL_PACKAGE_PATH="@TCL_PACKAGE_PATH@"
TIMESTAMP=$(date +"%Y-%m-%d_at_%H:%M:%S")
BACKUP_SUFFIX=macports-saved_${TIMESTAMP}
OUR_BASESTRING="MacPorts Installer addition"
OUR_STRING="${OUR_BASESTRING} on ${TIMESTAMP}"
OLD_MACPORTS_TCL_DIR=${PREFIX}/share/macports/Tcl
OLD_TCL_PACKAGE_DIR=/Library/Tcl
OLD_TCL_FILES="tcl8/8.4/http-2.7.12.tm tcl8/8.4/http-2.7.13.tm \
tcl8/8.6/http-2.9.5.tm \
tcl8/8.4/platform-1.0.12.tm tcl8/8.4/platform-1.0.13.tm \
tcl8/8.4/platform-1.0.14.tm tcl8/8.4/platform-1.0.18.tm \
tcl8/8.5/msgcat-1.5.2.tm \
tcl8/8.5/tcltest-2.3.5.tm tcl8/8.5/tcltest-2.3.8.tm \
tcl8/8.5/tcltest-2.5.3.tm \
tcl8/8.6/tdbc/sqlite3-1.1.3.tm \
libtcl8.5.dylib libtclstub8.5.a"
OLD_VENDOR_DIRS="thread2.7.0 thread2.7.2 thread2.7.3 thread2.8.7 \
tcllib1.15 tcllib1.17 tcllib1.18 tclx8.4 tcl8.5 itcl4.2.2 \
sqlite3.36.0 tdbc1.1.3 tdbcmysql1.1.3 tdbcodbc1.1.3 tdbcpostgres1.1.3"
# Create config files from defaults if not present
function setup_configs {
for f in archive_sites.conf macports.conf pubkeys.conf sources.conf variants.conf ; do
if [[ ! -f ${CONFIGPATH}/${f} ]]; then
echo "Copying ${f}.default to ${f}"
/bin/cp "${CONFIGPATH}/${f}.default" "${CONFIGPATH}/${f}"
/bin/chmod 644 "${CONFIGPATH}/${f}"
fi
done
}
# Command to update the MacPorts installation through "selfupdate":
function update_macports {
SCRIPT_DIR=$(dirname "$0")
# Add [default] tag to the central MacPorts repository, if it isn't already
echo "Adding [default] tag to sources.conf if needed..."
"${TCLSH}" "${SCRIPT_DIR}/upgrade_sources_conf_default.tcl" "${PREFIX}"
# Convert image directories (and direct mode installs) to image archives
echo "Updating port image format..."
"${TCLSH}" "${SCRIPT_DIR}/images_to_archives.tcl"
"${TCLSH}" "${SCRIPT_DIR}/dedup_portfiles.tcl"
"${TCLSH}" "${SCRIPT_DIR}/cxx_stdlib_overridden.tcl"
echo "Synchronizing the MacPorts installation with the project's rsync server..."
if ! "${BINPATH}/port" -v selfupdate; then
echo "An attempt to synchronize your recent MacPorts installation with the project's rsync server failed!"
echo "Please run 'sudo port -d selfupdate' manually to find out the cause of the error."
else
echo "Successful!"
fi
}
# Through this command we write an environment variable to an appropriate shell configuration file,
# backing up the original only if it exists and if it doesn't contain the ${OUR_STRING} identification string,
# which hints that we've already tweaked it and therefore already backed it up.
function write_setting () {
if [[ -f "${HOME}/.${CONF_FILE}" ]] && ! grep "${OUR_BASESTRING}" "${HOME}/.${CONF_FILE}" > /dev/null; then
echo "Backing up your ${HOME}/.${CONF_FILE} shell configuration file as ${HOME}/.${CONF_FILE}.${BACKUP_SUFFIX} before adapting it for MacPorts."
/bin/cp -fp "${HOME}/.${CONF_FILE}" "${HOME}/.${CONF_FILE}.${BACKUP_SUFFIX}" || {
echo "An attempt to backup your original configuration file failed! Please set your MacPorts compatible environment manually."
update_macports
exit 1
}
echo -e "\n##\n# Your previous ${HOME}/.${CONF_FILE} file was backed up as ${HOME}/.${CONF_FILE}.${BACKUP_SUFFIX}\n##" >> "${HOME}/.${CONF_FILE}"
fi
{
echo -e "\n# ${OUR_STRING}: adding an appropriate ${1} variable for use with MacPorts."
echo -e "# Finished adapting your ${1} environment variable for use with MacPorts.\n"
echo "${ENV_COMMAND}${ENV_SEP}${1}${ASSIGN}${2}"
} >> "${HOME}/.${CONF_FILE}"
chown "${USER}" "${HOME}/.${CONF_FILE}" || echo "Warning: unable to adapt permissions on your ${HOME}/.${CONF_FILE} shell configuration file!"
echo "An appropriate ${1} variable has been added to your shell environment by the MacPorts installer."
}
function cleanup_man () {
# Remove old non-compressed man pages
echo -e "\nRemoving old man pages..."
MAN1="port.1"
MAN5="macports.conf.5"
MAN7="portfile.7 portstyle.7 porthier.7 portgroup.7"
for m in ${MAN1}; do rm -vf "${MANPAGES}/man1/$m" ; done
for m in ${MAN5}; do rm -vf "${MANPAGES}/man5/$m" ; done
for m in ${MAN7}; do rm -vf "${MANPAGES}/man7/$m" ; done
echo -e "Done.\n"
}
# delete macports1.0 link from old tcl package dir
function delete_old_tcl_package_link {
# delete old link if present
if [[ -e "${OLD_TCL_PACKAGE_DIR}/macports1.0" ]]; then
rm -vrf "${OLD_TCL_PACKAGE_DIR}/macports1.0"
fi
# delete old macports1.0 link from new tcl package dir if present
if [[ -L "${TCL_PACKAGE_PATH}/macports1.0" ]]; then
rm -vrf "${TCL_PACKAGE_PATH}/macports1.0"
fi
}
# delete the old tcl package location in share/macports/Tcl
function delete_old_tcl_packages {
if [[ -d "${OLD_MACPORTS_TCL_DIR}" ]]; then
rm -vrf "${OLD_MACPORTS_TCL_DIR}"
fi
for f in ${OLD_TCL_FILES}; do
if [[ -f "${TCL_PACKAGE_PATH}/${f}" ]]; then
rm -vf "${TCL_PACKAGE_PATH}/${f}"
fi
done
for d in ${OLD_VENDOR_DIRS}; do
if [[ -d "${TCL_PACKAGE_PATH}/${d}" ]]; then
rm -vrf "${TCL_PACKAGE_PATH}/${d}"
fi
done
oldtclsh="$(dirname "${TCLSH}")/tclsh8.5"
if [[ -f "${oldtclsh}" ]]; then
rm -vf "${oldtclsh}"
fi
}
# create the user to use for privilege dropping
function create_run_user {
DSEDITGROUP=/usr/sbin/dseditgroup
if ! ${DSCL} -q . -read "/Groups/${RUNUSR}" > /dev/null 2>&1 ; then
echo "Creating group \"${RUNUSR}\""
${DSEDITGROUP} -q -o create "${RUNUSR}"
fi
if ! ${DSCL} -q . -list "/Users/${RUNUSR}" > /dev/null 2>&1 ; then
echo "Creating user \"${RUNUSR}\""
NEXTUID=501
while [[ -n "$(${DSCL} -q /Search -search /Users UniqueID $NEXTUID)" ]]; do
let "NEXTUID=NEXTUID+1"
done
${DSCL} -q . -create "/Users/${RUNUSR}" UniqueID $NEXTUID
# These are implicitly added on Mac OS X Lion. AuthenticationAuthority
# causes the user to be visible in the Users & Groups Preference Pane,
# and the others are just noise, so delete them.
# https://trac.macports.org/ticket/30168
${DSCL} -q . -delete "/Users/${RUNUSR}" AuthenticationAuthority
${DSCL} -q . -delete "/Users/${RUNUSR}" PasswordPolicyOptions
${DSCL} -q . -delete "/Users/${RUNUSR}" dsAttrTypeNative:KerberosKeys
${DSCL} -q . -delete "/Users/${RUNUSR}" dsAttrTypeNative:ShadowHashData
${DSCL} -q . -create "/Users/${RUNUSR}" RealName MacPorts
${DSCL} -q . -create "/Users/${RUNUSR}" Password "*"
${DSCL} -q . -create "/Users/${RUNUSR}" PrimaryGroupID "$(${DSCL} -q . -read "/Groups/${RUNUSR}" PrimaryGroupID | /usr/bin/awk '{print $2}')"
${DSCL} -q . -create "/Users/${RUNUSR}" NFSHomeDirectory "${PREFIX}/var/macports/home"
${DSCL} -q . -create "/Users/${RUNUSR}" UserShell /usr/bin/false
fi
if [[ "${OS_MAJOR}" -eq 8 ]]; then
GID=$(${DSCL} -q . -read "/Groups/${RUNUSR}" PrimaryGroupID | /usr/bin/awk '{print $2}')
if [[ "$(${DSCL} -q . -read "/Users/${RUNUSR}" PrimaryGroupID 2>/dev/null | /usr/bin/awk '{print $2}')" != "$GID" ]]; then
echo "Fixing PrimaryGroupID for user \"${RUNUSR}\""
${DSCL} -q . -create "/Users/${RUNUSR}" PrimaryGroupID "$GID"
${DSCL} -q . -create "/Users/${RUNUSR}" RealName MacPorts
fi
fi
if [[ "$(${DSCL} -q . -read "/Users/${RUNUSR}" NFSHomeDirectory)" = "NFSHomeDirectory: /var/empty" ]]; then
echo "Updating home directory location for user \"${RUNUSR}\""
${DSCL} -q . -create "/Users/${RUNUSR}" NFSHomeDirectory "${PREFIX}/var/macports/home"
fi
}
echo "The MacPorts Project, postflight script version ${VERSION}: checking the shell environment for user \"${USER}\"."
# create macports user
create_run_user
# Set up config files
setup_configs
# Remove old stuff
cleanup_man
delete_old_tcl_package_link
delete_old_tcl_packages
# Determine the user's shell, in order to choose an appropriate configuration file we'll be tweaking.
# Exit nicely if the shell is any other than bash or tcsh, as that's considered non-standard.
USHELL=$(${DSCL} . -read "/Users/${USER}" shell) || {
echo "An attempt to determine your shell name failed! Please set your MacPorts compatible environment manually."
update_macports
exit 1
}
# leave full path to shell
USHELL=${USHELL#*shell: }
case "${USHELL}" in
*/tcsh)
echo "Detected the tcsh shell."
ENV_COMMAND="setenv"
ENV_SEP=" "
ASSIGN=" "
if [[ -f "${HOME}/.tcshrc" ]]; then
CONF_FILE=tcshrc
elif [[ -f "${HOME}/.cshrc" ]]; then
CONF_FILE=cshrc
else
CONF_FILE=tcshrc
fi
;;
*/bash)
echo "Detected the bash shell."
ENV_COMMAND="export"
ENV_SEP=" "
ASSIGN="="
if [[ -f "${HOME}/.bash_profile" ]]; then
CONF_FILE=bash_profile
elif [[ -f "${HOME}/.bash_login" ]]; then
CONF_FILE=bash_login
else
CONF_FILE=profile
fi
;;
*/zsh)
echo "Detected the zsh shell."
ENV_COMMAND="export"
ENV_SEP=" "
ASSIGN="="
CONF_FILE="zprofile"
;;
*/xonsh)
echo "Detected the 🐚xonsh shell."
ENV_COMMAND='$'
ENV_SEP=""
ASSIGN="="
IS_XONSH=true
if [[ -f "${HOME}/.config/xonsh/rc.xsh" ]]; then
CONF_FILE="config/xonsh/rc.xsh"
else
CONF_FILE=xonshrc
fi
;;
*)
echo "Unknown shell ($USHELL)! Please set your MacPorts compatible environment manually."
update_macports
exit 0
;;
esac
# Adding our setting to the PATH variable if not already there:
# Run as the $USER: /usr/bin/su $USER -l
# Run a command in the shell: -c "/usr/bin/printenv PATH"
# Only process the last line output (profile may print info): tail -n 1
# Output each path on its own line: tr ":" "\n"
# Look for exactly the BINPATH: grep "^${BINPATH}$"
if /usr/bin/su "${USER}" -l -c "/usr/bin/printenv PATH" | tail -n 1 | tr ":" "\n" | grep "^${BINPATH}$" > /dev/null; then
echo "Your shell already has the right PATH environment variable for use with MacPorts!"
else
if $IS_XONSH; then
# ↓"${ENV_COMMAND}${ENV_SEP}${1}${ASSIGN}${2}"
# "$PATH.add('${PATH_TO_ADD}' ,front=True)"
backupASSIGN=${ASSIGN}
export ASSIGN=".add('"
write_setting PATH "${SBINPATH}',front=True)"
write_setting PATH "${BINPATH}' ,front=True)"
export ASSIGN=${backupASSIGN}
else
write_setting PATH "\"${BINPATH}:${SBINPATH}:\$PATH\""
fi
fi
# Adding our setting to the MANPATH variable only if it exists:
if /usr/bin/su "${USER}" -l -c "/usr/bin/printenv MANPATH" > /dev/null; then
# check for MANPAGES already in MANPATH
if /usr/bin/su "${USER}" -l -c "/usr/bin/printenv MANPATH" | tail -n 1 | tr ":" "\n" | grep "^${MANPAGES}$" >/dev/null; then
echo "Your shell already has the right MANPATH environment variable for use with MacPorts!"
else
if $IS_XONSH; then
# ↓"${ENV_COMMAND}${ENV_SEP}${1}${ASSIGN}${2}"
# "$PATH.add('${PATH_TO_ADD}' ,front=True)"
backupASSIGN=${ASSIGN}
export ASSIGN=".add('"
write_setting MANPATH "${MANPAGES}',front=True)"
export ASSIGN=${backupASSIGN}
else
write_setting MANPATH "\"${MANPAGES}:\$MANPATH\""
fi
fi
fi
# Adding a DISPLAY variable only if we're running on Tiger or less and if it doesn't already exist:
if [[ "${OS_MAJOR}" -ge 9 ]] || /usr/bin/su "${USER}" -l -c "/usr/bin/printenv DISPLAY" > /dev/null; then
echo "Your shell already has the right DISPLAY environment variable for use with MacPorts!"
else
write_setting DISPLAY ":0"
fi
# Postflight script is done with its job, update MacPorts and exit gracefully!
update_macports
echo "You have successfully installed the MacPorts system. Launch a terminal and try it out!"
echo "Read the port(1) manual page and https://guide.macports.org/ for help."
echo "See https://www.macports.org/contact.php if you need to get in touch with The MacPorts Project."
exit 0