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