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