mergemaster.sh revision 202817
152400Sbillf#!/bin/sh
252400Sbillf
352400Sbillf# mergemaster
452400Sbillf
552400Sbillf# Compare files created by /usr/src/etc/Makefile (or the directory
652400Sbillf# the user specifies) with the currently installed copies.
752400Sbillf
8201765Sdougb# Copyright 1998-2010 Douglas Barton
973651Sdougb# DougB@FreeBSD.org
1052400Sbillf
1152495Sbillf# $FreeBSD: head/usr.sbin/mergemaster/mergemaster.sh 202817 2010-01-22 17:17:47Z 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
26452400Sbillf# Check the command line options
26552400Sbillf#
266189992Sdougbwhile getopts ":ascrvhipCPm:t:du:w:D:A:FU" COMMAND_LINE_ARGUMENT ; do
26752400Sbillf  case "${COMMAND_LINE_ARGUMENT}" in
268155309Srwatson  A)
269186678Sdougb    ARCHSTRING='TARGET_ARCH='${OPTARG}
270155309Srwatson    ;;
271189992Sdougb  F)
272189992Sdougb    FREEBSD_ID=yes
273189992Sdougb    ;;
274158149Sgordon  U)
275158149Sgordon    AUTO_UPGRADE=yes
276158149Sgordon    ;;
27752400Sbillf  s)
27852400Sbillf    STRICT=yes
279110377Sdougb    unset DIFF_OPTIONS
28052400Sbillf    ;;
28152400Sbillf  c)
28252400Sbillf    DIFF_FLAG='-c'
28352400Sbillf    ;;
28452400Sbillf  r)
28552400Sbillf    RERUN=yes
28652400Sbillf    ;;
28752400Sbillf  v)
28852400Sbillf    case "${AUTO_RUN}" in
28952400Sbillf    '') VERBOSE=yes ;;
29052400Sbillf    esac
29152400Sbillf    ;;
29252400Sbillf  a)
29352400Sbillf    AUTO_RUN=yes
29452400Sbillf    unset VERBOSE
29552400Sbillf    ;;
29652400Sbillf  h)
29752400Sbillf    display_usage
29852400Sbillf    display_help
29952400Sbillf    exit 0
30052400Sbillf    ;;
30167949Sdougb  i)
30267949Sdougb    AUTO_INSTALL=yes
30367949Sdougb    ;;
30496045Sdougb  C)
30596045Sdougb    COMP_CONFS=yes
30696045Sdougb    ;;
307114501Sdougb  P)
308114501Sdougb    PRESERVE_FILES=yes
309114501Sdougb    ;;
31091193Sdougb  p)
31191193Sdougb    PRE_WORLD=yes
31296045Sdougb    unset COMP_CONFS
31396045Sdougb    unset AUTO_RUN
31491193Sdougb    ;;
31552400Sbillf  m)
31652400Sbillf    SOURCEDIR=${OPTARG}
31752400Sbillf    ;;
31852400Sbillf  t)
31952400Sbillf    TEMPROOT=${OPTARG}
32052400Sbillf    ;;
32152400Sbillf  d)
32252400Sbillf    TEMPROOT=${TEMPROOT}.`date +%m%d.%H.%M`
32352400Sbillf    ;;
32452400Sbillf  u)
32552400Sbillf    NEW_UMASK=${OPTARG}
32652400Sbillf    ;;
32752400Sbillf  w)
32852400Sbillf    SCREEN_WIDTH=${OPTARG}
32952400Sbillf    ;;
33067949Sdougb  D)
33167949Sdougb    DESTDIR=${OPTARG}
33267949Sdougb    ;;
33352400Sbillf  *)
33452400Sbillf    display_usage
33552400Sbillf    exit 1
33652400Sbillf    ;;
33752400Sbillf  esac
33852400Sbillfdone
33952400Sbillf
340202817Sdougb# Assign the location of the mtree database
341202817Sdougb#
342202817SdougbMTREEDB=${MTREEDB:-${DESTDIR}/var/db}
343202817SdougbMTREEFILE="${MTREEDB}/mergemaster.mtree"
344202817Sdougb
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`
348200425Sdougb  mkdir -p ${PRESERVE_FILES_DIR}
349114501Sdougbfi
350114501Sdougb
351186678Sdougb# Check for the mtree database in DESTDIR
352186678Sdougbcase "${AUTO_UPGRADE}" in
353186678Sdougb'') ;;	# If the option is not set no need to run the test or warn the user
354186678Sdougb*)
355200416Sdougb  if [ ! -s "${MTREEFILE}" ]; then
356186678Sdougb    echo ''
357200416Sdougb    echo "*** Unable to find mtree database (${MTREEFILE})."
358200416Sdougb    echo "    Skipping auto-upgrade on this run."
359193853Sdougb    echo "    It will be created for the next run when this one is complete."
360186678Sdougb    echo ''
361201291Sdougb    case "${AUTO_RUN}" in
362201291Sdougb    '')
363201291Sdougb      press_to_continue
364201291Sdougb      ;;
365201291Sdougb    esac
366186678Sdougb    unset AUTO_UPGRADE
367186678Sdougb  fi
368186678Sdougb  ;;
369186678Sdougbesac
370186678Sdougb
371186689Sdougbif [ -e "${DESTDIR}/etc/fstab" ]; then
372186689Sdougb  if grep -q nodev ${DESTDIR}/etc/fstab; then
373186689Sdougb    echo ''
374186689Sdougb    echo "*** You have the deprecated 'nodev' option in ${DESTDIR}/etc/fstab."
375186689Sdougb    echo "    This can prevent the filesystem from being mounted on reboot."
376186689Sdougb    echo "    Please update your fstab before continuing."
377186689Sdougb    echo "    See fstab(5) for more information."
378186689Sdougb    echo ''
379186689Sdougb    exit 1
380186689Sdougb  fi
381158149Sgordonfi
382158149Sgordon
38352400Sbillfecho ''
38452400Sbillf
38552400Sbillf# If the user has a pager defined, make sure we can run it
38652400Sbillf#
38752400Sbillfcase "${DONT_CHECK_PAGER}" in
38852400Sbillf'')
389186695Sdougbcheck_pager () {
390186695Sdougb  while ! type "${PAGER%% *}" >/dev/null; do
39152400Sbillf    echo " *** Your PAGER environment variable specifies '${PAGER}', but"
39264467Sbrian    echo "     due to the limited PATH that I use for security reasons,"
39367859Sdougb    echo "     I cannot execute it.  So, what would you like to do?"
39452400Sbillf    echo ''
39552400Sbillf    echo "  Use 'e' to exit mergemaster and fix your PAGER variable"
39664467Sbrian    if [ -x /usr/bin/less -o -x /usr/local/bin/less ]; then
39764467Sbrian    echo "  Use 'l' to set PAGER to 'less' for this run"
39852400Sbillf    fi
39952400Sbillf    echo "  Use 'm' to use plain old 'more' as your PAGER for this run"
40052400Sbillf    echo ''
40152400Sbillf    echo "  Default is to use plain old 'more' "
40252400Sbillf    echo ''
40367859Sdougb    echo -n "What should I do? [Use 'more'] "
40467859Sdougb    read FIXPAGER
40567859Sdougb
40652400Sbillf    case "${FIXPAGER}" in
40758910Salfred    [eE])
40852400Sbillf       exit 0
40952400Sbillf       ;;
41058910Salfred    [lL])
41164467Sbrian       if [ -x /usr/bin/less ]; then
41264467Sbrian         PAGER=/usr/bin/less
41364467Sbrian       elif [ -x /usr/local/bin/less ]; then
41458910Salfred         PAGER=/usr/local/bin/less
41564467Sbrian       else
41664467Sbrian         echo ''
41764467Sbrian         echo " *** Fatal Error:"
41864467Sbrian         echo "     You asked to use 'less' as your pager, but I can't"
41964467Sbrian         echo "     find it in /usr/bin or /usr/local/bin"
42064467Sbrian         exit 1
42158910Salfred       fi
42252400Sbillf       ;;
42360420Sbsd    [mM]|'')
42452400Sbillf       PAGER=more
42552400Sbillf       ;;
42658910Salfred    *)
42758910Salfred       echo ''
42858910Salfred       echo "invalid choice: ${FIXPAGER}"
42952400Sbillf    esac
43052400Sbillf    echo ''
43158910Salfred  done
432186695Sdougb}
433186695Sdougb  if [ -n "${PAGER}" ]; then
434186695Sdougb    check_pager
435186695Sdougb  fi
43652400Sbillf  ;;
43752400Sbillfesac
43852400Sbillf
43952400Sbillf# If user has a pager defined, or got assigned one above, use it.
44052400Sbillf# If not, use more.
44152400Sbillf#
44252400SbillfPAGER=${PAGER:-more}
44352400Sbillf
44452400Sbillfif [ -n "${VERBOSE}" -a ! "${PAGER}" = "more" ]; then
44552400Sbillf  echo " *** You have ${PAGER} defined as your pager so we will use that"
44652400Sbillf  echo ''
44752400Sbillf  sleep 3
44852400Sbillffi
44952400Sbillf
45052400Sbillf# Assign the diff flag once so we will not have to keep testing it
45152400Sbillf#
45252400SbillfDIFF_FLAG=${DIFF_FLAG:--u}
45352400Sbillf
45452400Sbillf# Assign the source directory
45552400Sbillf#
456186678SdougbSOURCEDIR=${SOURCEDIR:-/usr/src}
457186678Sdougbif [ ! -f ${SOURCEDIR}/Makefile.inc1 -a \
458186678Sdougb   -f ${SOURCEDIR}/../Makefile.inc1 ]; then
459186678Sdougb  echo " *** The source directory you specified (${SOURCEDIR})"
460186678Sdougb  echo "     will be reset to ${SOURCEDIR}/.."
461186678Sdougb  echo ''
462186678Sdougb  sleep 3
463186678Sdougb  SOURCEDIR=${SOURCEDIR}/..
464186678Sdougbfi
46552400Sbillf
466186678Sdougb# Setup make to use system files from SOURCEDIR
467186695SdougbMM_MAKE="make ${ARCHSTRING} -m ${SOURCEDIR}/share/mk"
468186678Sdougb
469158149Sgordon# Check DESTDIR against the mergemaster mtree database to see what
470158149Sgordon# files the user changed from the reference files.
471158149Sgordon#
472200416Sdougbif [ -n "${AUTO_UPGRADE}" -a -s "${MTREEFILE}" ]; then
473192230Sdougb	CHANGED=:
474200416Sdougb	for file in `mtree -eqL -f ${MTREEFILE} -p ${DESTDIR}/ \
475158149Sgordon		2>/dev/null | awk '($2 == "changed") {print $1}'`; do
476158149Sgordon		if [ -f "${DESTDIR}/$file" ]; then
477192230Sdougb			CHANGED="${CHANGED}${DESTDIR}/${file}:"
478158149Sgordon		fi
479158149Sgordon	done
480192230Sdougb	[ "$CHANGED" = ':' ] && unset CHANGED
481158149Sgordonfi
482158149Sgordon
48396045Sdougb# Check the width of the user's terminal
48496045Sdougb#
48596045Sdougbif [ -t 0 ]; then
486110377Sdougb  w=`tput columns`
48796045Sdougb  case "${w}" in
48896045Sdougb  0|'') ;; # No-op, since the input is not valid
48996045Sdougb  *)
49096045Sdougb    case "${SCREEN_WIDTH}" in
49196045Sdougb    '') SCREEN_WIDTH="${w}" ;;
49296045Sdougb    "${w}") ;; # No-op, since they are the same
49396045Sdougb    *)
49496045Sdougb      echo -n "*** You entered ${SCREEN_WIDTH} as your screen width, but stty "
49596045Sdougb      echo "thinks it is ${w}."
49696045Sdougb      echo ''
49796045Sdougb      echo -n "What would you like to use? [${w}] "
49896045Sdougb      read SCREEN_WIDTH
49997380Sdougb      case "${SCREEN_WIDTH}" in
50097380Sdougb      '') SCREEN_WIDTH="${w}" ;;
50197380Sdougb      esac
50296045Sdougb      ;;
50396045Sdougb    esac
50496045Sdougb  esac
50596045Sdougbfi
50696045Sdougb
50773651Sdougb# Define what CVS $Id tag to look for to aid portability.
50873651Sdougb#
50973651SdougbCVS_ID_TAG=FreeBSD
51073651Sdougb
51199152Sdougbdelete_temproot () {
512101362Sdougb  rm -rf "${TEMPROOT}" 2>/dev/null
513101362Sdougb  chflags -R 0 "${TEMPROOT}" 2>/dev/null
514201765Sdougb  rm -rf "${TEMPROOT}" || { echo "*** Unable to delete ${TEMPROOT}";  exit 1; }
51599152Sdougb}
51699152Sdougb
51752400Sbillfcase "${RERUN}" in
51852400Sbillf'')
51952400Sbillf  # Set up the loop to test for the existence of the
52052400Sbillf  # temp root directory.
52152400Sbillf  #
52252400Sbillf  TEST_TEMP_ROOT=yes
52352400Sbillf  while [ "${TEST_TEMP_ROOT}" = "yes" ]; do
52452400Sbillf    if [ -d "${TEMPROOT}" ]; then
52552400Sbillf      echo "*** The directory specified for the temporary root environment,"
52667859Sdougb      echo "    ${TEMPROOT}, exists.  This can be a security risk if untrusted"
52752400Sbillf      echo "    users have access to the system."
52852400Sbillf      echo ''
52952400Sbillf      case "${AUTO_RUN}" in
53052400Sbillf      '')
53152400Sbillf        echo "  Use 'd' to delete the old ${TEMPROOT} and continue"
53252400Sbillf        echo "  Use 't' to select a new temporary root directory"
53352400Sbillf        echo "  Use 'e' to exit mergemaster"
53452400Sbillf        echo ''
53552400Sbillf        echo "  Default is to use ${TEMPROOT} as is"
53652400Sbillf        echo ''
53767859Sdougb        echo -n "How should I deal with this? [Use the existing ${TEMPROOT}] "
53867859Sdougb        read DELORNOT
53967859Sdougb
54067859Sdougb        case "${DELORNOT}" in
54167859Sdougb        [dD])
54267859Sdougb          echo ''
54367859Sdougb          echo "   *** Deleting the old ${TEMPROOT}"
54467859Sdougb          echo ''
545201765Sdougb          delete_temproot
54667859Sdougb          unset TEST_TEMP_ROOT
54752400Sbillf          ;;
54867859Sdougb        [tT])
54967859Sdougb          echo "   *** Enter new directory name for temporary root environment"
55067859Sdougb          read TEMPROOT
55167859Sdougb          ;;
55267859Sdougb        [eE])
55367859Sdougb          exit 0
55467859Sdougb          ;;
55567859Sdougb        '')
55667859Sdougb          echo ''
55767859Sdougb          echo "   *** Leaving ${TEMPROOT} intact"
55867859Sdougb          echo ''
55967859Sdougb          unset TEST_TEMP_ROOT
56067859Sdougb          ;;
56167859Sdougb        *)
56267859Sdougb          echo ''
56367859Sdougb          echo "invalid choice: ${DELORNOT}"
56467859Sdougb          echo ''
56567859Sdougb          ;;
56667859Sdougb        esac
56767859Sdougb        ;;
56852400Sbillf      *)
56977323Sdougb        # If this is an auto-run, try a hopefully safe alternative then
57077323Sdougb        # re-test anyway.
57152400Sbillf        TEMPROOT=/var/tmp/temproot.`date +%m%d.%H.%M.%S`
57252400Sbillf        ;;
57352400Sbillf      esac
57452400Sbillf    else
57552400Sbillf      unset TEST_TEMP_ROOT
57652400Sbillf    fi
57752400Sbillf  done
57852400Sbillf
57952400Sbillf  echo "*** Creating the temporary root environment in ${TEMPROOT}"
58052400Sbillf
58152400Sbillf  if mkdir -p "${TEMPROOT}"; then
58252400Sbillf    echo " *** ${TEMPROOT} ready for use"
58352400Sbillf  fi
58452400Sbillf
58552400Sbillf  if [ ! -d "${TEMPROOT}" ]; then
58652400Sbillf    echo ''
58752400Sbillf    echo "  *** FATAL ERROR: Cannot create ${TEMPROOT}"
58852400Sbillf    echo ''
58952400Sbillf    exit 1
59052400Sbillf  fi
59152400Sbillf
59252400Sbillf  echo " *** Creating and populating directory structure in ${TEMPROOT}"
59352400Sbillf  echo ''
59452400Sbillf
59552400Sbillf  case "${VERBOSE}" in
59652400Sbillf  '') ;;
59752400Sbillf  *)
59897960Sdougb    press_to_continue
59952400Sbillf    ;;
60052400Sbillf  esac
60152400Sbillf
60291193Sdougb  case "${PRE_WORLD}" in
60391193Sdougb  '')
60491193Sdougb    { cd ${SOURCEDIR} &&
60591193Sdougb      case "${DESTDIR}" in
60691193Sdougb      '') ;;
60791193Sdougb      *)
608186695Sdougb        ${MM_MAKE} DESTDIR=${DESTDIR} distrib-dirs
60991193Sdougb        ;;
61091193Sdougb      esac
611186749Sdougb      od=${TEMPROOT}/usr/obj
612186695Sdougb      ${MM_MAKE} DESTDIR=${TEMPROOT} distrib-dirs &&
613186749Sdougb      MAKEOBJDIRPREFIX=$od ${MM_MAKE} _obj SUBDIR_OVERRIDE=etc &&
614186749Sdougb      MAKEOBJDIRPREFIX=$od ${MM_MAKE} everything SUBDIR_OVERRIDE=etc &&
615186749Sdougb      MAKEOBJDIRPREFIX=$od ${MM_MAKE} DESTDIR=${TEMPROOT} distribution;} ||
61691193Sdougb    { echo '';
61791193Sdougb     echo "  *** FATAL ERROR: Cannot 'cd' to ${SOURCEDIR} and install files to";
61891193Sdougb      echo "      the temproot environment";
61991193Sdougb      echo '';
62091193Sdougb      exit 1;}
62191193Sdougb    ;;
62291193Sdougb  *)
62391193Sdougb    # Only set up files that are crucial to {build|install}world
62491193Sdougb    { mkdir -p ${TEMPROOT}/etc &&
625186678Sdougb      cp -p ${SOURCEDIR}/etc/master.passwd ${TEMPROOT}/etc &&
626186678Sdougb      cp -p ${SOURCEDIR}/etc/group ${TEMPROOT}/etc;} ||
62791193Sdougb    { echo '';
62891193Sdougb      echo '  *** FATAL ERROR: Cannot copy files to the temproot environment';
62991193Sdougb      echo '';
63091193Sdougb      exit 1;}
63191193Sdougb    ;;
63291193Sdougb  esac
63352400Sbillf
63477323Sdougb  # Doing the inventory and removing files that we don't want to compare only
63577323Sdougb  # makes sense if we are not doing a rerun, since we have no way of knowing
63677323Sdougb  # what happened to the files during previous incarnations.
63767949Sdougb  case "${VERBOSE}" in
63867949Sdougb  '') ;;
63967949Sdougb  *)
64067949Sdougb    echo ''
64167949Sdougb    echo ' *** The following files exist only in the installed version of'
64267949Sdougb    echo "     ${DESTDIR}/etc.  In the vast majority of cases these files"
64367949Sdougb    echo '     are necessary parts of the system and should not be deleted.'
64467949Sdougb    echo '     However because these files are not updated by this process you'
64567949Sdougb    echo '     might want to verify their status before rebooting your system.'
64667949Sdougb    echo ''
64797960Sdougb    press_to_continue
648101348Sdougb    diff -qr ${DESTDIR}/etc ${TEMPROOT}/etc | grep "^Only in ${DESTDIR}/etc" | ${PAGER}
64967949Sdougb    echo ''
65097960Sdougb    press_to_continue
65167949Sdougb    ;;
65267949Sdougb  esac
65367949Sdougb
65452534Sbillf  case "${IGNORE_MOTD}" in
655202340Sdougb  '') ;;
656202339Sdougb  *)
657186678Sdougb     echo ''
658186678Sdougb     echo "*** You have the IGNORE_MOTD option set in your mergemaster rc file."
659186678Sdougb     echo "    This option is deprecated in favor of the IGNORE_FILES option."
660186678Sdougb     echo "    Please update your rc file accordingly."
661186678Sdougb     echo ''
662202339Sdougb     exit 1
66352534Sbillf     ;;
66452534Sbillf  esac
66552534Sbillf
666186678Sdougb  # Avoid comparing the following user specified files
667186678Sdougb  for file in ${IGNORE_FILES}; do
668186688Sdougb    test -e ${TEMPROOT}/${file} && unlink ${TEMPROOT}/${file}
669186678Sdougb  done
67052400Sbillf
671201291Sdougb  # We really don't want to have to deal with files like login.conf.db, pwd.db,
672201291Sdougb  # or spwd.db.  Instead, we want to compare the text versions, and run *_mkdb.
673201291Sdougb  # Prompt the user to do so below, as needed.
674201291Sdougb  #
675201291Sdougb  rm -f ${TEMPROOT}/etc/*.db ${TEMPROOT}/etc/passwd
67677478Sdougb
677201291Sdougb  # We only need to compare things like freebsd.cf once
678201291Sdougb  find ${TEMPROOT}/usr/obj -type f -delete 2>/dev/null
67994196Sdougb
680201291Sdougb  # Delete stuff we do not need to keep the mtree database small,
681201291Sdougb  # and to make the actual comparison faster.
682201291Sdougb  find ${TEMPROOT}/usr -type l -delete 2>/dev/null
683201291Sdougb  find ${TEMPROOT} -type f -size 0 -delete 2>/dev/null
684201291Sdougb  find -d ${TEMPROOT} -type d -empty -delete 2>/dev/null
685158149Sgordon
686201291Sdougb  # Build the mtree database in a temporary location.
687201291Sdougb  case "${PRE_WORLD}" in
688201323Sdougb  '') MTREENEW=`mktemp -t mergemaster.mtree`
689201323Sdougb      mtree -ci -p ${TEMPROOT} -k size,md5digest > ${MTREENEW} 2>/dev/null
690201291Sdougb      ;;
691201291Sdougb  *) # We don't want to mess with the mtree database on a pre-world run or
692201291Sdougb     # when re-scanning a previously-built tree.
693201291Sdougb     ;;
694201291Sdougb  esac
695201291Sdougb  ;; # End of the "RERUN" test
696158149Sgordonesac
697158149Sgordon
69852400Sbillf# Get ready to start comparing files
69952400Sbillf
70098084Sdougb# Check umask if not specified on the command line,
70198084Sdougb# and we are not doing an autorun
70252400Sbillf#
70398084Sdougbif [ -z "${NEW_UMASK}" -a -z "${AUTO_RUN}" ]; then
70498084Sdougb  USER_UMASK=`umask`
70552400Sbillf  case "${USER_UMASK}" in
70677335Sdougb  0022|022) ;;
70752400Sbillf  *)
70852400Sbillf    echo ''
70998084Sdougb    echo " *** Your umask is currently set to ${USER_UMASK}.  By default, this script"
71098084Sdougb    echo "     installs all files with the same user, group and modes that"
711186678Sdougb    echo "     they are created with by ${SOURCEDIR}/etc/Makefile, compared to"
71298084Sdougb    echo "     a umask of 022.  This umask allows world read permission when"
71398084Sdougb    echo "     the file's default permissions have it."
71452400Sbillf    echo ''
71598084Sdougb    echo "     No world permissions can sometimes cause problems.  A umask of"
71698084Sdougb    echo "     022 will restore the default behavior, but is not mandatory."
71798084Sdougb    echo "     /etc/master.passwd is a special case.  Its file permissions"
71898084Sdougb    echo "     will be 600 (rw-------) if installed."
71997960Sdougb    echo ''
72098084Sdougb    echo -n "What umask should I use? [${USER_UMASK}] "
72198084Sdougb    read NEW_UMASK
72298084Sdougb
72398084Sdougb    NEW_UMASK="${NEW_UMASK:-$USER_UMASK}"
72452400Sbillf    ;;
72552400Sbillf  esac
72652400Sbillf  echo ''
72798084Sdougbfi
72852400Sbillf
72998084SdougbCONFIRMED_UMASK=${NEW_UMASK:-0022}
73098084Sdougb
73152400Sbillf#
732114501Sdougb# Warn users who still have old rc files
733114501Sdougb#
734179315Sbzfor file in atm devfs diskless1 diskless2 network network6 pccard \
735114523Sdougb  serial syscons sysctl alpha amd64 i386 ia64 sparc64; do
736114501Sdougb  if [ -f "${DESTDIR}/etc/rc.${file}" ]; then
737114501Sdougb    OLD_RC_PRESENT=1
738114501Sdougb    break
739114501Sdougb  fi
740114501Sdougbdone
741114501Sdougb
742114501Sdougbcase "${OLD_RC_PRESENT}" in
743114501Sdougb1)
74452400Sbillf  echo ''
745114501Sdougb  echo " *** There are elements of the old rc system in ${DESTDIR}/etc/."
74667949Sdougb  echo ''
747114501Sdougb  echo '     While these scripts will not hurt anything, they are not'
748114501Sdougb  echo '     functional on an up to date system, and can be removed.'
74967949Sdougb  echo ''
750114501Sdougb
75152400Sbillf  case "${AUTO_RUN}" in
75252400Sbillf  '')
753114501Sdougb    echo -n 'Move these files to /var/tmp/mergemaster/old_rc? [yes] '
754114501Sdougb    read MOVE_OLD_RC
75567859Sdougb
756114501Sdougb    case "${MOVE_OLD_RC}" in
757114501Sdougb    [nN]*) ;;
75852400Sbillf    *)
759114501Sdougb      mkdir -p /var/tmp/mergemaster/old_rc
760179315Sbz        for file in atm devfs diskless1 diskless2 network network6 pccard \
761114523Sdougb          serial syscons sysctl alpha amd64 i386 ia64 sparc64; do
762114501Sdougb          if [ -f "${DESTDIR}/etc/rc.${file}" ]; then
763114501Sdougb            mv ${DESTDIR}/etc/rc.${file} /var/tmp/mergemaster/old_rc/
764114501Sdougb          fi
765114501Sdougb        done
766114501Sdougb      echo '  The files have been moved'
767114501Sdougb      press_to_continue
76852400Sbillf      ;;
76952400Sbillf    esac
77052400Sbillf    ;;
77152400Sbillf  *) ;;
77252400Sbillf  esac
773114501Sdougbesac
77452400Sbillf
77598084Sdougb# Use the umask/mode information to install the files
77652400Sbillf# Create directories as needed
77752400Sbillf#
778186678Sdougbinstall_error () {
779186678Sdougb  echo "*** FATAL ERROR: Unable to install ${1} to ${2}"
780186678Sdougb  echo ''
781186678Sdougb  exit 1
782186678Sdougb}
783186678Sdougb
78478490Sdougbdo_install_and_rm () {
785114501Sdougb  case "${PRESERVE_FILES}" in
786114501Sdougb  [Yy][Ee][Ss])
787114501Sdougb    if [ -f "${3}/${2##*/}" ]; then
788114565Sdougb      mkdir -p ${PRESERVE_FILES_DIR}/${2%/*}
789114565Sdougb      cp ${3}/${2##*/} ${PRESERVE_FILES_DIR}/${2%/*}
790114501Sdougb    fi
791114501Sdougb    ;;
792114501Sdougb  esac
793114501Sdougb
794186678Sdougb  if [ ! -d "${3}/${2##*/}" ]; then
795186678Sdougb    if install -m ${1} ${2} ${3}; then
796186678Sdougb      unlink ${2}
797186678Sdougb    else
798186678Sdougb      install_error ${2} ${3}
799186678Sdougb    fi
800186678Sdougb  else
801186678Sdougb    install_error ${2} ${3}
802186678Sdougb  fi
80378490Sdougb}
80478490Sdougb
80598084Sdougb# 4095 = "obase=10;ibase=8;07777" | bc
80698084Sdougbfind_mode () {
80798084Sdougb  local OCTAL
80898084Sdougb  OCTAL=$(( ~$(echo "obase=10; ibase=8; ${CONFIRMED_UMASK}" | bc) & 4095 &
809124053Sdougb    $(echo "obase=10; ibase=8; $(stat -f "%OMp%OLp" ${1})" | bc) ))
81098084Sdougb  printf "%04o\n" ${OCTAL}
81198084Sdougb}
81298084Sdougb
81352400Sbillfmm_install () {
81452400Sbillf  local INSTALL_DIR
81552400Sbillf  INSTALL_DIR=${1#.}
81652400Sbillf  INSTALL_DIR=${INSTALL_DIR%/*}
81767949Sdougb
81852400Sbillf  case "${INSTALL_DIR}" in
81952400Sbillf  '')
82052400Sbillf    INSTALL_DIR=/
82152400Sbillf    ;;
82252400Sbillf  esac
82352400Sbillf
82467949Sdougb  if [ -n "${DESTDIR}${INSTALL_DIR}" -a ! -d "${DESTDIR}${INSTALL_DIR}" ]; then
82598084Sdougb    DIR_MODE=`find_mode "${TEMPROOT}/${INSTALL_DIR}"`
826200425Sdougb    install -d -o root -g wheel -m "${DIR_MODE}" "${DESTDIR}${INSTALL_DIR}" ||
827200425Sdougb      install_error $1 ${DESTDIR}${INSTALL_DIR}
82852400Sbillf  fi
82952400Sbillf
83098084Sdougb  FILE_MODE=`find_mode "${1}"`
83152400Sbillf
83252400Sbillf  if [ ! -x "${1}" ]; then
83352400Sbillf    case "${1#.}" in
83464625Sgshapiro    /etc/mail/aliases)
83552400Sbillf      NEED_NEWALIASES=yes
83652400Sbillf      ;;
83752400Sbillf    /etc/login.conf)
83852400Sbillf      NEED_CAP_MKDB=yes
83952400Sbillf      ;;
84052400Sbillf    /etc/master.passwd)
84178490Sdougb      do_install_and_rm 600 "${1}" "${DESTDIR}${INSTALL_DIR}"
84252400Sbillf      NEED_PWD_MKDB=yes
84352400Sbillf      DONT_INSTALL=yes
84452400Sbillf      ;;
84552400Sbillf    /.cshrc | /.profile)
846200708Sdougb      local st_nlink
847200708Sdougb
848200708Sdougb      # install will unlink the file before it installs the new one,
849200708Sdougb      # so we have to restore/create the link afterwards.
850200708Sdougb      #
851200708Sdougb      st_nlink=0		# In case the file does not yet exist
852200708Sdougb      eval $(stat -s ${DESTDIR}${COMPFILE#.} 2>/dev/null)
853200708Sdougb
854200708Sdougb      do_install_and_rm "${FILE_MODE}" "${1}" "${DESTDIR}${INSTALL_DIR}"
855200708Sdougb
856200708Sdougb      if [ -n "${AUTO_INSTALL}" -a $st_nlink -gt 1 ]; then
857200708Sdougb        HANDLE_LINK=l
858200708Sdougb      else
859200701Sdougb        case "${LINK_EXPLAINED}" in
860200701Sdougb        '')
861200701Sdougb          echo "   *** Historically BSD derived systems have had a"
862200701Sdougb          echo "       hard link from /.cshrc and /.profile to"
863200701Sdougb          echo "       their namesakes in /root.  Please indicate"
864200701Sdougb          echo "       your preference below for bringing your"
865200701Sdougb          echo "       installed files up to date."
866200701Sdougb          echo ''
867200701Sdougb          LINK_EXPLAINED=yes
868200701Sdougb          ;;
869200701Sdougb        esac
870200701Sdougb
871200701Sdougb        echo "   Use 'd' to delete the temporary ${COMPFILE}"
872200708Sdougb        echo "   Use 'l' to delete the existing ${DESTDIR}/root/${COMPFILE##*/} and create the link"
87352400Sbillf        echo ''
874200701Sdougb        echo "   Default is to leave the temporary file to deal with by hand"
875200701Sdougb        echo ''
876200701Sdougb        echo -n "  How should I handle ${COMPFILE}? [Leave it to install later] "
877200701Sdougb        read HANDLE_LINK
878200708Sdougb      fi
87952400Sbillf
88052400Sbillf      case "${HANDLE_LINK}" in
88152400Sbillf      [dD]*)
88252400Sbillf        rm "${COMPFILE}"
88352400Sbillf        echo ''
88452400Sbillf        echo "   *** Deleting ${COMPFILE}"
88552400Sbillf        ;;
88652400Sbillf      [lL]*)
88752400Sbillf        echo ''
888200708Sdougb        unlink ${DESTDIR}/root/${COMPFILE##*/}
889200708Sdougb        if ln ${DESTDIR}${COMPFILE#.} ${DESTDIR}/root/${COMPFILE##*/}; then
89067949Sdougb          echo "   *** Link from ${DESTDIR}${COMPFILE#.} to ${DESTDIR}/root/${COMPFILE##*/} installed successfully"
89152400Sbillf        else
892200708Sdougb          echo "   *** Error linking ${DESTDIR}${COMPFILE#.} to ${DESTDIR}/root/${COMPFILE##*/}"
893200708Sdougb          echo "   *** ${COMPFILE} will remain for your consideration"
89452400Sbillf        fi
89552400Sbillf        ;;
89652400Sbillf      *)
89752400Sbillf        echo "   *** ${COMPFILE} will remain for your consideration"
89852400Sbillf        ;;
89952400Sbillf      esac
900200708Sdougb      return
90152400Sbillf      ;;
90252400Sbillf    esac
90352400Sbillf
90452400Sbillf    case "${DONT_INSTALL}" in
90552400Sbillf    '')
90678490Sdougb      do_install_and_rm "${FILE_MODE}" "${1}" "${DESTDIR}${INSTALL_DIR}"
90752400Sbillf      ;;
90852400Sbillf    *)
90952400Sbillf      unset DONT_INSTALL
91052400Sbillf      ;;
91152400Sbillf    esac
91278490Sdougb  else	# File matched -x
91378490Sdougb    do_install_and_rm "${FILE_MODE}" "${1}" "${DESTDIR}${INSTALL_DIR}"
91452400Sbillf  fi
91552400Sbillf  return $?
91652400Sbillf}
91752400Sbillf
918174841Sdougbif [ ! -d "${TEMPROOT}" ]; then
919174841Sdougb	echo "*** FATAL ERROR: The temproot directory (${TEMPROOT})"
920174841Sdougb	echo '                 has disappeared!'
921174841Sdougb	echo ''
922174841Sdougb	exit 1
923174841Sdougbfi
924174841Sdougb
92567949Sdougbecho ''
92667949Sdougbecho "*** Beginning comparison"
92767949Sdougbecho ''
92852400Sbillf
929124136Sdougb# Pre-world does not populate /etc/rc.d.
930124053Sdougb# It is very possible that a previous run would have deleted files in
931124053Sdougb# ${TEMPROOT}/etc/rc.d, thus creating a lot of false positives.
932124136Sdougbif [ -z "${PRE_WORLD}" -a -z "${RERUN}" ]; then
933124053Sdougb  echo "   *** Checking ${DESTDIR}/etc/rc.d for stale files"
934124053Sdougb  echo ''
935124053Sdougb  cd "${DESTDIR}/etc/rc.d" &&
936124053Sdougb  for file in *; do
937124053Sdougb    if [ ! -e "${TEMPROOT}/etc/rc.d/${file}" ]; then
938124053Sdougb      STALE_RC_FILES="${STALE_RC_FILES} ${file}"
939124053Sdougb    fi
940124053Sdougb  done
941124053Sdougb  case "${STALE_RC_FILES}" in
942126718Sdougb  ''|' *')
943124053Sdougb    echo '   *** No stale files found'
944124053Sdougb    ;;
945124053Sdougb  *)
946124053Sdougb    echo "   *** The following files exist in ${DESTDIR}/etc/rc.d but not in"
947124053Sdougb    echo "       ${TEMPROOT}/etc/rc.d/:"
948124053Sdougb    echo ''
949124053Sdougb    echo "${STALE_RC_FILES}"
950124053Sdougb    echo ''
951124053Sdougb    echo '       The presence of stale files in this directory can cause the'
952124053Sdougb    echo '       dreaded unpredictable results, and therefore it is highly'
953124053Sdougb    echo '       recommended that you delete them.'
954124053Sdougb    case "${AUTO_RUN}" in
955124053Sdougb    '')
956124053Sdougb      echo ''
957153604Sdougb      echo -n '   *** Delete them now? [n] '
958124053Sdougb      read DELETE_STALE_RC_FILES
959124053Sdougb      case "${DELETE_STALE_RC_FILES}" in
960153604Sdougb      [yY])
961124053Sdougb        echo '      *** Deleting ... '
962124053Sdougb        rm ${STALE_RC_FILES}
963124053Sdougb        echo '                       done.'
964124053Sdougb        ;;
965153604Sdougb      *)
966153604Sdougb        echo '      *** Files will not be deleted'
967153604Sdougb        ;;
968124053Sdougb      esac
969124053Sdougb      sleep 2
970124053Sdougb      ;;
971201291Sdougb    *)
972201291Sdougb      if [ -n "${DELETE_STALE_RC_FILES}" ]; then
973201291Sdougb        echo '      *** Deleting ... '
974201291Sdougb        rm ${STALE_RC_FILES}
975201291Sdougb        echo '                       done.'
976201291Sdougb      fi
977124053Sdougb    esac
978124053Sdougb    ;;
979124053Sdougb  esac
980124053Sdougb  echo ''
981124136Sdougbfi
982124053Sdougb
98367949Sdougbcd "${TEMPROOT}"
98467949Sdougb
98567949Sdougbif [ -r "${MM_PRE_COMPARE_SCRIPT}" ]; then
98667949Sdougb  . "${MM_PRE_COMPARE_SCRIPT}"
98767949Sdougbfi
98867949Sdougb
989200425Sdougb# Things that were files/directories/links in one version can sometimes
990200425Sdougb# change to something else in a newer version.  So we need to explicitly
991200425Sdougb# test for this, and warn the user if what we find does not match.
992200425Sdougb#
993200700Sdougbfor COMPFILE in `find . | sort` ; do
994200425Sdougb  if [ -e "${DESTDIR}${COMPFILE#.}" ]; then
995200425Sdougb    INSTALLED_TYPE=`stat -f '%HT' ${DESTDIR}${COMPFILE#.}`
996200425Sdougb  else
997200425Sdougb    continue
998200425Sdougb  fi
999200425Sdougb  TEMPROOT_TYPE=`stat -f '%HT' $COMPFILE`
1000200425Sdougb
1001200425Sdougb  if [ ! "$TEMPROOT_TYPE" = "$INSTALLED_TYPE" ]; then
1002200425Sdougb    [ "$COMPFILE" = '.' ] && continue
1003200425Sdougb    TEMPROOT_TYPE=`echo $TEMPROOT_TYPE | tr [:upper:] [:lower:]`
1004200425Sdougb    INSTALLED_TYPE=`echo $INSTALLED_TYPE | tr [:upper:] [:lower:]`
1005200425Sdougb
1006200425Sdougb    echo "*** The installed file ${DESTDIR}${COMPFILE#.} has the type \"$INSTALLED_TYPE\""
1007200425Sdougb    echo "    but the new version has the type \"$TEMPROOT_TYPE\""
1008200425Sdougb    echo ''
1009200425Sdougb    echo "    How would you like to handle this?"
1010200425Sdougb    echo ''
1011200425Sdougb    echo "    Use 'r' to remove ${DESTDIR}${COMPFILE#.}"
1012200425Sdougb    case "$TEMPROOT_TYPE" in
1013200425Sdougb    'symbolic link')
1014200425Sdougb	TARGET=`readlink $COMPFILE`
1015200425Sdougb	echo "    and create a link to $TARGET in its place" ;;
1016200425Sdougb    *)	echo "    You will be able to install it as a \"$TEMPROOT_TYPE\"" ;;
1017200425Sdougb    esac
1018200425Sdougb    echo ''
1019200425Sdougb    echo "    Use 'i' to ignore this"
1020200425Sdougb    echo ''
1021200425Sdougb    echo -n "    How to proceed? [i] "
1022200425Sdougb    read ANSWER
1023200425Sdougb    case "$ANSWER" in
1024200425Sdougb    [rR])	case "${PRESERVE_FILES}" in
1025200425Sdougb		[Yy][Ee][Ss])
1026200425Sdougb		mv ${DESTDIR}${COMPFILE#.} ${PRESERVE_FILES_DIR}/ || exit 1 ;;
1027200425Sdougb		*) rm -rf ${DESTDIR}${COMPFILE#.} ;;
1028200425Sdougb		esac
1029200425Sdougb		case "$TEMPROOT_TYPE" in
1030200425Sdougb		'symbolic link') ln -sf $TARGET ${DESTDIR}${COMPFILE#.} ;;
1031200425Sdougb		esac ;;
1032200425Sdougb    *)	echo ''
1033200425Sdougb        echo "*** See the man page about adding ${COMPFILE#.} to the list of IGNORE_FILES"
1034200425Sdougb        press_to_continue ;;
1035200425Sdougb    esac
1036200425Sdougb    echo ''
1037200425Sdougb  fi
1038200425Sdougbdone
1039200425Sdougb
1040200700Sdougbfor COMPFILE in `find . -type f | sort`; do
104167949Sdougb
104267949Sdougb  # First, check to see if the file exists in DESTDIR.  If not, the
104367949Sdougb  # diff_loop function knows how to handle it.
104467949Sdougb  #
104567949Sdougb  if [ ! -e "${DESTDIR}${COMPFILE#.}" ]; then
104677325Sdougb    case "${AUTO_RUN}" in
104777325Sdougb      '')
104877325Sdougb        diff_loop
104977325Sdougb        ;;
105077325Sdougb      *)
105177325Sdougb        case "${AUTO_INSTALL}" in
105277325Sdougb        '')
105377325Sdougb          # If this is an auto run, make it official
105477325Sdougb          echo "   *** ${COMPFILE} will remain for your consideration"
105577325Sdougb          ;;
105677325Sdougb        *)
105777325Sdougb          diff_loop
105877325Sdougb          ;;
105977325Sdougb        esac
106077325Sdougb        ;;
106177325Sdougb    esac # Auto run test
106267949Sdougb    continue
106367949Sdougb  fi
106467949Sdougb
106552400Sbillf  case "${STRICT}" in
106652400Sbillf  '' | [Nn][Oo])
106752400Sbillf    # Compare CVS $Id's first so if the file hasn't been modified
106852400Sbillf    # local changes will be ignored.
106952400Sbillf    # If the files have the same $Id, delete the one in temproot so the
107052400Sbillf    # user will have less to wade through if files are left to merge by hand.
107152400Sbillf    #
107273651Sdougb    CVSID1=`grep "[$]${CVS_ID_TAG}:" ${DESTDIR}${COMPFILE#.} 2>/dev/null`
107390564Sdougb    CVSID2=`grep "[$]${CVS_ID_TAG}:" ${COMPFILE} 2>/dev/null` || CVSID2=none
107452400Sbillf
107567949Sdougb    case "${CVSID2}" in
107673651Sdougb    "${CVSID1}")
107767949Sdougb      echo " *** Temp ${COMPFILE} and installed have the same CVS Id, deleting"
107867949Sdougb      rm "${COMPFILE}"
107967949Sdougb      ;;
108067949Sdougb    esac
108152400Sbillf    ;;
108252400Sbillf  esac
108352400Sbillf
108452400Sbillf  # If the file is still here either because the $Ids are different, the
108552400Sbillf  # file doesn't have an $Id, or we're using STRICT mode; look at the diff.
108652400Sbillf  #
108752400Sbillf  if [ -f "${COMPFILE}" ]; then
108852400Sbillf
108952400Sbillf    # Do an absolute diff first to see if the files are actually different.
109052400Sbillf    # If they're not different, delete the one in temproot.
109152400Sbillf    #
1092110377Sdougb    if diff -q ${DIFF_OPTIONS} "${DESTDIR}${COMPFILE#.}" "${COMPFILE}" > \
1093110377Sdougb      /dev/null 2>&1; then
109452400Sbillf      echo " *** Temp ${COMPFILE} and installed are the same, deleting"
109552400Sbillf      rm "${COMPFILE}"
109652400Sbillf    else
109777323Sdougb      # Ok, the files are different, so show the user where they differ.
109877323Sdougb      # Use user's choice of diff methods; and user's pager if they have one.
109977323Sdougb      # Use more if not.
110067859Sdougb      # Use unified diffs by default.  Context diffs give me a headache. :)
110152400Sbillf      #
1102189992Sdougb      # If the user chose the -F option, test for that before proceeding
1103189992Sdougb      #
1104189992Sdougb      if [ -n "$FREEBSD_ID" ]; then
1105201291Sdougb        if diff -q -I'[$]FreeBSD.*[$]' "${DESTDIR}${COMPFILE#.}" "${COMPFILE}" > \
1106189992Sdougb            /dev/null 2>&1; then
1107189992Sdougb          if mm_install "${COMPFILE}"; then
1108189992Sdougb            echo "*** Updated revision control Id for ${DESTDIR}${COMPFILE#.}"
1109189992Sdougb          else
1110189992Sdougb            echo "*** Problem installing ${COMPFILE}, it will remain to merge by hand later"
1111189992Sdougb          fi
1112189992Sdougb          continue
1113189992Sdougb        fi
1114189992Sdougb      fi
111552400Sbillf      case "${AUTO_RUN}" in
111652400Sbillf      '')
111758910Salfred        # prompt user to install/delete/merge changes
111858910Salfred        diff_loop
111952400Sbillf        ;;
112052400Sbillf      *)
112152400Sbillf        # If this is an auto run, make it official
112252400Sbillf        echo "   *** ${COMPFILE} will remain for your consideration"
112352400Sbillf        ;;
112452400Sbillf      esac # Auto run test
112552400Sbillf    fi # Yes, the files are different
112652400Sbillf  fi # Yes, the file still remains to be checked
1127189763Sdougbdone # This is for the for way up there at the beginning of the comparison
112852400Sbillf
112952534Sbillfecho ''
113052400Sbillfecho "*** Comparison complete"
1131158149Sgordon
1132192230Sdougbif [ -s "${MTREENEW}" ]; then
1133158149Sgordon  echo "*** Saving mtree database for future upgrades"
1134200416Sdougb  test -e "${MTREEFILE}" && unlink ${MTREEFILE}
1135200416Sdougb  mv ${MTREENEW} ${MTREEFILE}
1136158149Sgordonfi
1137158149Sgordon
113852400Sbillfecho ''
113952400Sbillf
114052400SbillfTEST_FOR_FILES=`find ${TEMPROOT} -type f -size +0 2>/dev/null`
114152400Sbillfif [ -n "${TEST_FOR_FILES}" ]; then
114252400Sbillf  echo "*** Files that remain for you to merge by hand:"
1143200700Sdougb  find "${TEMPROOT}" -type f -size +0 | sort
114477335Sdougb  echo ''
114552400Sbillf
1146201765Sdougb  case "${AUTO_RUN}" in
1147201765Sdougb  '')
1148201765Sdougb    echo -n "Do you wish to delete what is left of ${TEMPROOT}? [no] "
1149201765Sdougb    read DEL_TEMPROOT
1150201765Sdougb    case "${DEL_TEMPROOT}" in
1151201765Sdougb    [yY]*)
1152201765Sdougb      delete_temproot
1153201765Sdougb      ;;
1154201765Sdougb    *)
1155201765Sdougb      echo " *** ${TEMPROOT} will remain"
1156201765Sdougb      ;;
1157201765Sdougb    esac
115852400Sbillf    ;;
1159201765Sdougb  *) ;;
116052400Sbillf  esac
1161201765Sdougbelse
1162201765Sdougb  echo "*** ${TEMPROOT} is empty, deleting"
1163201765Sdougb  delete_temproot
1164201765Sdougbfi
116552400Sbillf
116668153Sdougbcase "${AUTO_INSTALLED_FILES}" in
116768153Sdougb'') ;;
116868153Sdougb*)
116973651Sdougb  case "${AUTO_RUN}" in
117073651Sdougb  '')
117173651Sdougb    (
117273651Sdougb      echo ''
117377323Sdougb      echo '*** You chose the automatic install option for files that did not'
117477323Sdougb      echo '    exist on your system.  The following were installed for you:'
117573651Sdougb      echo "${AUTO_INSTALLED_FILES}"
117673651Sdougb    ) | ${PAGER}
117773651Sdougb    ;;
117873651Sdougb  *)
117968153Sdougb    echo ''
118077323Sdougb    echo '*** You chose the automatic install option for files that did not'
118177323Sdougb    echo '    exist on your system.  The following were installed for you:'
118268153Sdougb    echo "${AUTO_INSTALLED_FILES}"
118373651Sdougb    ;;
118473651Sdougb  esac
118568153Sdougb  ;;
118668153Sdougbesac
118768153Sdougb
1188158149Sgordoncase "${AUTO_UPGRADED_FILES}" in
1189158149Sgordon'') ;;
1190158149Sgordon*)
1191158149Sgordon  case "${AUTO_RUN}" in
1192158149Sgordon  '')
1193158149Sgordon    (
1194158149Sgordon      echo ''
1195158149Sgordon      echo '*** You chose the automatic upgrade option for files that you did'
1196158149Sgordon      echo '    not alter on your system.  The following were upgraded for you:'
1197158149Sgordon      echo "${AUTO_UPGRADED_FILES}"
1198158149Sgordon    ) | ${PAGER}
1199158149Sgordon    ;;
1200158149Sgordon  *)
1201158149Sgordon    echo ''
1202158149Sgordon    echo '*** You chose the automatic upgrade option for files that you did'
1203158149Sgordon    echo '    not alter on your system.  The following were upgraded for you:'
1204158149Sgordon    echo "${AUTO_UPGRADED_FILES}"
1205158149Sgordon    ;;
1206158149Sgordon  esac
1207158149Sgordon  ;;
1208158149Sgordonesac
1209158149Sgordon
121073651Sdougbrun_it_now () {
121173651Sdougb  case "${AUTO_RUN}" in
121273651Sdougb  '')
121373651Sdougb    unset YES_OR_NO
121473651Sdougb    echo ''
121577335Sdougb    echo -n '    Would you like to run it now? y or n [n] '
121673651Sdougb    read YES_OR_NO
121773651Sdougb
121873651Sdougb    case "${YES_OR_NO}" in
121973651Sdougb    y)
122077335Sdougb      echo "    Running ${1}"
122177335Sdougb      echo ''
122273651Sdougb      eval "${1}"
122373651Sdougb      ;;
122477335Sdougb    ''|n)
122577335Sdougb      echo ''
122677335Sdougb      echo "       *** Cancelled"
122777335Sdougb      echo ''
122877335Sdougb      echo "    Make sure to run ${1} yourself"
122977335Sdougb      ;;
123073651Sdougb    *)
123177335Sdougb      echo ''
123277335Sdougb      echo "       *** Sorry, I do not understand your answer (${YES_OR_NO})"
123377335Sdougb      echo ''
123477335Sdougb      echo "    Make sure to run ${1} yourself"
123573651Sdougb    esac
123673651Sdougb    ;;
123773651Sdougb  *) ;;
123873651Sdougb  esac
123973651Sdougb}
124073651Sdougb
124152400Sbillfcase "${NEED_NEWALIASES}" in
124252400Sbillf'') ;;
124352400Sbillf*)
124452400Sbillf  echo ''
124577335Sdougb  if [ -n "${DESTDIR}" ]; then
124677335Sdougb    echo "*** You installed a new aliases file into ${DESTDIR}/etc/mail, but"
124777335Sdougb    echo "    the newaliases command is limited to the directories configured"
124877335Sdougb    echo "    in sendmail.cf.  Make sure to create your aliases database by"
124977335Sdougb    echo "    hand when your sendmail configuration is done."
125077335Sdougb  else
125177335Sdougb    echo "*** You installed a new aliases file, so make sure that you run"
125277335Sdougb    echo "    '/usr/bin/newaliases' to rebuild your aliases database"
125377335Sdougb    run_it_now '/usr/bin/newaliases'
125477335Sdougb  fi
125552400Sbillf  ;;
125652400Sbillfesac
125752400Sbillf
125852400Sbillfcase "${NEED_CAP_MKDB}" in
125952400Sbillf'') ;;
126052400Sbillf*)
126152400Sbillf  echo ''
126252400Sbillf  echo "*** You installed a login.conf file, so make sure that you run"
126377335Sdougb  echo "    '/usr/bin/cap_mkdb ${DESTDIR}/etc/login.conf'"
126477335Sdougb  echo "     to rebuild your login.conf database"
126577335Sdougb  run_it_now "/usr/bin/cap_mkdb ${DESTDIR}/etc/login.conf"
126652400Sbillf  ;;
126752400Sbillfesac
126852400Sbillf
126952400Sbillfcase "${NEED_PWD_MKDB}" in
127052400Sbillf'') ;;
127152400Sbillf*)
127252400Sbillf  echo ''
127352400Sbillf  echo "*** You installed a new master.passwd file, so make sure that you run"
127477335Sdougb  if [ -n "${DESTDIR}" ]; then
127577335Sdougb    echo "    '/usr/sbin/pwd_mkdb -d ${DESTDIR}/etc -p ${DESTDIR}/etc/master.passwd'"
127677335Sdougb    echo "    to rebuild your password files"
127777335Sdougb    run_it_now "/usr/sbin/pwd_mkdb -d ${DESTDIR}/etc -p ${DESTDIR}/etc/master.passwd"
127877335Sdougb  else
127977335Sdougb    echo "    '/usr/sbin/pwd_mkdb -p /etc/master.passwd'"
128077335Sdougb    echo "     to rebuild your password files"
128177335Sdougb    run_it_now '/usr/sbin/pwd_mkdb -p /etc/master.passwd'
128277335Sdougb  fi
128352400Sbillf  ;;
128452400Sbillfesac
128552400Sbillf
128652400Sbillfecho ''
128752400Sbillf
128867949Sdougbif [ -r "${MM_EXIT_SCRIPT}" ]; then
128967949Sdougb  . "${MM_EXIT_SCRIPT}"
129067949Sdougbfi
129167949Sdougb
129291193Sdougbcase "${COMP_CONFS}" in
129391193Sdougb'') ;;
129491193Sdougb*)
129591193Sdougb  . ${DESTDIR}/etc/defaults/rc.conf
129691193Sdougb
129791193Sdougb  (echo ''
129891193Sdougb  echo "*** Comparing conf files: ${rc_conf_files}"
129991193Sdougb
130091193Sdougb  for CONF_FILE in ${rc_conf_files}; do
130191193Sdougb    if [ -r "${DESTDIR}${CONF_FILE}" ]; then
130291193Sdougb      echo ''
130391193Sdougb      echo "*** From ${DESTDIR}${CONF_FILE}"
130491193Sdougb      echo "*** From ${DESTDIR}/etc/defaults/rc.conf"
130591193Sdougb
130691193Sdougb      for RC_CONF_VAR in `grep -i ^[a-z] ${DESTDIR}${CONF_FILE} |
130791193Sdougb        cut -d '=' -f 1`; do
130891193Sdougb        echo ''
130991223Sdougb        grep -w ^${RC_CONF_VAR} ${DESTDIR}${CONF_FILE}
131091223Sdougb        grep -w ^${RC_CONF_VAR} ${DESTDIR}/etc/defaults/rc.conf ||
131191193Sdougb          echo ' * No default variable with this name'
131291193Sdougb      done
131391193Sdougb    fi
131491193Sdougb  done) | ${PAGER}
131591193Sdougb  echo ''
131691193Sdougb  ;;
131791193Sdougbesac
131891193Sdougb
131991193Sdougbcase "${PRE_WORLD}" in
132091193Sdougb'') ;;
132191193Sdougb*)
1322186678Sdougb  MAKE_CONF="${SOURCEDIR}/share/examples/etc/make.conf"
132391193Sdougb
132491193Sdougb  (echo ''
132591193Sdougb  echo '*** Comparing make variables'
132691193Sdougb  echo ''
132791193Sdougb  echo "*** From ${DESTDIR}/etc/make.conf"
132891193Sdougb  echo "*** From ${MAKE_CONF}"
132991193Sdougb
1330101348Sdougb  for MAKE_VAR in `grep -i ^[a-z] ${DESTDIR}/etc/make.conf | cut -d '=' -f 1`; do
133191193Sdougb    echo ''
133291223Sdougb    grep -w ^${MAKE_VAR} ${DESTDIR}/etc/make.conf
133391223Sdougb    grep -w ^#${MAKE_VAR} ${MAKE_CONF} ||
133491193Sdougb      echo ' * No example variable with this name'
133591193Sdougb  done) | ${PAGER}
133691193Sdougb  ;;
133791193Sdougbesac
133891193Sdougb
1339200425Sdougbif [ -n "${PRESERVE_FILES}" ]; then
1340200425Sdougb  find -d $PRESERVE_FILES_DIR -type d -empty -delete 2>/dev/null
1341200425Sdougb  rmdir $PRESERVE_FILES_DIR 2>/dev/null
1342200425Sdougbfi
1343200425Sdougb
134452400Sbillfexit 0
1345