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