mergemaster.sh revision 192230
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 192230 2009-05-16 22:22:31Z 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}"
18189992Sdougb  echo 'Usage: mergemaster [-scrvahipFCPU]'
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
264186678Sdougb# Assign the location of the mtree database
265186678Sdougb#
266186678SdougbMTREEDB=${MTREEDB:-/var/db}
267186678SdougbMTREEFILE="${MTREEDB}/mergemaster.mtree"
268186678Sdougb
26952400Sbillf# Check the command line options
27052400Sbillf#
271189992Sdougbwhile getopts ":ascrvhipCPm:t:du:w:D:A:FU" COMMAND_LINE_ARGUMENT ; do
27252400Sbillf  case "${COMMAND_LINE_ARGUMENT}" in
273155309Srwatson  A)
274186678Sdougb    ARCHSTRING='TARGET_ARCH='${OPTARG}
275155309Srwatson    ;;
276189992Sdougb  F)
277189992Sdougb    FREEBSD_ID=yes
278189992Sdougb    ;;
279158149Sgordon  U)
280158149Sgordon    AUTO_UPGRADE=yes
281158149Sgordon    ;;
28252400Sbillf  s)
28352400Sbillf    STRICT=yes
284110377Sdougb    unset DIFF_OPTIONS
28552400Sbillf    ;;
28652400Sbillf  c)
28752400Sbillf    DIFF_FLAG='-c'
28852400Sbillf    ;;
28952400Sbillf  r)
29052400Sbillf    RERUN=yes
29152400Sbillf    ;;
29252400Sbillf  v)
29352400Sbillf    case "${AUTO_RUN}" in
29452400Sbillf    '') VERBOSE=yes ;;
29552400Sbillf    esac
29652400Sbillf    ;;
29752400Sbillf  a)
29852400Sbillf    AUTO_RUN=yes
29952400Sbillf    unset VERBOSE
30052400Sbillf    ;;
30152400Sbillf  h)
30252400Sbillf    display_usage
30352400Sbillf    display_help
30452400Sbillf    exit 0
30552400Sbillf    ;;
30667949Sdougb  i)
30767949Sdougb    AUTO_INSTALL=yes
30867949Sdougb    ;;
30996045Sdougb  C)
31096045Sdougb    COMP_CONFS=yes
31196045Sdougb    ;;
312114501Sdougb  P)
313114501Sdougb    PRESERVE_FILES=yes
314114501Sdougb    ;;
31591193Sdougb  p)
31691193Sdougb    PRE_WORLD=yes
31796045Sdougb    unset COMP_CONFS
31896045Sdougb    unset AUTO_RUN
31991193Sdougb    ;;
32052400Sbillf  m)
32152400Sbillf    SOURCEDIR=${OPTARG}
32252400Sbillf    ;;
32352400Sbillf  t)
32452400Sbillf    TEMPROOT=${OPTARG}
32552400Sbillf    ;;
32652400Sbillf  d)
32752400Sbillf    TEMPROOT=${TEMPROOT}.`date +%m%d.%H.%M`
32852400Sbillf    ;;
32952400Sbillf  u)
33052400Sbillf    NEW_UMASK=${OPTARG}
33152400Sbillf    ;;
33252400Sbillf  w)
33352400Sbillf    SCREEN_WIDTH=${OPTARG}
33452400Sbillf    ;;
33567949Sdougb  D)
33667949Sdougb    DESTDIR=${OPTARG}
33767949Sdougb    ;;
33852400Sbillf  *)
33952400Sbillf    display_usage
34052400Sbillf    exit 1
34152400Sbillf    ;;
34252400Sbillf  esac
34352400Sbillfdone
34452400Sbillf
345114501Sdougb# Don't force the user to set this in the mergemaster rc file
346114501Sdougbif [ -n "${PRESERVE_FILES}" -a -z "${PRESERVE_FILES_DIR}" ]; then
347114501Sdougb  PRESERVE_FILES_DIR=/var/tmp/mergemaster/preserved-files-`date +%y%m%d-%H%M%S`
348114501Sdougbfi
349114501Sdougb
350186678Sdougb# Check for the mtree database in DESTDIR
351186678Sdougbcase "${AUTO_UPGRADE}" in
352186678Sdougb'') ;;	# If the option is not set no need to run the test or warn the user
353186678Sdougb*)
354192230Sdougb  if [ ! -s "${DESTDIR}${MTREEFILE}" ]; then
355186678Sdougb    echo ''
356186678Sdougb    echo "*** Unable to find mtree database. Skipping auto-upgrade."
357186678Sdougb    echo ''
358186678Sdougb    press_to_continue
359186678Sdougb    unset AUTO_UPGRADE
360186678Sdougb  fi
361186678Sdougb  ;;
362186678Sdougbesac
363186678Sdougb
364186689Sdougbif [ -e "${DESTDIR}/etc/fstab" ]; then
365186689Sdougb  if grep -q nodev ${DESTDIR}/etc/fstab; then
366186689Sdougb    echo ''
367186689Sdougb    echo "*** You have the deprecated 'nodev' option in ${DESTDIR}/etc/fstab."
368186689Sdougb    echo "    This can prevent the filesystem from being mounted on reboot."
369186689Sdougb    echo "    Please update your fstab before continuing."
370186689Sdougb    echo "    See fstab(5) for more information."
371186689Sdougb    echo ''
372186689Sdougb    exit 1
373186689Sdougb  fi
374158149Sgordonfi
375158149Sgordon
37652400Sbillfecho ''
37752400Sbillf
37852400Sbillf# If the user has a pager defined, make sure we can run it
37952400Sbillf#
38052400Sbillfcase "${DONT_CHECK_PAGER}" in
38152400Sbillf'')
382186695Sdougbcheck_pager () {
383186695Sdougb  while ! type "${PAGER%% *}" >/dev/null; do
38452400Sbillf    echo " *** Your PAGER environment variable specifies '${PAGER}', but"
38564467Sbrian    echo "     due to the limited PATH that I use for security reasons,"
38667859Sdougb    echo "     I cannot execute it.  So, what would you like to do?"
38752400Sbillf    echo ''
38852400Sbillf    echo "  Use 'e' to exit mergemaster and fix your PAGER variable"
38964467Sbrian    if [ -x /usr/bin/less -o -x /usr/local/bin/less ]; then
39064467Sbrian    echo "  Use 'l' to set PAGER to 'less' for this run"
39152400Sbillf    fi
39252400Sbillf    echo "  Use 'm' to use plain old 'more' as your PAGER for this run"
39352400Sbillf    echo ''
39452400Sbillf    echo "  Default is to use plain old 'more' "
39552400Sbillf    echo ''
39667859Sdougb    echo -n "What should I do? [Use 'more'] "
39767859Sdougb    read FIXPAGER
39867859Sdougb
39952400Sbillf    case "${FIXPAGER}" in
40058910Salfred    [eE])
40152400Sbillf       exit 0
40252400Sbillf       ;;
40358910Salfred    [lL])
40464467Sbrian       if [ -x /usr/bin/less ]; then
40564467Sbrian         PAGER=/usr/bin/less
40664467Sbrian       elif [ -x /usr/local/bin/less ]; then
40758910Salfred         PAGER=/usr/local/bin/less
40864467Sbrian       else
40964467Sbrian         echo ''
41064467Sbrian         echo " *** Fatal Error:"
41164467Sbrian         echo "     You asked to use 'less' as your pager, but I can't"
41264467Sbrian         echo "     find it in /usr/bin or /usr/local/bin"
41364467Sbrian         exit 1
41458910Salfred       fi
41552400Sbillf       ;;
41660420Sbsd    [mM]|'')
41752400Sbillf       PAGER=more
41852400Sbillf       ;;
41958910Salfred    *)
42058910Salfred       echo ''
42158910Salfred       echo "invalid choice: ${FIXPAGER}"
42252400Sbillf    esac
42352400Sbillf    echo ''
42458910Salfred  done
425186695Sdougb}
426186695Sdougb  if [ -n "${PAGER}" ]; then
427186695Sdougb    check_pager
428186695Sdougb  fi
42952400Sbillf  ;;
43052400Sbillfesac
43152400Sbillf
43252400Sbillf# If user has a pager defined, or got assigned one above, use it.
43352400Sbillf# If not, use more.
43452400Sbillf#
43552400SbillfPAGER=${PAGER:-more}
43652400Sbillf
43752400Sbillfif [ -n "${VERBOSE}" -a ! "${PAGER}" = "more" ]; then
43852400Sbillf  echo " *** You have ${PAGER} defined as your pager so we will use that"
43952400Sbillf  echo ''
44052400Sbillf  sleep 3
44152400Sbillffi
44252400Sbillf
44352400Sbillf# Assign the diff flag once so we will not have to keep testing it
44452400Sbillf#
44552400SbillfDIFF_FLAG=${DIFF_FLAG:--u}
44652400Sbillf
44752400Sbillf# Assign the source directory
44852400Sbillf#
449186678SdougbSOURCEDIR=${SOURCEDIR:-/usr/src}
450186678Sdougbif [ ! -f ${SOURCEDIR}/Makefile.inc1 -a \
451186678Sdougb   -f ${SOURCEDIR}/../Makefile.inc1 ]; then
452186678Sdougb  echo " *** The source directory you specified (${SOURCEDIR})"
453186678Sdougb  echo "     will be reset to ${SOURCEDIR}/.."
454186678Sdougb  echo ''
455186678Sdougb  sleep 3
456186678Sdougb  SOURCEDIR=${SOURCEDIR}/..
457186678Sdougbfi
45852400Sbillf
459186678Sdougb# Setup make to use system files from SOURCEDIR
460186695SdougbMM_MAKE="make ${ARCHSTRING} -m ${SOURCEDIR}/share/mk"
461186678Sdougb
462158149Sgordon# Check DESTDIR against the mergemaster mtree database to see what
463158149Sgordon# files the user changed from the reference files.
464158149Sgordon#
465192230Sdougbif [ -n "${AUTO_UPGRADE}" -a -s "${DESTDIR}${MTREEFILE}" ]; then
466192230Sdougb	CHANGED=:
467192219Sdougb	for file in `mtree -eqL -f ${DESTDIR}${MTREEFILE} -p ${DESTDIR}/ \
468158149Sgordon		2>/dev/null | awk '($2 == "changed") {print $1}'`; do
469158149Sgordon		if [ -f "${DESTDIR}/$file" ]; then
470192230Sdougb			CHANGED="${CHANGED}${DESTDIR}/${file}:"
471158149Sgordon		fi
472158149Sgordon	done
473192230Sdougb	[ "$CHANGED" = ':' ] && unset CHANGED
474158149Sgordonfi
475158149Sgordon
47696045Sdougb# Check the width of the user's terminal
47796045Sdougb#
47896045Sdougbif [ -t 0 ]; then
479110377Sdougb  w=`tput columns`
48096045Sdougb  case "${w}" in
48196045Sdougb  0|'') ;; # No-op, since the input is not valid
48296045Sdougb  *)
48396045Sdougb    case "${SCREEN_WIDTH}" in
48496045Sdougb    '') SCREEN_WIDTH="${w}" ;;
48596045Sdougb    "${w}") ;; # No-op, since they are the same
48696045Sdougb    *)
48796045Sdougb      echo -n "*** You entered ${SCREEN_WIDTH} as your screen width, but stty "
48896045Sdougb      echo "thinks it is ${w}."
48996045Sdougb      echo ''
49096045Sdougb      echo -n "What would you like to use? [${w}] "
49196045Sdougb      read SCREEN_WIDTH
49297380Sdougb      case "${SCREEN_WIDTH}" in
49397380Sdougb      '') SCREEN_WIDTH="${w}" ;;
49497380Sdougb      esac
49596045Sdougb      ;;
49696045Sdougb    esac
49796045Sdougb  esac
49896045Sdougbfi
49996045Sdougb
50073651Sdougb# Define what CVS $Id tag to look for to aid portability.
50173651Sdougb#
50273651SdougbCVS_ID_TAG=FreeBSD
50373651Sdougb
50499152Sdougbdelete_temproot () {
505101362Sdougb  rm -rf "${TEMPROOT}" 2>/dev/null
506101362Sdougb  chflags -R 0 "${TEMPROOT}" 2>/dev/null
507101362Sdougb  rm -rf "${TEMPROOT}" || exit 1
50899152Sdougb}
50999152Sdougb
51052400Sbillfcase "${RERUN}" in
51152400Sbillf'')
51252400Sbillf  # Set up the loop to test for the existence of the
51352400Sbillf  # temp root directory.
51452400Sbillf  #
51552400Sbillf  TEST_TEMP_ROOT=yes
51652400Sbillf  while [ "${TEST_TEMP_ROOT}" = "yes" ]; do
51752400Sbillf    if [ -d "${TEMPROOT}" ]; then
51852400Sbillf      echo "*** The directory specified for the temporary root environment,"
51967859Sdougb      echo "    ${TEMPROOT}, exists.  This can be a security risk if untrusted"
52052400Sbillf      echo "    users have access to the system."
52152400Sbillf      echo ''
52252400Sbillf      case "${AUTO_RUN}" in
52352400Sbillf      '')
52452400Sbillf        echo "  Use 'd' to delete the old ${TEMPROOT} and continue"
52552400Sbillf        echo "  Use 't' to select a new temporary root directory"
52652400Sbillf        echo "  Use 'e' to exit mergemaster"
52752400Sbillf        echo ''
52852400Sbillf        echo "  Default is to use ${TEMPROOT} as is"
52952400Sbillf        echo ''
53067859Sdougb        echo -n "How should I deal with this? [Use the existing ${TEMPROOT}] "
53167859Sdougb        read DELORNOT
53267859Sdougb
53367859Sdougb        case "${DELORNOT}" in
53467859Sdougb        [dD])
53567859Sdougb          echo ''
53667859Sdougb          echo "   *** Deleting the old ${TEMPROOT}"
53767859Sdougb          echo ''
53899152Sdougb          delete_temproot || exit 1
53967859Sdougb          unset TEST_TEMP_ROOT
54052400Sbillf          ;;
54167859Sdougb        [tT])
54267859Sdougb          echo "   *** Enter new directory name for temporary root environment"
54367859Sdougb          read TEMPROOT
54467859Sdougb          ;;
54567859Sdougb        [eE])
54667859Sdougb          exit 0
54767859Sdougb          ;;
54867859Sdougb        '')
54967859Sdougb          echo ''
55067859Sdougb          echo "   *** Leaving ${TEMPROOT} intact"
55167859Sdougb          echo ''
55267859Sdougb          unset TEST_TEMP_ROOT
55367859Sdougb          ;;
55467859Sdougb        *)
55567859Sdougb          echo ''
55667859Sdougb          echo "invalid choice: ${DELORNOT}"
55767859Sdougb          echo ''
55867859Sdougb          ;;
55967859Sdougb        esac
56067859Sdougb        ;;
56152400Sbillf      *)
56277323Sdougb        # If this is an auto-run, try a hopefully safe alternative then
56377323Sdougb        # re-test anyway.
56452400Sbillf        TEMPROOT=/var/tmp/temproot.`date +%m%d.%H.%M.%S`
56552400Sbillf        ;;
56652400Sbillf      esac
56752400Sbillf    else
56852400Sbillf      unset TEST_TEMP_ROOT
56952400Sbillf    fi
57052400Sbillf  done
57152400Sbillf
57252400Sbillf  echo "*** Creating the temporary root environment in ${TEMPROOT}"
57352400Sbillf
57452400Sbillf  if mkdir -p "${TEMPROOT}"; then
57552400Sbillf    echo " *** ${TEMPROOT} ready for use"
57652400Sbillf  fi
57752400Sbillf
57852400Sbillf  if [ ! -d "${TEMPROOT}" ]; then
57952400Sbillf    echo ''
58052400Sbillf    echo "  *** FATAL ERROR: Cannot create ${TEMPROOT}"
58152400Sbillf    echo ''
58252400Sbillf    exit 1
58352400Sbillf  fi
58452400Sbillf
58552400Sbillf  echo " *** Creating and populating directory structure in ${TEMPROOT}"
58652400Sbillf  echo ''
58752400Sbillf
58852400Sbillf  case "${VERBOSE}" in
58952400Sbillf  '') ;;
59052400Sbillf  *)
59197960Sdougb    press_to_continue
59252400Sbillf    ;;
59352400Sbillf  esac
59452400Sbillf
59591193Sdougb  case "${PRE_WORLD}" in
59691193Sdougb  '')
59791193Sdougb    { cd ${SOURCEDIR} &&
59891193Sdougb      case "${DESTDIR}" in
59991193Sdougb      '') ;;
60091193Sdougb      *)
601186695Sdougb        ${MM_MAKE} DESTDIR=${DESTDIR} distrib-dirs
60291193Sdougb        ;;
60391193Sdougb      esac
604186749Sdougb      od=${TEMPROOT}/usr/obj
605186695Sdougb      ${MM_MAKE} DESTDIR=${TEMPROOT} distrib-dirs &&
606186749Sdougb      MAKEOBJDIRPREFIX=$od ${MM_MAKE} _obj SUBDIR_OVERRIDE=etc &&
607186749Sdougb      MAKEOBJDIRPREFIX=$od ${MM_MAKE} everything SUBDIR_OVERRIDE=etc &&
608186749Sdougb      MAKEOBJDIRPREFIX=$od ${MM_MAKE} DESTDIR=${TEMPROOT} distribution;} ||
60991193Sdougb    { echo '';
61091193Sdougb     echo "  *** FATAL ERROR: Cannot 'cd' to ${SOURCEDIR} and install files to";
61191193Sdougb      echo "      the temproot environment";
61291193Sdougb      echo '';
61391193Sdougb      exit 1;}
61491193Sdougb    ;;
61591193Sdougb  *)
61691193Sdougb    # Only set up files that are crucial to {build|install}world
61791193Sdougb    { mkdir -p ${TEMPROOT}/etc &&
618186678Sdougb      cp -p ${SOURCEDIR}/etc/master.passwd ${TEMPROOT}/etc &&
619186678Sdougb      cp -p ${SOURCEDIR}/etc/group ${TEMPROOT}/etc;} ||
62091193Sdougb    { echo '';
62191193Sdougb      echo '  *** FATAL ERROR: Cannot copy files to the temproot environment';
62291193Sdougb      echo '';
62391193Sdougb      exit 1;}
62491193Sdougb    ;;
62591193Sdougb  esac
62652400Sbillf
62777323Sdougb  # Doing the inventory and removing files that we don't want to compare only
62877323Sdougb  # makes sense if we are not doing a rerun, since we have no way of knowing
62977323Sdougb  # what happened to the files during previous incarnations.
63067949Sdougb  case "${VERBOSE}" in
63167949Sdougb  '') ;;
63267949Sdougb  *)
63367949Sdougb    echo ''
63467949Sdougb    echo ' *** The following files exist only in the installed version of'
63567949Sdougb    echo "     ${DESTDIR}/etc.  In the vast majority of cases these files"
63667949Sdougb    echo '     are necessary parts of the system and should not be deleted.'
63767949Sdougb    echo '     However because these files are not updated by this process you'
63867949Sdougb    echo '     might want to verify their status before rebooting your system.'
63967949Sdougb    echo ''
64097960Sdougb    press_to_continue
641101348Sdougb    diff -qr ${DESTDIR}/etc ${TEMPROOT}/etc | grep "^Only in ${DESTDIR}/etc" | ${PAGER}
64267949Sdougb    echo ''
64397960Sdougb    press_to_continue
64467949Sdougb    ;;
64567949Sdougb  esac
64667949Sdougb
64752534Sbillf  # Avoid comparing the motd if the user specifies it in .mergemasterrc
648186678Sdougb  # Compatibility shim to be removed in FreeBSD 9.x
64952534Sbillf  case "${IGNORE_MOTD}" in
65052534Sbillf  '') ;;
651186678Sdougb  *) IGNORE_FILES="${IGNORE_FILES} /etc/motd"
652186678Sdougb     echo ''
653186678Sdougb     echo "*** You have the IGNORE_MOTD option set in your mergemaster rc file."
654186678Sdougb     echo "    This option is deprecated in favor of the IGNORE_FILES option."
655186678Sdougb     echo "    Please update your rc file accordingly."
656186678Sdougb     echo ''
657186678Sdougb     press_to_continue
65852534Sbillf     ;;
65952534Sbillf  esac
66052534Sbillf
661186678Sdougb  # Avoid comparing the following user specified files
662186678Sdougb  for file in ${IGNORE_FILES}; do
663186688Sdougb    test -e ${TEMPROOT}/${file} && unlink ${TEMPROOT}/${file}
664186678Sdougb  done
66552400Sbillf  ;; # End of the "RERUN" test
66652400Sbillfesac
66752400Sbillf
668111901Sdougb# We really don't want to have to deal with files like login.conf.db, pwd.db,
669111901Sdougb# or spwd.db.  Instead, we want to compare the text versions, and run *_mkdb.
670111901Sdougb# Prompt the user to do so below, as needed.
67177478Sdougb#
672111905Sdougbrm -f ${TEMPROOT}/etc/*.db ${TEMPROOT}/etc/passwd
67377478Sdougb
67494196Sdougb# We only need to compare things like freebsd.cf once
67596045Sdougbfind ${TEMPROOT}/usr/obj -type f -delete 2>/dev/null
67694196Sdougb
677158149Sgordon# Delete 0 length files to make the mtree database as small as possible.
678158149Sgordonfind ${TEMPROOT} -type f -size 0 -delete 2>/dev/null
679158149Sgordon
680158149Sgordon# Build the mtree database in a temporary location.
681186678SdougbMTREENEW=`mktemp -t mergemaster.mtree`
682158149Sgordoncase "${PRE_WORLD}" in
683189761Sdougb'') mtree -ci -p ${TEMPROOT} -k size,md5digest > ${MTREENEW} 2>/dev/null
684158149Sgordon    ;;
685158149Sgordon*) # We don't want to mess with the mtree database on a pre-world run.
686158149Sgordon   ;;
687158149Sgordonesac
688158149Sgordon
68952400Sbillf# Get ready to start comparing files
69052400Sbillf
69198084Sdougb# Check umask if not specified on the command line,
69298084Sdougb# and we are not doing an autorun
69352400Sbillf#
69498084Sdougbif [ -z "${NEW_UMASK}" -a -z "${AUTO_RUN}" ]; then
69598084Sdougb  USER_UMASK=`umask`
69652400Sbillf  case "${USER_UMASK}" in
69777335Sdougb  0022|022) ;;
69852400Sbillf  *)
69952400Sbillf    echo ''
70098084Sdougb    echo " *** Your umask is currently set to ${USER_UMASK}.  By default, this script"
70198084Sdougb    echo "     installs all files with the same user, group and modes that"
702186678Sdougb    echo "     they are created with by ${SOURCEDIR}/etc/Makefile, compared to"
70398084Sdougb    echo "     a umask of 022.  This umask allows world read permission when"
70498084Sdougb    echo "     the file's default permissions have it."
70552400Sbillf    echo ''
70698084Sdougb    echo "     No world permissions can sometimes cause problems.  A umask of"
70798084Sdougb    echo "     022 will restore the default behavior, but is not mandatory."
70898084Sdougb    echo "     /etc/master.passwd is a special case.  Its file permissions"
70998084Sdougb    echo "     will be 600 (rw-------) if installed."
71097960Sdougb    echo ''
71198084Sdougb    echo -n "What umask should I use? [${USER_UMASK}] "
71298084Sdougb    read NEW_UMASK
71398084Sdougb
71498084Sdougb    NEW_UMASK="${NEW_UMASK:-$USER_UMASK}"
71552400Sbillf    ;;
71652400Sbillf  esac
71752400Sbillf  echo ''
71898084Sdougbfi
71952400Sbillf
72098084SdougbCONFIRMED_UMASK=${NEW_UMASK:-0022}
72198084Sdougb
72252400Sbillf#
723114501Sdougb# Warn users who still have old rc files
724114501Sdougb#
725179315Sbzfor file in atm devfs diskless1 diskless2 network network6 pccard \
726114523Sdougb  serial syscons sysctl alpha amd64 i386 ia64 sparc64; do
727114501Sdougb  if [ -f "${DESTDIR}/etc/rc.${file}" ]; then
728114501Sdougb    OLD_RC_PRESENT=1
729114501Sdougb    break
730114501Sdougb  fi
731114501Sdougbdone
732114501Sdougb
733114501Sdougbcase "${OLD_RC_PRESENT}" in
734114501Sdougb1)
73552400Sbillf  echo ''
736114501Sdougb  echo " *** There are elements of the old rc system in ${DESTDIR}/etc/."
73767949Sdougb  echo ''
738114501Sdougb  echo '     While these scripts will not hurt anything, they are not'
739114501Sdougb  echo '     functional on an up to date system, and can be removed.'
74067949Sdougb  echo ''
741114501Sdougb
74252400Sbillf  case "${AUTO_RUN}" in
74352400Sbillf  '')
744114501Sdougb    echo -n 'Move these files to /var/tmp/mergemaster/old_rc? [yes] '
745114501Sdougb    read MOVE_OLD_RC
74667859Sdougb
747114501Sdougb    case "${MOVE_OLD_RC}" in
748114501Sdougb    [nN]*) ;;
74952400Sbillf    *)
750114501Sdougb      mkdir -p /var/tmp/mergemaster/old_rc
751179315Sbz        for file in atm devfs diskless1 diskless2 network network6 pccard \
752114523Sdougb          serial syscons sysctl alpha amd64 i386 ia64 sparc64; do
753114501Sdougb          if [ -f "${DESTDIR}/etc/rc.${file}" ]; then
754114501Sdougb            mv ${DESTDIR}/etc/rc.${file} /var/tmp/mergemaster/old_rc/
755114501Sdougb          fi
756114501Sdougb        done
757114501Sdougb      echo '  The files have been moved'
758114501Sdougb      press_to_continue
75952400Sbillf      ;;
76052400Sbillf    esac
76152400Sbillf    ;;
76252400Sbillf  *) ;;
76352400Sbillf  esac
764114501Sdougbesac
76552400Sbillf
76698084Sdougb# Use the umask/mode information to install the files
76752400Sbillf# Create directories as needed
76852400Sbillf#
769186678Sdougbinstall_error () {
770186678Sdougb  echo "*** FATAL ERROR: Unable to install ${1} to ${2}"
771186678Sdougb  echo ''
772186678Sdougb  exit 1
773186678Sdougb}
774186678Sdougb
77578490Sdougbdo_install_and_rm () {
776114501Sdougb  case "${PRESERVE_FILES}" in
777114501Sdougb  [Yy][Ee][Ss])
778114501Sdougb    if [ -f "${3}/${2##*/}" ]; then
779114565Sdougb      mkdir -p ${PRESERVE_FILES_DIR}/${2%/*}
780114565Sdougb      cp ${3}/${2##*/} ${PRESERVE_FILES_DIR}/${2%/*}
781114501Sdougb    fi
782114501Sdougb    ;;
783114501Sdougb  esac
784114501Sdougb
785186678Sdougb  if [ ! -d "${3}/${2##*/}" ]; then
786186678Sdougb    if install -m ${1} ${2} ${3}; then
787186678Sdougb      unlink ${2}
788186678Sdougb    else
789186678Sdougb      install_error ${2} ${3}
790186678Sdougb    fi
791186678Sdougb  else
792186678Sdougb    install_error ${2} ${3}
793186678Sdougb  fi
79478490Sdougb}
79578490Sdougb
79698084Sdougb# 4095 = "obase=10;ibase=8;07777" | bc
79798084Sdougbfind_mode () {
79898084Sdougb  local OCTAL
79998084Sdougb  OCTAL=$(( ~$(echo "obase=10; ibase=8; ${CONFIRMED_UMASK}" | bc) & 4095 &
800124053Sdougb    $(echo "obase=10; ibase=8; $(stat -f "%OMp%OLp" ${1})" | bc) ))
80198084Sdougb  printf "%04o\n" ${OCTAL}
80298084Sdougb}
80398084Sdougb
80452400Sbillfmm_install () {
80552400Sbillf  local INSTALL_DIR
80652400Sbillf  INSTALL_DIR=${1#.}
80752400Sbillf  INSTALL_DIR=${INSTALL_DIR%/*}
80867949Sdougb
80952400Sbillf  case "${INSTALL_DIR}" in
81052400Sbillf  '')
81152400Sbillf    INSTALL_DIR=/
81252400Sbillf    ;;
81352400Sbillf  esac
81452400Sbillf
81567949Sdougb  if [ -n "${DESTDIR}${INSTALL_DIR}" -a ! -d "${DESTDIR}${INSTALL_DIR}" ]; then
81698084Sdougb    DIR_MODE=`find_mode "${TEMPROOT}/${INSTALL_DIR}"`
81767949Sdougb    install -d -o root -g wheel -m "${DIR_MODE}" "${DESTDIR}${INSTALL_DIR}"
81852400Sbillf  fi
81952400Sbillf
82098084Sdougb  FILE_MODE=`find_mode "${1}"`
82152400Sbillf
82252400Sbillf  if [ ! -x "${1}" ]; then
82352400Sbillf    case "${1#.}" in
82464625Sgshapiro    /etc/mail/aliases)
82552400Sbillf      NEED_NEWALIASES=yes
82652400Sbillf      ;;
82752400Sbillf    /etc/login.conf)
82852400Sbillf      NEED_CAP_MKDB=yes
82952400Sbillf      ;;
83052400Sbillf    /etc/master.passwd)
83178490Sdougb      do_install_and_rm 600 "${1}" "${DESTDIR}${INSTALL_DIR}"
83252400Sbillf      NEED_PWD_MKDB=yes
83352400Sbillf      DONT_INSTALL=yes
83452400Sbillf      ;;
83552400Sbillf    /.cshrc | /.profile)
83677335Sdougb    case "${AUTO_INSTALL}" in
83777335Sdougb    '')
83852400Sbillf      case "${LINK_EXPLAINED}" in
83952400Sbillf      '')
84067859Sdougb        echo "   *** Historically BSD derived systems have had a"
84167859Sdougb        echo "       hard link from /.cshrc and /.profile to"
84267859Sdougb        echo "       their namesakes in /root.  Please indicate"
84367859Sdougb        echo "       your preference below for bringing your"
84467859Sdougb        echo "       installed files up to date."
84552400Sbillf        echo ''
84652400Sbillf        LINK_EXPLAINED=yes
84752400Sbillf        ;;
84852400Sbillf      esac
84952400Sbillf
85052400Sbillf      echo "   Use 'd' to delete the temporary ${COMPFILE}"
85167949Sdougb      echo "   Use 'l' to delete the existing ${DESTDIR}${COMPFILE#.} and create the link"
85252400Sbillf      echo ''
85352400Sbillf      echo "   Default is to leave the temporary file to deal with by hand"
85452400Sbillf      echo ''
85567859Sdougb      echo -n "  How should I handle ${COMPFILE}? [Leave it to install later] "
85667859Sdougb      read HANDLE_LINK
85777335Sdougb      ;;
85877335Sdougb    *)  # Part of AUTO_INSTALL
85977335Sdougb      HANDLE_LINK=l
86077335Sdougb      ;;
86177335Sdougb    esac
86267859Sdougb
86352400Sbillf      case "${HANDLE_LINK}" in
86452400Sbillf      [dD]*)
86552400Sbillf        rm "${COMPFILE}"
86652400Sbillf        echo ''
86752400Sbillf        echo "   *** Deleting ${COMPFILE}"
86852400Sbillf        ;;
86952400Sbillf      [lL]*)
87052400Sbillf        echo ''
87167949Sdougb        rm -f "${DESTDIR}${COMPFILE#.}"
87267949Sdougb        if ln "${DESTDIR}/root/${COMPFILE##*/}" "${DESTDIR}${COMPFILE#.}"; then
87367949Sdougb          echo "   *** Link from ${DESTDIR}${COMPFILE#.} to ${DESTDIR}/root/${COMPFILE##*/} installed successfully"
87452400Sbillf          rm "${COMPFILE}"
87552400Sbillf        else
87667949Sdougb          echo "   *** Error linking ${DESTDIR}${COMPFILE#.} to ${DESTDIR}/root/${COMPFILE##*/}, ${COMPFILE} will remain to install by hand"
87752400Sbillf        fi
87852400Sbillf        ;;
87952400Sbillf      *)
88052400Sbillf        echo "   *** ${COMPFILE} will remain for your consideration"
88152400Sbillf        ;;
88252400Sbillf      esac
88352400Sbillf      DONT_INSTALL=yes
88452400Sbillf      ;;
88552400Sbillf    esac
88652400Sbillf
88752400Sbillf    case "${DONT_INSTALL}" in
88852400Sbillf    '')
88978490Sdougb      do_install_and_rm "${FILE_MODE}" "${1}" "${DESTDIR}${INSTALL_DIR}"
89052400Sbillf      ;;
89152400Sbillf    *)
89252400Sbillf      unset DONT_INSTALL
89352400Sbillf      ;;
89452400Sbillf    esac
89578490Sdougb  else	# File matched -x
89678490Sdougb    do_install_and_rm "${FILE_MODE}" "${1}" "${DESTDIR}${INSTALL_DIR}"
89752400Sbillf  fi
89852400Sbillf  return $?
89952400Sbillf}
90052400Sbillf
901174841Sdougbif [ ! -d "${TEMPROOT}" ]; then
902174841Sdougb	echo "*** FATAL ERROR: The temproot directory (${TEMPROOT})"
903174841Sdougb	echo '                 has disappeared!'
904174841Sdougb	echo ''
905174841Sdougb	exit 1
906174841Sdougbfi
907174841Sdougb
90867949Sdougbecho ''
90967949Sdougbecho "*** Beginning comparison"
91067949Sdougbecho ''
91152400Sbillf
912124136Sdougb# Pre-world does not populate /etc/rc.d.
913124053Sdougb# It is very possible that a previous run would have deleted files in
914124053Sdougb# ${TEMPROOT}/etc/rc.d, thus creating a lot of false positives.
915124136Sdougbif [ -z "${PRE_WORLD}" -a -z "${RERUN}" ]; then
916124053Sdougb  echo "   *** Checking ${DESTDIR}/etc/rc.d for stale files"
917124053Sdougb  echo ''
918124053Sdougb  cd "${DESTDIR}/etc/rc.d" &&
919124053Sdougb  for file in *; do
920124053Sdougb    if [ ! -e "${TEMPROOT}/etc/rc.d/${file}" ]; then
921124053Sdougb      STALE_RC_FILES="${STALE_RC_FILES} ${file}"
922124053Sdougb    fi
923124053Sdougb  done
924124053Sdougb  case "${STALE_RC_FILES}" in
925126718Sdougb  ''|' *')
926124053Sdougb    echo '   *** No stale files found'
927124053Sdougb    ;;
928124053Sdougb  *)
929124053Sdougb    echo "   *** The following files exist in ${DESTDIR}/etc/rc.d but not in"
930124053Sdougb    echo "       ${TEMPROOT}/etc/rc.d/:"
931124053Sdougb    echo ''
932124053Sdougb    echo "${STALE_RC_FILES}"
933124053Sdougb    echo ''
934124053Sdougb    echo '       The presence of stale files in this directory can cause the'
935124053Sdougb    echo '       dreaded unpredictable results, and therefore it is highly'
936124053Sdougb    echo '       recommended that you delete them.'
937124053Sdougb    case "${AUTO_RUN}" in
938124053Sdougb    '')
939124053Sdougb      echo ''
940153604Sdougb      echo -n '   *** Delete them now? [n] '
941124053Sdougb      read DELETE_STALE_RC_FILES
942124053Sdougb      case "${DELETE_STALE_RC_FILES}" in
943153604Sdougb      [yY])
944124053Sdougb        echo '      *** Deleting ... '
945124053Sdougb        rm ${STALE_RC_FILES}
946124053Sdougb        echo '                       done.'
947124053Sdougb        ;;
948153604Sdougb      *)
949153604Sdougb        echo '      *** Files will not be deleted'
950153604Sdougb        ;;
951124053Sdougb      esac
952124053Sdougb      sleep 2
953124053Sdougb      ;;
954124053Sdougb    esac
955124053Sdougb    ;;
956124053Sdougb  esac
957124053Sdougb  echo ''
958124136Sdougbfi
959124053Sdougb
96067949Sdougbcd "${TEMPROOT}"
96167949Sdougb
96267949Sdougbif [ -r "${MM_PRE_COMPARE_SCRIPT}" ]; then
96367949Sdougb  . "${MM_PRE_COMPARE_SCRIPT}"
96467949Sdougbfi
96567949Sdougb
96652400Sbillf# Using -size +0 avoids uselessly checking the empty log files created
967186678Sdougb# by ${SOURCEDIR}/etc/Makefile and the device entries in ./dev, but does
96867949Sdougb# check the scripts in ./dev, as we'd like (assuming no devfs of course).
96952400Sbillf#
97052400Sbillffor COMPFILE in `find . -type f -size +0`; do
97167949Sdougb
97267949Sdougb  # First, check to see if the file exists in DESTDIR.  If not, the
97367949Sdougb  # diff_loop function knows how to handle it.
97467949Sdougb  #
97567949Sdougb  if [ ! -e "${DESTDIR}${COMPFILE#.}" ]; then
97677325Sdougb    case "${AUTO_RUN}" in
97777325Sdougb      '')
97877325Sdougb        diff_loop
97977325Sdougb        ;;
98077325Sdougb      *)
98177325Sdougb        case "${AUTO_INSTALL}" in
98277325Sdougb        '')
98377325Sdougb          # If this is an auto run, make it official
98477325Sdougb          echo "   *** ${COMPFILE} will remain for your consideration"
98577325Sdougb          ;;
98677325Sdougb        *)
98777325Sdougb          diff_loop
98877325Sdougb          ;;
98977325Sdougb        esac
99077325Sdougb        ;;
99177325Sdougb    esac # Auto run test
99267949Sdougb    continue
99367949Sdougb  fi
99467949Sdougb
99552400Sbillf  case "${STRICT}" in
99652400Sbillf  '' | [Nn][Oo])
99752400Sbillf    # Compare CVS $Id's first so if the file hasn't been modified
99852400Sbillf    # local changes will be ignored.
99952400Sbillf    # If the files have the same $Id, delete the one in temproot so the
100052400Sbillf    # user will have less to wade through if files are left to merge by hand.
100152400Sbillf    #
100273651Sdougb    CVSID1=`grep "[$]${CVS_ID_TAG}:" ${DESTDIR}${COMPFILE#.} 2>/dev/null`
100390564Sdougb    CVSID2=`grep "[$]${CVS_ID_TAG}:" ${COMPFILE} 2>/dev/null` || CVSID2=none
100452400Sbillf
100567949Sdougb    case "${CVSID2}" in
100673651Sdougb    "${CVSID1}")
100767949Sdougb      echo " *** Temp ${COMPFILE} and installed have the same CVS Id, deleting"
100867949Sdougb      rm "${COMPFILE}"
100967949Sdougb      ;;
101067949Sdougb    esac
101152400Sbillf    ;;
101252400Sbillf  esac
101352400Sbillf
101452400Sbillf  # If the file is still here either because the $Ids are different, the
101552400Sbillf  # file doesn't have an $Id, or we're using STRICT mode; look at the diff.
101652400Sbillf  #
101752400Sbillf  if [ -f "${COMPFILE}" ]; then
101852400Sbillf
101952400Sbillf    # Do an absolute diff first to see if the files are actually different.
102052400Sbillf    # If they're not different, delete the one in temproot.
102152400Sbillf    #
1022110377Sdougb    if diff -q ${DIFF_OPTIONS} "${DESTDIR}${COMPFILE#.}" "${COMPFILE}" > \
1023110377Sdougb      /dev/null 2>&1; then
102452400Sbillf      echo " *** Temp ${COMPFILE} and installed are the same, deleting"
102552400Sbillf      rm "${COMPFILE}"
102652400Sbillf    else
102777323Sdougb      # Ok, the files are different, so show the user where they differ.
102877323Sdougb      # Use user's choice of diff methods; and user's pager if they have one.
102977323Sdougb      # Use more if not.
103067859Sdougb      # Use unified diffs by default.  Context diffs give me a headache. :)
103152400Sbillf      #
1032189992Sdougb      # If the user chose the -F option, test for that before proceeding
1033189992Sdougb      #
1034189992Sdougb      if [ -n "$FREEBSD_ID" ]; then
1035189992Sdougb        if diff -q -I'[$]FreeBSD:.*$' "${DESTDIR}${COMPFILE#.}" "${COMPFILE}" > \
1036189992Sdougb            /dev/null 2>&1; then
1037189992Sdougb          if mm_install "${COMPFILE}"; then
1038189992Sdougb            echo "*** Updated revision control Id for ${DESTDIR}${COMPFILE#.}"
1039189992Sdougb          else
1040189992Sdougb            echo "*** Problem installing ${COMPFILE}, it will remain to merge by hand later"
1041189992Sdougb          fi
1042189992Sdougb          continue
1043189992Sdougb        fi
1044189992Sdougb      fi
104552400Sbillf      case "${AUTO_RUN}" in
104652400Sbillf      '')
104758910Salfred        # prompt user to install/delete/merge changes
104858910Salfred        diff_loop
104952400Sbillf        ;;
105052400Sbillf      *)
105152400Sbillf        # If this is an auto run, make it official
105252400Sbillf        echo "   *** ${COMPFILE} will remain for your consideration"
105352400Sbillf        ;;
105452400Sbillf      esac # Auto run test
105552400Sbillf    fi # Yes, the files are different
105652400Sbillf  fi # Yes, the file still remains to be checked
1057189763Sdougbdone # This is for the for way up there at the beginning of the comparison
105852400Sbillf
105952534Sbillfecho ''
106052400Sbillfecho "*** Comparison complete"
1061158149Sgordon
1062192230Sdougbif [ -s "${MTREENEW}" ]; then
1063158149Sgordon  echo "*** Saving mtree database for future upgrades"
1064189761Sdougb  test -e "${DESTDIR}${MTREEFILE}" && unlink ${DESTDIR}${MTREEFILE}
1065189761Sdougb  mv ${MTREENEW} ${DESTDIR}${MTREEFILE}
1066158149Sgordonfi
1067158149Sgordon
106852400Sbillfecho ''
106952400Sbillf
107052400SbillfTEST_FOR_FILES=`find ${TEMPROOT} -type f -size +0 2>/dev/null`
107152400Sbillfif [ -n "${TEST_FOR_FILES}" ]; then
107252400Sbillf  echo "*** Files that remain for you to merge by hand:"
107352400Sbillf  find "${TEMPROOT}" -type f -size +0
107477335Sdougb  echo ''
107552400Sbillffi
107652400Sbillf
107752400Sbillfcase "${AUTO_RUN}" in
107852400Sbillf'')
107967859Sdougb  echo -n "Do you wish to delete what is left of ${TEMPROOT}? [no] "
108067859Sdougb  read DEL_TEMPROOT
108167859Sdougb
108252400Sbillf  case "${DEL_TEMPROOT}" in
108352400Sbillf  [yY]*)
108499152Sdougb    if delete_temproot; then
108552400Sbillf      echo " *** ${TEMPROOT} has been deleted"
108652400Sbillf    else
108752400Sbillf      echo " *** Unable to delete ${TEMPROOT}"
108852400Sbillf    fi
108952400Sbillf    ;;
109052400Sbillf  *)
109152400Sbillf    echo " *** ${TEMPROOT} will remain"
109252400Sbillf    ;;
109352400Sbillf  esac
109452400Sbillf  ;;
109552400Sbillf*) ;;
109652400Sbillfesac
109752400Sbillf
109868153Sdougbcase "${AUTO_INSTALLED_FILES}" in
109968153Sdougb'') ;;
110068153Sdougb*)
110173651Sdougb  case "${AUTO_RUN}" in
110273651Sdougb  '')
110373651Sdougb    (
110473651Sdougb      echo ''
110577323Sdougb      echo '*** You chose the automatic install option for files that did not'
110677323Sdougb      echo '    exist on your system.  The following were installed for you:'
110773651Sdougb      echo "${AUTO_INSTALLED_FILES}"
110873651Sdougb    ) | ${PAGER}
110973651Sdougb    ;;
111073651Sdougb  *)
111168153Sdougb    echo ''
111277323Sdougb    echo '*** You chose the automatic install option for files that did not'
111377323Sdougb    echo '    exist on your system.  The following were installed for you:'
111468153Sdougb    echo "${AUTO_INSTALLED_FILES}"
111573651Sdougb    ;;
111673651Sdougb  esac
111768153Sdougb  ;;
111868153Sdougbesac
111968153Sdougb
1120158149Sgordoncase "${AUTO_UPGRADED_FILES}" in
1121158149Sgordon'') ;;
1122158149Sgordon*)
1123158149Sgordon  case "${AUTO_RUN}" in
1124158149Sgordon  '')
1125158149Sgordon    (
1126158149Sgordon      echo ''
1127158149Sgordon      echo '*** You chose the automatic upgrade option for files that you did'
1128158149Sgordon      echo '    not alter on your system.  The following were upgraded for you:'
1129158149Sgordon      echo "${AUTO_UPGRADED_FILES}"
1130158149Sgordon    ) | ${PAGER}
1131158149Sgordon    ;;
1132158149Sgordon  *)
1133158149Sgordon    echo ''
1134158149Sgordon    echo '*** You chose the automatic upgrade option for files that you did'
1135158149Sgordon    echo '    not alter on your system.  The following were upgraded for you:'
1136158149Sgordon    echo "${AUTO_UPGRADED_FILES}"
1137158149Sgordon    ;;
1138158149Sgordon  esac
1139158149Sgordon  ;;
1140158149Sgordonesac
1141158149Sgordon
114273651Sdougbrun_it_now () {
114373651Sdougb  case "${AUTO_RUN}" in
114473651Sdougb  '')
114573651Sdougb    unset YES_OR_NO
114673651Sdougb    echo ''
114777335Sdougb    echo -n '    Would you like to run it now? y or n [n] '
114873651Sdougb    read YES_OR_NO
114973651Sdougb
115073651Sdougb    case "${YES_OR_NO}" in
115173651Sdougb    y)
115277335Sdougb      echo "    Running ${1}"
115377335Sdougb      echo ''
115473651Sdougb      eval "${1}"
115573651Sdougb      ;;
115677335Sdougb    ''|n)
115777335Sdougb      echo ''
115877335Sdougb      echo "       *** Cancelled"
115977335Sdougb      echo ''
116077335Sdougb      echo "    Make sure to run ${1} yourself"
116177335Sdougb      ;;
116273651Sdougb    *)
116377335Sdougb      echo ''
116477335Sdougb      echo "       *** Sorry, I do not understand your answer (${YES_OR_NO})"
116577335Sdougb      echo ''
116677335Sdougb      echo "    Make sure to run ${1} yourself"
116773651Sdougb    esac
116873651Sdougb    ;;
116973651Sdougb  *) ;;
117073651Sdougb  esac
117173651Sdougb}
117273651Sdougb
117352400Sbillfcase "${NEED_NEWALIASES}" in
117452400Sbillf'') ;;
117552400Sbillf*)
117652400Sbillf  echo ''
117777335Sdougb  if [ -n "${DESTDIR}" ]; then
117877335Sdougb    echo "*** You installed a new aliases file into ${DESTDIR}/etc/mail, but"
117977335Sdougb    echo "    the newaliases command is limited to the directories configured"
118077335Sdougb    echo "    in sendmail.cf.  Make sure to create your aliases database by"
118177335Sdougb    echo "    hand when your sendmail configuration is done."
118277335Sdougb  else
118377335Sdougb    echo "*** You installed a new aliases file, so make sure that you run"
118477335Sdougb    echo "    '/usr/bin/newaliases' to rebuild your aliases database"
118577335Sdougb    run_it_now '/usr/bin/newaliases'
118677335Sdougb  fi
118752400Sbillf  ;;
118852400Sbillfesac
118952400Sbillf
119052400Sbillfcase "${NEED_CAP_MKDB}" in
119152400Sbillf'') ;;
119252400Sbillf*)
119352400Sbillf  echo ''
119452400Sbillf  echo "*** You installed a login.conf file, so make sure that you run"
119577335Sdougb  echo "    '/usr/bin/cap_mkdb ${DESTDIR}/etc/login.conf'"
119677335Sdougb  echo "     to rebuild your login.conf database"
119777335Sdougb  run_it_now "/usr/bin/cap_mkdb ${DESTDIR}/etc/login.conf"
119852400Sbillf  ;;
119952400Sbillfesac
120052400Sbillf
120152400Sbillfcase "${NEED_PWD_MKDB}" in
120252400Sbillf'') ;;
120352400Sbillf*)
120452400Sbillf  echo ''
120552400Sbillf  echo "*** You installed a new master.passwd file, so make sure that you run"
120677335Sdougb  if [ -n "${DESTDIR}" ]; then
120777335Sdougb    echo "    '/usr/sbin/pwd_mkdb -d ${DESTDIR}/etc -p ${DESTDIR}/etc/master.passwd'"
120877335Sdougb    echo "    to rebuild your password files"
120977335Sdougb    run_it_now "/usr/sbin/pwd_mkdb -d ${DESTDIR}/etc -p ${DESTDIR}/etc/master.passwd"
121077335Sdougb  else
121177335Sdougb    echo "    '/usr/sbin/pwd_mkdb -p /etc/master.passwd'"
121277335Sdougb    echo "     to rebuild your password files"
121377335Sdougb    run_it_now '/usr/sbin/pwd_mkdb -p /etc/master.passwd'
121477335Sdougb  fi
121552400Sbillf  ;;
121652400Sbillfesac
121752400Sbillf
121852400Sbillfecho ''
121952400Sbillf
122067949Sdougbif [ -r "${MM_EXIT_SCRIPT}" ]; then
122167949Sdougb  . "${MM_EXIT_SCRIPT}"
122267949Sdougbfi
122367949Sdougb
122491193Sdougbcase "${COMP_CONFS}" in
122591193Sdougb'') ;;
122691193Sdougb*)
122791193Sdougb  . ${DESTDIR}/etc/defaults/rc.conf
122891193Sdougb
122991193Sdougb  (echo ''
123091193Sdougb  echo "*** Comparing conf files: ${rc_conf_files}"
123191193Sdougb
123291193Sdougb  for CONF_FILE in ${rc_conf_files}; do
123391193Sdougb    if [ -r "${DESTDIR}${CONF_FILE}" ]; then
123491193Sdougb      echo ''
123591193Sdougb      echo "*** From ${DESTDIR}${CONF_FILE}"
123691193Sdougb      echo "*** From ${DESTDIR}/etc/defaults/rc.conf"
123791193Sdougb
123891193Sdougb      for RC_CONF_VAR in `grep -i ^[a-z] ${DESTDIR}${CONF_FILE} |
123991193Sdougb        cut -d '=' -f 1`; do
124091193Sdougb        echo ''
124191223Sdougb        grep -w ^${RC_CONF_VAR} ${DESTDIR}${CONF_FILE}
124291223Sdougb        grep -w ^${RC_CONF_VAR} ${DESTDIR}/etc/defaults/rc.conf ||
124391193Sdougb          echo ' * No default variable with this name'
124491193Sdougb      done
124591193Sdougb    fi
124691193Sdougb  done) | ${PAGER}
124791193Sdougb  echo ''
124891193Sdougb  ;;
124991193Sdougbesac
125091193Sdougb
125191193Sdougbcase "${PRE_WORLD}" in
125291193Sdougb'') ;;
125391193Sdougb*)
1254186678Sdougb  MAKE_CONF="${SOURCEDIR}/share/examples/etc/make.conf"
125591193Sdougb
125691193Sdougb  (echo ''
125791193Sdougb  echo '*** Comparing make variables'
125891193Sdougb  echo ''
125991193Sdougb  echo "*** From ${DESTDIR}/etc/make.conf"
126091193Sdougb  echo "*** From ${MAKE_CONF}"
126191193Sdougb
1262101348Sdougb  for MAKE_VAR in `grep -i ^[a-z] ${DESTDIR}/etc/make.conf | cut -d '=' -f 1`; do
126391193Sdougb    echo ''
126491223Sdougb    grep -w ^${MAKE_VAR} ${DESTDIR}/etc/make.conf
126591223Sdougb    grep -w ^#${MAKE_VAR} ${MAKE_CONF} ||
126691193Sdougb      echo ' * No example variable with this name'
126791193Sdougb  done) | ${PAGER}
126891193Sdougb  ;;
126991193Sdougbesac
127091193Sdougb
127152400Sbillfexit 0
127267850Sdougb
1273