mergemaster.sh revision 207612
152400Sbillf#!/bin/sh
252400Sbillf
352400Sbillf# mergemaster
452400Sbillf
552400Sbillf# Compare files created by /usr/src/etc/Makefile (or the directory
652400Sbillf# the user specifies) with the currently installed copies.
752400Sbillf
8201765Sdougb# Copyright 1998-2010 Douglas Barton
973651Sdougb# DougB@FreeBSD.org
1052400Sbillf
1152495Sbillf# $FreeBSD: head/usr.sbin/mergemaster/mergemaster.sh 207612 2010-05-04 11:25:04Z nork $
1252400Sbillf
1368507SdougbPATH=/bin:/usr/bin:/usr/sbin
1452400Sbillf
1552400Sbillfdisplay_usage () {
1652533Sbillf  VERSION_NUMBER=`grep "[$]FreeBSD:" $0 | cut -d ' ' -f 4`
1752400Sbillf  echo "mergemaster version ${VERSION_NUMBER}"
18205145Sdougb  echo 'Usage: mergemaster [-scrvhpCP] [-a|[-iFU]]'
19189763Sdougb  echo '    [-m /path] [-t /path] [-d] [-u N] [-w N] [-A arch] [-D /path]'
2052400Sbillf  echo "Options:"
2152400Sbillf  echo "  -s  Strict comparison (diff every pair of files)"
2252400Sbillf  echo "  -c  Use context diff instead of unified diff"
2352400Sbillf  echo "  -r  Re-run on a previously cleaned directory (skip temproot creation)"
2452400Sbillf  echo "  -v  Be more verbose about the process, include additional checks"
2552400Sbillf  echo "  -a  Leave all files that differ to merge by hand"
2652400Sbillf  echo "  -h  Display more complete help"
2767949Sdougb  echo '  -i  Automatically install files that do not exist in destination directory'
2891193Sdougb  echo '  -p  Pre-buildworld mode, only compares crucial files'
29190320Sdougb  echo '  -F  Install files that differ only by revision control Id ($FreeBSD)'
3091193Sdougb  echo '  -C  Compare local rc.conf variables to the defaults'
31114501Sdougb  echo '  -P  Preserve files that are overwritten'
32189763Sdougb  echo "  -U  Attempt to auto upgrade files that have not been user modified"
33189763Sdougb  echo ''
3452400Sbillf  echo "  -m /path/directory  Specify location of source to do the make in"
3552400Sbillf  echo "  -t /path/directory  Specify temp root directory"
3652400Sbillf  echo "  -d  Add date and time to directory name (e.g., /var/tmp/temproot.`date +%m%d.%H.%M`)"
3752400Sbillf  echo "  -u N  Specify a numeric umask"
3852400Sbillf  echo "  -w N  Specify a screen width in columns to sdiff"
39155309Srwatson  echo "  -A architecture  Alternative architecture name to pass to make"
4067949Sdougb  echo '  -D /path/directory  Specify the destination directory to install files to'
4152400Sbillf  echo ''
4252400Sbillf}
4352400Sbillf
4452400Sbillfdisplay_help () {
4552400Sbillf  echo "* To specify a directory other than /var/tmp/temproot for the"
4652400Sbillf  echo "  temporary root environment, use -t /path/to/temp/root"
4752400Sbillf  echo "* The -w option takes a number as an argument for the column width"
4867859Sdougb  echo "  of the screen.  The default is 80."
4967949Sdougb  echo '* The -a option causes mergemaster to run without prompting.'
5052400Sbillf}
5152400Sbillf
5258910Salfred# Loop allowing the user to use sdiff to merge files and display the merged
5358910Salfred# file.
5458910Salfredmerge_loop () {
5567850Sdougb  case "${VERBOSE}" in
5667850Sdougb  '') ;;
5767850Sdougb  *)
5867850Sdougb      echo "   *** Type h at the sdiff prompt (%) to get usage help"
5967850Sdougb      ;;
6067850Sdougb  esac
6167850Sdougb  echo ''
6267850Sdougb  MERGE_AGAIN=yes
6367850Sdougb  while [ "${MERGE_AGAIN}" = "yes" ]; do
6467850Sdougb    # Prime file.merged so we don't blat the owner/group id's
6567850Sdougb    cp -p "${COMPFILE}" "${COMPFILE}.merged"
6667850Sdougb    sdiff -o "${COMPFILE}.merged" --text --suppress-common-lines \
6767949Sdougb      --width=${SCREEN_WIDTH:-80} "${DESTDIR}${COMPFILE#.}" "${COMPFILE}"
6867850Sdougb    INSTALL_MERGED=V
6967850Sdougb    while [ "${INSTALL_MERGED}" = "v" -o "${INSTALL_MERGED}" = "V" ]; do
7067850Sdougb      echo ''
7167850Sdougb      echo "  Use 'i' to install merged file"
7267850Sdougb      echo "  Use 'r' to re-do the merge"
7367850Sdougb      echo "  Use 'v' to view the merged file"
7467850Sdougb      echo "  Default is to leave the temporary file to deal with by hand"
7567850Sdougb      echo ''
7667859Sdougb      echo -n "    *** How should I deal with the merged file? [Leave it for later] "
7767859Sdougb      read INSTALL_MERGED
7858910Salfred
7967850Sdougb      case "${INSTALL_MERGED}" in
8067850Sdougb      [iI])
8167850Sdougb        mv "${COMPFILE}.merged" "${COMPFILE}"
8267850Sdougb        echo ''
8367850Sdougb        if mm_install "${COMPFILE}"; then
8467850Sdougb          echo "     *** Merged version of ${COMPFILE} installed successfully"
8567859Sdougb        else
8667850Sdougb          echo "     *** Problem installing ${COMPFILE}, it will remain to merge by hand later"
8767859Sdougb        fi
8867850Sdougb        unset MERGE_AGAIN
8967850Sdougb        ;;
9067850Sdougb      [rR])
9167850Sdougb        rm "${COMPFILE}.merged"
9267859Sdougb        ;;
9367850Sdougb      [vV])
9467850Sdougb        ${PAGER} "${COMPFILE}.merged"
9567850Sdougb        ;;
9667850Sdougb      '')
9767850Sdougb        echo "   *** ${COMPFILE} will remain for your consideration"
9867850Sdougb        unset MERGE_AGAIN
9967850Sdougb        ;;
10067850Sdougb      *)
10167850Sdougb        echo "invalid choice: ${INSTALL_MERGED}"
10267850Sdougb        INSTALL_MERGED=V
10367850Sdougb        ;;
10467850Sdougb      esac
10567850Sdougb    done
10667850Sdougb  done
10758910Salfred}
10858910Salfred
10958910Salfred# Loop showing user differences between files, allow merge, skip or install
11058910Salfred# options
11158910Salfreddiff_loop () {
11258910Salfred
11367850Sdougb  HANDLE_COMPFILE=v
11458910Salfred
11577323Sdougb  while [ "${HANDLE_COMPFILE}" = "v" -o "${HANDLE_COMPFILE}" = "V" -o \
11677323Sdougb    "${HANDLE_COMPFILE}" = "NOT V" ]; do
11767949Sdougb    if [ -f "${DESTDIR}${COMPFILE#.}" -a -f "${COMPFILE}" ]; then
118192230Sdougb      if [ -n "${AUTO_UPGRADE}" -a -n "${CHANGED}" ]; then
119192230Sdougb        case "${CHANGED}" in
120192230Sdougb        *:${DESTDIR}${COMPFILE#.}:*) ;;		# File has been modified
121192230Sdougb        *)
122158149Sgordon          echo ''
123158149Sgordon          echo "  *** ${COMPFILE} has not been user modified."
124158149Sgordon          echo ''
125158149Sgordon
126158149Sgordon          if mm_install "${COMPFILE}"; then
127158149Sgordon            echo "   *** ${COMPFILE} upgraded successfully"
128158149Sgordon            echo ''
129158149Sgordon            # Make the list print one file per line
130158149Sgordon            AUTO_UPGRADED_FILES="${AUTO_UPGRADED_FILES}      ${DESTDIR}${COMPFILE#.}
131158149Sgordon"
132158149Sgordon          else
133192230Sdougb            echo "   *** Problem upgrading ${COMPFILE}, it will remain to merge by hand"
134158149Sgordon          fi
135158149Sgordon          return
136192230Sdougb          ;;
137192230Sdougb        esac
138158149Sgordon      fi
13967850Sdougb      if [ "${HANDLE_COMPFILE}" = "v" -o "${HANDLE_COMPFILE}" = "V" ]; then
14090564Sdougb	echo ''
141109993Sdillon	echo '   ======================================================================   '
142109993Sdillon	echo ''
143109993Sdillon        (
144109993Sdillon          echo "  *** Displaying differences between ${COMPFILE} and installed version:"
145109993Sdillon          echo ''
146110377Sdougb          diff ${DIFF_FLAG} ${DIFF_OPTIONS} "${DESTDIR}${COMPFILE#.}" "${COMPFILE}"
147109993Sdillon        ) | ${PAGER}
148109993Sdillon        echo ''
14967850Sdougb      fi
15067850Sdougb    else
151109993Sdillon      echo ''
15267850Sdougb      echo "  *** There is no installed version of ${COMPFILE}"
15391193Sdougb      echo ''
15467949Sdougb      case "${AUTO_INSTALL}" in
15567949Sdougb      [Yy][Ee][Ss])
15667949Sdougb        echo ''
15767949Sdougb        if mm_install "${COMPFILE}"; then
15867949Sdougb          echo "   *** ${COMPFILE} installed successfully"
15968507Sdougb          echo ''
16067949Sdougb          # Make the list print one file per line
16167949Sdougb          AUTO_INSTALLED_FILES="${AUTO_INSTALLED_FILES}      ${DESTDIR}${COMPFILE#.}
16267949Sdougb"
16367949Sdougb        else
16467949Sdougb          echo "   *** Problem installing ${COMPFILE}, it will remain to merge by hand"
16567949Sdougb        fi
16667949Sdougb        return
16767949Sdougb        ;;
16867949Sdougb      *)
16967949Sdougb        NO_INSTALLED=yes
17067949Sdougb        ;;
17167949Sdougb      esac
17267850Sdougb    fi
17367859Sdougb
17467850Sdougb    echo "  Use 'd' to delete the temporary ${COMPFILE}"
17567850Sdougb    echo "  Use 'i' to install the temporary ${COMPFILE}"
17667850Sdougb    case "${NO_INSTALLED}" in
17767850Sdougb    '')
17877326Sdougb      echo "  Use 'm' to merge the temporary and installed versions"
179109993Sdillon      echo "  Use 'v' to view the diff results again"
18067850Sdougb      ;;
18167850Sdougb    esac
18267850Sdougb    echo ''
18367850Sdougb    echo "  Default is to leave the temporary file to deal with by hand"
18467850Sdougb    echo ''
18567859Sdougb    echo -n "How should I deal with this? [Leave it for later] "
18667859Sdougb    read HANDLE_COMPFILE
18767859Sdougb
18867850Sdougb    case "${HANDLE_COMPFILE}" in
18967850Sdougb    [dD])
19067850Sdougb      rm "${COMPFILE}"
19167850Sdougb      echo ''
19267850Sdougb      echo "   *** Deleting ${COMPFILE}"
19367850Sdougb      ;;
19467850Sdougb    [iI])
19567850Sdougb      echo ''
19667850Sdougb      if mm_install "${COMPFILE}"; then
19767850Sdougb        echo "   *** ${COMPFILE} installed successfully"
19867850Sdougb      else
19967850Sdougb        echo "   *** Problem installing ${COMPFILE}, it will remain to merge by hand"
20067850Sdougb      fi
20167850Sdougb      ;;
20267850Sdougb    [mM])
20367850Sdougb      case "${NO_INSTALLED}" in
20467850Sdougb      '')
20567850Sdougb        # interact with user to merge files
20667850Sdougb        merge_loop
20767850Sdougb        ;;
20867850Sdougb      *)
20967850Sdougb        echo ''
21067850Sdougb        echo "   *** There is no installed version of ${COMPFILE}"
21167850Sdougb        echo ''
21267850Sdougb        HANDLE_COMPFILE="NOT V"
21367850Sdougb        ;;
21467850Sdougb      esac # End of "No installed version of file but user selected merge" test
21567850Sdougb      ;;
21667850Sdougb    [vV])
21767850Sdougb      continue
21867850Sdougb      ;;
21967850Sdougb    '')
22067850Sdougb      echo ''
22167850Sdougb      echo "   *** ${COMPFILE} will remain for your consideration"
22267850Sdougb      ;;
22367850Sdougb    *)
22467850Sdougb      # invalid choice, show menu again.
22567850Sdougb      echo "invalid choice: ${HANDLE_COMPFILE}"
22667850Sdougb      echo ''
22767850Sdougb      HANDLE_COMPFILE="NOT V"
22867850Sdougb      continue
22967850Sdougb      ;;
23067850Sdougb    esac  # End of "How to handle files that are different"
23167859Sdougb  done
23267850Sdougb  unset NO_INSTALLED
23367850Sdougb  echo ''
23467850Sdougb  case "${VERBOSE}" in
23567850Sdougb  '') ;;
23667850Sdougb  *)
23767850Sdougb    sleep 3
23867850Sdougb    ;;
23967850Sdougb  esac
24058910Salfred}
24158910Salfred
24297960Sdougbpress_to_continue () {
24397960Sdougb  local DISCARD
24497960Sdougb  echo -n ' *** Press the [Enter] or [Return] key to continue '
24597960Sdougb  read DISCARD
24697960Sdougb}
24797960Sdougb
24852400Sbillf# Set the default path for the temporary root environment
24952400Sbillf#
25052400SbillfTEMPROOT='/var/tmp/temproot'
25152400Sbillf
25273651Sdougb# Read /etc/mergemaster.rc first so the one in $HOME can override
25373651Sdougb#
25473651Sdougbif [ -r /etc/mergemaster.rc ]; then
25573651Sdougb  . /etc/mergemaster.rc
25673651Sdougbfi
25773651Sdougb
25852400Sbillf# Read .mergemasterrc before command line so CLI can override
25952400Sbillf#
26067949Sdougbif [ -r "$HOME/.mergemasterrc" ]; then
26152400Sbillf  . "$HOME/.mergemasterrc"
26252400Sbillffi
26352400Sbillf
26452400Sbillf# Check the command line options
26552400Sbillf#
266189992Sdougbwhile getopts ":ascrvhipCPm:t:du:w:D:A:FU" COMMAND_LINE_ARGUMENT ; do
26752400Sbillf  case "${COMMAND_LINE_ARGUMENT}" in
268155309Srwatson  A)
269186678Sdougb    ARCHSTRING='TARGET_ARCH='${OPTARG}
270155309Srwatson    ;;
271189992Sdougb  F)
272189992Sdougb    FREEBSD_ID=yes
273189992Sdougb    ;;
274158149Sgordon  U)
275158149Sgordon    AUTO_UPGRADE=yes
276158149Sgordon    ;;
27752400Sbillf  s)
27852400Sbillf    STRICT=yes
279110377Sdougb    unset DIFF_OPTIONS
28052400Sbillf    ;;
28152400Sbillf  c)
28252400Sbillf    DIFF_FLAG='-c'
28352400Sbillf    ;;
28452400Sbillf  r)
28552400Sbillf    RERUN=yes
28652400Sbillf    ;;
28752400Sbillf  v)
28852400Sbillf    case "${AUTO_RUN}" in
28952400Sbillf    '') VERBOSE=yes ;;
29052400Sbillf    esac
29152400Sbillf    ;;
29252400Sbillf  a)
29352400Sbillf    AUTO_RUN=yes
29452400Sbillf    unset VERBOSE
29552400Sbillf    ;;
29652400Sbillf  h)
29752400Sbillf    display_usage
29852400Sbillf    display_help
29952400Sbillf    exit 0
30052400Sbillf    ;;
30167949Sdougb  i)
30267949Sdougb    AUTO_INSTALL=yes
30367949Sdougb    ;;
30496045Sdougb  C)
30596045Sdougb    COMP_CONFS=yes
30696045Sdougb    ;;
307114501Sdougb  P)
308114501Sdougb    PRESERVE_FILES=yes
309114501Sdougb    ;;
31091193Sdougb  p)
31191193Sdougb    PRE_WORLD=yes
31296045Sdougb    unset COMP_CONFS
31396045Sdougb    unset AUTO_RUN
31491193Sdougb    ;;
31552400Sbillf  m)
31652400Sbillf    SOURCEDIR=${OPTARG}
31752400Sbillf    ;;
31852400Sbillf  t)
31952400Sbillf    TEMPROOT=${OPTARG}
32052400Sbillf    ;;
32152400Sbillf  d)
32252400Sbillf    TEMPROOT=${TEMPROOT}.`date +%m%d.%H.%M`
32352400Sbillf    ;;
32452400Sbillf  u)
32552400Sbillf    NEW_UMASK=${OPTARG}
32652400Sbillf    ;;
32752400Sbillf  w)
32852400Sbillf    SCREEN_WIDTH=${OPTARG}
32952400Sbillf    ;;
33067949Sdougb  D)
33167949Sdougb    DESTDIR=${OPTARG}
33267949Sdougb    ;;
33352400Sbillf  *)
33452400Sbillf    display_usage
33552400Sbillf    exit 1
33652400Sbillf    ;;
33752400Sbillf  esac
33852400Sbillfdone
33952400Sbillf
340205145Sdougbif [ -n "$AUTO_RUN" ]; then
341205145Sdougb  if [ -n "$FREEBSD_ID" -o -n "$AUTO_UPGRADE" -o -n "$AUTO_INSTALL" ]; then
342205145Sdougb    echo ''
343205145Sdougb    echo "*** You have included the -a option along with one or more options"
344205145Sdougb    echo '    that indicate that you wish mergemaster to actually make updates'
345205145Sdougb    echo '    (-F, -U, or -i), however these options are not compatible.'
346205145Sdougb    echo '    Please read mergemaster(8) for more information.'
347205145Sdougb    echo ''
348205145Sdougb    exit 1
349205145Sdougb  fi
350205145Sdougbfi
351205145Sdougb
352202817Sdougb# Assign the location of the mtree database
353202817Sdougb#
354202817SdougbMTREEDB=${MTREEDB:-${DESTDIR}/var/db}
355202817SdougbMTREEFILE="${MTREEDB}/mergemaster.mtree"
356202817Sdougb
357114501Sdougb# Don't force the user to set this in the mergemaster rc file
358114501Sdougbif [ -n "${PRESERVE_FILES}" -a -z "${PRESERVE_FILES_DIR}" ]; then
359114501Sdougb  PRESERVE_FILES_DIR=/var/tmp/mergemaster/preserved-files-`date +%y%m%d-%H%M%S`
360200425Sdougb  mkdir -p ${PRESERVE_FILES_DIR}
361114501Sdougbfi
362114501Sdougb
363186678Sdougb# Check for the mtree database in DESTDIR
364186678Sdougbcase "${AUTO_UPGRADE}" in
365186678Sdougb'') ;;	# If the option is not set no need to run the test or warn the user
366186678Sdougb*)
367200416Sdougb  if [ ! -s "${MTREEFILE}" ]; then
368186678Sdougb    echo ''
369200416Sdougb    echo "*** Unable to find mtree database (${MTREEFILE})."
370200416Sdougb    echo "    Skipping auto-upgrade on this run."
371193853Sdougb    echo "    It will be created for the next run when this one is complete."
372186678Sdougb    echo ''
373201291Sdougb    case "${AUTO_RUN}" in
374201291Sdougb    '')
375201291Sdougb      press_to_continue
376201291Sdougb      ;;
377201291Sdougb    esac
378186678Sdougb    unset AUTO_UPGRADE
379186678Sdougb  fi
380186678Sdougb  ;;
381186678Sdougbesac
382186678Sdougb
383186689Sdougbif [ -e "${DESTDIR}/etc/fstab" ]; then
384186689Sdougb  if grep -q nodev ${DESTDIR}/etc/fstab; then
385186689Sdougb    echo ''
386186689Sdougb    echo "*** You have the deprecated 'nodev' option in ${DESTDIR}/etc/fstab."
387186689Sdougb    echo "    This can prevent the filesystem from being mounted on reboot."
388186689Sdougb    echo "    Please update your fstab before continuing."
389186689Sdougb    echo "    See fstab(5) for more information."
390186689Sdougb    echo ''
391186689Sdougb    exit 1
392186689Sdougb  fi
393158149Sgordonfi
394158149Sgordon
39552400Sbillfecho ''
39652400Sbillf
39752400Sbillf# If the user has a pager defined, make sure we can run it
39852400Sbillf#
39952400Sbillfcase "${DONT_CHECK_PAGER}" in
40052400Sbillf'')
401186695Sdougbcheck_pager () {
402186695Sdougb  while ! type "${PAGER%% *}" >/dev/null; do
40352400Sbillf    echo " *** Your PAGER environment variable specifies '${PAGER}', but"
40464467Sbrian    echo "     due to the limited PATH that I use for security reasons,"
40567859Sdougb    echo "     I cannot execute it.  So, what would you like to do?"
40652400Sbillf    echo ''
40752400Sbillf    echo "  Use 'e' to exit mergemaster and fix your PAGER variable"
40864467Sbrian    if [ -x /usr/bin/less -o -x /usr/local/bin/less ]; then
40964467Sbrian    echo "  Use 'l' to set PAGER to 'less' for this run"
41052400Sbillf    fi
41152400Sbillf    echo "  Use 'm' to use plain old 'more' as your PAGER for this run"
41252400Sbillf    echo ''
41352400Sbillf    echo "  Default is to use plain old 'more' "
41452400Sbillf    echo ''
41567859Sdougb    echo -n "What should I do? [Use 'more'] "
41667859Sdougb    read FIXPAGER
41767859Sdougb
41852400Sbillf    case "${FIXPAGER}" in
41958910Salfred    [eE])
42052400Sbillf       exit 0
42152400Sbillf       ;;
42258910Salfred    [lL])
42364467Sbrian       if [ -x /usr/bin/less ]; then
42464467Sbrian         PAGER=/usr/bin/less
42564467Sbrian       elif [ -x /usr/local/bin/less ]; then
42658910Salfred         PAGER=/usr/local/bin/less
42764467Sbrian       else
42864467Sbrian         echo ''
42964467Sbrian         echo " *** Fatal Error:"
43064467Sbrian         echo "     You asked to use 'less' as your pager, but I can't"
43164467Sbrian         echo "     find it in /usr/bin or /usr/local/bin"
43264467Sbrian         exit 1
43358910Salfred       fi
43452400Sbillf       ;;
43560420Sbsd    [mM]|'')
43652400Sbillf       PAGER=more
43752400Sbillf       ;;
43858910Salfred    *)
43958910Salfred       echo ''
44058910Salfred       echo "invalid choice: ${FIXPAGER}"
44152400Sbillf    esac
44252400Sbillf    echo ''
44358910Salfred  done
444186695Sdougb}
445186695Sdougb  if [ -n "${PAGER}" ]; then
446186695Sdougb    check_pager
447186695Sdougb  fi
44852400Sbillf  ;;
44952400Sbillfesac
45052400Sbillf
45152400Sbillf# If user has a pager defined, or got assigned one above, use it.
45252400Sbillf# If not, use more.
45352400Sbillf#
45452400SbillfPAGER=${PAGER:-more}
45552400Sbillf
45652400Sbillfif [ -n "${VERBOSE}" -a ! "${PAGER}" = "more" ]; then
45752400Sbillf  echo " *** You have ${PAGER} defined as your pager so we will use that"
45852400Sbillf  echo ''
45952400Sbillf  sleep 3
46052400Sbillffi
46152400Sbillf
46252400Sbillf# Assign the diff flag once so we will not have to keep testing it
46352400Sbillf#
46452400SbillfDIFF_FLAG=${DIFF_FLAG:--u}
46552400Sbillf
46652400Sbillf# Assign the source directory
46752400Sbillf#
468186678SdougbSOURCEDIR=${SOURCEDIR:-/usr/src}
469186678Sdougbif [ ! -f ${SOURCEDIR}/Makefile.inc1 -a \
470186678Sdougb   -f ${SOURCEDIR}/../Makefile.inc1 ]; then
471186678Sdougb  echo " *** The source directory you specified (${SOURCEDIR})"
472186678Sdougb  echo "     will be reset to ${SOURCEDIR}/.."
473186678Sdougb  echo ''
474186678Sdougb  sleep 3
475186678Sdougb  SOURCEDIR=${SOURCEDIR}/..
476186678Sdougbfi
47752400Sbillf
478186678Sdougb# Setup make to use system files from SOURCEDIR
479186695SdougbMM_MAKE="make ${ARCHSTRING} -m ${SOURCEDIR}/share/mk"
480186678Sdougb
481158149Sgordon# Check DESTDIR against the mergemaster mtree database to see what
482158149Sgordon# files the user changed from the reference files.
483158149Sgordon#
484200416Sdougbif [ -n "${AUTO_UPGRADE}" -a -s "${MTREEFILE}" ]; then
485192230Sdougb	CHANGED=:
486200416Sdougb	for file in `mtree -eqL -f ${MTREEFILE} -p ${DESTDIR}/ \
487158149Sgordon		2>/dev/null | awk '($2 == "changed") {print $1}'`; do
488158149Sgordon		if [ -f "${DESTDIR}/$file" ]; then
489192230Sdougb			CHANGED="${CHANGED}${DESTDIR}/${file}:"
490158149Sgordon		fi
491158149Sgordon	done
492192230Sdougb	[ "$CHANGED" = ':' ] && unset CHANGED
493158149Sgordonfi
494158149Sgordon
49596045Sdougb# Check the width of the user's terminal
49696045Sdougb#
49796045Sdougbif [ -t 0 ]; then
498110377Sdougb  w=`tput columns`
49996045Sdougb  case "${w}" in
50096045Sdougb  0|'') ;; # No-op, since the input is not valid
50196045Sdougb  *)
50296045Sdougb    case "${SCREEN_WIDTH}" in
50396045Sdougb    '') SCREEN_WIDTH="${w}" ;;
50496045Sdougb    "${w}") ;; # No-op, since they are the same
50596045Sdougb    *)
50696045Sdougb      echo -n "*** You entered ${SCREEN_WIDTH} as your screen width, but stty "
50796045Sdougb      echo "thinks it is ${w}."
50896045Sdougb      echo ''
50996045Sdougb      echo -n "What would you like to use? [${w}] "
51096045Sdougb      read SCREEN_WIDTH
51197380Sdougb      case "${SCREEN_WIDTH}" in
51297380Sdougb      '') SCREEN_WIDTH="${w}" ;;
51397380Sdougb      esac
51496045Sdougb      ;;
51596045Sdougb    esac
51696045Sdougb  esac
51796045Sdougbfi
51896045Sdougb
51973651Sdougb# Define what CVS $Id tag to look for to aid portability.
52073651Sdougb#
52173651SdougbCVS_ID_TAG=FreeBSD
52273651Sdougb
52399152Sdougbdelete_temproot () {
524101362Sdougb  rm -rf "${TEMPROOT}" 2>/dev/null
525101362Sdougb  chflags -R 0 "${TEMPROOT}" 2>/dev/null
526201765Sdougb  rm -rf "${TEMPROOT}" || { echo "*** Unable to delete ${TEMPROOT}";  exit 1; }
52799152Sdougb}
52899152Sdougb
52952400Sbillfcase "${RERUN}" in
53052400Sbillf'')
53152400Sbillf  # Set up the loop to test for the existence of the
53252400Sbillf  # temp root directory.
53352400Sbillf  #
53452400Sbillf  TEST_TEMP_ROOT=yes
53552400Sbillf  while [ "${TEST_TEMP_ROOT}" = "yes" ]; do
53652400Sbillf    if [ -d "${TEMPROOT}" ]; then
53752400Sbillf      echo "*** The directory specified for the temporary root environment,"
53867859Sdougb      echo "    ${TEMPROOT}, exists.  This can be a security risk if untrusted"
53952400Sbillf      echo "    users have access to the system."
54052400Sbillf      echo ''
54152400Sbillf      case "${AUTO_RUN}" in
54252400Sbillf      '')
54352400Sbillf        echo "  Use 'd' to delete the old ${TEMPROOT} and continue"
54452400Sbillf        echo "  Use 't' to select a new temporary root directory"
54552400Sbillf        echo "  Use 'e' to exit mergemaster"
54652400Sbillf        echo ''
54752400Sbillf        echo "  Default is to use ${TEMPROOT} as is"
54852400Sbillf        echo ''
54967859Sdougb        echo -n "How should I deal with this? [Use the existing ${TEMPROOT}] "
55067859Sdougb        read DELORNOT
55167859Sdougb
55267859Sdougb        case "${DELORNOT}" in
55367859Sdougb        [dD])
55467859Sdougb          echo ''
55567859Sdougb          echo "   *** Deleting the old ${TEMPROOT}"
55667859Sdougb          echo ''
557201765Sdougb          delete_temproot
55867859Sdougb          unset TEST_TEMP_ROOT
55952400Sbillf          ;;
56067859Sdougb        [tT])
56167859Sdougb          echo "   *** Enter new directory name for temporary root environment"
56267859Sdougb          read TEMPROOT
56367859Sdougb          ;;
56467859Sdougb        [eE])
56567859Sdougb          exit 0
56667859Sdougb          ;;
56767859Sdougb        '')
56867859Sdougb          echo ''
56967859Sdougb          echo "   *** Leaving ${TEMPROOT} intact"
57067859Sdougb          echo ''
57167859Sdougb          unset TEST_TEMP_ROOT
57267859Sdougb          ;;
57367859Sdougb        *)
57467859Sdougb          echo ''
57567859Sdougb          echo "invalid choice: ${DELORNOT}"
57667859Sdougb          echo ''
57767859Sdougb          ;;
57867859Sdougb        esac
57967859Sdougb        ;;
58052400Sbillf      *)
58177323Sdougb        # If this is an auto-run, try a hopefully safe alternative then
58277323Sdougb        # re-test anyway.
58352400Sbillf        TEMPROOT=/var/tmp/temproot.`date +%m%d.%H.%M.%S`
58452400Sbillf        ;;
58552400Sbillf      esac
58652400Sbillf    else
58752400Sbillf      unset TEST_TEMP_ROOT
58852400Sbillf    fi
58952400Sbillf  done
59052400Sbillf
59152400Sbillf  echo "*** Creating the temporary root environment in ${TEMPROOT}"
59252400Sbillf
59352400Sbillf  if mkdir -p "${TEMPROOT}"; then
59452400Sbillf    echo " *** ${TEMPROOT} ready for use"
59552400Sbillf  fi
59652400Sbillf
59752400Sbillf  if [ ! -d "${TEMPROOT}" ]; then
59852400Sbillf    echo ''
59952400Sbillf    echo "  *** FATAL ERROR: Cannot create ${TEMPROOT}"
60052400Sbillf    echo ''
60152400Sbillf    exit 1
60252400Sbillf  fi
60352400Sbillf
60452400Sbillf  echo " *** Creating and populating directory structure in ${TEMPROOT}"
60552400Sbillf  echo ''
60652400Sbillf
60752400Sbillf  case "${VERBOSE}" in
60852400Sbillf  '') ;;
60952400Sbillf  *)
61097960Sdougb    press_to_continue
61152400Sbillf    ;;
61252400Sbillf  esac
61352400Sbillf
61491193Sdougb  case "${PRE_WORLD}" in
61591193Sdougb  '')
61691193Sdougb    { cd ${SOURCEDIR} &&
61791193Sdougb      case "${DESTDIR}" in
61891193Sdougb      '') ;;
61991193Sdougb      *)
620186695Sdougb        ${MM_MAKE} DESTDIR=${DESTDIR} distrib-dirs
62191193Sdougb        ;;
62291193Sdougb      esac
623186749Sdougb      od=${TEMPROOT}/usr/obj
624186695Sdougb      ${MM_MAKE} DESTDIR=${TEMPROOT} distrib-dirs &&
625186749Sdougb      MAKEOBJDIRPREFIX=$od ${MM_MAKE} _obj SUBDIR_OVERRIDE=etc &&
626186749Sdougb      MAKEOBJDIRPREFIX=$od ${MM_MAKE} everything SUBDIR_OVERRIDE=etc &&
627186749Sdougb      MAKEOBJDIRPREFIX=$od ${MM_MAKE} DESTDIR=${TEMPROOT} distribution;} ||
62891193Sdougb    { echo '';
62991193Sdougb     echo "  *** FATAL ERROR: Cannot 'cd' to ${SOURCEDIR} and install files to";
63091193Sdougb      echo "      the temproot environment";
63191193Sdougb      echo '';
63291193Sdougb      exit 1;}
63391193Sdougb    ;;
63491193Sdougb  *)
63591193Sdougb    # Only set up files that are crucial to {build|install}world
63691193Sdougb    { mkdir -p ${TEMPROOT}/etc &&
637186678Sdougb      cp -p ${SOURCEDIR}/etc/master.passwd ${TEMPROOT}/etc &&
638186678Sdougb      cp -p ${SOURCEDIR}/etc/group ${TEMPROOT}/etc;} ||
63991193Sdougb    { echo '';
64091193Sdougb      echo '  *** FATAL ERROR: Cannot copy files to the temproot environment';
64191193Sdougb      echo '';
64291193Sdougb      exit 1;}
64391193Sdougb    ;;
64491193Sdougb  esac
64552400Sbillf
64677323Sdougb  # Doing the inventory and removing files that we don't want to compare only
64777323Sdougb  # makes sense if we are not doing a rerun, since we have no way of knowing
64877323Sdougb  # what happened to the files during previous incarnations.
64967949Sdougb  case "${VERBOSE}" in
65067949Sdougb  '') ;;
65167949Sdougb  *)
65267949Sdougb    echo ''
65367949Sdougb    echo ' *** The following files exist only in the installed version of'
65467949Sdougb    echo "     ${DESTDIR}/etc.  In the vast majority of cases these files"
65567949Sdougb    echo '     are necessary parts of the system and should not be deleted.'
65667949Sdougb    echo '     However because these files are not updated by this process you'
65767949Sdougb    echo '     might want to verify their status before rebooting your system.'
65867949Sdougb    echo ''
65997960Sdougb    press_to_continue
660101348Sdougb    diff -qr ${DESTDIR}/etc ${TEMPROOT}/etc | grep "^Only in ${DESTDIR}/etc" | ${PAGER}
66167949Sdougb    echo ''
66297960Sdougb    press_to_continue
66367949Sdougb    ;;
66467949Sdougb  esac
66567949Sdougb
66652534Sbillf  case "${IGNORE_MOTD}" in
667202340Sdougb  '') ;;
668202339Sdougb  *)
669186678Sdougb     echo ''
670186678Sdougb     echo "*** You have the IGNORE_MOTD option set in your mergemaster rc file."
671186678Sdougb     echo "    This option is deprecated in favor of the IGNORE_FILES option."
672186678Sdougb     echo "    Please update your rc file accordingly."
673186678Sdougb     echo ''
674202339Sdougb     exit 1
67552534Sbillf     ;;
67652534Sbillf  esac
67752534Sbillf
678186678Sdougb  # Avoid comparing the following user specified files
679186678Sdougb  for file in ${IGNORE_FILES}; do
680186688Sdougb    test -e ${TEMPROOT}/${file} && unlink ${TEMPROOT}/${file}
681186678Sdougb  done
68252400Sbillf
683201291Sdougb  # We really don't want to have to deal with files like login.conf.db, pwd.db,
684201291Sdougb  # or spwd.db.  Instead, we want to compare the text versions, and run *_mkdb.
685201291Sdougb  # Prompt the user to do so below, as needed.
686201291Sdougb  #
687201291Sdougb  rm -f ${TEMPROOT}/etc/*.db ${TEMPROOT}/etc/passwd
68877478Sdougb
689201291Sdougb  # We only need to compare things like freebsd.cf once
690201291Sdougb  find ${TEMPROOT}/usr/obj -type f -delete 2>/dev/null
69194196Sdougb
692201291Sdougb  # Delete stuff we do not need to keep the mtree database small,
693201291Sdougb  # and to make the actual comparison faster.
694201291Sdougb  find ${TEMPROOT}/usr -type l -delete 2>/dev/null
695201291Sdougb  find ${TEMPROOT} -type f -size 0 -delete 2>/dev/null
696201291Sdougb  find -d ${TEMPROOT} -type d -empty -delete 2>/dev/null
697158149Sgordon
698201291Sdougb  # Build the mtree database in a temporary location.
699201291Sdougb  case "${PRE_WORLD}" in
700201323Sdougb  '') MTREENEW=`mktemp -t mergemaster.mtree`
701201323Sdougb      mtree -ci -p ${TEMPROOT} -k size,md5digest > ${MTREENEW} 2>/dev/null
702201291Sdougb      ;;
703201291Sdougb  *) # We don't want to mess with the mtree database on a pre-world run or
704201291Sdougb     # when re-scanning a previously-built tree.
705201291Sdougb     ;;
706201291Sdougb  esac
707201291Sdougb  ;; # End of the "RERUN" test
708158149Sgordonesac
709158149Sgordon
71052400Sbillf# Get ready to start comparing files
71152400Sbillf
71298084Sdougb# Check umask if not specified on the command line,
71398084Sdougb# and we are not doing an autorun
71452400Sbillf#
71598084Sdougbif [ -z "${NEW_UMASK}" -a -z "${AUTO_RUN}" ]; then
71698084Sdougb  USER_UMASK=`umask`
71752400Sbillf  case "${USER_UMASK}" in
71877335Sdougb  0022|022) ;;
71952400Sbillf  *)
72052400Sbillf    echo ''
72198084Sdougb    echo " *** Your umask is currently set to ${USER_UMASK}.  By default, this script"
72298084Sdougb    echo "     installs all files with the same user, group and modes that"
723186678Sdougb    echo "     they are created with by ${SOURCEDIR}/etc/Makefile, compared to"
72498084Sdougb    echo "     a umask of 022.  This umask allows world read permission when"
72598084Sdougb    echo "     the file's default permissions have it."
72652400Sbillf    echo ''
72798084Sdougb    echo "     No world permissions can sometimes cause problems.  A umask of"
72898084Sdougb    echo "     022 will restore the default behavior, but is not mandatory."
72998084Sdougb    echo "     /etc/master.passwd is a special case.  Its file permissions"
73098084Sdougb    echo "     will be 600 (rw-------) if installed."
73197960Sdougb    echo ''
73298084Sdougb    echo -n "What umask should I use? [${USER_UMASK}] "
73398084Sdougb    read NEW_UMASK
73498084Sdougb
73598084Sdougb    NEW_UMASK="${NEW_UMASK:-$USER_UMASK}"
73652400Sbillf    ;;
73752400Sbillf  esac
73852400Sbillf  echo ''
73998084Sdougbfi
74052400Sbillf
74198084SdougbCONFIRMED_UMASK=${NEW_UMASK:-0022}
74298084Sdougb
74352400Sbillf#
744114501Sdougb# Warn users who still have old rc files
745114501Sdougb#
746179315Sbzfor file in atm devfs diskless1 diskless2 network network6 pccard \
747114523Sdougb  serial syscons sysctl alpha amd64 i386 ia64 sparc64; do
748114501Sdougb  if [ -f "${DESTDIR}/etc/rc.${file}" ]; then
749114501Sdougb    OLD_RC_PRESENT=1
750114501Sdougb    break
751114501Sdougb  fi
752114501Sdougbdone
753114501Sdougb
754114501Sdougbcase "${OLD_RC_PRESENT}" in
755114501Sdougb1)
75652400Sbillf  echo ''
757114501Sdougb  echo " *** There are elements of the old rc system in ${DESTDIR}/etc/."
75867949Sdougb  echo ''
759114501Sdougb  echo '     While these scripts will not hurt anything, they are not'
760114501Sdougb  echo '     functional on an up to date system, and can be removed.'
76167949Sdougb  echo ''
762114501Sdougb
76352400Sbillf  case "${AUTO_RUN}" in
76452400Sbillf  '')
765114501Sdougb    echo -n 'Move these files to /var/tmp/mergemaster/old_rc? [yes] '
766114501Sdougb    read MOVE_OLD_RC
76767859Sdougb
768114501Sdougb    case "${MOVE_OLD_RC}" in
769114501Sdougb    [nN]*) ;;
77052400Sbillf    *)
771114501Sdougb      mkdir -p /var/tmp/mergemaster/old_rc
772179315Sbz        for file in atm devfs diskless1 diskless2 network network6 pccard \
773114523Sdougb          serial syscons sysctl alpha amd64 i386 ia64 sparc64; do
774114501Sdougb          if [ -f "${DESTDIR}/etc/rc.${file}" ]; then
775114501Sdougb            mv ${DESTDIR}/etc/rc.${file} /var/tmp/mergemaster/old_rc/
776114501Sdougb          fi
777114501Sdougb        done
778114501Sdougb      echo '  The files have been moved'
779114501Sdougb      press_to_continue
78052400Sbillf      ;;
78152400Sbillf    esac
78252400Sbillf    ;;
78352400Sbillf  *) ;;
78452400Sbillf  esac
785114501Sdougbesac
78652400Sbillf
78798084Sdougb# Use the umask/mode information to install the files
78852400Sbillf# Create directories as needed
78952400Sbillf#
790186678Sdougbinstall_error () {
791186678Sdougb  echo "*** FATAL ERROR: Unable to install ${1} to ${2}"
792186678Sdougb  echo ''
793186678Sdougb  exit 1
794186678Sdougb}
795186678Sdougb
79678490Sdougbdo_install_and_rm () {
797114501Sdougb  case "${PRESERVE_FILES}" in
798114501Sdougb  [Yy][Ee][Ss])
799114501Sdougb    if [ -f "${3}/${2##*/}" ]; then
800114565Sdougb      mkdir -p ${PRESERVE_FILES_DIR}/${2%/*}
801114565Sdougb      cp ${3}/${2##*/} ${PRESERVE_FILES_DIR}/${2%/*}
802114501Sdougb    fi
803114501Sdougb    ;;
804114501Sdougb  esac
805114501Sdougb
806186678Sdougb  if [ ! -d "${3}/${2##*/}" ]; then
807186678Sdougb    if install -m ${1} ${2} ${3}; then
808186678Sdougb      unlink ${2}
809186678Sdougb    else
810186678Sdougb      install_error ${2} ${3}
811186678Sdougb    fi
812186678Sdougb  else
813186678Sdougb    install_error ${2} ${3}
814186678Sdougb  fi
81578490Sdougb}
81678490Sdougb
81798084Sdougb# 4095 = "obase=10;ibase=8;07777" | bc
81898084Sdougbfind_mode () {
81998084Sdougb  local OCTAL
82098084Sdougb  OCTAL=$(( ~$(echo "obase=10; ibase=8; ${CONFIRMED_UMASK}" | bc) & 4095 &
821124053Sdougb    $(echo "obase=10; ibase=8; $(stat -f "%OMp%OLp" ${1})" | bc) ))
82298084Sdougb  printf "%04o\n" ${OCTAL}
82398084Sdougb}
82498084Sdougb
82552400Sbillfmm_install () {
82652400Sbillf  local INSTALL_DIR
82752400Sbillf  INSTALL_DIR=${1#.}
82852400Sbillf  INSTALL_DIR=${INSTALL_DIR%/*}
82967949Sdougb
83052400Sbillf  case "${INSTALL_DIR}" in
83152400Sbillf  '')
83252400Sbillf    INSTALL_DIR=/
83352400Sbillf    ;;
83452400Sbillf  esac
83552400Sbillf
83667949Sdougb  if [ -n "${DESTDIR}${INSTALL_DIR}" -a ! -d "${DESTDIR}${INSTALL_DIR}" ]; then
83798084Sdougb    DIR_MODE=`find_mode "${TEMPROOT}/${INSTALL_DIR}"`
838200425Sdougb    install -d -o root -g wheel -m "${DIR_MODE}" "${DESTDIR}${INSTALL_DIR}" ||
839200425Sdougb      install_error $1 ${DESTDIR}${INSTALL_DIR}
84052400Sbillf  fi
84152400Sbillf
84298084Sdougb  FILE_MODE=`find_mode "${1}"`
84352400Sbillf
84452400Sbillf  if [ ! -x "${1}" ]; then
84552400Sbillf    case "${1#.}" in
84664625Sgshapiro    /etc/mail/aliases)
84752400Sbillf      NEED_NEWALIASES=yes
84852400Sbillf      ;;
84952400Sbillf    /etc/login.conf)
85052400Sbillf      NEED_CAP_MKDB=yes
85152400Sbillf      ;;
852207612Snork    /etc/services)
853207612Snork      NEED_SERVICES_MKDB=yes
854207612Snork      ;;
85552400Sbillf    /etc/master.passwd)
85678490Sdougb      do_install_and_rm 600 "${1}" "${DESTDIR}${INSTALL_DIR}"
85752400Sbillf      NEED_PWD_MKDB=yes
85852400Sbillf      DONT_INSTALL=yes
85952400Sbillf      ;;
86052400Sbillf    /.cshrc | /.profile)
861200708Sdougb      local st_nlink
862200708Sdougb
863200708Sdougb      # install will unlink the file before it installs the new one,
864200708Sdougb      # so we have to restore/create the link afterwards.
865200708Sdougb      #
866200708Sdougb      st_nlink=0		# In case the file does not yet exist
867200708Sdougb      eval $(stat -s ${DESTDIR}${COMPFILE#.} 2>/dev/null)
868200708Sdougb
869200708Sdougb      do_install_and_rm "${FILE_MODE}" "${1}" "${DESTDIR}${INSTALL_DIR}"
870200708Sdougb
871200708Sdougb      if [ -n "${AUTO_INSTALL}" -a $st_nlink -gt 1 ]; then
872200708Sdougb        HANDLE_LINK=l
873200708Sdougb      else
874200701Sdougb        case "${LINK_EXPLAINED}" in
875200701Sdougb        '')
876200701Sdougb          echo "   *** Historically BSD derived systems have had a"
877200701Sdougb          echo "       hard link from /.cshrc and /.profile to"
878200701Sdougb          echo "       their namesakes in /root.  Please indicate"
879200701Sdougb          echo "       your preference below for bringing your"
880200701Sdougb          echo "       installed files up to date."
881200701Sdougb          echo ''
882200701Sdougb          LINK_EXPLAINED=yes
883200701Sdougb          ;;
884200701Sdougb        esac
885200701Sdougb
886200701Sdougb        echo "   Use 'd' to delete the temporary ${COMPFILE}"
887200708Sdougb        echo "   Use 'l' to delete the existing ${DESTDIR}/root/${COMPFILE##*/} and create the link"
88852400Sbillf        echo ''
889200701Sdougb        echo "   Default is to leave the temporary file to deal with by hand"
890200701Sdougb        echo ''
891200701Sdougb        echo -n "  How should I handle ${COMPFILE}? [Leave it to install later] "
892200701Sdougb        read HANDLE_LINK
893200708Sdougb      fi
89452400Sbillf
89552400Sbillf      case "${HANDLE_LINK}" in
89652400Sbillf      [dD]*)
89752400Sbillf        rm "${COMPFILE}"
89852400Sbillf        echo ''
89952400Sbillf        echo "   *** Deleting ${COMPFILE}"
90052400Sbillf        ;;
90152400Sbillf      [lL]*)
90252400Sbillf        echo ''
903200708Sdougb        unlink ${DESTDIR}/root/${COMPFILE##*/}
904200708Sdougb        if ln ${DESTDIR}${COMPFILE#.} ${DESTDIR}/root/${COMPFILE##*/}; then
90567949Sdougb          echo "   *** Link from ${DESTDIR}${COMPFILE#.} to ${DESTDIR}/root/${COMPFILE##*/} installed successfully"
90652400Sbillf        else
907200708Sdougb          echo "   *** Error linking ${DESTDIR}${COMPFILE#.} to ${DESTDIR}/root/${COMPFILE##*/}"
908200708Sdougb          echo "   *** ${COMPFILE} will remain for your consideration"
90952400Sbillf        fi
91052400Sbillf        ;;
91152400Sbillf      *)
91252400Sbillf        echo "   *** ${COMPFILE} will remain for your consideration"
91352400Sbillf        ;;
91452400Sbillf      esac
915200708Sdougb      return
91652400Sbillf      ;;
91752400Sbillf    esac
91852400Sbillf
91952400Sbillf    case "${DONT_INSTALL}" in
92052400Sbillf    '')
92178490Sdougb      do_install_and_rm "${FILE_MODE}" "${1}" "${DESTDIR}${INSTALL_DIR}"
92252400Sbillf      ;;
92352400Sbillf    *)
92452400Sbillf      unset DONT_INSTALL
92552400Sbillf      ;;
92652400Sbillf    esac
92778490Sdougb  else	# File matched -x
92878490Sdougb    do_install_and_rm "${FILE_MODE}" "${1}" "${DESTDIR}${INSTALL_DIR}"
92952400Sbillf  fi
93052400Sbillf  return $?
93152400Sbillf}
93252400Sbillf
933174841Sdougbif [ ! -d "${TEMPROOT}" ]; then
934174841Sdougb	echo "*** FATAL ERROR: The temproot directory (${TEMPROOT})"
935174841Sdougb	echo '                 has disappeared!'
936174841Sdougb	echo ''
937174841Sdougb	exit 1
938174841Sdougbfi
939174841Sdougb
94067949Sdougbecho ''
94167949Sdougbecho "*** Beginning comparison"
94267949Sdougbecho ''
94352400Sbillf
944124136Sdougb# Pre-world does not populate /etc/rc.d.
945124053Sdougb# It is very possible that a previous run would have deleted files in
946124053Sdougb# ${TEMPROOT}/etc/rc.d, thus creating a lot of false positives.
947124136Sdougbif [ -z "${PRE_WORLD}" -a -z "${RERUN}" ]; then
948124053Sdougb  echo "   *** Checking ${DESTDIR}/etc/rc.d for stale files"
949124053Sdougb  echo ''
950124053Sdougb  cd "${DESTDIR}/etc/rc.d" &&
951124053Sdougb  for file in *; do
952124053Sdougb    if [ ! -e "${TEMPROOT}/etc/rc.d/${file}" ]; then
953124053Sdougb      STALE_RC_FILES="${STALE_RC_FILES} ${file}"
954124053Sdougb    fi
955124053Sdougb  done
956124053Sdougb  case "${STALE_RC_FILES}" in
957126718Sdougb  ''|' *')
958124053Sdougb    echo '   *** No stale files found'
959124053Sdougb    ;;
960124053Sdougb  *)
961124053Sdougb    echo "   *** The following files exist in ${DESTDIR}/etc/rc.d but not in"
962124053Sdougb    echo "       ${TEMPROOT}/etc/rc.d/:"
963124053Sdougb    echo ''
964124053Sdougb    echo "${STALE_RC_FILES}"
965124053Sdougb    echo ''
966124053Sdougb    echo '       The presence of stale files in this directory can cause the'
967124053Sdougb    echo '       dreaded unpredictable results, and therefore it is highly'
968124053Sdougb    echo '       recommended that you delete them.'
969124053Sdougb    case "${AUTO_RUN}" in
970124053Sdougb    '')
971124053Sdougb      echo ''
972153604Sdougb      echo -n '   *** Delete them now? [n] '
973124053Sdougb      read DELETE_STALE_RC_FILES
974124053Sdougb      case "${DELETE_STALE_RC_FILES}" in
975153604Sdougb      [yY])
976124053Sdougb        echo '      *** Deleting ... '
977124053Sdougb        rm ${STALE_RC_FILES}
978124053Sdougb        echo '                       done.'
979124053Sdougb        ;;
980153604Sdougb      *)
981153604Sdougb        echo '      *** Files will not be deleted'
982153604Sdougb        ;;
983124053Sdougb      esac
984124053Sdougb      sleep 2
985124053Sdougb      ;;
986201291Sdougb    *)
987201291Sdougb      if [ -n "${DELETE_STALE_RC_FILES}" ]; then
988201291Sdougb        echo '      *** Deleting ... '
989201291Sdougb        rm ${STALE_RC_FILES}
990201291Sdougb        echo '                       done.'
991201291Sdougb      fi
992124053Sdougb    esac
993124053Sdougb    ;;
994124053Sdougb  esac
995124053Sdougb  echo ''
996124136Sdougbfi
997124053Sdougb
99867949Sdougbcd "${TEMPROOT}"
99967949Sdougb
100067949Sdougbif [ -r "${MM_PRE_COMPARE_SCRIPT}" ]; then
100167949Sdougb  . "${MM_PRE_COMPARE_SCRIPT}"
100267949Sdougbfi
100367949Sdougb
1004200425Sdougb# Things that were files/directories/links in one version can sometimes
1005200425Sdougb# change to something else in a newer version.  So we need to explicitly
1006200425Sdougb# test for this, and warn the user if what we find does not match.
1007200425Sdougb#
1008200700Sdougbfor COMPFILE in `find . | sort` ; do
1009200425Sdougb  if [ -e "${DESTDIR}${COMPFILE#.}" ]; then
1010200425Sdougb    INSTALLED_TYPE=`stat -f '%HT' ${DESTDIR}${COMPFILE#.}`
1011200425Sdougb  else
1012200425Sdougb    continue
1013200425Sdougb  fi
1014200425Sdougb  TEMPROOT_TYPE=`stat -f '%HT' $COMPFILE`
1015200425Sdougb
1016200425Sdougb  if [ ! "$TEMPROOT_TYPE" = "$INSTALLED_TYPE" ]; then
1017200425Sdougb    [ "$COMPFILE" = '.' ] && continue
1018200425Sdougb    TEMPROOT_TYPE=`echo $TEMPROOT_TYPE | tr [:upper:] [:lower:]`
1019200425Sdougb    INSTALLED_TYPE=`echo $INSTALLED_TYPE | tr [:upper:] [:lower:]`
1020200425Sdougb
1021200425Sdougb    echo "*** The installed file ${DESTDIR}${COMPFILE#.} has the type \"$INSTALLED_TYPE\""
1022200425Sdougb    echo "    but the new version has the type \"$TEMPROOT_TYPE\""
1023200425Sdougb    echo ''
1024200425Sdougb    echo "    How would you like to handle this?"
1025200425Sdougb    echo ''
1026200425Sdougb    echo "    Use 'r' to remove ${DESTDIR}${COMPFILE#.}"
1027200425Sdougb    case "$TEMPROOT_TYPE" in
1028200425Sdougb    'symbolic link')
1029200425Sdougb	TARGET=`readlink $COMPFILE`
1030200425Sdougb	echo "    and create a link to $TARGET in its place" ;;
1031200425Sdougb    *)	echo "    You will be able to install it as a \"$TEMPROOT_TYPE\"" ;;
1032200425Sdougb    esac
1033200425Sdougb    echo ''
1034200425Sdougb    echo "    Use 'i' to ignore this"
1035200425Sdougb    echo ''
1036200425Sdougb    echo -n "    How to proceed? [i] "
1037200425Sdougb    read ANSWER
1038200425Sdougb    case "$ANSWER" in
1039200425Sdougb    [rR])	case "${PRESERVE_FILES}" in
1040200425Sdougb		[Yy][Ee][Ss])
1041200425Sdougb		mv ${DESTDIR}${COMPFILE#.} ${PRESERVE_FILES_DIR}/ || exit 1 ;;
1042200425Sdougb		*) rm -rf ${DESTDIR}${COMPFILE#.} ;;
1043200425Sdougb		esac
1044200425Sdougb		case "$TEMPROOT_TYPE" in
1045200425Sdougb		'symbolic link') ln -sf $TARGET ${DESTDIR}${COMPFILE#.} ;;
1046200425Sdougb		esac ;;
1047200425Sdougb    *)	echo ''
1048200425Sdougb        echo "*** See the man page about adding ${COMPFILE#.} to the list of IGNORE_FILES"
1049200425Sdougb        press_to_continue ;;
1050200425Sdougb    esac
1051200425Sdougb    echo ''
1052200425Sdougb  fi
1053200425Sdougbdone
1054200425Sdougb
1055200700Sdougbfor COMPFILE in `find . -type f | sort`; do
105667949Sdougb
105767949Sdougb  # First, check to see if the file exists in DESTDIR.  If not, the
105867949Sdougb  # diff_loop function knows how to handle it.
105967949Sdougb  #
106067949Sdougb  if [ ! -e "${DESTDIR}${COMPFILE#.}" ]; then
106177325Sdougb    case "${AUTO_RUN}" in
106277325Sdougb      '')
106377325Sdougb        diff_loop
106477325Sdougb        ;;
106577325Sdougb      *)
106677325Sdougb        case "${AUTO_INSTALL}" in
106777325Sdougb        '')
106877325Sdougb          # If this is an auto run, make it official
106977325Sdougb          echo "   *** ${COMPFILE} will remain for your consideration"
107077325Sdougb          ;;
107177325Sdougb        *)
107277325Sdougb          diff_loop
107377325Sdougb          ;;
107477325Sdougb        esac
107577325Sdougb        ;;
107677325Sdougb    esac # Auto run test
107767949Sdougb    continue
107867949Sdougb  fi
107967949Sdougb
108052400Sbillf  case "${STRICT}" in
108152400Sbillf  '' | [Nn][Oo])
108252400Sbillf    # Compare CVS $Id's first so if the file hasn't been modified
108352400Sbillf    # local changes will be ignored.
108452400Sbillf    # If the files have the same $Id, delete the one in temproot so the
108552400Sbillf    # user will have less to wade through if files are left to merge by hand.
108652400Sbillf    #
108773651Sdougb    CVSID1=`grep "[$]${CVS_ID_TAG}:" ${DESTDIR}${COMPFILE#.} 2>/dev/null`
108890564Sdougb    CVSID2=`grep "[$]${CVS_ID_TAG}:" ${COMPFILE} 2>/dev/null` || CVSID2=none
108952400Sbillf
109067949Sdougb    case "${CVSID2}" in
109173651Sdougb    "${CVSID1}")
109267949Sdougb      echo " *** Temp ${COMPFILE} and installed have the same CVS Id, deleting"
109367949Sdougb      rm "${COMPFILE}"
109467949Sdougb      ;;
109567949Sdougb    esac
109652400Sbillf    ;;
109752400Sbillf  esac
109852400Sbillf
109952400Sbillf  # If the file is still here either because the $Ids are different, the
110052400Sbillf  # file doesn't have an $Id, or we're using STRICT mode; look at the diff.
110152400Sbillf  #
110252400Sbillf  if [ -f "${COMPFILE}" ]; then
110352400Sbillf
110452400Sbillf    # Do an absolute diff first to see if the files are actually different.
110552400Sbillf    # If they're not different, delete the one in temproot.
110652400Sbillf    #
1107110377Sdougb    if diff -q ${DIFF_OPTIONS} "${DESTDIR}${COMPFILE#.}" "${COMPFILE}" > \
1108110377Sdougb      /dev/null 2>&1; then
110952400Sbillf      echo " *** Temp ${COMPFILE} and installed are the same, deleting"
111052400Sbillf      rm "${COMPFILE}"
111152400Sbillf    else
111277323Sdougb      # Ok, the files are different, so show the user where they differ.
111377323Sdougb      # Use user's choice of diff methods; and user's pager if they have one.
111477323Sdougb      # Use more if not.
111567859Sdougb      # Use unified diffs by default.  Context diffs give me a headache. :)
111652400Sbillf      #
1117189992Sdougb      # If the user chose the -F option, test for that before proceeding
1118189992Sdougb      #
1119189992Sdougb      if [ -n "$FREEBSD_ID" ]; then
1120201291Sdougb        if diff -q -I'[$]FreeBSD.*[$]' "${DESTDIR}${COMPFILE#.}" "${COMPFILE}" > \
1121189992Sdougb            /dev/null 2>&1; then
1122189992Sdougb          if mm_install "${COMPFILE}"; then
1123189992Sdougb            echo "*** Updated revision control Id for ${DESTDIR}${COMPFILE#.}"
1124189992Sdougb          else
1125189992Sdougb            echo "*** Problem installing ${COMPFILE}, it will remain to merge by hand later"
1126189992Sdougb          fi
1127189992Sdougb          continue
1128189992Sdougb        fi
1129189992Sdougb      fi
113052400Sbillf      case "${AUTO_RUN}" in
113152400Sbillf      '')
113258910Salfred        # prompt user to install/delete/merge changes
113358910Salfred        diff_loop
113452400Sbillf        ;;
113552400Sbillf      *)
113652400Sbillf        # If this is an auto run, make it official
113752400Sbillf        echo "   *** ${COMPFILE} will remain for your consideration"
113852400Sbillf        ;;
113952400Sbillf      esac # Auto run test
114052400Sbillf    fi # Yes, the files are different
114152400Sbillf  fi # Yes, the file still remains to be checked
1142189763Sdougbdone # This is for the for way up there at the beginning of the comparison
114352400Sbillf
114452534Sbillfecho ''
114552400Sbillfecho "*** Comparison complete"
1146158149Sgordon
1147192230Sdougbif [ -s "${MTREENEW}" ]; then
1148158149Sgordon  echo "*** Saving mtree database for future upgrades"
1149200416Sdougb  test -e "${MTREEFILE}" && unlink ${MTREEFILE}
1150200416Sdougb  mv ${MTREENEW} ${MTREEFILE}
1151158149Sgordonfi
1152158149Sgordon
115352400Sbillfecho ''
115452400Sbillf
115552400SbillfTEST_FOR_FILES=`find ${TEMPROOT} -type f -size +0 2>/dev/null`
115652400Sbillfif [ -n "${TEST_FOR_FILES}" ]; then
115752400Sbillf  echo "*** Files that remain for you to merge by hand:"
1158200700Sdougb  find "${TEMPROOT}" -type f -size +0 | sort
115977335Sdougb  echo ''
116052400Sbillf
1161201765Sdougb  case "${AUTO_RUN}" in
1162201765Sdougb  '')
1163201765Sdougb    echo -n "Do you wish to delete what is left of ${TEMPROOT}? [no] "
1164201765Sdougb    read DEL_TEMPROOT
1165201765Sdougb    case "${DEL_TEMPROOT}" in
1166201765Sdougb    [yY]*)
1167201765Sdougb      delete_temproot
1168201765Sdougb      ;;
1169201765Sdougb    *)
1170201765Sdougb      echo " *** ${TEMPROOT} will remain"
1171201765Sdougb      ;;
1172201765Sdougb    esac
117352400Sbillf    ;;
1174201765Sdougb  *) ;;
117552400Sbillf  esac
1176201765Sdougbelse
1177201765Sdougb  echo "*** ${TEMPROOT} is empty, deleting"
1178201765Sdougb  delete_temproot
1179201765Sdougbfi
118052400Sbillf
118168153Sdougbcase "${AUTO_INSTALLED_FILES}" in
118268153Sdougb'') ;;
118368153Sdougb*)
118473651Sdougb  case "${AUTO_RUN}" in
118573651Sdougb  '')
118673651Sdougb    (
118773651Sdougb      echo ''
118877323Sdougb      echo '*** You chose the automatic install option for files that did not'
118977323Sdougb      echo '    exist on your system.  The following were installed for you:'
119073651Sdougb      echo "${AUTO_INSTALLED_FILES}"
119173651Sdougb    ) | ${PAGER}
119273651Sdougb    ;;
119373651Sdougb  *)
119468153Sdougb    echo ''
119577323Sdougb    echo '*** You chose the automatic install option for files that did not'
119677323Sdougb    echo '    exist on your system.  The following were installed for you:'
119768153Sdougb    echo "${AUTO_INSTALLED_FILES}"
119873651Sdougb    ;;
119973651Sdougb  esac
120068153Sdougb  ;;
120168153Sdougbesac
120268153Sdougb
1203158149Sgordoncase "${AUTO_UPGRADED_FILES}" in
1204158149Sgordon'') ;;
1205158149Sgordon*)
1206158149Sgordon  case "${AUTO_RUN}" in
1207158149Sgordon  '')
1208158149Sgordon    (
1209158149Sgordon      echo ''
1210158149Sgordon      echo '*** You chose the automatic upgrade option for files that you did'
1211158149Sgordon      echo '    not alter on your system.  The following were upgraded for you:'
1212158149Sgordon      echo "${AUTO_UPGRADED_FILES}"
1213158149Sgordon    ) | ${PAGER}
1214158149Sgordon    ;;
1215158149Sgordon  *)
1216158149Sgordon    echo ''
1217158149Sgordon    echo '*** You chose the automatic upgrade option for files that you did'
1218158149Sgordon    echo '    not alter on your system.  The following were upgraded for you:'
1219158149Sgordon    echo "${AUTO_UPGRADED_FILES}"
1220158149Sgordon    ;;
1221158149Sgordon  esac
1222158149Sgordon  ;;
1223158149Sgordonesac
1224158149Sgordon
122573651Sdougbrun_it_now () {
122673651Sdougb  case "${AUTO_RUN}" in
122773651Sdougb  '')
122873651Sdougb    unset YES_OR_NO
122973651Sdougb    echo ''
123077335Sdougb    echo -n '    Would you like to run it now? y or n [n] '
123173651Sdougb    read YES_OR_NO
123273651Sdougb
123373651Sdougb    case "${YES_OR_NO}" in
123473651Sdougb    y)
123577335Sdougb      echo "    Running ${1}"
123677335Sdougb      echo ''
123773651Sdougb      eval "${1}"
123873651Sdougb      ;;
123977335Sdougb    ''|n)
124077335Sdougb      echo ''
124177335Sdougb      echo "       *** Cancelled"
124277335Sdougb      echo ''
124377335Sdougb      echo "    Make sure to run ${1} yourself"
124477335Sdougb      ;;
124573651Sdougb    *)
124677335Sdougb      echo ''
124777335Sdougb      echo "       *** Sorry, I do not understand your answer (${YES_OR_NO})"
124877335Sdougb      echo ''
124977335Sdougb      echo "    Make sure to run ${1} yourself"
125073651Sdougb    esac
125173651Sdougb    ;;
125273651Sdougb  *) ;;
125373651Sdougb  esac
125473651Sdougb}
125573651Sdougb
125652400Sbillfcase "${NEED_NEWALIASES}" in
125752400Sbillf'') ;;
125852400Sbillf*)
125952400Sbillf  echo ''
126077335Sdougb  if [ -n "${DESTDIR}" ]; then
126177335Sdougb    echo "*** You installed a new aliases file into ${DESTDIR}/etc/mail, but"
126277335Sdougb    echo "    the newaliases command is limited to the directories configured"
126377335Sdougb    echo "    in sendmail.cf.  Make sure to create your aliases database by"
126477335Sdougb    echo "    hand when your sendmail configuration is done."
126577335Sdougb  else
126677335Sdougb    echo "*** You installed a new aliases file, so make sure that you run"
126777335Sdougb    echo "    '/usr/bin/newaliases' to rebuild your aliases database"
126877335Sdougb    run_it_now '/usr/bin/newaliases'
126977335Sdougb  fi
127052400Sbillf  ;;
127152400Sbillfesac
127252400Sbillf
127352400Sbillfcase "${NEED_CAP_MKDB}" in
127452400Sbillf'') ;;
127552400Sbillf*)
127652400Sbillf  echo ''
127752400Sbillf  echo "*** You installed a login.conf file, so make sure that you run"
127877335Sdougb  echo "    '/usr/bin/cap_mkdb ${DESTDIR}/etc/login.conf'"
127977335Sdougb  echo "     to rebuild your login.conf database"
128077335Sdougb  run_it_now "/usr/bin/cap_mkdb ${DESTDIR}/etc/login.conf"
128152400Sbillf  ;;
128252400Sbillfesac
128352400Sbillf
1284207612Snorkcase "${NEED_SERVICES_MKDB}" in
1285207612Snork'') ;;
1286207612Snork*)
1287207612Snork  echo ''
1288207612Snork  echo "*** You installed a services file, so make sure that you run"
1289207612Snork  echo "    '/usr/sbin/services_mkdb -q -o ${DESTDIR}/var/db/services.db ${DESTDIR}/etc/services'"
1290207612Snork  echo "     to rebuild your services database"
1291207612Snork  run_it_now "/usr/sbin/services_mkdb -q -o ${DESTDIR}/var/db/services.db ${DESTDIR}/etc/services"
1292207612Snork  ;;
1293207612Snorkesac
1294207612Snork
129552400Sbillfcase "${NEED_PWD_MKDB}" in
129652400Sbillf'') ;;
129752400Sbillf*)
129852400Sbillf  echo ''
129952400Sbillf  echo "*** You installed a new master.passwd file, so make sure that you run"
130077335Sdougb  if [ -n "${DESTDIR}" ]; then
130177335Sdougb    echo "    '/usr/sbin/pwd_mkdb -d ${DESTDIR}/etc -p ${DESTDIR}/etc/master.passwd'"
130277335Sdougb    echo "    to rebuild your password files"
130377335Sdougb    run_it_now "/usr/sbin/pwd_mkdb -d ${DESTDIR}/etc -p ${DESTDIR}/etc/master.passwd"
130477335Sdougb  else
130577335Sdougb    echo "    '/usr/sbin/pwd_mkdb -p /etc/master.passwd'"
130677335Sdougb    echo "     to rebuild your password files"
130777335Sdougb    run_it_now '/usr/sbin/pwd_mkdb -p /etc/master.passwd'
130877335Sdougb  fi
130952400Sbillf  ;;
131052400Sbillfesac
131152400Sbillf
131252400Sbillfecho ''
131352400Sbillf
131467949Sdougbif [ -r "${MM_EXIT_SCRIPT}" ]; then
131567949Sdougb  . "${MM_EXIT_SCRIPT}"
131667949Sdougbfi
131767949Sdougb
131891193Sdougbcase "${COMP_CONFS}" in
131991193Sdougb'') ;;
132091193Sdougb*)
132191193Sdougb  . ${DESTDIR}/etc/defaults/rc.conf
132291193Sdougb
132391193Sdougb  (echo ''
132491193Sdougb  echo "*** Comparing conf files: ${rc_conf_files}"
132591193Sdougb
132691193Sdougb  for CONF_FILE in ${rc_conf_files}; do
132791193Sdougb    if [ -r "${DESTDIR}${CONF_FILE}" ]; then
132891193Sdougb      echo ''
132991193Sdougb      echo "*** From ${DESTDIR}${CONF_FILE}"
133091193Sdougb      echo "*** From ${DESTDIR}/etc/defaults/rc.conf"
133191193Sdougb
133291193Sdougb      for RC_CONF_VAR in `grep -i ^[a-z] ${DESTDIR}${CONF_FILE} |
133391193Sdougb        cut -d '=' -f 1`; do
133491193Sdougb        echo ''
133591223Sdougb        grep -w ^${RC_CONF_VAR} ${DESTDIR}${CONF_FILE}
133691223Sdougb        grep -w ^${RC_CONF_VAR} ${DESTDIR}/etc/defaults/rc.conf ||
133791193Sdougb          echo ' * No default variable with this name'
133891193Sdougb      done
133991193Sdougb    fi
134091193Sdougb  done) | ${PAGER}
134191193Sdougb  echo ''
134291193Sdougb  ;;
134391193Sdougbesac
134491193Sdougb
134591193Sdougbcase "${PRE_WORLD}" in
134691193Sdougb'') ;;
134791193Sdougb*)
1348186678Sdougb  MAKE_CONF="${SOURCEDIR}/share/examples/etc/make.conf"
134991193Sdougb
135091193Sdougb  (echo ''
135191193Sdougb  echo '*** Comparing make variables'
135291193Sdougb  echo ''
135391193Sdougb  echo "*** From ${DESTDIR}/etc/make.conf"
135491193Sdougb  echo "*** From ${MAKE_CONF}"
135591193Sdougb
1356101348Sdougb  for MAKE_VAR in `grep -i ^[a-z] ${DESTDIR}/etc/make.conf | cut -d '=' -f 1`; do
135791193Sdougb    echo ''
135891223Sdougb    grep -w ^${MAKE_VAR} ${DESTDIR}/etc/make.conf
135991223Sdougb    grep -w ^#${MAKE_VAR} ${MAKE_CONF} ||
136091193Sdougb      echo ' * No example variable with this name'
136191193Sdougb  done) | ${PAGER}
136291193Sdougb  ;;
136391193Sdougbesac
136491193Sdougb
1365200425Sdougbif [ -n "${PRESERVE_FILES}" ]; then
1366200425Sdougb  find -d $PRESERVE_FILES_DIR -type d -empty -delete 2>/dev/null
1367200425Sdougb  rmdir $PRESERVE_FILES_DIR 2>/dev/null
1368200425Sdougbfi
1369200425Sdougb
137052400Sbillfexit 0
1371