Prevent MacPorts editing .bash_profile over and over again...

Jan Stary hans at stare.cz
Tue Mar 21 20:43:28 UTC 2017


Here is a diff to postflight and an accompanying diff to installing.xml
(what other places need to be touched if this goes through?)

	Jan


diff --git a/portmgr/dmg/postflight.in b/portmgr/dmg/postflight.in
index 750553f0..a3a8bd80 100755
--- a/portmgr/dmg/postflight.in
+++ b/portmgr/dmg/postflight.in
@@ -87,28 +87,6 @@ function update_macports {
     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 confguration 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 "${ENV_COMMAND} ${1}${ASSIGN}${2}"
-        echo -e "# Finished adapting your ${1} environment variable for use with MacPorts.\n"
-    } >> "${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..."
@@ -195,8 +173,6 @@ function create_run_user {
     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
@@ -207,78 +183,11 @@ 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."
-        LOGIN_FLAG=""
-        ENV_COMMAND="setenv"
-        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."
-        LOGIN_FLAG="-l"
-        ENV_COMMAND="export"
-        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
-        ;;
-    *)
-        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
-    write_setting PATH "\"${BINPATH}:${SBINPATH}:\$PATH\""
-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
-        write_setting MANPATH "\"${MANPAGES}:\$MANPATH\""
-    fi
-fi
-
-# Adding a DISPLAY variable only if we're running on Tiger or less and if it doesn't already exist:
-if (($(sw_vers -productVersion | awk -F . '{print $2}') >= 5)) || /usr/bin/su "${USER}" -l -c "/usr/bin/printenv DISPLAY" > /dev/null > /dev/null; then
-    echo "Your shell already has the right DISPLAY environment variable for use with MacPorts!"
-else
-    write_setting DISPLAY ":0"
-fi
+echo "Remember to set your environment:"
+echo "Prepend ${BINPATH} and ${SBINPATH} to PATH."
+echo "Prepend ${MANPAGES} to MANPATH if you use MANPATH."
+echo "Remember to set DISPLAY if you are on 10.4 or older."
+echo "See https://guide.macports.org/#installing.shell for details."
 
 # Postflight script is done with its job, update MacPorts and exit gracefully!
 update_macports




diff --git a/guide/xml/installing.xml b/guide/xml/installing.xml
index 31f8719..18adb66 100644
--- a/guide/xml/installing.xml
+++ b/guide/xml/installing.xml
@@ -412,34 +412,25 @@
     <section id="installing.shell">
         <title>MacPorts and the Shell</title>
 
-        <para>MacPorts requires that some environment variables be set in the shell. When MacPorts is installed using
-            the OS X package installer, a <quote>postflight</quote> script is run after installation that automatically
-            adds or modifies a shell configuration file in your home directory, ensuring that it defines variables
-            according to the rules described in the following section. Those <link
-                linkend="installing.macports.source">installing MacPorts from source code</link> must modify their
-            environment manually using the rules as a guide.</para>
-
-        <para>Depending on your shell and which configuration files already exist, the installer may use
-            <filename>.profile</filename>, <filename>.bash_login</filename>, <filename>.bash_profile</filename>,
-            <filename>.tcshrc</filename>, or <filename>.cshrc</filename>.</para>
+        <para>MacPorts requires that some environment variables be set in the shell.
+            Depending on your shell, this means you need to edit some of the following config files:
+            <filename>.profile</filename>, <filename>.bash_login</filename> or <filename>.bash_profile</filename> for bash,
+	    <filename>.tcshrc</filename> or <filename>.cshrc</filename> for tcsh.</para>
 
         <section id="installing.shell.postflight">
-            <title>The Postflight Script</title>
+            <title>Configure Your Environment</title>
 
-            <para>The postflight script automatically sets the <varname>PATH</varname> variable, and optionally the
-                <varname>MANPATH</varname> and <varname>DISPLAY</varname> variables according to the rules described
-                below. If a current shell configuration file exists at installation time it is renamed to
-                <quote>mpsaved_$timestamp</quote>. Those <link linkend="installing.macports.source">installing MacPorts
-                    from source code</link> must modify their environment manually using the rules as a guide.</para>
+	    <para>The <varname>PATH</varname> environment variable,
+		    and optionally <varname>MANPATH</varname> and <varname>DISPLAY</varname> variables need to be edited
+		    according to the rules described below.</para>
 
             <itemizedlist>
                 <listitem>
                     <para>Required: <varname>PATH</varname> variable</para>
 
-                    <para>This variable is set by the postflight script to append the MacPorts executable paths to the
-                        default path as shown. The MacPorts paths are appended at the front of <varname>PATH</varname>
-                        so the MacPorts libraries will take precedence over vendor-supplied libraries for ported
-                        software at runtime.</para>
+		    <para>This variable should have the MacPorts executable paths prepended to the default path as shown.
+			    The MacPorts paths are at the front of <varname>PATH</varname> so that the MacPorts binaries
+			    will take precedence over vendor-supplied libraries.</para>
 
                     <programlisting>export PATH=/opt/local/bin:/opt/local/sbin:$PATH</programlisting>
 
@@ -456,30 +447,18 @@
                 <listitem>
                     <para>Optional: <varname>MANPATH</varname> variable</para>
 
-                    <para>Condition: If prior to MacPorts installation a <varname>MANPATH</varname> variable exists in
-                        a current <filename>.profile</filename> that contains neither the value
-                        <filename>${prefix}/share/man,</filename> nor any empty values, the postflight script sets the
-                        <varname>MANPATH</varname> variable as shown below. Otherwise, the <varname>MANPATH</varname>
-                        variable is omitted.</para>
+		    <para>If your shell configuration sets a <varname>MANPATH</varname> variable,
+			it should be edited to contain <filename>${prefix}/share/man</filename>.</para>
 
                     <programlisting>export MANPATH=/opt/local/share/man:$MANPATH</programlisting>
-
-                    <para>Here are some examples of paths that contain empty values:</para>
-
-                    <simplelist>
-                        <member>/usr/share/man:</member>
-                        <member>:/usr/share/man</member>
-                        <member>/usr/share/man::/usr/X11R6/man</member>
-                    </simplelist>
                 </listitem>
 
                 <listitem>
                     <para>Optional: <varname>DISPLAY</varname> variable</para>
 
-                    <para>Condition: If installing on a Mac OS X version earlier than 10.5 (Leopard), and if a shell
-                        configuration file exists at time of MacPorts installation without a <varname>DISPLAY</varname>
-                        variable, the postflight script sets a <varname>DISPLAY</varname> variable as shown below. The
-                        <varname>DISPLAY</varname> variable is always omitted on Mac OS X 10.5 or higher.</para>
+		    <para>If installing on a Mac OS X version earlier than 10.5 (Leopard),
+			    a <varname>DISPLAY</varname> variable should be set as shown below.
+			    On Mac OS X 10.5 or higher, the <varname>DISPLAY</varname> variable is always omitted.</para>
 
                     <programlisting>export DISPLAY=:0.0</programlisting>
                 </listitem>
@@ -487,7 +466,7 @@
         </section>
 
         <section id="installing.shell.verifyprofile">
-            <title>Verify the Configuration File</title>
+            <title>Verify the Configuration</title>
 
             <para>To verify that the file containing the MacPorts variables is in effect, type <command>env</command> in
                 the terminal to verify the current environment settings after the file has been created. Example output


More information about the macports-users mailing list