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