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