mergemaster.sh revision 179315
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 179315 2008-05-26 10:40:09Z bz $
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# Assign the location of the mtree database
248#
249MTREEDB='/var/db/mergemaster.mtree'
250
251# Read /etc/mergemaster.rc first so the one in $HOME can override
252#
253if [ -r /etc/mergemaster.rc ]; then
254  . /etc/mergemaster.rc
255fi
256
257# Read .mergemasterrc before command line so CLI can override
258#
259if [ -r "$HOME/.mergemasterrc" ]; then
260  . "$HOME/.mergemasterrc"
261fi
262
263# Check the command line options
264#
265while 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
334done
335
336# Don't force the user to set this in the mergemaster rc file
337if [ -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`
339fi
340
341# Check the for the mtree database in DESTDIR.
342if [ ! -f ${DESTDIR}${MTREEDB} ]; then
343  echo "*** Unable to find mtree database. Skipping auto-upgrade."
344  unset AUTO_UPGRADE
345fi
346
347echo ''
348
349# If the user has a pager defined, make sure we can run it
350#
351case "${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  ;;
396esac
397
398# If user has a pager defined, or got assigned one above, use it.
399# If not, use more.
400#
401PAGER=${PAGER:-more}
402
403if [ -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
407fi
408
409# Assign the diff flag once so we will not have to keep testing it
410#
411DIFF_FLAG=${DIFF_FLAG:--u}
412
413# Assign the source directory
414#
415SOURCEDIR=${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#
420CHANGED=
421if [ -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
428fi
429
430# Check the width of the user's terminal
431#
432if [ -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
452fi
453
454# Define what CVS $Id tag to look for to aid portability.
455#
456CVS_ID_TAG=FreeBSD
457
458delete_temproot () {
459  rm -rf "${TEMPROOT}" 2>/dev/null
460  chflags -R 0 "${TEMPROOT}" 2>/dev/null
461  rm -rf "${TEMPROOT}" || exit 1
462}
463
464case "${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
614esac
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#
620rm -f ${TEMPROOT}/etc/*.db ${TEMPROOT}/etc/passwd
621
622# We only need to compare things like freebsd.cf once
623find ${TEMPROOT}/usr/obj -type f -delete 2>/dev/null
624
625# Delete 0 length files to make the mtree database as small as possible.
626find ${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?
630case "${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   ;;
635esac
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#
642if [ -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 ''
666fi
667
668CONFIRMED_UMASK=${NEW_UMASK:-0022}
669
670#
671# Warn users who still have old rc files
672#
673for file in atm devfs diskless1 diskless2 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
679done
680
681case "${OLD_RC_PRESENT}" in
6821)
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 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
712esac
713
714# Use the umask/mode information to install the files
715# Create directories as needed
716#
717do_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
732find_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
739mm_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
841if [ ! -d "${TEMPROOT}" ]; then
842	echo "*** FATAL ERROR: The temproot directory (${TEMPROOT})"
843	echo '                 has disappeared!'
844	echo ''
845	exit 1
846fi
847
848echo ''
849echo "*** Beginning comparison"
850echo ''
851
852# Pre-world does not populate /etc/rc.d.
853# It is very possible that a previous run would have deleted files in
854# ${TEMPROOT}/etc/rc.d, thus creating a lot of false positives.
855if [ -z "${PRE_WORLD}" -a -z "${RERUN}" ]; then
856  echo "   *** Checking ${DESTDIR}/etc/rc.d for stale files"
857  echo ''
858  cd "${DESTDIR}/etc/rc.d" &&
859  for file in *; do
860    if [ ! -e "${TEMPROOT}/etc/rc.d/${file}" ]; then
861      STALE_RC_FILES="${STALE_RC_FILES} ${file}"
862    fi
863  done
864  case "${STALE_RC_FILES}" in
865  ''|' *')
866    echo '   *** No stale files found'
867    ;;
868  *)
869    echo "   *** The following files exist in ${DESTDIR}/etc/rc.d but not in"
870    echo "       ${TEMPROOT}/etc/rc.d/:"
871    echo ''
872    echo "${STALE_RC_FILES}"
873    echo ''
874    echo '       The presence of stale files in this directory can cause the'
875    echo '       dreaded unpredictable results, and therefore it is highly'
876    echo '       recommended that you delete them.'
877    case "${AUTO_RUN}" in
878    '')
879      echo ''
880      echo -n '   *** Delete them now? [n] '
881      read DELETE_STALE_RC_FILES
882      case "${DELETE_STALE_RC_FILES}" in
883      [yY])
884        echo '      *** Deleting ... '
885        rm ${STALE_RC_FILES}
886        echo '                       done.'
887        ;;
888      *)
889        echo '      *** Files will not be deleted'
890        ;;
891      esac
892      sleep 2
893      ;;
894    esac
895    ;;
896  esac
897  echo ''
898fi
899
900cd "${TEMPROOT}"
901
902if [ -r "${MM_PRE_COMPARE_SCRIPT}" ]; then
903  . "${MM_PRE_COMPARE_SCRIPT}"
904fi
905
906# Using -size +0 avoids uselessly checking the empty log files created
907# by ${SOURCEDIR}/Makefile and the device entries in ./dev, but does
908# check the scripts in ./dev, as we'd like (assuming no devfs of course).
909#
910for COMPFILE in `find . -type f -size +0`; do
911
912  # First, check to see if the file exists in DESTDIR.  If not, the
913  # diff_loop function knows how to handle it.
914  #
915  if [ ! -e "${DESTDIR}${COMPFILE#.}" ]; then
916    case "${AUTO_RUN}" in
917      '')
918        diff_loop
919        ;;
920      *)
921        case "${AUTO_INSTALL}" in
922        '')
923          # If this is an auto run, make it official
924          echo "   *** ${COMPFILE} will remain for your consideration"
925          ;;
926        *)
927          diff_loop
928          ;;
929        esac
930        ;;
931    esac # Auto run test
932    continue
933  fi
934
935  case "${STRICT}" in
936  '' | [Nn][Oo])
937    # Compare CVS $Id's first so if the file hasn't been modified
938    # local changes will be ignored.
939    # If the files have the same $Id, delete the one in temproot so the
940    # user will have less to wade through if files are left to merge by hand.
941    #
942    CVSID1=`grep "[$]${CVS_ID_TAG}:" ${DESTDIR}${COMPFILE#.} 2>/dev/null`
943    CVSID2=`grep "[$]${CVS_ID_TAG}:" ${COMPFILE} 2>/dev/null` || CVSID2=none
944
945    case "${CVSID2}" in
946    "${CVSID1}")
947      echo " *** Temp ${COMPFILE} and installed have the same CVS Id, deleting"
948      rm "${COMPFILE}"
949      ;;
950    esac
951    ;;
952  esac
953
954  # If the file is still here either because the $Ids are different, the
955  # file doesn't have an $Id, or we're using STRICT mode; look at the diff.
956  #
957  if [ -f "${COMPFILE}" ]; then
958
959    # Do an absolute diff first to see if the files are actually different.
960    # If they're not different, delete the one in temproot.
961    #
962    if diff -q ${DIFF_OPTIONS} "${DESTDIR}${COMPFILE#.}" "${COMPFILE}" > \
963      /dev/null 2>&1; then
964      echo " *** Temp ${COMPFILE} and installed are the same, deleting"
965      rm "${COMPFILE}"
966    else
967      # Ok, the files are different, so show the user where they differ.
968      # Use user's choice of diff methods; and user's pager if they have one.
969      # Use more if not.
970      # Use unified diffs by default.  Context diffs give me a headache. :)
971      #
972      case "${AUTO_RUN}" in
973      '')
974        # prompt user to install/delete/merge changes
975        diff_loop
976        ;;
977      *)
978        # If this is an auto run, make it official
979        echo "   *** ${COMPFILE} will remain for your consideration"
980        ;;
981      esac # Auto run test
982    fi # Yes, the files are different
983  fi # Yes, the file still remains to be checked
984done # This is for the do way up there at the beginning of the comparison
985
986echo ''
987echo "*** Comparison complete"
988
989if [ -f "${DESTDIR}${MTREEDB}.new" ]; then
990  echo "*** Saving mtree database for future upgrades"
991  mv -f ${DESTDIR}${MTREEDB}.new ${DESTDIR}${MTREEDB} 2>/dev/null
992fi
993
994echo ''
995
996TEST_FOR_FILES=`find ${TEMPROOT} -type f -size +0 2>/dev/null`
997if [ -n "${TEST_FOR_FILES}" ]; then
998  echo "*** Files that remain for you to merge by hand:"
999  find "${TEMPROOT}" -type f -size +0
1000  echo ''
1001fi
1002
1003case "${AUTO_RUN}" in
1004'')
1005  echo -n "Do you wish to delete what is left of ${TEMPROOT}? [no] "
1006  read DEL_TEMPROOT
1007
1008  case "${DEL_TEMPROOT}" in
1009  [yY]*)
1010    if delete_temproot; then
1011      echo " *** ${TEMPROOT} has been deleted"
1012    else
1013      echo " *** Unable to delete ${TEMPROOT}"
1014    fi
1015    ;;
1016  *)
1017    echo " *** ${TEMPROOT} will remain"
1018    ;;
1019  esac
1020  ;;
1021*) ;;
1022esac
1023
1024case "${AUTO_INSTALLED_FILES}" in
1025'') ;;
1026*)
1027  case "${AUTO_RUN}" in
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    ) | ${PAGER}
1035    ;;
1036  *)
1037    echo ''
1038    echo '*** You chose the automatic install option for files that did not'
1039    echo '    exist on your system.  The following were installed for you:'
1040    echo "${AUTO_INSTALLED_FILES}"
1041    ;;
1042  esac
1043  ;;
1044esac
1045
1046case "${AUTO_UPGRADED_FILES}" in
1047'') ;;
1048*)
1049  case "${AUTO_RUN}" in
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    ) | ${PAGER}
1057    ;;
1058  *)
1059    echo ''
1060    echo '*** You chose the automatic upgrade option for files that you did'
1061    echo '    not alter on your system.  The following were upgraded for you:'
1062    echo "${AUTO_UPGRADED_FILES}"
1063    ;;
1064  esac
1065  ;;
1066esac
1067
1068run_it_now () {
1069  case "${AUTO_RUN}" in
1070  '')
1071    unset YES_OR_NO
1072    echo ''
1073    echo -n '    Would you like to run it now? y or n [n] '
1074    read YES_OR_NO
1075
1076    case "${YES_OR_NO}" in
1077    y)
1078      echo "    Running ${1}"
1079      echo ''
1080      eval "${1}"
1081      ;;
1082    ''|n)
1083      echo ''
1084      echo "       *** Cancelled"
1085      echo ''
1086      echo "    Make sure to run ${1} yourself"
1087      ;;
1088    *)
1089      echo ''
1090      echo "       *** Sorry, I do not understand your answer (${YES_OR_NO})"
1091      echo ''
1092      echo "    Make sure to run ${1} yourself"
1093    esac
1094    ;;
1095  *) ;;
1096  esac
1097}
1098
1099case "${NEED_MAKEDEV}" in
1100'') ;;
1101*)
1102  echo ''
1103  echo "*** You installed a new ${DESTDIR}/dev/MAKEDEV script, so make sure that you run"
1104  echo "    'cd ${DESTDIR}/dev && /bin/sh MAKEDEV all' to rebuild your devices"
1105  run_it_now "cd ${DESTDIR}/dev && /bin/sh MAKEDEV all"
1106  ;;
1107esac
1108
1109case "${NEED_NEWALIASES}" in
1110'') ;;
1111*)
1112  echo ''
1113  if [ -n "${DESTDIR}" ]; then
1114    echo "*** You installed a new aliases file into ${DESTDIR}/etc/mail, but"
1115    echo "    the newaliases command is limited to the directories configured"
1116    echo "    in sendmail.cf.  Make sure to create your aliases database by"
1117    echo "    hand when your sendmail configuration is done."
1118  else
1119    echo "*** You installed a new aliases file, so make sure that you run"
1120    echo "    '/usr/bin/newaliases' to rebuild your aliases database"
1121    run_it_now '/usr/bin/newaliases'
1122  fi
1123  ;;
1124esac
1125
1126case "${NEED_CAP_MKDB}" in
1127'') ;;
1128*)
1129  echo ''
1130  echo "*** You installed a login.conf file, so make sure that you run"
1131  echo "    '/usr/bin/cap_mkdb ${DESTDIR}/etc/login.conf'"
1132  echo "     to rebuild your login.conf database"
1133  run_it_now "/usr/bin/cap_mkdb ${DESTDIR}/etc/login.conf"
1134  ;;
1135esac
1136
1137case "${NEED_PWD_MKDB}" in
1138'') ;;
1139*)
1140  echo ''
1141  echo "*** You installed a new master.passwd file, so make sure that you run"
1142  if [ -n "${DESTDIR}" ]; then
1143    echo "    '/usr/sbin/pwd_mkdb -d ${DESTDIR}/etc -p ${DESTDIR}/etc/master.passwd'"
1144    echo "    to rebuild your password files"
1145    run_it_now "/usr/sbin/pwd_mkdb -d ${DESTDIR}/etc -p ${DESTDIR}/etc/master.passwd"
1146  else
1147    echo "    '/usr/sbin/pwd_mkdb -p /etc/master.passwd'"
1148    echo "     to rebuild your password files"
1149    run_it_now '/usr/sbin/pwd_mkdb -p /etc/master.passwd'
1150  fi
1151  ;;
1152esac
1153
1154echo ''
1155
1156if [ -r "${MM_EXIT_SCRIPT}" ]; then
1157  . "${MM_EXIT_SCRIPT}"
1158fi
1159
1160case "${COMP_CONFS}" in
1161'') ;;
1162*)
1163  . ${DESTDIR}/etc/defaults/rc.conf
1164
1165  (echo ''
1166  echo "*** Comparing conf files: ${rc_conf_files}"
1167
1168  for CONF_FILE in ${rc_conf_files}; do
1169    if [ -r "${DESTDIR}${CONF_FILE}" ]; then
1170      echo ''
1171      echo "*** From ${DESTDIR}${CONF_FILE}"
1172      echo "*** From ${DESTDIR}/etc/defaults/rc.conf"
1173
1174      for RC_CONF_VAR in `grep -i ^[a-z] ${DESTDIR}${CONF_FILE} |
1175        cut -d '=' -f 1`; do
1176        echo ''
1177        grep -w ^${RC_CONF_VAR} ${DESTDIR}${CONF_FILE}
1178        grep -w ^${RC_CONF_VAR} ${DESTDIR}/etc/defaults/rc.conf ||
1179          echo ' * No default variable with this name'
1180      done
1181    fi
1182  done) | ${PAGER}
1183  echo ''
1184  ;;
1185esac
1186
1187case "${PRE_WORLD}" in
1188'') ;;
1189*)
1190  MAKE_CONF="${SOURCEDIR%etc}share/examples/etc/make.conf"
1191
1192  (echo ''
1193  echo '*** Comparing make variables'
1194  echo ''
1195  echo "*** From ${DESTDIR}/etc/make.conf"
1196  echo "*** From ${MAKE_CONF}"
1197
1198  for MAKE_VAR in `grep -i ^[a-z] ${DESTDIR}/etc/make.conf | cut -d '=' -f 1`; do
1199    echo ''
1200    grep -w ^${MAKE_VAR} ${DESTDIR}/etc/make.conf
1201    grep -w ^#${MAKE_VAR} ${MAKE_CONF} ||
1202      echo ' * No example variable with this name'
1203  done) | ${PAGER}
1204  ;;
1205esac
1206
1207exit 0
1208
1209