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