mergemaster.sh revision 192230
1#!/bin/sh
2
3# mergemaster
4
5# Compare files created by /usr/src/etc/Makefile (or the directory
6# the user specifies) with the currently installed copies.
7
8# Copyright 1998-2009 Douglas Barton
9# DougB@FreeBSD.org
10
11# $FreeBSD: head/usr.sbin/mergemaster/mergemaster.sh 192230 2009-05-16 22:22:31Z dougb $
12
13PATH=/bin:/usr/bin:/usr/sbin
14
15display_usage () {
16  VERSION_NUMBER=`grep "[$]FreeBSD:" $0 | cut -d ' ' -f 4`
17  echo "mergemaster version ${VERSION_NUMBER}"
18  echo 'Usage: mergemaster [-scrvahipFCPU]'
19  echo '    [-m /path] [-t /path] [-d] [-u N] [-w N] [-A arch] [-D /path]'
20  echo "Options:"
21  echo "  -s  Strict comparison (diff every pair of files)"
22  echo "  -c  Use context diff instead of unified diff"
23  echo "  -r  Re-run on a previously cleaned directory (skip temproot creation)"
24  echo "  -v  Be more verbose about the process, include additional checks"
25  echo "  -a  Leave all files that differ to merge by hand"
26  echo "  -h  Display more complete help"
27  echo '  -i  Automatically install files that do not exist in destination directory'
28  echo '  -p  Pre-buildworld mode, only compares crucial files'
29  echo '  -F  Install files that differ only by revision control Id ($FreeBSD)'
30  echo '  -C  Compare local rc.conf variables to the defaults'
31  echo '  -P  Preserve files that are overwritten'
32  echo "  -U  Attempt to auto upgrade files that have not been user modified"
33  echo ''
34  echo "  -m /path/directory  Specify location of source to do the make in"
35  echo "  -t /path/directory  Specify temp root directory"
36  echo "  -d  Add date and time to directory name (e.g., /var/tmp/temproot.`date +%m%d.%H.%M`)"
37  echo "  -u N  Specify a numeric umask"
38  echo "  -w N  Specify a screen width in columns to sdiff"
39  echo "  -A architecture  Alternative architecture name to pass to make"
40  echo '  -D /path/directory  Specify the destination directory to install files to'
41  echo ''
42}
43
44display_help () {
45  echo "* To specify a directory other than /var/tmp/temproot for the"
46  echo "  temporary root environment, use -t /path/to/temp/root"
47  echo "* The -w option takes a number as an argument for the column width"
48  echo "  of the screen.  The default is 80."
49  echo '* The -a option causes mergemaster to run without prompting.'
50}
51
52# Loop allowing the user to use sdiff to merge files and display the merged
53# file.
54merge_loop () {
55  case "${VERBOSE}" in
56  '') ;;
57  *)
58      echo "   *** Type h at the sdiff prompt (%) to get usage help"
59      ;;
60  esac
61  echo ''
62  MERGE_AGAIN=yes
63  while [ "${MERGE_AGAIN}" = "yes" ]; do
64    # Prime file.merged so we don't blat the owner/group id's
65    cp -p "${COMPFILE}" "${COMPFILE}.merged"
66    sdiff -o "${COMPFILE}.merged" --text --suppress-common-lines \
67      --width=${SCREEN_WIDTH:-80} "${DESTDIR}${COMPFILE#.}" "${COMPFILE}"
68    INSTALL_MERGED=V
69    while [ "${INSTALL_MERGED}" = "v" -o "${INSTALL_MERGED}" = "V" ]; do
70      echo ''
71      echo "  Use 'i' to install merged file"
72      echo "  Use 'r' to re-do the merge"
73      echo "  Use 'v' to view the merged file"
74      echo "  Default is to leave the temporary file to deal with by hand"
75      echo ''
76      echo -n "    *** How should I deal with the merged file? [Leave it for later] "
77      read INSTALL_MERGED
78
79      case "${INSTALL_MERGED}" in
80      [iI])
81        mv "${COMPFILE}.merged" "${COMPFILE}"
82        echo ''
83        if mm_install "${COMPFILE}"; then
84          echo "     *** Merged version of ${COMPFILE} installed successfully"
85        else
86          echo "     *** Problem installing ${COMPFILE}, it will remain to merge by hand later"
87        fi
88        unset MERGE_AGAIN
89        ;;
90      [rR])
91        rm "${COMPFILE}.merged"
92        ;;
93      [vV])
94        ${PAGER} "${COMPFILE}.merged"
95        ;;
96      '')
97        echo "   *** ${COMPFILE} will remain for your consideration"
98        unset MERGE_AGAIN
99        ;;
100      *)
101        echo "invalid choice: ${INSTALL_MERGED}"
102        INSTALL_MERGED=V
103        ;;
104      esac
105    done
106  done
107}
108
109# Loop showing user differences between files, allow merge, skip or install
110# options
111diff_loop () {
112
113  HANDLE_COMPFILE=v
114
115  while [ "${HANDLE_COMPFILE}" = "v" -o "${HANDLE_COMPFILE}" = "V" -o \
116    "${HANDLE_COMPFILE}" = "NOT V" ]; do
117    if [ -f "${DESTDIR}${COMPFILE#.}" -a -f "${COMPFILE}" ]; then
118      if [ -n "${AUTO_UPGRADE}" -a -n "${CHANGED}" ]; then
119        case "${CHANGED}" in
120        *:${DESTDIR}${COMPFILE#.}:*) ;;		# File has been modified
121        *)
122          echo ''
123          echo "  *** ${COMPFILE} has not been user modified."
124          echo ''
125
126          if mm_install "${COMPFILE}"; then
127            echo "   *** ${COMPFILE} upgraded successfully"
128            echo ''
129            # Make the list print one file per line
130            AUTO_UPGRADED_FILES="${AUTO_UPGRADED_FILES}      ${DESTDIR}${COMPFILE#.}
131"
132          else
133            echo "   *** Problem upgrading ${COMPFILE}, it will remain to merge by hand"
134          fi
135          return
136          ;;
137        esac
138      fi
139      if [ "${HANDLE_COMPFILE}" = "v" -o "${HANDLE_COMPFILE}" = "V" ]; then
140	echo ''
141	echo '   ======================================================================   '
142	echo ''
143        (
144          echo "  *** Displaying differences between ${COMPFILE} and installed version:"
145          echo ''
146          diff ${DIFF_FLAG} ${DIFF_OPTIONS} "${DESTDIR}${COMPFILE#.}" "${COMPFILE}"
147        ) | ${PAGER}
148        echo ''
149      fi
150    else
151      echo ''
152      echo "  *** There is no installed version of ${COMPFILE}"
153      echo ''
154      case "${AUTO_INSTALL}" in
155      [Yy][Ee][Ss])
156        echo ''
157        if mm_install "${COMPFILE}"; then
158          echo "   *** ${COMPFILE} installed successfully"
159          echo ''
160          # Make the list print one file per line
161          AUTO_INSTALLED_FILES="${AUTO_INSTALLED_FILES}      ${DESTDIR}${COMPFILE#.}
162"
163        else
164          echo "   *** Problem installing ${COMPFILE}, it will remain to merge by hand"
165        fi
166        return
167        ;;
168      *)
169        NO_INSTALLED=yes
170        ;;
171      esac
172    fi
173
174    echo "  Use 'd' to delete the temporary ${COMPFILE}"
175    echo "  Use 'i' to install the temporary ${COMPFILE}"
176    case "${NO_INSTALLED}" in
177    '')
178      echo "  Use 'm' to merge the temporary and installed versions"
179      echo "  Use 'v' to view the diff results again"
180      ;;
181    esac
182    echo ''
183    echo "  Default is to leave the temporary file to deal with by hand"
184    echo ''
185    echo -n "How should I deal with this? [Leave it for later] "
186    read HANDLE_COMPFILE
187
188    case "${HANDLE_COMPFILE}" in
189    [dD])
190      rm "${COMPFILE}"
191      echo ''
192      echo "   *** Deleting ${COMPFILE}"
193      ;;
194    [iI])
195      echo ''
196      if mm_install "${COMPFILE}"; then
197        echo "   *** ${COMPFILE} installed successfully"
198      else
199        echo "   *** Problem installing ${COMPFILE}, it will remain to merge by hand"
200      fi
201      ;;
202    [mM])
203      case "${NO_INSTALLED}" in
204      '')
205        # interact with user to merge files
206        merge_loop
207        ;;
208      *)
209        echo ''
210        echo "   *** There is no installed version of ${COMPFILE}"
211        echo ''
212        HANDLE_COMPFILE="NOT V"
213        ;;
214      esac # End of "No installed version of file but user selected merge" test
215      ;;
216    [vV])
217      continue
218      ;;
219    '')
220      echo ''
221      echo "   *** ${COMPFILE} will remain for your consideration"
222      ;;
223    *)
224      # invalid choice, show menu again.
225      echo "invalid choice: ${HANDLE_COMPFILE}"
226      echo ''
227      HANDLE_COMPFILE="NOT V"
228      continue
229      ;;
230    esac  # End of "How to handle files that are different"
231  done
232  unset NO_INSTALLED
233  echo ''
234  case "${VERBOSE}" in
235  '') ;;
236  *)
237    sleep 3
238    ;;
239  esac
240}
241
242press_to_continue () {
243  local DISCARD
244  echo -n ' *** Press the [Enter] or [Return] key to continue '
245  read DISCARD
246}
247
248# Set the default path for the temporary root environment
249#
250TEMPROOT='/var/tmp/temproot'
251
252# Read /etc/mergemaster.rc first so the one in $HOME can override
253#
254if [ -r /etc/mergemaster.rc ]; then
255  . /etc/mergemaster.rc
256fi
257
258# Read .mergemasterrc before command line so CLI can override
259#
260if [ -r "$HOME/.mergemasterrc" ]; then
261  . "$HOME/.mergemasterrc"
262fi
263
264# Assign the location of the mtree database
265#
266MTREEDB=${MTREEDB:-/var/db}
267MTREEFILE="${MTREEDB}/mergemaster.mtree"
268
269# Check the command line options
270#
271while getopts ":ascrvhipCPm:t:du:w:D:A:FU" COMMAND_LINE_ARGUMENT ; do
272  case "${COMMAND_LINE_ARGUMENT}" in
273  A)
274    ARCHSTRING='TARGET_ARCH='${OPTARG}
275    ;;
276  F)
277    FREEBSD_ID=yes
278    ;;
279  U)
280    AUTO_UPGRADE=yes
281    ;;
282  s)
283    STRICT=yes
284    unset DIFF_OPTIONS
285    ;;
286  c)
287    DIFF_FLAG='-c'
288    ;;
289  r)
290    RERUN=yes
291    ;;
292  v)
293    case "${AUTO_RUN}" in
294    '') VERBOSE=yes ;;
295    esac
296    ;;
297  a)
298    AUTO_RUN=yes
299    unset VERBOSE
300    ;;
301  h)
302    display_usage
303    display_help
304    exit 0
305    ;;
306  i)
307    AUTO_INSTALL=yes
308    ;;
309  C)
310    COMP_CONFS=yes
311    ;;
312  P)
313    PRESERVE_FILES=yes
314    ;;
315  p)
316    PRE_WORLD=yes
317    unset COMP_CONFS
318    unset AUTO_RUN
319    ;;
320  m)
321    SOURCEDIR=${OPTARG}
322    ;;
323  t)
324    TEMPROOT=${OPTARG}
325    ;;
326  d)
327    TEMPROOT=${TEMPROOT}.`date +%m%d.%H.%M`
328    ;;
329  u)
330    NEW_UMASK=${OPTARG}
331    ;;
332  w)
333    SCREEN_WIDTH=${OPTARG}
334    ;;
335  D)
336    DESTDIR=${OPTARG}
337    ;;
338  *)
339    display_usage
340    exit 1
341    ;;
342  esac
343done
344
345# Don't force the user to set this in the mergemaster rc file
346if [ -n "${PRESERVE_FILES}" -a -z "${PRESERVE_FILES_DIR}" ]; then
347  PRESERVE_FILES_DIR=/var/tmp/mergemaster/preserved-files-`date +%y%m%d-%H%M%S`
348fi
349
350# Check for the mtree database in DESTDIR
351case "${AUTO_UPGRADE}" in
352'') ;;	# If the option is not set no need to run the test or warn the user
353*)
354  if [ ! -s "${DESTDIR}${MTREEFILE}" ]; then
355    echo ''
356    echo "*** Unable to find mtree database. Skipping auto-upgrade."
357    echo ''
358    press_to_continue
359    unset AUTO_UPGRADE
360  fi
361  ;;
362esac
363
364if [ -e "${DESTDIR}/etc/fstab" ]; then
365  if grep -q nodev ${DESTDIR}/etc/fstab; then
366    echo ''
367    echo "*** You have the deprecated 'nodev' option in ${DESTDIR}/etc/fstab."
368    echo "    This can prevent the filesystem from being mounted on reboot."
369    echo "    Please update your fstab before continuing."
370    echo "    See fstab(5) for more information."
371    echo ''
372    exit 1
373  fi
374fi
375
376echo ''
377
378# If the user has a pager defined, make sure we can run it
379#
380case "${DONT_CHECK_PAGER}" in
381'')
382check_pager () {
383  while ! type "${PAGER%% *}" >/dev/null; do
384    echo " *** Your PAGER environment variable specifies '${PAGER}', but"
385    echo "     due to the limited PATH that I use for security reasons,"
386    echo "     I cannot execute it.  So, what would you like to do?"
387    echo ''
388    echo "  Use 'e' to exit mergemaster and fix your PAGER variable"
389    if [ -x /usr/bin/less -o -x /usr/local/bin/less ]; then
390    echo "  Use 'l' to set PAGER to 'less' for this run"
391    fi
392    echo "  Use 'm' to use plain old 'more' as your PAGER for this run"
393    echo ''
394    echo "  Default is to use plain old 'more' "
395    echo ''
396    echo -n "What should I do? [Use 'more'] "
397    read FIXPAGER
398
399    case "${FIXPAGER}" in
400    [eE])
401       exit 0
402       ;;
403    [lL])
404       if [ -x /usr/bin/less ]; then
405         PAGER=/usr/bin/less
406       elif [ -x /usr/local/bin/less ]; then
407         PAGER=/usr/local/bin/less
408       else
409         echo ''
410         echo " *** Fatal Error:"
411         echo "     You asked to use 'less' as your pager, but I can't"
412         echo "     find it in /usr/bin or /usr/local/bin"
413         exit 1
414       fi
415       ;;
416    [mM]|'')
417       PAGER=more
418       ;;
419    *)
420       echo ''
421       echo "invalid choice: ${FIXPAGER}"
422    esac
423    echo ''
424  done
425}
426  if [ -n "${PAGER}" ]; then
427    check_pager
428  fi
429  ;;
430esac
431
432# If user has a pager defined, or got assigned one above, use it.
433# If not, use more.
434#
435PAGER=${PAGER:-more}
436
437if [ -n "${VERBOSE}" -a ! "${PAGER}" = "more" ]; then
438  echo " *** You have ${PAGER} defined as your pager so we will use that"
439  echo ''
440  sleep 3
441fi
442
443# Assign the diff flag once so we will not have to keep testing it
444#
445DIFF_FLAG=${DIFF_FLAG:--u}
446
447# Assign the source directory
448#
449SOURCEDIR=${SOURCEDIR:-/usr/src}
450if [ ! -f ${SOURCEDIR}/Makefile.inc1 -a \
451   -f ${SOURCEDIR}/../Makefile.inc1 ]; then
452  echo " *** The source directory you specified (${SOURCEDIR})"
453  echo "     will be reset to ${SOURCEDIR}/.."
454  echo ''
455  sleep 3
456  SOURCEDIR=${SOURCEDIR}/..
457fi
458
459# Setup make to use system files from SOURCEDIR
460MM_MAKE="make ${ARCHSTRING} -m ${SOURCEDIR}/share/mk"
461
462# Check DESTDIR against the mergemaster mtree database to see what
463# files the user changed from the reference files.
464#
465if [ -n "${AUTO_UPGRADE}" -a -s "${DESTDIR}${MTREEFILE}" ]; then
466	CHANGED=:
467	for file in `mtree -eqL -f ${DESTDIR}${MTREEFILE} -p ${DESTDIR}/ \
468		2>/dev/null | awk '($2 == "changed") {print $1}'`; do
469		if [ -f "${DESTDIR}/$file" ]; then
470			CHANGED="${CHANGED}${DESTDIR}/${file}:"
471		fi
472	done
473	[ "$CHANGED" = ':' ] && unset CHANGED
474fi
475
476# Check the width of the user's terminal
477#
478if [ -t 0 ]; then
479  w=`tput columns`
480  case "${w}" in
481  0|'') ;; # No-op, since the input is not valid
482  *)
483    case "${SCREEN_WIDTH}" in
484    '') SCREEN_WIDTH="${w}" ;;
485    "${w}") ;; # No-op, since they are the same
486    *)
487      echo -n "*** You entered ${SCREEN_WIDTH} as your screen width, but stty "
488      echo "thinks it is ${w}."
489      echo ''
490      echo -n "What would you like to use? [${w}] "
491      read SCREEN_WIDTH
492      case "${SCREEN_WIDTH}" in
493      '') SCREEN_WIDTH="${w}" ;;
494      esac
495      ;;
496    esac
497  esac
498fi
499
500# Define what CVS $Id tag to look for to aid portability.
501#
502CVS_ID_TAG=FreeBSD
503
504delete_temproot () {
505  rm -rf "${TEMPROOT}" 2>/dev/null
506  chflags -R 0 "${TEMPROOT}" 2>/dev/null
507  rm -rf "${TEMPROOT}" || exit 1
508}
509
510case "${RERUN}" in
511'')
512  # Set up the loop to test for the existence of the
513  # temp root directory.
514  #
515  TEST_TEMP_ROOT=yes
516  while [ "${TEST_TEMP_ROOT}" = "yes" ]; do
517    if [ -d "${TEMPROOT}" ]; then
518      echo "*** The directory specified for the temporary root environment,"
519      echo "    ${TEMPROOT}, exists.  This can be a security risk if untrusted"
520      echo "    users have access to the system."
521      echo ''
522      case "${AUTO_RUN}" in
523      '')
524        echo "  Use 'd' to delete the old ${TEMPROOT} and continue"
525        echo "  Use 't' to select a new temporary root directory"
526        echo "  Use 'e' to exit mergemaster"
527        echo ''
528        echo "  Default is to use ${TEMPROOT} as is"
529        echo ''
530        echo -n "How should I deal with this? [Use the existing ${TEMPROOT}] "
531        read DELORNOT
532
533        case "${DELORNOT}" in
534        [dD])
535          echo ''
536          echo "   *** Deleting the old ${TEMPROOT}"
537          echo ''
538          delete_temproot || exit 1
539          unset TEST_TEMP_ROOT
540          ;;
541        [tT])
542          echo "   *** Enter new directory name for temporary root environment"
543          read TEMPROOT
544          ;;
545        [eE])
546          exit 0
547          ;;
548        '')
549          echo ''
550          echo "   *** Leaving ${TEMPROOT} intact"
551          echo ''
552          unset TEST_TEMP_ROOT
553          ;;
554        *)
555          echo ''
556          echo "invalid choice: ${DELORNOT}"
557          echo ''
558          ;;
559        esac
560        ;;
561      *)
562        # If this is an auto-run, try a hopefully safe alternative then
563        # re-test anyway.
564        TEMPROOT=/var/tmp/temproot.`date +%m%d.%H.%M.%S`
565        ;;
566      esac
567    else
568      unset TEST_TEMP_ROOT
569    fi
570  done
571
572  echo "*** Creating the temporary root environment in ${TEMPROOT}"
573
574  if mkdir -p "${TEMPROOT}"; then
575    echo " *** ${TEMPROOT} ready for use"
576  fi
577
578  if [ ! -d "${TEMPROOT}" ]; then
579    echo ''
580    echo "  *** FATAL ERROR: Cannot create ${TEMPROOT}"
581    echo ''
582    exit 1
583  fi
584
585  echo " *** Creating and populating directory structure in ${TEMPROOT}"
586  echo ''
587
588  case "${VERBOSE}" in
589  '') ;;
590  *)
591    press_to_continue
592    ;;
593  esac
594
595  case "${PRE_WORLD}" in
596  '')
597    { cd ${SOURCEDIR} &&
598      case "${DESTDIR}" in
599      '') ;;
600      *)
601        ${MM_MAKE} DESTDIR=${DESTDIR} distrib-dirs
602        ;;
603      esac
604      od=${TEMPROOT}/usr/obj
605      ${MM_MAKE} DESTDIR=${TEMPROOT} distrib-dirs &&
606      MAKEOBJDIRPREFIX=$od ${MM_MAKE} _obj SUBDIR_OVERRIDE=etc &&
607      MAKEOBJDIRPREFIX=$od ${MM_MAKE} everything SUBDIR_OVERRIDE=etc &&
608      MAKEOBJDIRPREFIX=$od ${MM_MAKE} DESTDIR=${TEMPROOT} distribution;} ||
609    { echo '';
610     echo "  *** FATAL ERROR: Cannot 'cd' to ${SOURCEDIR} and install files to";
611      echo "      the temproot environment";
612      echo '';
613      exit 1;}
614    ;;
615  *)
616    # Only set up files that are crucial to {build|install}world
617    { mkdir -p ${TEMPROOT}/etc &&
618      cp -p ${SOURCEDIR}/etc/master.passwd ${TEMPROOT}/etc &&
619      cp -p ${SOURCEDIR}/etc/group ${TEMPROOT}/etc;} ||
620    { echo '';
621      echo '  *** FATAL ERROR: Cannot copy files to the temproot environment';
622      echo '';
623      exit 1;}
624    ;;
625  esac
626
627  # Doing the inventory and removing files that we don't want to compare only
628  # makes sense if we are not doing a rerun, since we have no way of knowing
629  # what happened to the files during previous incarnations.
630  case "${VERBOSE}" in
631  '') ;;
632  *)
633    echo ''
634    echo ' *** The following files exist only in the installed version of'
635    echo "     ${DESTDIR}/etc.  In the vast majority of cases these files"
636    echo '     are necessary parts of the system and should not be deleted.'
637    echo '     However because these files are not updated by this process you'
638    echo '     might want to verify their status before rebooting your system.'
639    echo ''
640    press_to_continue
641    diff -qr ${DESTDIR}/etc ${TEMPROOT}/etc | grep "^Only in ${DESTDIR}/etc" | ${PAGER}
642    echo ''
643    press_to_continue
644    ;;
645  esac
646
647  # Avoid comparing the motd if the user specifies it in .mergemasterrc
648  # Compatibility shim to be removed in FreeBSD 9.x
649  case "${IGNORE_MOTD}" in
650  '') ;;
651  *) IGNORE_FILES="${IGNORE_FILES} /etc/motd"
652     echo ''
653     echo "*** You have the IGNORE_MOTD option set in your mergemaster rc file."
654     echo "    This option is deprecated in favor of the IGNORE_FILES option."
655     echo "    Please update your rc file accordingly."
656     echo ''
657     press_to_continue
658     ;;
659  esac
660
661  # Avoid comparing the following user specified files
662  for file in ${IGNORE_FILES}; do
663    test -e ${TEMPROOT}/${file} && unlink ${TEMPROOT}/${file}
664  done
665  ;; # End of the "RERUN" test
666esac
667
668# We really don't want to have to deal with files like login.conf.db, pwd.db,
669# or spwd.db.  Instead, we want to compare the text versions, and run *_mkdb.
670# Prompt the user to do so below, as needed.
671#
672rm -f ${TEMPROOT}/etc/*.db ${TEMPROOT}/etc/passwd
673
674# We only need to compare things like freebsd.cf once
675find ${TEMPROOT}/usr/obj -type f -delete 2>/dev/null
676
677# Delete 0 length files to make the mtree database as small as possible.
678find ${TEMPROOT} -type f -size 0 -delete 2>/dev/null
679
680# Build the mtree database in a temporary location.
681MTREENEW=`mktemp -t mergemaster.mtree`
682case "${PRE_WORLD}" in
683'') mtree -ci -p ${TEMPROOT} -k size,md5digest > ${MTREENEW} 2>/dev/null
684    ;;
685*) # We don't want to mess with the mtree database on a pre-world run.
686   ;;
687esac
688
689# Get ready to start comparing files
690
691# Check umask if not specified on the command line,
692# and we are not doing an autorun
693#
694if [ -z "${NEW_UMASK}" -a -z "${AUTO_RUN}" ]; then
695  USER_UMASK=`umask`
696  case "${USER_UMASK}" in
697  0022|022) ;;
698  *)
699    echo ''
700    echo " *** Your umask is currently set to ${USER_UMASK}.  By default, this script"
701    echo "     installs all files with the same user, group and modes that"
702    echo "     they are created with by ${SOURCEDIR}/etc/Makefile, compared to"
703    echo "     a umask of 022.  This umask allows world read permission when"
704    echo "     the file's default permissions have it."
705    echo ''
706    echo "     No world permissions can sometimes cause problems.  A umask of"
707    echo "     022 will restore the default behavior, but is not mandatory."
708    echo "     /etc/master.passwd is a special case.  Its file permissions"
709    echo "     will be 600 (rw-------) if installed."
710    echo ''
711    echo -n "What umask should I use? [${USER_UMASK}] "
712    read NEW_UMASK
713
714    NEW_UMASK="${NEW_UMASK:-$USER_UMASK}"
715    ;;
716  esac
717  echo ''
718fi
719
720CONFIRMED_UMASK=${NEW_UMASK:-0022}
721
722#
723# Warn users who still have old rc files
724#
725for file in atm devfs diskless1 diskless2 network network6 pccard \
726  serial syscons sysctl alpha amd64 i386 ia64 sparc64; do
727  if [ -f "${DESTDIR}/etc/rc.${file}" ]; then
728    OLD_RC_PRESENT=1
729    break
730  fi
731done
732
733case "${OLD_RC_PRESENT}" in
7341)
735  echo ''
736  echo " *** There are elements of the old rc system in ${DESTDIR}/etc/."
737  echo ''
738  echo '     While these scripts will not hurt anything, they are not'
739  echo '     functional on an up to date system, and can be removed.'
740  echo ''
741
742  case "${AUTO_RUN}" in
743  '')
744    echo -n 'Move these files to /var/tmp/mergemaster/old_rc? [yes] '
745    read MOVE_OLD_RC
746
747    case "${MOVE_OLD_RC}" in
748    [nN]*) ;;
749    *)
750      mkdir -p /var/tmp/mergemaster/old_rc
751        for file in atm devfs diskless1 diskless2 network network6 pccard \
752          serial syscons sysctl alpha amd64 i386 ia64 sparc64; do
753          if [ -f "${DESTDIR}/etc/rc.${file}" ]; then
754            mv ${DESTDIR}/etc/rc.${file} /var/tmp/mergemaster/old_rc/
755          fi
756        done
757      echo '  The files have been moved'
758      press_to_continue
759      ;;
760    esac
761    ;;
762  *) ;;
763  esac
764esac
765
766# Use the umask/mode information to install the files
767# Create directories as needed
768#
769install_error () {
770  echo "*** FATAL ERROR: Unable to install ${1} to ${2}"
771  echo ''
772  exit 1
773}
774
775do_install_and_rm () {
776  case "${PRESERVE_FILES}" in
777  [Yy][Ee][Ss])
778    if [ -f "${3}/${2##*/}" ]; then
779      mkdir -p ${PRESERVE_FILES_DIR}/${2%/*}
780      cp ${3}/${2##*/} ${PRESERVE_FILES_DIR}/${2%/*}
781    fi
782    ;;
783  esac
784
785  if [ ! -d "${3}/${2##*/}" ]; then
786    if install -m ${1} ${2} ${3}; then
787      unlink ${2}
788    else
789      install_error ${2} ${3}
790    fi
791  else
792    install_error ${2} ${3}
793  fi
794}
795
796# 4095 = "obase=10;ibase=8;07777" | bc
797find_mode () {
798  local OCTAL
799  OCTAL=$(( ~$(echo "obase=10; ibase=8; ${CONFIRMED_UMASK}" | bc) & 4095 &
800    $(echo "obase=10; ibase=8; $(stat -f "%OMp%OLp" ${1})" | bc) ))
801  printf "%04o\n" ${OCTAL}
802}
803
804mm_install () {
805  local INSTALL_DIR
806  INSTALL_DIR=${1#.}
807  INSTALL_DIR=${INSTALL_DIR%/*}
808
809  case "${INSTALL_DIR}" in
810  '')
811    INSTALL_DIR=/
812    ;;
813  esac
814
815  if [ -n "${DESTDIR}${INSTALL_DIR}" -a ! -d "${DESTDIR}${INSTALL_DIR}" ]; then
816    DIR_MODE=`find_mode "${TEMPROOT}/${INSTALL_DIR}"`
817    install -d -o root -g wheel -m "${DIR_MODE}" "${DESTDIR}${INSTALL_DIR}"
818  fi
819
820  FILE_MODE=`find_mode "${1}"`
821
822  if [ ! -x "${1}" ]; then
823    case "${1#.}" in
824    /etc/mail/aliases)
825      NEED_NEWALIASES=yes
826      ;;
827    /etc/login.conf)
828      NEED_CAP_MKDB=yes
829      ;;
830    /etc/master.passwd)
831      do_install_and_rm 600 "${1}" "${DESTDIR}${INSTALL_DIR}"
832      NEED_PWD_MKDB=yes
833      DONT_INSTALL=yes
834      ;;
835    /.cshrc | /.profile)
836    case "${AUTO_INSTALL}" in
837    '')
838      case "${LINK_EXPLAINED}" in
839      '')
840        echo "   *** Historically BSD derived systems have had a"
841        echo "       hard link from /.cshrc and /.profile to"
842        echo "       their namesakes in /root.  Please indicate"
843        echo "       your preference below for bringing your"
844        echo "       installed files up to date."
845        echo ''
846        LINK_EXPLAINED=yes
847        ;;
848      esac
849
850      echo "   Use 'd' to delete the temporary ${COMPFILE}"
851      echo "   Use 'l' to delete the existing ${DESTDIR}${COMPFILE#.} and create the link"
852      echo ''
853      echo "   Default is to leave the temporary file to deal with by hand"
854      echo ''
855      echo -n "  How should I handle ${COMPFILE}? [Leave it to install later] "
856      read HANDLE_LINK
857      ;;
858    *)  # Part of AUTO_INSTALL
859      HANDLE_LINK=l
860      ;;
861    esac
862
863      case "${HANDLE_LINK}" in
864      [dD]*)
865        rm "${COMPFILE}"
866        echo ''
867        echo "   *** Deleting ${COMPFILE}"
868        ;;
869      [lL]*)
870        echo ''
871        rm -f "${DESTDIR}${COMPFILE#.}"
872        if ln "${DESTDIR}/root/${COMPFILE##*/}" "${DESTDIR}${COMPFILE#.}"; then
873          echo "   *** Link from ${DESTDIR}${COMPFILE#.} to ${DESTDIR}/root/${COMPFILE##*/} installed successfully"
874          rm "${COMPFILE}"
875        else
876          echo "   *** Error linking ${DESTDIR}${COMPFILE#.} to ${DESTDIR}/root/${COMPFILE##*/}, ${COMPFILE} will remain to install by hand"
877        fi
878        ;;
879      *)
880        echo "   *** ${COMPFILE} will remain for your consideration"
881        ;;
882      esac
883      DONT_INSTALL=yes
884      ;;
885    esac
886
887    case "${DONT_INSTALL}" in
888    '')
889      do_install_and_rm "${FILE_MODE}" "${1}" "${DESTDIR}${INSTALL_DIR}"
890      ;;
891    *)
892      unset DONT_INSTALL
893      ;;
894    esac
895  else	# File matched -x
896    do_install_and_rm "${FILE_MODE}" "${1}" "${DESTDIR}${INSTALL_DIR}"
897  fi
898  return $?
899}
900
901if [ ! -d "${TEMPROOT}" ]; then
902	echo "*** FATAL ERROR: The temproot directory (${TEMPROOT})"
903	echo '                 has disappeared!'
904	echo ''
905	exit 1
906fi
907
908echo ''
909echo "*** Beginning comparison"
910echo ''
911
912# Pre-world does not populate /etc/rc.d.
913# It is very possible that a previous run would have deleted files in
914# ${TEMPROOT}/etc/rc.d, thus creating a lot of false positives.
915if [ -z "${PRE_WORLD}" -a -z "${RERUN}" ]; then
916  echo "   *** Checking ${DESTDIR}/etc/rc.d for stale files"
917  echo ''
918  cd "${DESTDIR}/etc/rc.d" &&
919  for file in *; do
920    if [ ! -e "${TEMPROOT}/etc/rc.d/${file}" ]; then
921      STALE_RC_FILES="${STALE_RC_FILES} ${file}"
922    fi
923  done
924  case "${STALE_RC_FILES}" in
925  ''|' *')
926    echo '   *** No stale files found'
927    ;;
928  *)
929    echo "   *** The following files exist in ${DESTDIR}/etc/rc.d but not in"
930    echo "       ${TEMPROOT}/etc/rc.d/:"
931    echo ''
932    echo "${STALE_RC_FILES}"
933    echo ''
934    echo '       The presence of stale files in this directory can cause the'
935    echo '       dreaded unpredictable results, and therefore it is highly'
936    echo '       recommended that you delete them.'
937    case "${AUTO_RUN}" in
938    '')
939      echo ''
940      echo -n '   *** Delete them now? [n] '
941      read DELETE_STALE_RC_FILES
942      case "${DELETE_STALE_RC_FILES}" in
943      [yY])
944        echo '      *** Deleting ... '
945        rm ${STALE_RC_FILES}
946        echo '                       done.'
947        ;;
948      *)
949        echo '      *** Files will not be deleted'
950        ;;
951      esac
952      sleep 2
953      ;;
954    esac
955    ;;
956  esac
957  echo ''
958fi
959
960cd "${TEMPROOT}"
961
962if [ -r "${MM_PRE_COMPARE_SCRIPT}" ]; then
963  . "${MM_PRE_COMPARE_SCRIPT}"
964fi
965
966# Using -size +0 avoids uselessly checking the empty log files created
967# by ${SOURCEDIR}/etc/Makefile and the device entries in ./dev, but does
968# check the scripts in ./dev, as we'd like (assuming no devfs of course).
969#
970for COMPFILE in `find . -type f -size +0`; do
971
972  # First, check to see if the file exists in DESTDIR.  If not, the
973  # diff_loop function knows how to handle it.
974  #
975  if [ ! -e "${DESTDIR}${COMPFILE#.}" ]; then
976    case "${AUTO_RUN}" in
977      '')
978        diff_loop
979        ;;
980      *)
981        case "${AUTO_INSTALL}" in
982        '')
983          # If this is an auto run, make it official
984          echo "   *** ${COMPFILE} will remain for your consideration"
985          ;;
986        *)
987          diff_loop
988          ;;
989        esac
990        ;;
991    esac # Auto run test
992    continue
993  fi
994
995  case "${STRICT}" in
996  '' | [Nn][Oo])
997    # Compare CVS $Id's first so if the file hasn't been modified
998    # local changes will be ignored.
999    # If the files have the same $Id, delete the one in temproot so the
1000    # user will have less to wade through if files are left to merge by hand.
1001    #
1002    CVSID1=`grep "[$]${CVS_ID_TAG}:" ${DESTDIR}${COMPFILE#.} 2>/dev/null`
1003    CVSID2=`grep "[$]${CVS_ID_TAG}:" ${COMPFILE} 2>/dev/null` || CVSID2=none
1004
1005    case "${CVSID2}" in
1006    "${CVSID1}")
1007      echo " *** Temp ${COMPFILE} and installed have the same CVS Id, deleting"
1008      rm "${COMPFILE}"
1009      ;;
1010    esac
1011    ;;
1012  esac
1013
1014  # If the file is still here either because the $Ids are different, the
1015  # file doesn't have an $Id, or we're using STRICT mode; look at the diff.
1016  #
1017  if [ -f "${COMPFILE}" ]; then
1018
1019    # Do an absolute diff first to see if the files are actually different.
1020    # If they're not different, delete the one in temproot.
1021    #
1022    if diff -q ${DIFF_OPTIONS} "${DESTDIR}${COMPFILE#.}" "${COMPFILE}" > \
1023      /dev/null 2>&1; then
1024      echo " *** Temp ${COMPFILE} and installed are the same, deleting"
1025      rm "${COMPFILE}"
1026    else
1027      # Ok, the files are different, so show the user where they differ.
1028      # Use user's choice of diff methods; and user's pager if they have one.
1029      # Use more if not.
1030      # Use unified diffs by default.  Context diffs give me a headache. :)
1031      #
1032      # If the user chose the -F option, test for that before proceeding
1033      #
1034      if [ -n "$FREEBSD_ID" ]; then
1035        if diff -q -I'[$]FreeBSD:.*$' "${DESTDIR}${COMPFILE#.}" "${COMPFILE}" > \
1036            /dev/null 2>&1; then
1037          if mm_install "${COMPFILE}"; then
1038            echo "*** Updated revision control Id for ${DESTDIR}${COMPFILE#.}"
1039          else
1040            echo "*** Problem installing ${COMPFILE}, it will remain to merge by hand later"
1041          fi
1042          continue
1043        fi
1044      fi
1045      case "${AUTO_RUN}" in
1046      '')
1047        # prompt user to install/delete/merge changes
1048        diff_loop
1049        ;;
1050      *)
1051        # If this is an auto run, make it official
1052        echo "   *** ${COMPFILE} will remain for your consideration"
1053        ;;
1054      esac # Auto run test
1055    fi # Yes, the files are different
1056  fi # Yes, the file still remains to be checked
1057done # This is for the for way up there at the beginning of the comparison
1058
1059echo ''
1060echo "*** Comparison complete"
1061
1062if [ -s "${MTREENEW}" ]; then
1063  echo "*** Saving mtree database for future upgrades"
1064  test -e "${DESTDIR}${MTREEFILE}" && unlink ${DESTDIR}${MTREEFILE}
1065  mv ${MTREENEW} ${DESTDIR}${MTREEFILE}
1066fi
1067
1068echo ''
1069
1070TEST_FOR_FILES=`find ${TEMPROOT} -type f -size +0 2>/dev/null`
1071if [ -n "${TEST_FOR_FILES}" ]; then
1072  echo "*** Files that remain for you to merge by hand:"
1073  find "${TEMPROOT}" -type f -size +0
1074  echo ''
1075fi
1076
1077case "${AUTO_RUN}" in
1078'')
1079  echo -n "Do you wish to delete what is left of ${TEMPROOT}? [no] "
1080  read DEL_TEMPROOT
1081
1082  case "${DEL_TEMPROOT}" in
1083  [yY]*)
1084    if delete_temproot; then
1085      echo " *** ${TEMPROOT} has been deleted"
1086    else
1087      echo " *** Unable to delete ${TEMPROOT}"
1088    fi
1089    ;;
1090  *)
1091    echo " *** ${TEMPROOT} will remain"
1092    ;;
1093  esac
1094  ;;
1095*) ;;
1096esac
1097
1098case "${AUTO_INSTALLED_FILES}" in
1099'') ;;
1100*)
1101  case "${AUTO_RUN}" in
1102  '')
1103    (
1104      echo ''
1105      echo '*** You chose the automatic install option for files that did not'
1106      echo '    exist on your system.  The following were installed for you:'
1107      echo "${AUTO_INSTALLED_FILES}"
1108    ) | ${PAGER}
1109    ;;
1110  *)
1111    echo ''
1112    echo '*** You chose the automatic install option for files that did not'
1113    echo '    exist on your system.  The following were installed for you:'
1114    echo "${AUTO_INSTALLED_FILES}"
1115    ;;
1116  esac
1117  ;;
1118esac
1119
1120case "${AUTO_UPGRADED_FILES}" in
1121'') ;;
1122*)
1123  case "${AUTO_RUN}" in
1124  '')
1125    (
1126      echo ''
1127      echo '*** You chose the automatic upgrade option for files that you did'
1128      echo '    not alter on your system.  The following were upgraded for you:'
1129      echo "${AUTO_UPGRADED_FILES}"
1130    ) | ${PAGER}
1131    ;;
1132  *)
1133    echo ''
1134    echo '*** You chose the automatic upgrade option for files that you did'
1135    echo '    not alter on your system.  The following were upgraded for you:'
1136    echo "${AUTO_UPGRADED_FILES}"
1137    ;;
1138  esac
1139  ;;
1140esac
1141
1142run_it_now () {
1143  case "${AUTO_RUN}" in
1144  '')
1145    unset YES_OR_NO
1146    echo ''
1147    echo -n '    Would you like to run it now? y or n [n] '
1148    read YES_OR_NO
1149
1150    case "${YES_OR_NO}" in
1151    y)
1152      echo "    Running ${1}"
1153      echo ''
1154      eval "${1}"
1155      ;;
1156    ''|n)
1157      echo ''
1158      echo "       *** Cancelled"
1159      echo ''
1160      echo "    Make sure to run ${1} yourself"
1161      ;;
1162    *)
1163      echo ''
1164      echo "       *** Sorry, I do not understand your answer (${YES_OR_NO})"
1165      echo ''
1166      echo "    Make sure to run ${1} yourself"
1167    esac
1168    ;;
1169  *) ;;
1170  esac
1171}
1172
1173case "${NEED_NEWALIASES}" in
1174'') ;;
1175*)
1176  echo ''
1177  if [ -n "${DESTDIR}" ]; then
1178    echo "*** You installed a new aliases file into ${DESTDIR}/etc/mail, but"
1179    echo "    the newaliases command is limited to the directories configured"
1180    echo "    in sendmail.cf.  Make sure to create your aliases database by"
1181    echo "    hand when your sendmail configuration is done."
1182  else
1183    echo "*** You installed a new aliases file, so make sure that you run"
1184    echo "    '/usr/bin/newaliases' to rebuild your aliases database"
1185    run_it_now '/usr/bin/newaliases'
1186  fi
1187  ;;
1188esac
1189
1190case "${NEED_CAP_MKDB}" in
1191'') ;;
1192*)
1193  echo ''
1194  echo "*** You installed a login.conf file, so make sure that you run"
1195  echo "    '/usr/bin/cap_mkdb ${DESTDIR}/etc/login.conf'"
1196  echo "     to rebuild your login.conf database"
1197  run_it_now "/usr/bin/cap_mkdb ${DESTDIR}/etc/login.conf"
1198  ;;
1199esac
1200
1201case "${NEED_PWD_MKDB}" in
1202'') ;;
1203*)
1204  echo ''
1205  echo "*** You installed a new master.passwd file, so make sure that you run"
1206  if [ -n "${DESTDIR}" ]; then
1207    echo "    '/usr/sbin/pwd_mkdb -d ${DESTDIR}/etc -p ${DESTDIR}/etc/master.passwd'"
1208    echo "    to rebuild your password files"
1209    run_it_now "/usr/sbin/pwd_mkdb -d ${DESTDIR}/etc -p ${DESTDIR}/etc/master.passwd"
1210  else
1211    echo "    '/usr/sbin/pwd_mkdb -p /etc/master.passwd'"
1212    echo "     to rebuild your password files"
1213    run_it_now '/usr/sbin/pwd_mkdb -p /etc/master.passwd'
1214  fi
1215  ;;
1216esac
1217
1218echo ''
1219
1220if [ -r "${MM_EXIT_SCRIPT}" ]; then
1221  . "${MM_EXIT_SCRIPT}"
1222fi
1223
1224case "${COMP_CONFS}" in
1225'') ;;
1226*)
1227  . ${DESTDIR}/etc/defaults/rc.conf
1228
1229  (echo ''
1230  echo "*** Comparing conf files: ${rc_conf_files}"
1231
1232  for CONF_FILE in ${rc_conf_files}; do
1233    if [ -r "${DESTDIR}${CONF_FILE}" ]; then
1234      echo ''
1235      echo "*** From ${DESTDIR}${CONF_FILE}"
1236      echo "*** From ${DESTDIR}/etc/defaults/rc.conf"
1237
1238      for RC_CONF_VAR in `grep -i ^[a-z] ${DESTDIR}${CONF_FILE} |
1239        cut -d '=' -f 1`; do
1240        echo ''
1241        grep -w ^${RC_CONF_VAR} ${DESTDIR}${CONF_FILE}
1242        grep -w ^${RC_CONF_VAR} ${DESTDIR}/etc/defaults/rc.conf ||
1243          echo ' * No default variable with this name'
1244      done
1245    fi
1246  done) | ${PAGER}
1247  echo ''
1248  ;;
1249esac
1250
1251case "${PRE_WORLD}" in
1252'') ;;
1253*)
1254  MAKE_CONF="${SOURCEDIR}/share/examples/etc/make.conf"
1255
1256  (echo ''
1257  echo '*** Comparing make variables'
1258  echo ''
1259  echo "*** From ${DESTDIR}/etc/make.conf"
1260  echo "*** From ${MAKE_CONF}"
1261
1262  for MAKE_VAR in `grep -i ^[a-z] ${DESTDIR}/etc/make.conf | cut -d '=' -f 1`; do
1263    echo ''
1264    grep -w ^${MAKE_VAR} ${DESTDIR}/etc/make.conf
1265    grep -w ^#${MAKE_VAR} ${MAKE_CONF} ||
1266      echo ' * No example variable with this name'
1267  done) | ${PAGER}
1268  ;;
1269esac
1270
1271exit 0
1272
1273