mergemaster.sh revision 202817
164562Sgshapiro#!/bin/sh
264562Sgshapiro
3261194Sgshapiro# mergemaster
464562Sgshapiro
564562Sgshapiro# Compare files created by /usr/src/etc/Makefile (or the directory
664562Sgshapiro# the user specifies) with the currently installed copies.
764562Sgshapiro
864562Sgshapiro# Copyright 1998-2010 Douglas Barton
964562Sgshapiro# DougB@FreeBSD.org
1064562Sgshapiro
1164562Sgshapiro# $FreeBSD: head/usr.sbin/mergemaster/mergemaster.sh 202817 2010-01-22 17:17:47Z dougb $
1264562Sgshapiro
13266527SgshapiroPATH=/bin:/usr/bin:/usr/sbin
1464562Sgshapiro
1564562Sgshapirodisplay_usage () {
1664562Sgshapiro  VERSION_NUMBER=`grep "[$]FreeBSD:" $0 | cut -d ' ' -f 4`
1764562Sgshapiro  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# Check the command line options
265#
266while getopts ":ascrvhipCPm:t:du:w:D:A:FU" COMMAND_LINE_ARGUMENT ; do
267  case "${COMMAND_LINE_ARGUMENT}" in
268  A)
269    ARCHSTRING='TARGET_ARCH='${OPTARG}
270    ;;
271  F)
272    FREEBSD_ID=yes
273    ;;
274  U)
275    AUTO_UPGRADE=yes
276    ;;
277  s)
278    STRICT=yes
279    unset DIFF_OPTIONS
280    ;;
281  c)
282    DIFF_FLAG='-c'
283    ;;
284  r)
285    RERUN=yes
286    ;;
287  v)
288    case "${AUTO_RUN}" in
289    '') VERBOSE=yes ;;
290    esac
291    ;;
292  a)
293    AUTO_RUN=yes
294    unset VERBOSE
295    ;;
296  h)
297    display_usage
298    display_help
299    exit 0
300    ;;
301  i)
302    AUTO_INSTALL=yes
303    ;;
304  C)
305    COMP_CONFS=yes
306    ;;
307  P)
308    PRESERVE_FILES=yes
309    ;;
310  p)
311    PRE_WORLD=yes
312    unset COMP_CONFS
313    unset AUTO_RUN
314    ;;
315  m)
316    SOURCEDIR=${OPTARG}
317    ;;
318  t)
319    TEMPROOT=${OPTARG}
320    ;;
321  d)
322    TEMPROOT=${TEMPROOT}.`date +%m%d.%H.%M`
323    ;;
324  u)
325    NEW_UMASK=${OPTARG}
326    ;;
327  w)
328    SCREEN_WIDTH=${OPTARG}
329    ;;
330  D)
331    DESTDIR=${OPTARG}
332    ;;
333  *)
334    display_usage
335    exit 1
336    ;;
337  esac
338done
339
340# Assign the location of the mtree database
341#
342MTREEDB=${MTREEDB:-${DESTDIR}/var/db}
343MTREEFILE="${MTREEDB}/mergemaster.mtree"
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`
348  mkdir -p ${PRESERVE_FILES_DIR}
349fi
350
351# Check for the mtree database in DESTDIR
352case "${AUTO_UPGRADE}" in
353'') ;;	# If the option is not set no need to run the test or warn the user
354*)
355  if [ ! -s "${MTREEFILE}" ]; then
356    echo ''
357    echo "*** Unable to find mtree database (${MTREEFILE})."
358    echo "    Skipping auto-upgrade on this run."
359    echo "    It will be created for the next run when this one is complete."
360    echo ''
361    case "${AUTO_RUN}" in
362    '')
363      press_to_continue
364      ;;
365    esac
366    unset AUTO_UPGRADE
367  fi
368  ;;
369esac
370
371if [ -e "${DESTDIR}/etc/fstab" ]; then
372  if grep -q nodev ${DESTDIR}/etc/fstab; then
373    echo ''
374    echo "*** You have the deprecated 'nodev' option in ${DESTDIR}/etc/fstab."
375    echo "    This can prevent the filesystem from being mounted on reboot."
376    echo "    Please update your fstab before continuing."
377    echo "    See fstab(5) for more information."
378    echo ''
379    exit 1
380  fi
381fi
382
383echo ''
384
385# If the user has a pager defined, make sure we can run it
386#
387case "${DONT_CHECK_PAGER}" in
388'')
389check_pager () {
390  while ! type "${PAGER%% *}" >/dev/null; do
391    echo " *** Your PAGER environment variable specifies '${PAGER}', but"
392    echo "     due to the limited PATH that I use for security reasons,"
393    echo "     I cannot execute it.  So, what would you like to do?"
394    echo ''
395    echo "  Use 'e' to exit mergemaster and fix your PAGER variable"
396    if [ -x /usr/bin/less -o -x /usr/local/bin/less ]; then
397    echo "  Use 'l' to set PAGER to 'less' for this run"
398    fi
399    echo "  Use 'm' to use plain old 'more' as your PAGER for this run"
400    echo ''
401    echo "  Default is to use plain old 'more' "
402    echo ''
403    echo -n "What should I do? [Use 'more'] "
404    read FIXPAGER
405
406    case "${FIXPAGER}" in
407    [eE])
408       exit 0
409       ;;
410    [lL])
411       if [ -x /usr/bin/less ]; then
412         PAGER=/usr/bin/less
413       elif [ -x /usr/local/bin/less ]; then
414         PAGER=/usr/local/bin/less
415       else
416         echo ''
417         echo " *** Fatal Error:"
418         echo "     You asked to use 'less' as your pager, but I can't"
419         echo "     find it in /usr/bin or /usr/local/bin"
420         exit 1
421       fi
422       ;;
423    [mM]|'')
424       PAGER=more
425       ;;
426    *)
427       echo ''
428       echo "invalid choice: ${FIXPAGER}"
429    esac
430    echo ''
431  done
432}
433  if [ -n "${PAGER}" ]; then
434    check_pager
435  fi
436  ;;
437esac
438
439# If user has a pager defined, or got assigned one above, use it.
440# If not, use more.
441#
442PAGER=${PAGER:-more}
443
444if [ -n "${VERBOSE}" -a ! "${PAGER}" = "more" ]; then
445  echo " *** You have ${PAGER} defined as your pager so we will use that"
446  echo ''
447  sleep 3
448fi
449
450# Assign the diff flag once so we will not have to keep testing it
451#
452DIFF_FLAG=${DIFF_FLAG:--u}
453
454# Assign the source directory
455#
456SOURCEDIR=${SOURCEDIR:-/usr/src}
457if [ ! -f ${SOURCEDIR}/Makefile.inc1 -a \
458   -f ${SOURCEDIR}/../Makefile.inc1 ]; then
459  echo " *** The source directory you specified (${SOURCEDIR})"
460  echo "     will be reset to ${SOURCEDIR}/.."
461  echo ''
462  sleep 3
463  SOURCEDIR=${SOURCEDIR}/..
464fi
465
466# Setup make to use system files from SOURCEDIR
467MM_MAKE="make ${ARCHSTRING} -m ${SOURCEDIR}/share/mk"
468
469# Check DESTDIR against the mergemaster mtree database to see what
470# files the user changed from the reference files.
471#
472if [ -n "${AUTO_UPGRADE}" -a -s "${MTREEFILE}" ]; then
473	CHANGED=:
474	for file in `mtree -eqL -f ${MTREEFILE} -p ${DESTDIR}/ \
475		2>/dev/null | awk '($2 == "changed") {print $1}'`; do
476		if [ -f "${DESTDIR}/$file" ]; then
477			CHANGED="${CHANGED}${DESTDIR}/${file}:"
478		fi
479	done
480	[ "$CHANGED" = ':' ] && unset CHANGED
481fi
482
483# Check the width of the user's terminal
484#
485if [ -t 0 ]; then
486  w=`tput columns`
487  case "${w}" in
488  0|'') ;; # No-op, since the input is not valid
489  *)
490    case "${SCREEN_WIDTH}" in
491    '') SCREEN_WIDTH="${w}" ;;
492    "${w}") ;; # No-op, since they are the same
493    *)
494      echo -n "*** You entered ${SCREEN_WIDTH} as your screen width, but stty "
495      echo "thinks it is ${w}."
496      echo ''
497      echo -n "What would you like to use? [${w}] "
498      read SCREEN_WIDTH
499      case "${SCREEN_WIDTH}" in
500      '') SCREEN_WIDTH="${w}" ;;
501      esac
502      ;;
503    esac
504  esac
505fi
506
507# Define what CVS $Id tag to look for to aid portability.
508#
509CVS_ID_TAG=FreeBSD
510
511delete_temproot () {
512  rm -rf "${TEMPROOT}" 2>/dev/null
513  chflags -R 0 "${TEMPROOT}" 2>/dev/null
514  rm -rf "${TEMPROOT}" || { echo "*** Unable to delete ${TEMPROOT}";  exit 1; }
515}
516
517case "${RERUN}" in
518'')
519  # Set up the loop to test for the existence of the
520  # temp root directory.
521  #
522  TEST_TEMP_ROOT=yes
523  while [ "${TEST_TEMP_ROOT}" = "yes" ]; do
524    if [ -d "${TEMPROOT}" ]; then
525      echo "*** The directory specified for the temporary root environment,"
526      echo "    ${TEMPROOT}, exists.  This can be a security risk if untrusted"
527      echo "    users have access to the system."
528      echo ''
529      case "${AUTO_RUN}" in
530      '')
531        echo "  Use 'd' to delete the old ${TEMPROOT} and continue"
532        echo "  Use 't' to select a new temporary root directory"
533        echo "  Use 'e' to exit mergemaster"
534        echo ''
535        echo "  Default is to use ${TEMPROOT} as is"
536        echo ''
537        echo -n "How should I deal with this? [Use the existing ${TEMPROOT}] "
538        read DELORNOT
539
540        case "${DELORNOT}" in
541        [dD])
542          echo ''
543          echo "   *** Deleting the old ${TEMPROOT}"
544          echo ''
545          delete_temproot
546          unset TEST_TEMP_ROOT
547          ;;
548        [tT])
549          echo "   *** Enter new directory name for temporary root environment"
550          read TEMPROOT
551          ;;
552        [eE])
553          exit 0
554          ;;
555        '')
556          echo ''
557          echo "   *** Leaving ${TEMPROOT} intact"
558          echo ''
559          unset TEST_TEMP_ROOT
560          ;;
561        *)
562          echo ''
563          echo "invalid choice: ${DELORNOT}"
564          echo ''
565          ;;
566        esac
567        ;;
568      *)
569        # If this is an auto-run, try a hopefully safe alternative then
570        # re-test anyway.
571        TEMPROOT=/var/tmp/temproot.`date +%m%d.%H.%M.%S`
572        ;;
573      esac
574    else
575      unset TEST_TEMP_ROOT
576    fi
577  done
578
579  echo "*** Creating the temporary root environment in ${TEMPROOT}"
580
581  if mkdir -p "${TEMPROOT}"; then
582    echo " *** ${TEMPROOT} ready for use"
583  fi
584
585  if [ ! -d "${TEMPROOT}" ]; then
586    echo ''
587    echo "  *** FATAL ERROR: Cannot create ${TEMPROOT}"
588    echo ''
589    exit 1
590  fi
591
592  echo " *** Creating and populating directory structure in ${TEMPROOT}"
593  echo ''
594
595  case "${VERBOSE}" in
596  '') ;;
597  *)
598    press_to_continue
599    ;;
600  esac
601
602  case "${PRE_WORLD}" in
603  '')
604    { cd ${SOURCEDIR} &&
605      case "${DESTDIR}" in
606      '') ;;
607      *)
608        ${MM_MAKE} DESTDIR=${DESTDIR} distrib-dirs
609        ;;
610      esac
611      od=${TEMPROOT}/usr/obj
612      ${MM_MAKE} DESTDIR=${TEMPROOT} distrib-dirs &&
613      MAKEOBJDIRPREFIX=$od ${MM_MAKE} _obj SUBDIR_OVERRIDE=etc &&
614      MAKEOBJDIRPREFIX=$od ${MM_MAKE} everything SUBDIR_OVERRIDE=etc &&
615      MAKEOBJDIRPREFIX=$od ${MM_MAKE} DESTDIR=${TEMPROOT} distribution;} ||
616    { echo '';
617     echo "  *** FATAL ERROR: Cannot 'cd' to ${SOURCEDIR} and install files to";
618      echo "      the temproot environment";
619      echo '';
620      exit 1;}
621    ;;
622  *)
623    # Only set up files that are crucial to {build|install}world
624    { mkdir -p ${TEMPROOT}/etc &&
625      cp -p ${SOURCEDIR}/etc/master.passwd ${TEMPROOT}/etc &&
626      cp -p ${SOURCEDIR}/etc/group ${TEMPROOT}/etc;} ||
627    { echo '';
628      echo '  *** FATAL ERROR: Cannot copy files to the temproot environment';
629      echo '';
630      exit 1;}
631    ;;
632  esac
633
634  # Doing the inventory and removing files that we don't want to compare only
635  # makes sense if we are not doing a rerun, since we have no way of knowing
636  # what happened to the files during previous incarnations.
637  case "${VERBOSE}" in
638  '') ;;
639  *)
640    echo ''
641    echo ' *** The following files exist only in the installed version of'
642    echo "     ${DESTDIR}/etc.  In the vast majority of cases these files"
643    echo '     are necessary parts of the system and should not be deleted.'
644    echo '     However because these files are not updated by this process you'
645    echo '     might want to verify their status before rebooting your system.'
646    echo ''
647    press_to_continue
648    diff -qr ${DESTDIR}/etc ${TEMPROOT}/etc | grep "^Only in ${DESTDIR}/etc" | ${PAGER}
649    echo ''
650    press_to_continue
651    ;;
652  esac
653
654  case "${IGNORE_MOTD}" in
655  '') ;;
656  *)
657     echo ''
658     echo "*** You have the IGNORE_MOTD option set in your mergemaster rc file."
659     echo "    This option is deprecated in favor of the IGNORE_FILES option."
660     echo "    Please update your rc file accordingly."
661     echo ''
662     exit 1
663     ;;
664  esac
665
666  # Avoid comparing the following user specified files
667  for file in ${IGNORE_FILES}; do
668    test -e ${TEMPROOT}/${file} && unlink ${TEMPROOT}/${file}
669  done
670
671  # We really don't want to have to deal with files like login.conf.db, pwd.db,
672  # or spwd.db.  Instead, we want to compare the text versions, and run *_mkdb.
673  # Prompt the user to do so below, as needed.
674  #
675  rm -f ${TEMPROOT}/etc/*.db ${TEMPROOT}/etc/passwd
676
677  # We only need to compare things like freebsd.cf once
678  find ${TEMPROOT}/usr/obj -type f -delete 2>/dev/null
679
680  # Delete stuff we do not need to keep the mtree database small,
681  # and to make the actual comparison faster.
682  find ${TEMPROOT}/usr -type l -delete 2>/dev/null
683  find ${TEMPROOT} -type f -size 0 -delete 2>/dev/null
684  find -d ${TEMPROOT} -type d -empty -delete 2>/dev/null
685
686  # Build the mtree database in a temporary location.
687  case "${PRE_WORLD}" in
688  '') MTREENEW=`mktemp -t mergemaster.mtree`
689      mtree -ci -p ${TEMPROOT} -k size,md5digest > ${MTREENEW} 2>/dev/null
690      ;;
691  *) # We don't want to mess with the mtree database on a pre-world run or
692     # when re-scanning a previously-built tree.
693     ;;
694  esac
695  ;; # End of the "RERUN" test
696esac
697
698# Get ready to start comparing files
699
700# Check umask if not specified on the command line,
701# and we are not doing an autorun
702#
703if [ -z "${NEW_UMASK}" -a -z "${AUTO_RUN}" ]; then
704  USER_UMASK=`umask`
705  case "${USER_UMASK}" in
706  0022|022) ;;
707  *)
708    echo ''
709    echo " *** Your umask is currently set to ${USER_UMASK}.  By default, this script"
710    echo "     installs all files with the same user, group and modes that"
711    echo "     they are created with by ${SOURCEDIR}/etc/Makefile, compared to"
712    echo "     a umask of 022.  This umask allows world read permission when"
713    echo "     the file's default permissions have it."
714    echo ''
715    echo "     No world permissions can sometimes cause problems.  A umask of"
716    echo "     022 will restore the default behavior, but is not mandatory."
717    echo "     /etc/master.passwd is a special case.  Its file permissions"
718    echo "     will be 600 (rw-------) if installed."
719    echo ''
720    echo -n "What umask should I use? [${USER_UMASK}] "
721    read NEW_UMASK
722
723    NEW_UMASK="${NEW_UMASK:-$USER_UMASK}"
724    ;;
725  esac
726  echo ''
727fi
728
729CONFIRMED_UMASK=${NEW_UMASK:-0022}
730
731#
732# Warn users who still have old rc files
733#
734for file in atm devfs diskless1 diskless2 network network6 pccard \
735  serial syscons sysctl alpha amd64 i386 ia64 sparc64; do
736  if [ -f "${DESTDIR}/etc/rc.${file}" ]; then
737    OLD_RC_PRESENT=1
738    break
739  fi
740done
741
742case "${OLD_RC_PRESENT}" in
7431)
744  echo ''
745  echo " *** There are elements of the old rc system in ${DESTDIR}/etc/."
746  echo ''
747  echo '     While these scripts will not hurt anything, they are not'
748  echo '     functional on an up to date system, and can be removed.'
749  echo ''
750
751  case "${AUTO_RUN}" in
752  '')
753    echo -n 'Move these files to /var/tmp/mergemaster/old_rc? [yes] '
754    read MOVE_OLD_RC
755
756    case "${MOVE_OLD_RC}" in
757    [nN]*) ;;
758    *)
759      mkdir -p /var/tmp/mergemaster/old_rc
760        for file in atm devfs diskless1 diskless2 network network6 pccard \
761          serial syscons sysctl alpha amd64 i386 ia64 sparc64; do
762          if [ -f "${DESTDIR}/etc/rc.${file}" ]; then
763            mv ${DESTDIR}/etc/rc.${file} /var/tmp/mergemaster/old_rc/
764          fi
765        done
766      echo '  The files have been moved'
767      press_to_continue
768      ;;
769    esac
770    ;;
771  *) ;;
772  esac
773esac
774
775# Use the umask/mode information to install the files
776# Create directories as needed
777#
778install_error () {
779  echo "*** FATAL ERROR: Unable to install ${1} to ${2}"
780  echo ''
781  exit 1
782}
783
784do_install_and_rm () {
785  case "${PRESERVE_FILES}" in
786  [Yy][Ee][Ss])
787    if [ -f "${3}/${2##*/}" ]; then
788      mkdir -p ${PRESERVE_FILES_DIR}/${2%/*}
789      cp ${3}/${2##*/} ${PRESERVE_FILES_DIR}/${2%/*}
790    fi
791    ;;
792  esac
793
794  if [ ! -d "${3}/${2##*/}" ]; then
795    if install -m ${1} ${2} ${3}; then
796      unlink ${2}
797    else
798      install_error ${2} ${3}
799    fi
800  else
801    install_error ${2} ${3}
802  fi
803}
804
805# 4095 = "obase=10;ibase=8;07777" | bc
806find_mode () {
807  local OCTAL
808  OCTAL=$(( ~$(echo "obase=10; ibase=8; ${CONFIRMED_UMASK}" | bc) & 4095 &
809    $(echo "obase=10; ibase=8; $(stat -f "%OMp%OLp" ${1})" | bc) ))
810  printf "%04o\n" ${OCTAL}
811}
812
813mm_install () {
814  local INSTALL_DIR
815  INSTALL_DIR=${1#.}
816  INSTALL_DIR=${INSTALL_DIR%/*}
817
818  case "${INSTALL_DIR}" in
819  '')
820    INSTALL_DIR=/
821    ;;
822  esac
823
824  if [ -n "${DESTDIR}${INSTALL_DIR}" -a ! -d "${DESTDIR}${INSTALL_DIR}" ]; then
825    DIR_MODE=`find_mode "${TEMPROOT}/${INSTALL_DIR}"`
826    install -d -o root -g wheel -m "${DIR_MODE}" "${DESTDIR}${INSTALL_DIR}" ||
827      install_error $1 ${DESTDIR}${INSTALL_DIR}
828  fi
829
830  FILE_MODE=`find_mode "${1}"`
831
832  if [ ! -x "${1}" ]; then
833    case "${1#.}" in
834    /etc/mail/aliases)
835      NEED_NEWALIASES=yes
836      ;;
837    /etc/login.conf)
838      NEED_CAP_MKDB=yes
839      ;;
840    /etc/master.passwd)
841      do_install_and_rm 600 "${1}" "${DESTDIR}${INSTALL_DIR}"
842      NEED_PWD_MKDB=yes
843      DONT_INSTALL=yes
844      ;;
845    /.cshrc | /.profile)
846      local st_nlink
847
848      # install will unlink the file before it installs the new one,
849      # so we have to restore/create the link afterwards.
850      #
851      st_nlink=0		# In case the file does not yet exist
852      eval $(stat -s ${DESTDIR}${COMPFILE#.} 2>/dev/null)
853
854      do_install_and_rm "${FILE_MODE}" "${1}" "${DESTDIR}${INSTALL_DIR}"
855
856      if [ -n "${AUTO_INSTALL}" -a $st_nlink -gt 1 ]; then
857        HANDLE_LINK=l
858      else
859        case "${LINK_EXPLAINED}" in
860        '')
861          echo "   *** Historically BSD derived systems have had a"
862          echo "       hard link from /.cshrc and /.profile to"
863          echo "       their namesakes in /root.  Please indicate"
864          echo "       your preference below for bringing your"
865          echo "       installed files up to date."
866          echo ''
867          LINK_EXPLAINED=yes
868          ;;
869        esac
870
871        echo "   Use 'd' to delete the temporary ${COMPFILE}"
872        echo "   Use 'l' to delete the existing ${DESTDIR}/root/${COMPFILE##*/} and create the link"
873        echo ''
874        echo "   Default is to leave the temporary file to deal with by hand"
875        echo ''
876        echo -n "  How should I handle ${COMPFILE}? [Leave it to install later] "
877        read HANDLE_LINK
878      fi
879
880      case "${HANDLE_LINK}" in
881      [dD]*)
882        rm "${COMPFILE}"
883        echo ''
884        echo "   *** Deleting ${COMPFILE}"
885        ;;
886      [lL]*)
887        echo ''
888        unlink ${DESTDIR}/root/${COMPFILE##*/}
889        if ln ${DESTDIR}${COMPFILE#.} ${DESTDIR}/root/${COMPFILE##*/}; then
890          echo "   *** Link from ${DESTDIR}${COMPFILE#.} to ${DESTDIR}/root/${COMPFILE##*/} installed successfully"
891        else
892          echo "   *** Error linking ${DESTDIR}${COMPFILE#.} to ${DESTDIR}/root/${COMPFILE##*/}"
893          echo "   *** ${COMPFILE} will remain for your consideration"
894        fi
895        ;;
896      *)
897        echo "   *** ${COMPFILE} will remain for your consideration"
898        ;;
899      esac
900      return
901      ;;
902    esac
903
904    case "${DONT_INSTALL}" in
905    '')
906      do_install_and_rm "${FILE_MODE}" "${1}" "${DESTDIR}${INSTALL_DIR}"
907      ;;
908    *)
909      unset DONT_INSTALL
910      ;;
911    esac
912  else	# File matched -x
913    do_install_and_rm "${FILE_MODE}" "${1}" "${DESTDIR}${INSTALL_DIR}"
914  fi
915  return $?
916}
917
918if [ ! -d "${TEMPROOT}" ]; then
919	echo "*** FATAL ERROR: The temproot directory (${TEMPROOT})"
920	echo '                 has disappeared!'
921	echo ''
922	exit 1
923fi
924
925echo ''
926echo "*** Beginning comparison"
927echo ''
928
929# Pre-world does not populate /etc/rc.d.
930# It is very possible that a previous run would have deleted files in
931# ${TEMPROOT}/etc/rc.d, thus creating a lot of false positives.
932if [ -z "${PRE_WORLD}" -a -z "${RERUN}" ]; then
933  echo "   *** Checking ${DESTDIR}/etc/rc.d for stale files"
934  echo ''
935  cd "${DESTDIR}/etc/rc.d" &&
936  for file in *; do
937    if [ ! -e "${TEMPROOT}/etc/rc.d/${file}" ]; then
938      STALE_RC_FILES="${STALE_RC_FILES} ${file}"
939    fi
940  done
941  case "${STALE_RC_FILES}" in
942  ''|' *')
943    echo '   *** No stale files found'
944    ;;
945  *)
946    echo "   *** The following files exist in ${DESTDIR}/etc/rc.d but not in"
947    echo "       ${TEMPROOT}/etc/rc.d/:"
948    echo ''
949    echo "${STALE_RC_FILES}"
950    echo ''
951    echo '       The presence of stale files in this directory can cause the'
952    echo '       dreaded unpredictable results, and therefore it is highly'
953    echo '       recommended that you delete them.'
954    case "${AUTO_RUN}" in
955    '')
956      echo ''
957      echo -n '   *** Delete them now? [n] '
958      read DELETE_STALE_RC_FILES
959      case "${DELETE_STALE_RC_FILES}" in
960      [yY])
961        echo '      *** Deleting ... '
962        rm ${STALE_RC_FILES}
963        echo '                       done.'
964        ;;
965      *)
966        echo '      *** Files will not be deleted'
967        ;;
968      esac
969      sleep 2
970      ;;
971    *)
972      if [ -n "${DELETE_STALE_RC_FILES}" ]; then
973        echo '      *** Deleting ... '
974        rm ${STALE_RC_FILES}
975        echo '                       done.'
976      fi
977    esac
978    ;;
979  esac
980  echo ''
981fi
982
983cd "${TEMPROOT}"
984
985if [ -r "${MM_PRE_COMPARE_SCRIPT}" ]; then
986  . "${MM_PRE_COMPARE_SCRIPT}"
987fi
988
989# Things that were files/directories/links in one version can sometimes
990# change to something else in a newer version.  So we need to explicitly
991# test for this, and warn the user if what we find does not match.
992#
993for COMPFILE in `find . | sort` ; do
994  if [ -e "${DESTDIR}${COMPFILE#.}" ]; then
995    INSTALLED_TYPE=`stat -f '%HT' ${DESTDIR}${COMPFILE#.}`
996  else
997    continue
998  fi
999  TEMPROOT_TYPE=`stat -f '%HT' $COMPFILE`
1000
1001  if [ ! "$TEMPROOT_TYPE" = "$INSTALLED_TYPE" ]; then
1002    [ "$COMPFILE" = '.' ] && continue
1003    TEMPROOT_TYPE=`echo $TEMPROOT_TYPE | tr [:upper:] [:lower:]`
1004    INSTALLED_TYPE=`echo $INSTALLED_TYPE | tr [:upper:] [:lower:]`
1005
1006    echo "*** The installed file ${DESTDIR}${COMPFILE#.} has the type \"$INSTALLED_TYPE\""
1007    echo "    but the new version has the type \"$TEMPROOT_TYPE\""
1008    echo ''
1009    echo "    How would you like to handle this?"
1010    echo ''
1011    echo "    Use 'r' to remove ${DESTDIR}${COMPFILE#.}"
1012    case "$TEMPROOT_TYPE" in
1013    'symbolic link')
1014	TARGET=`readlink $COMPFILE`
1015	echo "    and create a link to $TARGET in its place" ;;
1016    *)	echo "    You will be able to install it as a \"$TEMPROOT_TYPE\"" ;;
1017    esac
1018    echo ''
1019    echo "    Use 'i' to ignore this"
1020    echo ''
1021    echo -n "    How to proceed? [i] "
1022    read ANSWER
1023    case "$ANSWER" in
1024    [rR])	case "${PRESERVE_FILES}" in
1025		[Yy][Ee][Ss])
1026		mv ${DESTDIR}${COMPFILE#.} ${PRESERVE_FILES_DIR}/ || exit 1 ;;
1027		*) rm -rf ${DESTDIR}${COMPFILE#.} ;;
1028		esac
1029		case "$TEMPROOT_TYPE" in
1030		'symbolic link') ln -sf $TARGET ${DESTDIR}${COMPFILE#.} ;;
1031		esac ;;
1032    *)	echo ''
1033        echo "*** See the man page about adding ${COMPFILE#.} to the list of IGNORE_FILES"
1034        press_to_continue ;;
1035    esac
1036    echo ''
1037  fi
1038done
1039
1040for COMPFILE in `find . -type f | sort`; do
1041
1042  # First, check to see if the file exists in DESTDIR.  If not, the
1043  # diff_loop function knows how to handle it.
1044  #
1045  if [ ! -e "${DESTDIR}${COMPFILE#.}" ]; then
1046    case "${AUTO_RUN}" in
1047      '')
1048        diff_loop
1049        ;;
1050      *)
1051        case "${AUTO_INSTALL}" in
1052        '')
1053          # If this is an auto run, make it official
1054          echo "   *** ${COMPFILE} will remain for your consideration"
1055          ;;
1056        *)
1057          diff_loop
1058          ;;
1059        esac
1060        ;;
1061    esac # Auto run test
1062    continue
1063  fi
1064
1065  case "${STRICT}" in
1066  '' | [Nn][Oo])
1067    # Compare CVS $Id's first so if the file hasn't been modified
1068    # local changes will be ignored.
1069    # If the files have the same $Id, delete the one in temproot so the
1070    # user will have less to wade through if files are left to merge by hand.
1071    #
1072    CVSID1=`grep "[$]${CVS_ID_TAG}:" ${DESTDIR}${COMPFILE#.} 2>/dev/null`
1073    CVSID2=`grep "[$]${CVS_ID_TAG}:" ${COMPFILE} 2>/dev/null` || CVSID2=none
1074
1075    case "${CVSID2}" in
1076    "${CVSID1}")
1077      echo " *** Temp ${COMPFILE} and installed have the same CVS Id, deleting"
1078      rm "${COMPFILE}"
1079      ;;
1080    esac
1081    ;;
1082  esac
1083
1084  # If the file is still here either because the $Ids are different, the
1085  # file doesn't have an $Id, or we're using STRICT mode; look at the diff.
1086  #
1087  if [ -f "${COMPFILE}" ]; then
1088
1089    # Do an absolute diff first to see if the files are actually different.
1090    # If they're not different, delete the one in temproot.
1091    #
1092    if diff -q ${DIFF_OPTIONS} "${DESTDIR}${COMPFILE#.}" "${COMPFILE}" > \
1093      /dev/null 2>&1; then
1094      echo " *** Temp ${COMPFILE} and installed are the same, deleting"
1095      rm "${COMPFILE}"
1096    else
1097      # Ok, the files are different, so show the user where they differ.
1098      # Use user's choice of diff methods; and user's pager if they have one.
1099      # Use more if not.
1100      # Use unified diffs by default.  Context diffs give me a headache. :)
1101      #
1102      # If the user chose the -F option, test for that before proceeding
1103      #
1104      if [ -n "$FREEBSD_ID" ]; then
1105        if diff -q -I'[$]FreeBSD.*[$]' "${DESTDIR}${COMPFILE#.}" "${COMPFILE}" > \
1106            /dev/null 2>&1; then
1107          if mm_install "${COMPFILE}"; then
1108            echo "*** Updated revision control Id for ${DESTDIR}${COMPFILE#.}"
1109          else
1110            echo "*** Problem installing ${COMPFILE}, it will remain to merge by hand later"
1111          fi
1112          continue
1113        fi
1114      fi
1115      case "${AUTO_RUN}" in
1116      '')
1117        # prompt user to install/delete/merge changes
1118        diff_loop
1119        ;;
1120      *)
1121        # If this is an auto run, make it official
1122        echo "   *** ${COMPFILE} will remain for your consideration"
1123        ;;
1124      esac # Auto run test
1125    fi # Yes, the files are different
1126  fi # Yes, the file still remains to be checked
1127done # This is for the for way up there at the beginning of the comparison
1128
1129echo ''
1130echo "*** Comparison complete"
1131
1132if [ -s "${MTREENEW}" ]; then
1133  echo "*** Saving mtree database for future upgrades"
1134  test -e "${MTREEFILE}" && unlink ${MTREEFILE}
1135  mv ${MTREENEW} ${MTREEFILE}
1136fi
1137
1138echo ''
1139
1140TEST_FOR_FILES=`find ${TEMPROOT} -type f -size +0 2>/dev/null`
1141if [ -n "${TEST_FOR_FILES}" ]; then
1142  echo "*** Files that remain for you to merge by hand:"
1143  find "${TEMPROOT}" -type f -size +0 | sort
1144  echo ''
1145
1146  case "${AUTO_RUN}" in
1147  '')
1148    echo -n "Do you wish to delete what is left of ${TEMPROOT}? [no] "
1149    read DEL_TEMPROOT
1150    case "${DEL_TEMPROOT}" in
1151    [yY]*)
1152      delete_temproot
1153      ;;
1154    *)
1155      echo " *** ${TEMPROOT} will remain"
1156      ;;
1157    esac
1158    ;;
1159  *) ;;
1160  esac
1161else
1162  echo "*** ${TEMPROOT} is empty, deleting"
1163  delete_temproot
1164fi
1165
1166case "${AUTO_INSTALLED_FILES}" in
1167'') ;;
1168*)
1169  case "${AUTO_RUN}" in
1170  '')
1171    (
1172      echo ''
1173      echo '*** You chose the automatic install option for files that did not'
1174      echo '    exist on your system.  The following were installed for you:'
1175      echo "${AUTO_INSTALLED_FILES}"
1176    ) | ${PAGER}
1177    ;;
1178  *)
1179    echo ''
1180    echo '*** You chose the automatic install option for files that did not'
1181    echo '    exist on your system.  The following were installed for you:'
1182    echo "${AUTO_INSTALLED_FILES}"
1183    ;;
1184  esac
1185  ;;
1186esac
1187
1188case "${AUTO_UPGRADED_FILES}" in
1189'') ;;
1190*)
1191  case "${AUTO_RUN}" in
1192  '')
1193    (
1194      echo ''
1195      echo '*** You chose the automatic upgrade option for files that you did'
1196      echo '    not alter on your system.  The following were upgraded for you:'
1197      echo "${AUTO_UPGRADED_FILES}"
1198    ) | ${PAGER}
1199    ;;
1200  *)
1201    echo ''
1202    echo '*** You chose the automatic upgrade option for files that you did'
1203    echo '    not alter on your system.  The following were upgraded for you:'
1204    echo "${AUTO_UPGRADED_FILES}"
1205    ;;
1206  esac
1207  ;;
1208esac
1209
1210run_it_now () {
1211  case "${AUTO_RUN}" in
1212  '')
1213    unset YES_OR_NO
1214    echo ''
1215    echo -n '    Would you like to run it now? y or n [n] '
1216    read YES_OR_NO
1217
1218    case "${YES_OR_NO}" in
1219    y)
1220      echo "    Running ${1}"
1221      echo ''
1222      eval "${1}"
1223      ;;
1224    ''|n)
1225      echo ''
1226      echo "       *** Cancelled"
1227      echo ''
1228      echo "    Make sure to run ${1} yourself"
1229      ;;
1230    *)
1231      echo ''
1232      echo "       *** Sorry, I do not understand your answer (${YES_OR_NO})"
1233      echo ''
1234      echo "    Make sure to run ${1} yourself"
1235    esac
1236    ;;
1237  *) ;;
1238  esac
1239}
1240
1241case "${NEED_NEWALIASES}" in
1242'') ;;
1243*)
1244  echo ''
1245  if [ -n "${DESTDIR}" ]; then
1246    echo "*** You installed a new aliases file into ${DESTDIR}/etc/mail, but"
1247    echo "    the newaliases command is limited to the directories configured"
1248    echo "    in sendmail.cf.  Make sure to create your aliases database by"
1249    echo "    hand when your sendmail configuration is done."
1250  else
1251    echo "*** You installed a new aliases file, so make sure that you run"
1252    echo "    '/usr/bin/newaliases' to rebuild your aliases database"
1253    run_it_now '/usr/bin/newaliases'
1254  fi
1255  ;;
1256esac
1257
1258case "${NEED_CAP_MKDB}" in
1259'') ;;
1260*)
1261  echo ''
1262  echo "*** You installed a login.conf file, so make sure that you run"
1263  echo "    '/usr/bin/cap_mkdb ${DESTDIR}/etc/login.conf'"
1264  echo "     to rebuild your login.conf database"
1265  run_it_now "/usr/bin/cap_mkdb ${DESTDIR}/etc/login.conf"
1266  ;;
1267esac
1268
1269case "${NEED_PWD_MKDB}" in
1270'') ;;
1271*)
1272  echo ''
1273  echo "*** You installed a new master.passwd file, so make sure that you run"
1274  if [ -n "${DESTDIR}" ]; then
1275    echo "    '/usr/sbin/pwd_mkdb -d ${DESTDIR}/etc -p ${DESTDIR}/etc/master.passwd'"
1276    echo "    to rebuild your password files"
1277    run_it_now "/usr/sbin/pwd_mkdb -d ${DESTDIR}/etc -p ${DESTDIR}/etc/master.passwd"
1278  else
1279    echo "    '/usr/sbin/pwd_mkdb -p /etc/master.passwd'"
1280    echo "     to rebuild your password files"
1281    run_it_now '/usr/sbin/pwd_mkdb -p /etc/master.passwd'
1282  fi
1283  ;;
1284esac
1285
1286echo ''
1287
1288if [ -r "${MM_EXIT_SCRIPT}" ]; then
1289  . "${MM_EXIT_SCRIPT}"
1290fi
1291
1292case "${COMP_CONFS}" in
1293'') ;;
1294*)
1295  . ${DESTDIR}/etc/defaults/rc.conf
1296
1297  (echo ''
1298  echo "*** Comparing conf files: ${rc_conf_files}"
1299
1300  for CONF_FILE in ${rc_conf_files}; do
1301    if [ -r "${DESTDIR}${CONF_FILE}" ]; then
1302      echo ''
1303      echo "*** From ${DESTDIR}${CONF_FILE}"
1304      echo "*** From ${DESTDIR}/etc/defaults/rc.conf"
1305
1306      for RC_CONF_VAR in `grep -i ^[a-z] ${DESTDIR}${CONF_FILE} |
1307        cut -d '=' -f 1`; do
1308        echo ''
1309        grep -w ^${RC_CONF_VAR} ${DESTDIR}${CONF_FILE}
1310        grep -w ^${RC_CONF_VAR} ${DESTDIR}/etc/defaults/rc.conf ||
1311          echo ' * No default variable with this name'
1312      done
1313    fi
1314  done) | ${PAGER}
1315  echo ''
1316  ;;
1317esac
1318
1319case "${PRE_WORLD}" in
1320'') ;;
1321*)
1322  MAKE_CONF="${SOURCEDIR}/share/examples/etc/make.conf"
1323
1324  (echo ''
1325  echo '*** Comparing make variables'
1326  echo ''
1327  echo "*** From ${DESTDIR}/etc/make.conf"
1328  echo "*** From ${MAKE_CONF}"
1329
1330  for MAKE_VAR in `grep -i ^[a-z] ${DESTDIR}/etc/make.conf | cut -d '=' -f 1`; do
1331    echo ''
1332    grep -w ^${MAKE_VAR} ${DESTDIR}/etc/make.conf
1333    grep -w ^#${MAKE_VAR} ${MAKE_CONF} ||
1334      echo ' * No example variable with this name'
1335  done) | ${PAGER}
1336  ;;
1337esac
1338
1339if [ -n "${PRESERVE_FILES}" ]; then
1340  find -d $PRESERVE_FILES_DIR -type d -empty -delete 2>/dev/null
1341  rmdir $PRESERVE_FILES_DIR 2>/dev/null
1342fi
1343
1344exit 0
1345