mergemaster.sh revision 186678
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
8186678Sdougb# Copyright 1998-2009 Douglas Barton
973651Sdougb# DougB@FreeBSD.org
1052400Sbillf
1152495Sbillf# $FreeBSD: head/usr.sbin/mergemaster/mergemaster.sh 186678 2009-01-01 11:41:13Z dougb $
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}"
18114501Sdougb  echo 'Usage: mergemaster [-scrvahipCP] [-m /path]'
1967949Sdougb  echo '         [-t /path] [-d] [-u N] [-w N] [-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'
2991193Sdougb  echo '  -C  Compare local rc.conf variables to the defaults'
30114501Sdougb  echo '  -P  Preserve files that are overwritten'
3152400Sbillf  echo "  -m /path/directory  Specify location of source to do the make in"
3252400Sbillf  echo "  -t /path/directory  Specify temp root directory"
3352400Sbillf  echo "  -d  Add date and time to directory name (e.g., /var/tmp/temproot.`date +%m%d.%H.%M`)"
3452400Sbillf  echo "  -u N  Specify a numeric umask"
3552400Sbillf  echo "  -w N  Specify a screen width in columns to sdiff"
36155309Srwatson  echo "  -A architecture  Alternative architecture name to pass to make"
3767949Sdougb  echo '  -D /path/directory  Specify the destination directory to install files to'
38158149Sgordon  echo "  -U Attempt to auto upgrade files that have not been user modified."
3952400Sbillf  echo ''
4052400Sbillf}
4152400Sbillf
4252400Sbillfdisplay_help () {
4352400Sbillf  echo "* To specify a directory other than /var/tmp/temproot for the"
4452400Sbillf  echo "  temporary root environment, use -t /path/to/temp/root"
4552400Sbillf  echo "* The -w option takes a number as an argument for the column width"
4667859Sdougb  echo "  of the screen.  The default is 80."
4767949Sdougb  echo '* The -a option causes mergemaster to run without prompting.'
4852400Sbillf}
4952400Sbillf
5058910Salfred# Loop allowing the user to use sdiff to merge files and display the merged
5158910Salfred# file.
5258910Salfredmerge_loop () {
5367850Sdougb  case "${VERBOSE}" in
5467850Sdougb  '') ;;
5567850Sdougb  *)
5667850Sdougb      echo "   *** Type h at the sdiff prompt (%) to get usage help"
5767850Sdougb      ;;
5867850Sdougb  esac
5967850Sdougb  echo ''
6067850Sdougb  MERGE_AGAIN=yes
6167850Sdougb  while [ "${MERGE_AGAIN}" = "yes" ]; do
6267850Sdougb    # Prime file.merged so we don't blat the owner/group id's
6367850Sdougb    cp -p "${COMPFILE}" "${COMPFILE}.merged"
6467850Sdougb    sdiff -o "${COMPFILE}.merged" --text --suppress-common-lines \
6567949Sdougb      --width=${SCREEN_WIDTH:-80} "${DESTDIR}${COMPFILE#.}" "${COMPFILE}"
6667850Sdougb    INSTALL_MERGED=V
6767850Sdougb    while [ "${INSTALL_MERGED}" = "v" -o "${INSTALL_MERGED}" = "V" ]; do
6867850Sdougb      echo ''
6967850Sdougb      echo "  Use 'i' to install merged file"
7067850Sdougb      echo "  Use 'r' to re-do the merge"
7167850Sdougb      echo "  Use 'v' to view the merged file"
7267850Sdougb      echo "  Default is to leave the temporary file to deal with by hand"
7367850Sdougb      echo ''
7467859Sdougb      echo -n "    *** How should I deal with the merged file? [Leave it for later] "
7567859Sdougb      read INSTALL_MERGED
7658910Salfred
7767850Sdougb      case "${INSTALL_MERGED}" in
7867850Sdougb      [iI])
7967850Sdougb        mv "${COMPFILE}.merged" "${COMPFILE}"
8067850Sdougb        echo ''
8167850Sdougb        if mm_install "${COMPFILE}"; then
8267850Sdougb          echo "     *** Merged version of ${COMPFILE} installed successfully"
8367859Sdougb        else
8467850Sdougb          echo "     *** Problem installing ${COMPFILE}, it will remain to merge by hand later"
8567859Sdougb        fi
8667850Sdougb        unset MERGE_AGAIN
8767850Sdougb        ;;
8867850Sdougb      [rR])
8967850Sdougb        rm "${COMPFILE}.merged"
9067859Sdougb        ;;
9167850Sdougb      [vV])
9267850Sdougb        ${PAGER} "${COMPFILE}.merged"
9367850Sdougb        ;;
9467850Sdougb      '')
9567850Sdougb        echo "   *** ${COMPFILE} will remain for your consideration"
9667850Sdougb        unset MERGE_AGAIN
9767850Sdougb        ;;
9867850Sdougb      *)
9967850Sdougb        echo "invalid choice: ${INSTALL_MERGED}"
10067850Sdougb        INSTALL_MERGED=V
10167850Sdougb        ;;
10267850Sdougb      esac
10367850Sdougb    done
10467850Sdougb  done
10558910Salfred}
10658910Salfred
10758910Salfred# Loop showing user differences between files, allow merge, skip or install
10858910Salfred# options
10958910Salfreddiff_loop () {
11058910Salfred
11167850Sdougb  HANDLE_COMPFILE=v
11258910Salfred
11377323Sdougb  while [ "${HANDLE_COMPFILE}" = "v" -o "${HANDLE_COMPFILE}" = "V" -o \
11477323Sdougb    "${HANDLE_COMPFILE}" = "NOT V" ]; do
11567949Sdougb    if [ -f "${DESTDIR}${COMPFILE#.}" -a -f "${COMPFILE}" ]; then
116158149Sgordon      if [ -n "${AUTO_UPGRADE}" ]; then
117158149Sgordon        if echo "${CHANGED}" | grep -qsv ${DESTDIR}${COMPFILE#.}; then
118158149Sgordon          echo ''
119158149Sgordon          echo "  *** ${COMPFILE} has not been user modified."
120158149Sgordon          echo ''
121158149Sgordon
122158149Sgordon          if mm_install "${COMPFILE}"; then
123158149Sgordon            echo "   *** ${COMPFILE} upgraded successfully"
124158149Sgordon            echo ''
125158149Sgordon            # Make the list print one file per line
126158149Sgordon            AUTO_UPGRADED_FILES="${AUTO_UPGRADED_FILES}      ${DESTDIR}${COMPFILE#.}
127158149Sgordon"
128158149Sgordon          else
129158149Sgordon          echo "   *** Problem upgrading ${COMPFILE}, it will remain to merge by hand"
130158149Sgordon          fi
131158149Sgordon          return
132158149Sgordon        fi
133158149Sgordon      fi
13467850Sdougb      if [ "${HANDLE_COMPFILE}" = "v" -o "${HANDLE_COMPFILE}" = "V" ]; then
13590564Sdougb	echo ''
136109993Sdillon	echo '   ======================================================================   '
137109993Sdillon	echo ''
138109993Sdillon        (
139109993Sdillon          echo "  *** Displaying differences between ${COMPFILE} and installed version:"
140109993Sdillon          echo ''
141110377Sdougb          diff ${DIFF_FLAG} ${DIFF_OPTIONS} "${DESTDIR}${COMPFILE#.}" "${COMPFILE}"
142109993Sdillon        ) | ${PAGER}
143109993Sdillon        echo ''
14467850Sdougb      fi
14567850Sdougb    else
146109993Sdillon      echo ''
14767850Sdougb      echo "  *** There is no installed version of ${COMPFILE}"
14891193Sdougb      echo ''
14967949Sdougb      case "${AUTO_INSTALL}" in
15067949Sdougb      [Yy][Ee][Ss])
15167949Sdougb        echo ''
15267949Sdougb        if mm_install "${COMPFILE}"; then
15367949Sdougb          echo "   *** ${COMPFILE} installed successfully"
15468507Sdougb          echo ''
15567949Sdougb          # Make the list print one file per line
15667949Sdougb          AUTO_INSTALLED_FILES="${AUTO_INSTALLED_FILES}      ${DESTDIR}${COMPFILE#.}
15767949Sdougb"
15867949Sdougb        else
15967949Sdougb          echo "   *** Problem installing ${COMPFILE}, it will remain to merge by hand"
16067949Sdougb        fi
16167949Sdougb        return
16267949Sdougb        ;;
16367949Sdougb      *)
16467949Sdougb        NO_INSTALLED=yes
16567949Sdougb        ;;
16667949Sdougb      esac
16767850Sdougb    fi
16867859Sdougb
16967850Sdougb    echo "  Use 'd' to delete the temporary ${COMPFILE}"
17067850Sdougb    echo "  Use 'i' to install the temporary ${COMPFILE}"
17167850Sdougb    case "${NO_INSTALLED}" in
17267850Sdougb    '')
17377326Sdougb      echo "  Use 'm' to merge the temporary and installed versions"
174109993Sdillon      echo "  Use 'v' to view the diff results again"
17567850Sdougb      ;;
17667850Sdougb    esac
17767850Sdougb    echo ''
17867850Sdougb    echo "  Default is to leave the temporary file to deal with by hand"
17967850Sdougb    echo ''
18067859Sdougb    echo -n "How should I deal with this? [Leave it for later] "
18167859Sdougb    read HANDLE_COMPFILE
18267859Sdougb
18367850Sdougb    case "${HANDLE_COMPFILE}" in
18467850Sdougb    [dD])
18567850Sdougb      rm "${COMPFILE}"
18667850Sdougb      echo ''
18767850Sdougb      echo "   *** Deleting ${COMPFILE}"
18867850Sdougb      ;;
18967850Sdougb    [iI])
19067850Sdougb      echo ''
19167850Sdougb      if mm_install "${COMPFILE}"; then
19267850Sdougb        echo "   *** ${COMPFILE} installed successfully"
19367850Sdougb      else
19467850Sdougb        echo "   *** Problem installing ${COMPFILE}, it will remain to merge by hand"
19567850Sdougb      fi
19667850Sdougb      ;;
19767850Sdougb    [mM])
19867850Sdougb      case "${NO_INSTALLED}" in
19967850Sdougb      '')
20067850Sdougb        # interact with user to merge files
20167850Sdougb        merge_loop
20267850Sdougb        ;;
20367850Sdougb      *)
20467850Sdougb        echo ''
20567850Sdougb        echo "   *** There is no installed version of ${COMPFILE}"
20667850Sdougb        echo ''
20767850Sdougb        HANDLE_COMPFILE="NOT V"
20867850Sdougb        ;;
20967850Sdougb      esac # End of "No installed version of file but user selected merge" test
21067850Sdougb      ;;
21167850Sdougb    [vV])
21267850Sdougb      continue
21367850Sdougb      ;;
21467850Sdougb    '')
21567850Sdougb      echo ''
21667850Sdougb      echo "   *** ${COMPFILE} will remain for your consideration"
21767850Sdougb      ;;
21867850Sdougb    *)
21967850Sdougb      # invalid choice, show menu again.
22067850Sdougb      echo "invalid choice: ${HANDLE_COMPFILE}"
22167850Sdougb      echo ''
22267850Sdougb      HANDLE_COMPFILE="NOT V"
22367850Sdougb      continue
22467850Sdougb      ;;
22567850Sdougb    esac  # End of "How to handle files that are different"
22667859Sdougb  done
22767850Sdougb  unset NO_INSTALLED
22867850Sdougb  echo ''
22967850Sdougb  case "${VERBOSE}" in
23067850Sdougb  '') ;;
23167850Sdougb  *)
23267850Sdougb    sleep 3
23367850Sdougb    ;;
23467850Sdougb  esac
23558910Salfred}
23658910Salfred
23797960Sdougbpress_to_continue () {
23897960Sdougb  local DISCARD
23997960Sdougb  echo -n ' *** Press the [Enter] or [Return] key to continue '
24097960Sdougb  read DISCARD
24197960Sdougb}
24297960Sdougb
24352400Sbillf# Set the default path for the temporary root environment
24452400Sbillf#
24552400SbillfTEMPROOT='/var/tmp/temproot'
24652400Sbillf
24773651Sdougb# Read /etc/mergemaster.rc first so the one in $HOME can override
24873651Sdougb#
24973651Sdougbif [ -r /etc/mergemaster.rc ]; then
25073651Sdougb  . /etc/mergemaster.rc
25173651Sdougbfi
25273651Sdougb
25352400Sbillf# Read .mergemasterrc before command line so CLI can override
25452400Sbillf#
25567949Sdougbif [ -r "$HOME/.mergemasterrc" ]; then
25652400Sbillf  . "$HOME/.mergemasterrc"
25752400Sbillffi
25852400Sbillf
259186678Sdougb# Assign the location of the mtree database
260186678Sdougb#
261186678SdougbMTREEDB=${MTREEDB:-/var/db}
262186678SdougbMTREEFILE="${MTREEDB}/mergemaster.mtree"
263186678Sdougb
26452400Sbillf# Check the command line options
26552400Sbillf#
266158149Sgordonwhile getopts ":ascrvhipCPm:t:du:w:D:A:U" COMMAND_LINE_ARGUMENT ; do
26752400Sbillf  case "${COMMAND_LINE_ARGUMENT}" in
268155309Srwatson  A)
269186678Sdougb    ARCHSTRING='TARGET_ARCH='${OPTARG}
270155309Srwatson    ;;
271158149Sgordon  U)
272158149Sgordon    AUTO_UPGRADE=yes
273158149Sgordon    ;;
27452400Sbillf  s)
27552400Sbillf    STRICT=yes
276110377Sdougb    unset DIFF_OPTIONS
27752400Sbillf    ;;
27852400Sbillf  c)
27952400Sbillf    DIFF_FLAG='-c'
28052400Sbillf    ;;
28152400Sbillf  r)
28252400Sbillf    RERUN=yes
28352400Sbillf    ;;
28452400Sbillf  v)
28552400Sbillf    case "${AUTO_RUN}" in
28652400Sbillf    '') VERBOSE=yes ;;
28752400Sbillf    esac
28852400Sbillf    ;;
28952400Sbillf  a)
29052400Sbillf    AUTO_RUN=yes
29152400Sbillf    unset VERBOSE
29252400Sbillf    ;;
29352400Sbillf  h)
29452400Sbillf    display_usage
29552400Sbillf    display_help
29652400Sbillf    exit 0
29752400Sbillf    ;;
29867949Sdougb  i)
29967949Sdougb    AUTO_INSTALL=yes
30067949Sdougb    ;;
30196045Sdougb  C)
30296045Sdougb    COMP_CONFS=yes
30396045Sdougb    ;;
304114501Sdougb  P)
305114501Sdougb    PRESERVE_FILES=yes
306114501Sdougb    ;;
30791193Sdougb  p)
30891193Sdougb    PRE_WORLD=yes
30996045Sdougb    unset COMP_CONFS
31096045Sdougb    unset AUTO_RUN
31191193Sdougb    ;;
31252400Sbillf  m)
31352400Sbillf    SOURCEDIR=${OPTARG}
31452400Sbillf    ;;
31552400Sbillf  t)
31652400Sbillf    TEMPROOT=${OPTARG}
31752400Sbillf    ;;
31852400Sbillf  d)
31952400Sbillf    TEMPROOT=${TEMPROOT}.`date +%m%d.%H.%M`
32052400Sbillf    ;;
32152400Sbillf  u)
32252400Sbillf    NEW_UMASK=${OPTARG}
32352400Sbillf    ;;
32452400Sbillf  w)
32552400Sbillf    SCREEN_WIDTH=${OPTARG}
32652400Sbillf    ;;
32767949Sdougb  D)
32867949Sdougb    DESTDIR=${OPTARG}
32967949Sdougb    ;;
33052400Sbillf  *)
33152400Sbillf    display_usage
33252400Sbillf    exit 1
33352400Sbillf    ;;
33452400Sbillf  esac
33552400Sbillfdone
33652400Sbillf
337114501Sdougb# Don't force the user to set this in the mergemaster rc file
338114501Sdougbif [ -n "${PRESERVE_FILES}" -a -z "${PRESERVE_FILES_DIR}" ]; then
339114501Sdougb  PRESERVE_FILES_DIR=/var/tmp/mergemaster/preserved-files-`date +%y%m%d-%H%M%S`
340114501Sdougbfi
341114501Sdougb
342186678Sdougb# Check for the mtree database in DESTDIR
343186678Sdougbcase "${AUTO_UPGRADE}" in
344186678Sdougb'') ;;	# If the option is not set no need to run the test or warn the user
345186678Sdougb*)
346186678Sdougb  if [ ! -f "${DESTDIR}${MTREEFILE}" ]; then
347186678Sdougb    echo ''
348186678Sdougb    echo "*** Unable to find mtree database. Skipping auto-upgrade."
349186678Sdougb    echo ''
350186678Sdougb    press_to_continue
351186678Sdougb    unset AUTO_UPGRADE
352186678Sdougb  fi
353186678Sdougb  ;;
354186678Sdougbesac
355186678Sdougb
356186678Sdougbif grep -q nodev ${DESTDIR}/etc/fstab; then
357186678Sdougb  echo ''
358186678Sdougb  echo "*** You have the deprecated 'nodev' option in ${DESTDIR}/etc/fstab."
359186678Sdougb  echo "    This can prevent your system from mounting the filesystem on reboot."
360186678Sdougb  echo "    Please update your fstab before continuing."
361186678Sdougb  echo "    See fstab(5) for more information."
362186678Sdougb  echo ''
363186678Sdougb  exit 1
364158149Sgordonfi
365158149Sgordon
36652400Sbillfecho ''
36752400Sbillf
36852400Sbillf# If the user has a pager defined, make sure we can run it
36952400Sbillf#
37052400Sbillfcase "${DONT_CHECK_PAGER}" in
37152400Sbillf'')
37264467Sbrian  while ! type "${PAGER%% *}" >/dev/null && [ -n "${PAGER}" ]; do
37352400Sbillf    echo " *** Your PAGER environment variable specifies '${PAGER}', but"
37464467Sbrian    echo "     due to the limited PATH that I use for security reasons,"
37567859Sdougb    echo "     I cannot execute it.  So, what would you like to do?"
37652400Sbillf    echo ''
37752400Sbillf    echo "  Use 'e' to exit mergemaster and fix your PAGER variable"
37864467Sbrian    if [ -x /usr/bin/less -o -x /usr/local/bin/less ]; then
37964467Sbrian    echo "  Use 'l' to set PAGER to 'less' for this run"
38052400Sbillf    fi
38152400Sbillf    echo "  Use 'm' to use plain old 'more' as your PAGER for this run"
38252400Sbillf    echo ''
38352400Sbillf    echo "  Default is to use plain old 'more' "
38452400Sbillf    echo ''
38567859Sdougb    echo -n "What should I do? [Use 'more'] "
38667859Sdougb    read FIXPAGER
38767859Sdougb
38852400Sbillf    case "${FIXPAGER}" in
38958910Salfred    [eE])
39052400Sbillf       exit 0
39152400Sbillf       ;;
39258910Salfred    [lL])
39364467Sbrian       if [ -x /usr/bin/less ]; then
39464467Sbrian         PAGER=/usr/bin/less
39564467Sbrian       elif [ -x /usr/local/bin/less ]; then
39658910Salfred         PAGER=/usr/local/bin/less
39764467Sbrian       else
39864467Sbrian         echo ''
39964467Sbrian         echo " *** Fatal Error:"
40064467Sbrian         echo "     You asked to use 'less' as your pager, but I can't"
40164467Sbrian         echo "     find it in /usr/bin or /usr/local/bin"
40264467Sbrian         exit 1
40358910Salfred       fi
40452400Sbillf       ;;
40560420Sbsd    [mM]|'')
40652400Sbillf       PAGER=more
40752400Sbillf       ;;
40858910Salfred    *)
40958910Salfred       echo ''
41058910Salfred       echo "invalid choice: ${FIXPAGER}"
41152400Sbillf    esac
41252400Sbillf    echo ''
41358910Salfred  done
41452400Sbillf  ;;
41552400Sbillfesac
41652400Sbillf
41752400Sbillf# If user has a pager defined, or got assigned one above, use it.
41852400Sbillf# If not, use more.
41952400Sbillf#
42052400SbillfPAGER=${PAGER:-more}
42152400Sbillf
42252400Sbillfif [ -n "${VERBOSE}" -a ! "${PAGER}" = "more" ]; then
42352400Sbillf  echo " *** You have ${PAGER} defined as your pager so we will use that"
42452400Sbillf  echo ''
42552400Sbillf  sleep 3
42652400Sbillffi
42752400Sbillf
42852400Sbillf# Assign the diff flag once so we will not have to keep testing it
42952400Sbillf#
43052400SbillfDIFF_FLAG=${DIFF_FLAG:--u}
43152400Sbillf
43252400Sbillf# Assign the source directory
43352400Sbillf#
434186678SdougbSOURCEDIR=${SOURCEDIR:-/usr/src}
435186678Sdougbif [ ! -f ${SOURCEDIR}/Makefile.inc1 -a \
436186678Sdougb   -f ${SOURCEDIR}/../Makefile.inc1 ]; then
437186678Sdougb  echo " *** The source directory you specified (${SOURCEDIR})"
438186678Sdougb  echo "     will be reset to ${SOURCEDIR}/.."
439186678Sdougb  echo ''
440186678Sdougb  sleep 3
441186678Sdougb  SOURCEDIR=${SOURCEDIR}/..
442186678Sdougbfi
44352400Sbillf
444186678Sdougb# Setup make to use system files from SOURCEDIR
445186678SdougbMM_MAKE="make -m ${SOURCEDIR}/share/mk"
446186678Sdougb
447158149Sgordon# Check DESTDIR against the mergemaster mtree database to see what
448158149Sgordon# files the user changed from the reference files.
449158149Sgordon#
450158149SgordonCHANGED=
451186678Sdougbif [ -n "${AUTO_UPGRADE}" -a -f "${DESTDIR}${MTREEFILE}" ]; then
452186678Sdougb	for file in `mtree -eq -f ${DESTDIR}${MTREEFILE} -p ${DESTDIR}/ \
453158149Sgordon		2>/dev/null | awk '($2 == "changed") {print $1}'`; do
454158149Sgordon		if [ -f "${DESTDIR}/$file" ]; then
455158149Sgordon			CHANGED="${CHANGED} ${DESTDIR}/$file"
456158149Sgordon		fi
457158149Sgordon	done
458158149Sgordonfi
459158149Sgordon
46096045Sdougb# Check the width of the user's terminal
46196045Sdougb#
46296045Sdougbif [ -t 0 ]; then
463110377Sdougb  w=`tput columns`
46496045Sdougb  case "${w}" in
46596045Sdougb  0|'') ;; # No-op, since the input is not valid
46696045Sdougb  *)
46796045Sdougb    case "${SCREEN_WIDTH}" in
46896045Sdougb    '') SCREEN_WIDTH="${w}" ;;
46996045Sdougb    "${w}") ;; # No-op, since they are the same
47096045Sdougb    *)
47196045Sdougb      echo -n "*** You entered ${SCREEN_WIDTH} as your screen width, but stty "
47296045Sdougb      echo "thinks it is ${w}."
47396045Sdougb      echo ''
47496045Sdougb      echo -n "What would you like to use? [${w}] "
47596045Sdougb      read SCREEN_WIDTH
47697380Sdougb      case "${SCREEN_WIDTH}" in
47797380Sdougb      '') SCREEN_WIDTH="${w}" ;;
47897380Sdougb      esac
47996045Sdougb      ;;
48096045Sdougb    esac
48196045Sdougb  esac
48296045Sdougbfi
48396045Sdougb
48473651Sdougb# Define what CVS $Id tag to look for to aid portability.
48573651Sdougb#
48673651SdougbCVS_ID_TAG=FreeBSD
48773651Sdougb
48899152Sdougbdelete_temproot () {
489101362Sdougb  rm -rf "${TEMPROOT}" 2>/dev/null
490101362Sdougb  chflags -R 0 "${TEMPROOT}" 2>/dev/null
491101362Sdougb  rm -rf "${TEMPROOT}" || exit 1
49299152Sdougb}
49399152Sdougb
49452400Sbillfcase "${RERUN}" in
49552400Sbillf'')
49652400Sbillf  # Set up the loop to test for the existence of the
49752400Sbillf  # temp root directory.
49852400Sbillf  #
49952400Sbillf  TEST_TEMP_ROOT=yes
50052400Sbillf  while [ "${TEST_TEMP_ROOT}" = "yes" ]; do
50152400Sbillf    if [ -d "${TEMPROOT}" ]; then
50252400Sbillf      echo "*** The directory specified for the temporary root environment,"
50367859Sdougb      echo "    ${TEMPROOT}, exists.  This can be a security risk if untrusted"
50452400Sbillf      echo "    users have access to the system."
50552400Sbillf      echo ''
50652400Sbillf      case "${AUTO_RUN}" in
50752400Sbillf      '')
50852400Sbillf        echo "  Use 'd' to delete the old ${TEMPROOT} and continue"
50952400Sbillf        echo "  Use 't' to select a new temporary root directory"
51052400Sbillf        echo "  Use 'e' to exit mergemaster"
51152400Sbillf        echo ''
51252400Sbillf        echo "  Default is to use ${TEMPROOT} as is"
51352400Sbillf        echo ''
51467859Sdougb        echo -n "How should I deal with this? [Use the existing ${TEMPROOT}] "
51567859Sdougb        read DELORNOT
51667859Sdougb
51767859Sdougb        case "${DELORNOT}" in
51867859Sdougb        [dD])
51967859Sdougb          echo ''
52067859Sdougb          echo "   *** Deleting the old ${TEMPROOT}"
52167859Sdougb          echo ''
52299152Sdougb          delete_temproot || exit 1
52367859Sdougb          unset TEST_TEMP_ROOT
52452400Sbillf          ;;
52567859Sdougb        [tT])
52667859Sdougb          echo "   *** Enter new directory name for temporary root environment"
52767859Sdougb          read TEMPROOT
52867859Sdougb          ;;
52967859Sdougb        [eE])
53067859Sdougb          exit 0
53167859Sdougb          ;;
53267859Sdougb        '')
53367859Sdougb          echo ''
53467859Sdougb          echo "   *** Leaving ${TEMPROOT} intact"
53567859Sdougb          echo ''
53667859Sdougb          unset TEST_TEMP_ROOT
53767859Sdougb          ;;
53867859Sdougb        *)
53967859Sdougb          echo ''
54067859Sdougb          echo "invalid choice: ${DELORNOT}"
54167859Sdougb          echo ''
54267859Sdougb          ;;
54367859Sdougb        esac
54467859Sdougb        ;;
54552400Sbillf      *)
54677323Sdougb        # If this is an auto-run, try a hopefully safe alternative then
54777323Sdougb        # re-test anyway.
54852400Sbillf        TEMPROOT=/var/tmp/temproot.`date +%m%d.%H.%M.%S`
54952400Sbillf        ;;
55052400Sbillf      esac
55152400Sbillf    else
55252400Sbillf      unset TEST_TEMP_ROOT
55352400Sbillf    fi
55452400Sbillf  done
55552400Sbillf
55652400Sbillf  echo "*** Creating the temporary root environment in ${TEMPROOT}"
55752400Sbillf
55852400Sbillf  if mkdir -p "${TEMPROOT}"; then
55952400Sbillf    echo " *** ${TEMPROOT} ready for use"
56052400Sbillf  fi
56152400Sbillf
56252400Sbillf  if [ ! -d "${TEMPROOT}" ]; then
56352400Sbillf    echo ''
56452400Sbillf    echo "  *** FATAL ERROR: Cannot create ${TEMPROOT}"
56552400Sbillf    echo ''
56652400Sbillf    exit 1
56752400Sbillf  fi
56852400Sbillf
56952400Sbillf  echo " *** Creating and populating directory structure in ${TEMPROOT}"
57052400Sbillf  echo ''
57152400Sbillf
57252400Sbillf  case "${VERBOSE}" in
57352400Sbillf  '') ;;
57452400Sbillf  *)
57597960Sdougb    press_to_continue
57652400Sbillf    ;;
57752400Sbillf  esac
57852400Sbillf
57991193Sdougb  case "${PRE_WORLD}" in
58091193Sdougb  '')
58191193Sdougb    { cd ${SOURCEDIR} &&
58291193Sdougb      case "${DESTDIR}" in
58391193Sdougb      '') ;;
58491193Sdougb      *)
585186678Sdougb      ${MM_MAKE} DESTDIR=${DESTDIR} ${ARCHSTRING} distrib-dirs
58691193Sdougb        ;;
58791193Sdougb      esac
588186678Sdougb      ${MM_MAKE} DESTDIR=${TEMPROOT} ${ARCHSTRING} distrib-dirs &&
589186678Sdougb      MAKEOBJDIRPREFIX=${TEMPROOT}/usr/obj ${MM_MAKE} ${ARCHSTRING} obj SUBDIR_OVERRIDE=etc &&
590186678Sdougb      MAKEOBJDIRPREFIX=${TEMPROOT}/usr/obj ${MM_MAKE} ${ARCHSTRING} all SUBDIR_OVERRIDE=etc &&
591186678Sdougb      MAKEOBJDIRPREFIX=${TEMPROOT}/usr/obj ${MM_MAKE} ${ARCHSTRING} \
592155309Srwatson	  DESTDIR=${TEMPROOT} distribution;} ||
59391193Sdougb    { echo '';
59491193Sdougb     echo "  *** FATAL ERROR: Cannot 'cd' to ${SOURCEDIR} and install files to";
59591193Sdougb      echo "      the temproot environment";
59691193Sdougb      echo '';
59791193Sdougb      exit 1;}
59891193Sdougb    ;;
59991193Sdougb  *)
60091193Sdougb    # Only set up files that are crucial to {build|install}world
60191193Sdougb    { mkdir -p ${TEMPROOT}/etc &&
602186678Sdougb      cp -p ${SOURCEDIR}/etc/master.passwd ${TEMPROOT}/etc &&
603186678Sdougb      cp -p ${SOURCEDIR}/etc/group ${TEMPROOT}/etc;} ||
60491193Sdougb    { echo '';
60591193Sdougb      echo '  *** FATAL ERROR: Cannot copy files to the temproot environment';
60691193Sdougb      echo '';
60791193Sdougb      exit 1;}
60891193Sdougb    ;;
60991193Sdougb  esac
61052400Sbillf
61177323Sdougb  # Doing the inventory and removing files that we don't want to compare only
61277323Sdougb  # makes sense if we are not doing a rerun, since we have no way of knowing
61377323Sdougb  # what happened to the files during previous incarnations.
61467949Sdougb  case "${VERBOSE}" in
61567949Sdougb  '') ;;
61667949Sdougb  *)
61767949Sdougb    echo ''
61867949Sdougb    echo ' *** The following files exist only in the installed version of'
61967949Sdougb    echo "     ${DESTDIR}/etc.  In the vast majority of cases these files"
62067949Sdougb    echo '     are necessary parts of the system and should not be deleted.'
62167949Sdougb    echo '     However because these files are not updated by this process you'
62267949Sdougb    echo '     might want to verify their status before rebooting your system.'
62367949Sdougb    echo ''
62497960Sdougb    press_to_continue
625101348Sdougb    diff -qr ${DESTDIR}/etc ${TEMPROOT}/etc | grep "^Only in ${DESTDIR}/etc" | ${PAGER}
62667949Sdougb    echo ''
62797960Sdougb    press_to_continue
62867949Sdougb    ;;
62967949Sdougb  esac
63067949Sdougb
63152534Sbillf  # Avoid comparing the motd if the user specifies it in .mergemasterrc
632186678Sdougb  # Compatibility shim to be removed in FreeBSD 9.x
63352534Sbillf  case "${IGNORE_MOTD}" in
63452534Sbillf  '') ;;
635186678Sdougb  *) IGNORE_FILES="${IGNORE_FILES} /etc/motd"
636186678Sdougb     echo ''
637186678Sdougb     echo "*** You have the IGNORE_MOTD option set in your mergemaster rc file."
638186678Sdougb     echo "    This option is deprecated in favor of the IGNORE_FILES option."
639186678Sdougb     echo "    Please update your rc file accordingly."
640186678Sdougb     echo ''
641186678Sdougb     press_to_continue
64252534Sbillf     ;;
64352534Sbillf  esac
64452534Sbillf
645186678Sdougb  # Avoid comparing the following user specified files
646186678Sdougb  for file in ${IGNORE_FILES}; do
647186678Sdougb    test -e ${file} && unlink ${file}
648186678Sdougb  done
64952400Sbillf  ;; # End of the "RERUN" test
65052400Sbillfesac
65152400Sbillf
652111901Sdougb# We really don't want to have to deal with files like login.conf.db, pwd.db,
653111901Sdougb# or spwd.db.  Instead, we want to compare the text versions, and run *_mkdb.
654111901Sdougb# Prompt the user to do so below, as needed.
65577478Sdougb#
656111905Sdougbrm -f ${TEMPROOT}/etc/*.db ${TEMPROOT}/etc/passwd
65777478Sdougb
65894196Sdougb# We only need to compare things like freebsd.cf once
65996045Sdougbfind ${TEMPROOT}/usr/obj -type f -delete 2>/dev/null
66094196Sdougb
661158149Sgordon# Delete 0 length files to make the mtree database as small as possible.
662158149Sgordonfind ${TEMPROOT} -type f -size 0 -delete 2>/dev/null
663158149Sgordon
664158149Sgordon# Build the mtree database in a temporary location.
665186678SdougbMTREENEW=`mktemp -t mergemaster.mtree`
666158149Sgordoncase "${PRE_WORLD}" in
667186678Sdougb'') mtree -ci -p ${TEMPROOT} -k size,md5digest > ${DESTDIR}${MTREENEW} 2>/dev/null
668158149Sgordon    ;;
669158149Sgordon*) # We don't want to mess with the mtree database on a pre-world run.
670158149Sgordon   ;;
671158149Sgordonesac
672158149Sgordon
67352400Sbillf# Get ready to start comparing files
67452400Sbillf
67598084Sdougb# Check umask if not specified on the command line,
67698084Sdougb# and we are not doing an autorun
67752400Sbillf#
67898084Sdougbif [ -z "${NEW_UMASK}" -a -z "${AUTO_RUN}" ]; then
67998084Sdougb  USER_UMASK=`umask`
68052400Sbillf  case "${USER_UMASK}" in
68177335Sdougb  0022|022) ;;
68252400Sbillf  *)
68352400Sbillf    echo ''
68498084Sdougb    echo " *** Your umask is currently set to ${USER_UMASK}.  By default, this script"
68598084Sdougb    echo "     installs all files with the same user, group and modes that"
686186678Sdougb    echo "     they are created with by ${SOURCEDIR}/etc/Makefile, compared to"
68798084Sdougb    echo "     a umask of 022.  This umask allows world read permission when"
68898084Sdougb    echo "     the file's default permissions have it."
68952400Sbillf    echo ''
69098084Sdougb    echo "     No world permissions can sometimes cause problems.  A umask of"
69198084Sdougb    echo "     022 will restore the default behavior, but is not mandatory."
69298084Sdougb    echo "     /etc/master.passwd is a special case.  Its file permissions"
69398084Sdougb    echo "     will be 600 (rw-------) if installed."
69497960Sdougb    echo ''
69598084Sdougb    echo -n "What umask should I use? [${USER_UMASK}] "
69698084Sdougb    read NEW_UMASK
69798084Sdougb
69898084Sdougb    NEW_UMASK="${NEW_UMASK:-$USER_UMASK}"
69952400Sbillf    ;;
70052400Sbillf  esac
70152400Sbillf  echo ''
70298084Sdougbfi
70352400Sbillf
70498084SdougbCONFIRMED_UMASK=${NEW_UMASK:-0022}
70598084Sdougb
70652400Sbillf#
707114501Sdougb# Warn users who still have old rc files
708114501Sdougb#
709179315Sbzfor file in atm devfs diskless1 diskless2 network network6 pccard \
710114523Sdougb  serial syscons sysctl alpha amd64 i386 ia64 sparc64; do
711114501Sdougb  if [ -f "${DESTDIR}/etc/rc.${file}" ]; then
712114501Sdougb    OLD_RC_PRESENT=1
713114501Sdougb    break
714114501Sdougb  fi
715114501Sdougbdone
716114501Sdougb
717114501Sdougbcase "${OLD_RC_PRESENT}" in
718114501Sdougb1)
71952400Sbillf  echo ''
720114501Sdougb  echo " *** There are elements of the old rc system in ${DESTDIR}/etc/."
72167949Sdougb  echo ''
722114501Sdougb  echo '     While these scripts will not hurt anything, they are not'
723114501Sdougb  echo '     functional on an up to date system, and can be removed.'
72467949Sdougb  echo ''
725114501Sdougb
72652400Sbillf  case "${AUTO_RUN}" in
72752400Sbillf  '')
728114501Sdougb    echo -n 'Move these files to /var/tmp/mergemaster/old_rc? [yes] '
729114501Sdougb    read MOVE_OLD_RC
73067859Sdougb
731114501Sdougb    case "${MOVE_OLD_RC}" in
732114501Sdougb    [nN]*) ;;
73352400Sbillf    *)
734114501Sdougb      mkdir -p /var/tmp/mergemaster/old_rc
735179315Sbz        for file in atm devfs diskless1 diskless2 network network6 pccard \
736114523Sdougb          serial syscons sysctl alpha amd64 i386 ia64 sparc64; do
737114501Sdougb          if [ -f "${DESTDIR}/etc/rc.${file}" ]; then
738114501Sdougb            mv ${DESTDIR}/etc/rc.${file} /var/tmp/mergemaster/old_rc/
739114501Sdougb          fi
740114501Sdougb        done
741114501Sdougb      echo '  The files have been moved'
742114501Sdougb      press_to_continue
74352400Sbillf      ;;
74452400Sbillf    esac
74552400Sbillf    ;;
74652400Sbillf  *) ;;
74752400Sbillf  esac
748114501Sdougbesac
74952400Sbillf
75098084Sdougb# Use the umask/mode information to install the files
75152400Sbillf# Create directories as needed
75252400Sbillf#
753186678Sdougbinstall_error () {
754186678Sdougb  echo "*** FATAL ERROR: Unable to install ${1} to ${2}"
755186678Sdougb  echo ''
756186678Sdougb  exit 1
757186678Sdougb}
758186678Sdougb
75978490Sdougbdo_install_and_rm () {
760114501Sdougb  case "${PRESERVE_FILES}" in
761114501Sdougb  [Yy][Ee][Ss])
762114501Sdougb    if [ -f "${3}/${2##*/}" ]; then
763114565Sdougb      mkdir -p ${PRESERVE_FILES_DIR}/${2%/*}
764114565Sdougb      cp ${3}/${2##*/} ${PRESERVE_FILES_DIR}/${2%/*}
765114501Sdougb    fi
766114501Sdougb    ;;
767114501Sdougb  esac
768114501Sdougb
769186678Sdougb  if [ ! -d "${3}/${2##*/}" ]; then
770186678Sdougb    if install -m ${1} ${2} ${3}; then
771186678Sdougb      unlink ${2}
772186678Sdougb    else
773186678Sdougb      install_error ${2} ${3}
774186678Sdougb    fi
775186678Sdougb  else
776186678Sdougb    install_error ${2} ${3}
777186678Sdougb  fi
77878490Sdougb}
77978490Sdougb
78098084Sdougb# 4095 = "obase=10;ibase=8;07777" | bc
78198084Sdougbfind_mode () {
78298084Sdougb  local OCTAL
78398084Sdougb  OCTAL=$(( ~$(echo "obase=10; ibase=8; ${CONFIRMED_UMASK}" | bc) & 4095 &
784124053Sdougb    $(echo "obase=10; ibase=8; $(stat -f "%OMp%OLp" ${1})" | bc) ))
78598084Sdougb  printf "%04o\n" ${OCTAL}
78698084Sdougb}
78798084Sdougb
78852400Sbillfmm_install () {
78952400Sbillf  local INSTALL_DIR
79052400Sbillf  INSTALL_DIR=${1#.}
79152400Sbillf  INSTALL_DIR=${INSTALL_DIR%/*}
79267949Sdougb
79352400Sbillf  case "${INSTALL_DIR}" in
79452400Sbillf  '')
79552400Sbillf    INSTALL_DIR=/
79652400Sbillf    ;;
79752400Sbillf  esac
79852400Sbillf
79967949Sdougb  if [ -n "${DESTDIR}${INSTALL_DIR}" -a ! -d "${DESTDIR}${INSTALL_DIR}" ]; then
80098084Sdougb    DIR_MODE=`find_mode "${TEMPROOT}/${INSTALL_DIR}"`
80167949Sdougb    install -d -o root -g wheel -m "${DIR_MODE}" "${DESTDIR}${INSTALL_DIR}"
80252400Sbillf  fi
80352400Sbillf
80498084Sdougb  FILE_MODE=`find_mode "${1}"`
80552400Sbillf
80652400Sbillf  if [ ! -x "${1}" ]; then
80752400Sbillf    case "${1#.}" in
80864625Sgshapiro    /etc/mail/aliases)
80952400Sbillf      NEED_NEWALIASES=yes
81052400Sbillf      ;;
81152400Sbillf    /etc/login.conf)
81252400Sbillf      NEED_CAP_MKDB=yes
81352400Sbillf      ;;
81452400Sbillf    /etc/master.passwd)
81578490Sdougb      do_install_and_rm 600 "${1}" "${DESTDIR}${INSTALL_DIR}"
81652400Sbillf      NEED_PWD_MKDB=yes
81752400Sbillf      DONT_INSTALL=yes
81852400Sbillf      ;;
81952400Sbillf    /.cshrc | /.profile)
82077335Sdougb    case "${AUTO_INSTALL}" in
82177335Sdougb    '')
82252400Sbillf      case "${LINK_EXPLAINED}" in
82352400Sbillf      '')
82467859Sdougb        echo "   *** Historically BSD derived systems have had a"
82567859Sdougb        echo "       hard link from /.cshrc and /.profile to"
82667859Sdougb        echo "       their namesakes in /root.  Please indicate"
82767859Sdougb        echo "       your preference below for bringing your"
82867859Sdougb        echo "       installed files up to date."
82952400Sbillf        echo ''
83052400Sbillf        LINK_EXPLAINED=yes
83152400Sbillf        ;;
83252400Sbillf      esac
83352400Sbillf
83452400Sbillf      echo "   Use 'd' to delete the temporary ${COMPFILE}"
83567949Sdougb      echo "   Use 'l' to delete the existing ${DESTDIR}${COMPFILE#.} and create the link"
83652400Sbillf      echo ''
83752400Sbillf      echo "   Default is to leave the temporary file to deal with by hand"
83852400Sbillf      echo ''
83967859Sdougb      echo -n "  How should I handle ${COMPFILE}? [Leave it to install later] "
84067859Sdougb      read HANDLE_LINK
84177335Sdougb      ;;
84277335Sdougb    *)  # Part of AUTO_INSTALL
84377335Sdougb      HANDLE_LINK=l
84477335Sdougb      ;;
84577335Sdougb    esac
84667859Sdougb
84752400Sbillf      case "${HANDLE_LINK}" in
84852400Sbillf      [dD]*)
84952400Sbillf        rm "${COMPFILE}"
85052400Sbillf        echo ''
85152400Sbillf        echo "   *** Deleting ${COMPFILE}"
85252400Sbillf        ;;
85352400Sbillf      [lL]*)
85452400Sbillf        echo ''
85567949Sdougb        rm -f "${DESTDIR}${COMPFILE#.}"
85667949Sdougb        if ln "${DESTDIR}/root/${COMPFILE##*/}" "${DESTDIR}${COMPFILE#.}"; then
85767949Sdougb          echo "   *** Link from ${DESTDIR}${COMPFILE#.} to ${DESTDIR}/root/${COMPFILE##*/} installed successfully"
85852400Sbillf          rm "${COMPFILE}"
85952400Sbillf        else
86067949Sdougb          echo "   *** Error linking ${DESTDIR}${COMPFILE#.} to ${DESTDIR}/root/${COMPFILE##*/}, ${COMPFILE} will remain to install by hand"
86152400Sbillf        fi
86252400Sbillf        ;;
86352400Sbillf      *)
86452400Sbillf        echo "   *** ${COMPFILE} will remain for your consideration"
86552400Sbillf        ;;
86652400Sbillf      esac
86752400Sbillf      DONT_INSTALL=yes
86852400Sbillf      ;;
86952400Sbillf    esac
87052400Sbillf
87152400Sbillf    case "${DONT_INSTALL}" in
87252400Sbillf    '')
87378490Sdougb      do_install_and_rm "${FILE_MODE}" "${1}" "${DESTDIR}${INSTALL_DIR}"
87452400Sbillf      ;;
87552400Sbillf    *)
87652400Sbillf      unset DONT_INSTALL
87752400Sbillf      ;;
87852400Sbillf    esac
87978490Sdougb  else	# File matched -x
88078490Sdougb    do_install_and_rm "${FILE_MODE}" "${1}" "${DESTDIR}${INSTALL_DIR}"
88152400Sbillf  fi
88252400Sbillf  return $?
88352400Sbillf}
88452400Sbillf
885174841Sdougbif [ ! -d "${TEMPROOT}" ]; then
886174841Sdougb	echo "*** FATAL ERROR: The temproot directory (${TEMPROOT})"
887174841Sdougb	echo '                 has disappeared!'
888174841Sdougb	echo ''
889174841Sdougb	exit 1
890174841Sdougbfi
891174841Sdougb
89267949Sdougbecho ''
89367949Sdougbecho "*** Beginning comparison"
89467949Sdougbecho ''
89552400Sbillf
896124136Sdougb# Pre-world does not populate /etc/rc.d.
897124053Sdougb# It is very possible that a previous run would have deleted files in
898124053Sdougb# ${TEMPROOT}/etc/rc.d, thus creating a lot of false positives.
899124136Sdougbif [ -z "${PRE_WORLD}" -a -z "${RERUN}" ]; then
900124053Sdougb  echo "   *** Checking ${DESTDIR}/etc/rc.d for stale files"
901124053Sdougb  echo ''
902124053Sdougb  cd "${DESTDIR}/etc/rc.d" &&
903124053Sdougb  for file in *; do
904124053Sdougb    if [ ! -e "${TEMPROOT}/etc/rc.d/${file}" ]; then
905124053Sdougb      STALE_RC_FILES="${STALE_RC_FILES} ${file}"
906124053Sdougb    fi
907124053Sdougb  done
908124053Sdougb  case "${STALE_RC_FILES}" in
909126718Sdougb  ''|' *')
910124053Sdougb    echo '   *** No stale files found'
911124053Sdougb    ;;
912124053Sdougb  *)
913124053Sdougb    echo "   *** The following files exist in ${DESTDIR}/etc/rc.d but not in"
914124053Sdougb    echo "       ${TEMPROOT}/etc/rc.d/:"
915124053Sdougb    echo ''
916124053Sdougb    echo "${STALE_RC_FILES}"
917124053Sdougb    echo ''
918124053Sdougb    echo '       The presence of stale files in this directory can cause the'
919124053Sdougb    echo '       dreaded unpredictable results, and therefore it is highly'
920124053Sdougb    echo '       recommended that you delete them.'
921124053Sdougb    case "${AUTO_RUN}" in
922124053Sdougb    '')
923124053Sdougb      echo ''
924153604Sdougb      echo -n '   *** Delete them now? [n] '
925124053Sdougb      read DELETE_STALE_RC_FILES
926124053Sdougb      case "${DELETE_STALE_RC_FILES}" in
927153604Sdougb      [yY])
928124053Sdougb        echo '      *** Deleting ... '
929124053Sdougb        rm ${STALE_RC_FILES}
930124053Sdougb        echo '                       done.'
931124053Sdougb        ;;
932153604Sdougb      *)
933153604Sdougb        echo '      *** Files will not be deleted'
934153604Sdougb        ;;
935124053Sdougb      esac
936124053Sdougb      sleep 2
937124053Sdougb      ;;
938124053Sdougb    esac
939124053Sdougb    ;;
940124053Sdougb  esac
941124053Sdougb  echo ''
942124136Sdougbfi
943124053Sdougb
94467949Sdougbcd "${TEMPROOT}"
94567949Sdougb
94667949Sdougbif [ -r "${MM_PRE_COMPARE_SCRIPT}" ]; then
94767949Sdougb  . "${MM_PRE_COMPARE_SCRIPT}"
94867949Sdougbfi
94967949Sdougb
95052400Sbillf# Using -size +0 avoids uselessly checking the empty log files created
951186678Sdougb# by ${SOURCEDIR}/etc/Makefile and the device entries in ./dev, but does
95267949Sdougb# check the scripts in ./dev, as we'd like (assuming no devfs of course).
95352400Sbillf#
95452400Sbillffor COMPFILE in `find . -type f -size +0`; do
95567949Sdougb
95667949Sdougb  # First, check to see if the file exists in DESTDIR.  If not, the
95767949Sdougb  # diff_loop function knows how to handle it.
95867949Sdougb  #
95967949Sdougb  if [ ! -e "${DESTDIR}${COMPFILE#.}" ]; then
96077325Sdougb    case "${AUTO_RUN}" in
96177325Sdougb      '')
96277325Sdougb        diff_loop
96377325Sdougb        ;;
96477325Sdougb      *)
96577325Sdougb        case "${AUTO_INSTALL}" in
96677325Sdougb        '')
96777325Sdougb          # If this is an auto run, make it official
96877325Sdougb          echo "   *** ${COMPFILE} will remain for your consideration"
96977325Sdougb          ;;
97077325Sdougb        *)
97177325Sdougb          diff_loop
97277325Sdougb          ;;
97377325Sdougb        esac
97477325Sdougb        ;;
97577325Sdougb    esac # Auto run test
97667949Sdougb    continue
97767949Sdougb  fi
97867949Sdougb
97952400Sbillf  case "${STRICT}" in
98052400Sbillf  '' | [Nn][Oo])
98152400Sbillf    # Compare CVS $Id's first so if the file hasn't been modified
98252400Sbillf    # local changes will be ignored.
98352400Sbillf    # If the files have the same $Id, delete the one in temproot so the
98452400Sbillf    # user will have less to wade through if files are left to merge by hand.
98552400Sbillf    #
98673651Sdougb    CVSID1=`grep "[$]${CVS_ID_TAG}:" ${DESTDIR}${COMPFILE#.} 2>/dev/null`
98790564Sdougb    CVSID2=`grep "[$]${CVS_ID_TAG}:" ${COMPFILE} 2>/dev/null` || CVSID2=none
98852400Sbillf
98967949Sdougb    case "${CVSID2}" in
99073651Sdougb    "${CVSID1}")
99167949Sdougb      echo " *** Temp ${COMPFILE} and installed have the same CVS Id, deleting"
99267949Sdougb      rm "${COMPFILE}"
99367949Sdougb      ;;
99467949Sdougb    esac
99552400Sbillf    ;;
99652400Sbillf  esac
99752400Sbillf
99852400Sbillf  # If the file is still here either because the $Ids are different, the
99952400Sbillf  # file doesn't have an $Id, or we're using STRICT mode; look at the diff.
100052400Sbillf  #
100152400Sbillf  if [ -f "${COMPFILE}" ]; then
100252400Sbillf
100352400Sbillf    # Do an absolute diff first to see if the files are actually different.
100452400Sbillf    # If they're not different, delete the one in temproot.
100552400Sbillf    #
1006110377Sdougb    if diff -q ${DIFF_OPTIONS} "${DESTDIR}${COMPFILE#.}" "${COMPFILE}" > \
1007110377Sdougb      /dev/null 2>&1; then
100852400Sbillf      echo " *** Temp ${COMPFILE} and installed are the same, deleting"
100952400Sbillf      rm "${COMPFILE}"
101052400Sbillf    else
101177323Sdougb      # Ok, the files are different, so show the user where they differ.
101277323Sdougb      # Use user's choice of diff methods; and user's pager if they have one.
101377323Sdougb      # Use more if not.
101467859Sdougb      # Use unified diffs by default.  Context diffs give me a headache. :)
101552400Sbillf      #
101652400Sbillf      case "${AUTO_RUN}" in
101752400Sbillf      '')
101858910Salfred        # prompt user to install/delete/merge changes
101958910Salfred        diff_loop
102052400Sbillf        ;;
102152400Sbillf      *)
102252400Sbillf        # If this is an auto run, make it official
102352400Sbillf        echo "   *** ${COMPFILE} will remain for your consideration"
102452400Sbillf        ;;
102552400Sbillf      esac # Auto run test
102652400Sbillf    fi # Yes, the files are different
102752400Sbillf  fi # Yes, the file still remains to be checked
102852400Sbillfdone # This is for the do way up there at the beginning of the comparison
102952400Sbillf
103052534Sbillfecho ''
103152400Sbillfecho "*** Comparison complete"
1032158149Sgordon
1033186678Sdougbif [ -f "${DESTDIR}${MTREENEW}" ]; then
1034158149Sgordon  echo "*** Saving mtree database for future upgrades"
1035186678Sdougb  test -e "${MTREEFILE}" && unlink ${MTREEFILE}
1036186678Sdougb  mv ${DESTDIR}${MTREENEW} ${DESTDIR}${MTREEFILE}
1037158149Sgordonfi
1038158149Sgordon
103952400Sbillfecho ''
104052400Sbillf
104152400SbillfTEST_FOR_FILES=`find ${TEMPROOT} -type f -size +0 2>/dev/null`
104252400Sbillfif [ -n "${TEST_FOR_FILES}" ]; then
104352400Sbillf  echo "*** Files that remain for you to merge by hand:"
104452400Sbillf  find "${TEMPROOT}" -type f -size +0
104577335Sdougb  echo ''
104652400Sbillffi
104752400Sbillf
104852400Sbillfcase "${AUTO_RUN}" in
104952400Sbillf'')
105067859Sdougb  echo -n "Do you wish to delete what is left of ${TEMPROOT}? [no] "
105167859Sdougb  read DEL_TEMPROOT
105267859Sdougb
105352400Sbillf  case "${DEL_TEMPROOT}" in
105452400Sbillf  [yY]*)
105599152Sdougb    if delete_temproot; then
105652400Sbillf      echo " *** ${TEMPROOT} has been deleted"
105752400Sbillf    else
105852400Sbillf      echo " *** Unable to delete ${TEMPROOT}"
105952400Sbillf    fi
106052400Sbillf    ;;
106152400Sbillf  *)
106252400Sbillf    echo " *** ${TEMPROOT} will remain"
106352400Sbillf    ;;
106452400Sbillf  esac
106552400Sbillf  ;;
106652400Sbillf*) ;;
106752400Sbillfesac
106852400Sbillf
106968153Sdougbcase "${AUTO_INSTALLED_FILES}" in
107068153Sdougb'') ;;
107168153Sdougb*)
107273651Sdougb  case "${AUTO_RUN}" in
107373651Sdougb  '')
107473651Sdougb    (
107573651Sdougb      echo ''
107677323Sdougb      echo '*** You chose the automatic install option for files that did not'
107777323Sdougb      echo '    exist on your system.  The following were installed for you:'
107873651Sdougb      echo "${AUTO_INSTALLED_FILES}"
107973651Sdougb    ) | ${PAGER}
108073651Sdougb    ;;
108173651Sdougb  *)
108268153Sdougb    echo ''
108377323Sdougb    echo '*** You chose the automatic install option for files that did not'
108477323Sdougb    echo '    exist on your system.  The following were installed for you:'
108568153Sdougb    echo "${AUTO_INSTALLED_FILES}"
108673651Sdougb    ;;
108773651Sdougb  esac
108868153Sdougb  ;;
108968153Sdougbesac
109068153Sdougb
1091158149Sgordoncase "${AUTO_UPGRADED_FILES}" in
1092158149Sgordon'') ;;
1093158149Sgordon*)
1094158149Sgordon  case "${AUTO_RUN}" in
1095158149Sgordon  '')
1096158149Sgordon    (
1097158149Sgordon      echo ''
1098158149Sgordon      echo '*** You chose the automatic upgrade option for files that you did'
1099158149Sgordon      echo '    not alter on your system.  The following were upgraded for you:'
1100158149Sgordon      echo "${AUTO_UPGRADED_FILES}"
1101158149Sgordon    ) | ${PAGER}
1102158149Sgordon    ;;
1103158149Sgordon  *)
1104158149Sgordon    echo ''
1105158149Sgordon    echo '*** You chose the automatic upgrade option for files that you did'
1106158149Sgordon    echo '    not alter on your system.  The following were upgraded for you:'
1107158149Sgordon    echo "${AUTO_UPGRADED_FILES}"
1108158149Sgordon    ;;
1109158149Sgordon  esac
1110158149Sgordon  ;;
1111158149Sgordonesac
1112158149Sgordon
111373651Sdougbrun_it_now () {
111473651Sdougb  case "${AUTO_RUN}" in
111573651Sdougb  '')
111673651Sdougb    unset YES_OR_NO
111773651Sdougb    echo ''
111877335Sdougb    echo -n '    Would you like to run it now? y or n [n] '
111973651Sdougb    read YES_OR_NO
112073651Sdougb
112173651Sdougb    case "${YES_OR_NO}" in
112273651Sdougb    y)
112377335Sdougb      echo "    Running ${1}"
112477335Sdougb      echo ''
112573651Sdougb      eval "${1}"
112673651Sdougb      ;;
112777335Sdougb    ''|n)
112877335Sdougb      echo ''
112977335Sdougb      echo "       *** Cancelled"
113077335Sdougb      echo ''
113177335Sdougb      echo "    Make sure to run ${1} yourself"
113277335Sdougb      ;;
113373651Sdougb    *)
113477335Sdougb      echo ''
113577335Sdougb      echo "       *** Sorry, I do not understand your answer (${YES_OR_NO})"
113677335Sdougb      echo ''
113777335Sdougb      echo "    Make sure to run ${1} yourself"
113873651Sdougb    esac
113973651Sdougb    ;;
114073651Sdougb  *) ;;
114173651Sdougb  esac
114273651Sdougb}
114373651Sdougb
114452400Sbillfcase "${NEED_NEWALIASES}" in
114552400Sbillf'') ;;
114652400Sbillf*)
114752400Sbillf  echo ''
114877335Sdougb  if [ -n "${DESTDIR}" ]; then
114977335Sdougb    echo "*** You installed a new aliases file into ${DESTDIR}/etc/mail, but"
115077335Sdougb    echo "    the newaliases command is limited to the directories configured"
115177335Sdougb    echo "    in sendmail.cf.  Make sure to create your aliases database by"
115277335Sdougb    echo "    hand when your sendmail configuration is done."
115377335Sdougb  else
115477335Sdougb    echo "*** You installed a new aliases file, so make sure that you run"
115577335Sdougb    echo "    '/usr/bin/newaliases' to rebuild your aliases database"
115677335Sdougb    run_it_now '/usr/bin/newaliases'
115777335Sdougb  fi
115852400Sbillf  ;;
115952400Sbillfesac
116052400Sbillf
116152400Sbillfcase "${NEED_CAP_MKDB}" in
116252400Sbillf'') ;;
116352400Sbillf*)
116452400Sbillf  echo ''
116552400Sbillf  echo "*** You installed a login.conf file, so make sure that you run"
116677335Sdougb  echo "    '/usr/bin/cap_mkdb ${DESTDIR}/etc/login.conf'"
116777335Sdougb  echo "     to rebuild your login.conf database"
116877335Sdougb  run_it_now "/usr/bin/cap_mkdb ${DESTDIR}/etc/login.conf"
116952400Sbillf  ;;
117052400Sbillfesac
117152400Sbillf
117252400Sbillfcase "${NEED_PWD_MKDB}" in
117352400Sbillf'') ;;
117452400Sbillf*)
117552400Sbillf  echo ''
117652400Sbillf  echo "*** You installed a new master.passwd file, so make sure that you run"
117777335Sdougb  if [ -n "${DESTDIR}" ]; then
117877335Sdougb    echo "    '/usr/sbin/pwd_mkdb -d ${DESTDIR}/etc -p ${DESTDIR}/etc/master.passwd'"
117977335Sdougb    echo "    to rebuild your password files"
118077335Sdougb    run_it_now "/usr/sbin/pwd_mkdb -d ${DESTDIR}/etc -p ${DESTDIR}/etc/master.passwd"
118177335Sdougb  else
118277335Sdougb    echo "    '/usr/sbin/pwd_mkdb -p /etc/master.passwd'"
118377335Sdougb    echo "     to rebuild your password files"
118477335Sdougb    run_it_now '/usr/sbin/pwd_mkdb -p /etc/master.passwd'
118577335Sdougb  fi
118652400Sbillf  ;;
118752400Sbillfesac
118852400Sbillf
118952400Sbillfecho ''
119052400Sbillf
119167949Sdougbif [ -r "${MM_EXIT_SCRIPT}" ]; then
119267949Sdougb  . "${MM_EXIT_SCRIPT}"
119367949Sdougbfi
119467949Sdougb
119591193Sdougbcase "${COMP_CONFS}" in
119691193Sdougb'') ;;
119791193Sdougb*)
119891193Sdougb  . ${DESTDIR}/etc/defaults/rc.conf
119991193Sdougb
120091193Sdougb  (echo ''
120191193Sdougb  echo "*** Comparing conf files: ${rc_conf_files}"
120291193Sdougb
120391193Sdougb  for CONF_FILE in ${rc_conf_files}; do
120491193Sdougb    if [ -r "${DESTDIR}${CONF_FILE}" ]; then
120591193Sdougb      echo ''
120691193Sdougb      echo "*** From ${DESTDIR}${CONF_FILE}"
120791193Sdougb      echo "*** From ${DESTDIR}/etc/defaults/rc.conf"
120891193Sdougb
120991193Sdougb      for RC_CONF_VAR in `grep -i ^[a-z] ${DESTDIR}${CONF_FILE} |
121091193Sdougb        cut -d '=' -f 1`; do
121191193Sdougb        echo ''
121291223Sdougb        grep -w ^${RC_CONF_VAR} ${DESTDIR}${CONF_FILE}
121391223Sdougb        grep -w ^${RC_CONF_VAR} ${DESTDIR}/etc/defaults/rc.conf ||
121491193Sdougb          echo ' * No default variable with this name'
121591193Sdougb      done
121691193Sdougb    fi
121791193Sdougb  done) | ${PAGER}
121891193Sdougb  echo ''
121991193Sdougb  ;;
122091193Sdougbesac
122191193Sdougb
122291193Sdougbcase "${PRE_WORLD}" in
122391193Sdougb'') ;;
122491193Sdougb*)
1225186678Sdougb  MAKE_CONF="${SOURCEDIR}/share/examples/etc/make.conf"
122691193Sdougb
122791193Sdougb  (echo ''
122891193Sdougb  echo '*** Comparing make variables'
122991193Sdougb  echo ''
123091193Sdougb  echo "*** From ${DESTDIR}/etc/make.conf"
123191193Sdougb  echo "*** From ${MAKE_CONF}"
123291193Sdougb
1233101348Sdougb  for MAKE_VAR in `grep -i ^[a-z] ${DESTDIR}/etc/make.conf | cut -d '=' -f 1`; do
123491193Sdougb    echo ''
123591223Sdougb    grep -w ^${MAKE_VAR} ${DESTDIR}/etc/make.conf
123691223Sdougb    grep -w ^#${MAKE_VAR} ${MAKE_CONF} ||
123791193Sdougb      echo ' * No example variable with this name'
123891193Sdougb  done) | ${PAGER}
123991193Sdougb  ;;
124091193Sdougbesac
124191193Sdougb
124252400Sbillfexit 0
124367850Sdougb
1244