mergemaster.sh revision 52400
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, 1999 Douglas Barton
9# Doug@gorean.org
10
11# $Revision: 1.36 $
12# $Date: 1999/09/10 20:56:33 $
13
14PATH=/bin:/usr/bin:/usr/sbin
15
16display_usage () {
17  VERSION_NUMBER=`grep "[$]Revision:" $0 | cut -d ' ' -f 3`
18  echo "mergemaster version ${VERSION_NUMBER}"
19  echo "Usage: mergemaster [-scrvah] [-m /path] [-t /path] [-d] [-u N] [-w N]"
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 "  -m /path/directory  Specify location of source to do the make in"
28  echo "  -t /path/directory  Specify temp root directory"
29  echo "  -d  Add date and time to directory name (e.g., /var/tmp/temproot.`date +%m%d.%H.%M`)"
30  echo "  -u N  Specify a numeric umask"
31  echo "  -w N  Specify a screen width in columns to sdiff"
32  echo ''
33}
34
35display_help () {
36  echo "* To create a temporary root environment, compare CVS revision \$Ids"
37  echo "  for files that have them, and compare diffs for files that do not,"
38  echo "  or have different ones, just type, mergemaster"
39  echo "* To specify a directory other than /var/tmp/temproot for the"
40  echo "  temporary root environment, use -t /path/to/temp/root"
41  echo "* The -w option takes a number as an argument for the column width"
42  echo "  of the screen. The default is 80."
43  echo "* The -a option causes mergemaster to run without prompting"
44}
45
46# Set the default path for the temporary root environment
47#
48TEMPROOT='/var/tmp/temproot'
49
50# Read .mergemasterrc before command line so CLI can override
51#
52if [ -f "$HOME/.mergemasterrc" ]; then
53  . "$HOME/.mergemasterrc"
54fi
55
56# Check the command line options
57#
58while getopts ":ascrvhm:t:du:w:" COMMAND_LINE_ARGUMENT ; do
59  case "${COMMAND_LINE_ARGUMENT}" in
60  s)
61    STRICT=yes
62    ;;
63  c)
64    DIFF_FLAG='-c'
65    ;;
66  r)
67    RERUN=yes
68    ;;
69  v)
70    case "${AUTO_RUN}" in
71    '') VERBOSE=yes ;;
72    esac
73    ;;
74  a)
75    AUTO_RUN=yes
76    unset VERBOSE
77    ;;
78  h)
79    display_usage
80    display_help
81    exit 0
82    ;;
83  m)
84    SOURCEDIR=${OPTARG}
85    ;;
86  t)
87    TEMPROOT=${OPTARG}
88    ;;
89  d)
90    TEMPROOT=${TEMPROOT}.`date +%m%d.%H.%M`
91    ;;
92  u)
93    NEW_UMASK=${OPTARG}
94    ;;
95  w)
96    SCREEN_WIDTH=${OPTARG}
97    ;;
98  *)
99    display_usage
100    exit 1
101    ;;
102  esac
103done
104
105echo ''
106
107# If the user has a pager defined, make sure we can run it
108#
109case "${DONT_CHECK_PAGER}" in
110'')
111  if [ -n "${PAGER}" -a ! -x "${PAGER}" ]; then
112    echo " *** Your PAGER environment variable specifies '${PAGER}', but"
113    echo "     I cannot execute it. In general it is good practice to"
114    echo "     specify the full path for environment variables like"
115    echo "     PAGER and EDITOR. Meanwhile, what would you like to do?"
116    echo ''
117    echo "  Use 'e' to exit mergemaster and fix your PAGER variable"
118    if [ -x /usr/local/bin/less ]; then
119    echo "  Use 'l' to set PAGER to /usr/local/bin/less for this run"
120    fi
121    echo "  Use 'm' to use plain old 'more' as your PAGER for this run"
122    echo ''
123    echo "  Default is to use plain old 'more' "
124    echo ''
125    read -p "What should I do? [Use 'more'] " FIXPAGER
126    case "${FIXPAGER}" in
127    [eE]*)
128       exit 0
129       ;;
130    [lL]*)
131       PAGER=/usr/local/bin/less
132       ;;
133    *)
134       PAGER=more
135       ;;
136    esac
137    echo ''
138  fi
139  ;;
140esac
141
142# If user has a pager defined, or got assigned one above, use it.
143# If not, use more.
144#
145PAGER=${PAGER:-more}
146
147if [ -n "${VERBOSE}" -a ! "${PAGER}" = "more" ]; then
148  echo " *** You have ${PAGER} defined as your pager so we will use that"
149  echo ''
150  sleep 3
151fi
152
153# Assign the diff flag once so we will not have to keep testing it
154#
155DIFF_FLAG=${DIFF_FLAG:--u}
156
157# Assign the source directory
158#
159SOURCEDIR=${SOURCEDIR:-/usr/src/etc}
160
161case "${RERUN}" in
162'')
163  # Set up the loop to test for the existence of the
164  # temp root directory.
165  #
166  TEST_TEMP_ROOT=yes
167  while [ "${TEST_TEMP_ROOT}" = "yes" ]; do
168    if [ -d "${TEMPROOT}" ]; then
169      echo "*** The directory specified for the temporary root environment,"
170      echo "    ${TEMPROOT}, exists. This can be a security risk if untrusted"
171      echo "    users have access to the system."
172      echo ''
173      case "${AUTO_RUN}" in
174      '')
175        echo "  Use 'd' to delete the old ${TEMPROOT} and continue"
176        echo "  Use 't' to select a new temporary root directory"
177        echo "  Use 'e' to exit mergemaster"
178        echo ''
179        echo "  Default is to use ${TEMPROOT} as is"
180        echo ''
181        read -p "How should I deal with this? [Use the existing ${TEMPROOT}] " DELORNOT
182          case "${DELORNOT}" in
183          [dD]*)
184            echo ''
185            echo "   *** Deleting the old ${TEMPROOT}"
186            echo ''
187            rm -rf "${TEMPROOT}"
188            unset TEST_TEMP_ROOT
189            ;;
190          [tT]*)
191            echo "   *** Enter new directory name for temporary root environment"
192            read TEMPROOT
193            ;;
194          [eE]*)
195            exit 0
196            ;;
197          *)
198            echo ''
199            echo "   *** Leaving ${TEMPROOT} intact"
200            echo ''
201            unset TEST_TEMP_ROOT
202            ;;
203          esac
204          ;;
205      *)
206        # If this is an auto-run, try a hopefully safe alternative then re-test anyway
207        TEMPROOT=/var/tmp/temproot.`date +%m%d.%H.%M.%S`
208        ;;
209      esac
210    else
211      unset TEST_TEMP_ROOT
212    fi
213  done
214
215  echo "*** Creating the temporary root environment in ${TEMPROOT}"
216
217  if mkdir -p "${TEMPROOT}"; then
218    echo " *** ${TEMPROOT} ready for use"
219  fi
220
221  if [ ! -d "${TEMPROOT}" ]; then
222    echo ''
223    echo "  *** FATAL ERROR: Cannot create ${TEMPROOT}"
224    echo ''
225    exit 1
226  fi
227
228  echo " *** Creating and populating directory structure in ${TEMPROOT}"
229  echo ''
230
231  case "${VERBOSE}" in
232  '') ;;
233  *)
234    echo " *** Press [Enter] or [Return] key to continue"
235    read ANY_KEY
236    unset ANY_KEY
237    ;;
238  esac
239
240  { cd ${SOURCEDIR} &&
241    make DESTDIR=${TEMPROOT} distrib-dirs &&
242    make DESTDIR=${TEMPROOT} distribution;} ||
243  { echo '';
244    echo "  *** FATAL ERROR: Cannot 'cd' to ${SOURCEDIR} and install files to the";
245    echo "      temproot environment";
246    echo '';
247    exit 1;}
248
249  # We really don't want to have to deal with these files, since
250  # master.passwd is the real file that should be compared, then
251  # the user should run pwd_mkdb if necessary.
252  # Only do this if we are not rerun'ing, since if we are the
253  # files will not be there.
254  #
255  rm ${TEMPROOT}/etc/spwd.db ${TEMPROOT}/etc/passwd ${TEMPROOT}/etc/pwd.db
256
257  ;; # End of the "RERUN" test
258esac
259
260# Get ready to start comparing files
261
262case "${VERBOSE}" in
263'') ;;
264*)
265  echo ''
266  echo " *** The following files exist only in the installed version"
267  echo "     of /etc. In the far majority of cases these files are"
268  echo "     necessary parts of the system and should not be deleted,"
269  echo "     however because these files are not updated by this process"
270  echo "     you might want to verify their status before rebooting your system."
271  echo ''
272  echo " *** Press [Enter] or [Return] key to continue"
273  read ANY_KEY
274  unset ANY_KEY
275  diff -qr /etc ${TEMPROOT}/etc | grep "^Only in /etc" | ${PAGER}
276  echo ''
277  echo " *** Press [Enter] or [Return] key to continue"
278  read ANY_KEY
279  unset ANY_KEY
280  ;;
281esac
282
283# Check umask if not specified on the command line,
284# and we are not doing an autorun
285#
286if [ -z "${NEW_UMASK}" -a -z "${AUTO_RUN}" ]; then
287  USER_UMASK=`umask`
288  case "${USER_UMASK}" in
289  0022) ;;
290  *)
291    echo ''
292    echo " *** Your umask is currently set to ${USER_UMASK}. By default, this script"
293    echo "     installs all files with the same user, group and modes that"
294    echo "     they are created with by ${SOURCEDIR}/Makefile, compared to"
295    echo "     a umask of 022. This umask allows world read permission when"
296    echo "     the file's default permissions have it."
297    echo "     No world permissions can sometimes cause problems. A umask of"
298    echo "     022 will restore the default behavior, but is not mandatory."
299    echo "     /etc/master.passwd is a special case. Its file permissions"
300    echo "     will be 600 (rw-------) if installed."
301    echo ''
302    read -p "What umask should I use? [${USER_UMASK}] " NEW_UMASK
303    NEW_UMASK="${NEW_UMASK:-$USER_UMASK}"
304    ;;
305  esac
306  echo ''
307fi
308
309CONFIRMED_UMASK=${NEW_UMASK:-0022}
310
311# Warn users who have an /etc/sysconfig file
312#
313if [ -f /etc/sysconfig ]; then
314  echo " *** There is an /etc/sysconfig file on this system. Starting with"
315  echo "     FreeBSD version 2.2.2 those settings have moved from /etc/sysconfig"
316  echo "     to /etc/rc.conf. If you are upgrading an older system make sure"
317  echo "     that you transfer your settings by hand from sysconfig to rc.conf and"
318  echo "     install the rc.conf file. If you have already made this transition,"
319  echo "     you should consider renaming or deleting the /etc/sysconfig file."
320  echo ''
321  case "${AUTO_RUN}" in
322  '')
323    read -p "Continue with the merge process? [yes] " CONT_OR_NOT
324    case "${CONT_OR_NOT}" in
325    [nN]*)
326      exit 0
327      ;;
328    *)
329      echo "   *** Continuing"
330      echo ''
331      ;;
332    esac
333    ;;
334  *) ;;
335  esac
336fi
337
338echo ''
339echo "*** Beginning comparison"
340echo ''
341
342cd "${TEMPROOT}"
343
344# Use the umask/mode information to install the files
345# Create directories as needed
346#
347mm_install () {
348  local INSTALL_DIR
349  INSTALL_DIR=${1#.}
350  INSTALL_DIR=${INSTALL_DIR%/*}
351  case "${INSTALL_DIR}" in
352  '')
353    INSTALL_DIR=/
354    ;;
355  esac
356
357  if [ -n "${INSTALL_DIR}" -a ! -d "${INSTALL_DIR}" ]; then
358    DIR_MODE=`perl -e 'printf "%04o\n", (((stat("$ARGV[0]"))[2] & 07777) &~ oct("$ARGV[1]"))' "${TEMPROOT}/${INSTALL_DIR}" "${CONFIRMED_UMASK}"`
359    install -d -o root -g wheel -m "${DIR_MODE}" "${INSTALL_DIR}"
360  fi
361
362  FILE_MODE=`perl -e 'printf "%04o\n", (((stat("$ARGV[0]"))[2] & 07777) &~ oct("$ARGV[1]"))' "${1}" "${CONFIRMED_UMASK}"`
363
364  if [ ! -x "${1}" ]; then
365    case "${1#.}" in
366    /dev/MAKEDEV)
367      NEED_MAKEDEV=yes
368      ;;
369    /etc/aliases)
370      NEED_NEWALIASES=yes
371      ;;
372    /etc/login.conf)
373      NEED_CAP_MKDB=yes
374      ;;
375    /etc/master.passwd)
376      install -m 600 "${1}" "${INSTALL_DIR}"
377      NEED_PWD_MKDB=yes
378      DONT_INSTALL=yes
379      ;;
380    /.cshrc | /.profile)
381      case "${LINK_EXPLAINED}" in
382      '')
383        echo "   *** Historically FreeBSD has had a hard link from"
384        echo "       /.cshrc and /.profile to their namesakes in"
385        echo "       /root. Please indicate your preference below"
386        echo "       for bringing your installed files up to date."
387        echo ''
388        LINK_EXPLAINED=yes
389        ;;
390      esac
391
392      echo "   Use 'd' to delete the temporary ${COMPFILE}"
393      echo "   Use 'l' to delete the existing ${COMPFILE#.} and create the link"
394      echo ''
395      echo "   Default is to leave the temporary file to deal with by hand"
396      echo ''
397      read -p "  How should I handle ${COMPFILE}? [Leave it to install later] " HANDLE_LINK
398      case "${HANDLE_LINK}" in
399      [dD]*)
400        rm "${COMPFILE}"
401        echo ''
402        echo "   *** Deleting ${COMPFILE}"
403        ;;
404      [lL]*)
405        echo ''
406        if [ -e "${COMPFILE#.}" ]; then
407          rm "${COMPFILE#.}"
408        fi
409        if ln "/root/${COMPFILE##*/}" "${COMPFILE#.}"; then
410          echo "   *** Link from ${COMPFILE#.} to /root/${COMPFILE##*/} installed successfully"
411          rm "${COMPFILE}"
412        else
413          echo "   *** Error linking ${COMPFILE#.} to /root/${COMPFILE##*/}, ${COMPFILE} will remain to install by hand"
414        fi
415        ;;
416      *)
417        echo "   *** ${COMPFILE} will remain for your consideration"
418        ;;
419      esac
420      DONT_INSTALL=yes
421      ;;
422    esac
423
424    case "${DONT_INSTALL}" in
425    '')
426      install -m "${FILE_MODE}" "${1}" "${INSTALL_DIR}"
427      ;;
428    *)
429      unset DONT_INSTALL
430      ;;
431    esac
432
433  else
434    install -m "${FILE_MODE}" "${1}" "${INSTALL_DIR}"
435  fi
436  return $?
437}
438
439compare_ids () {
440  case "${1}" in
441 "${2}")
442    echo " *** Temp ${COMPFILE} and installed have the same ${IDTAG}, deleting"
443    rm "${COMPFILE}"
444    ;;
445  esac
446}
447
448# Using -size +0 avoids uselessly checking the empty log files created
449# by ${SOURCEDIR}/Makefile and the device entries in ./dev, but does
450# check the scripts in ./dev, as we'd like.
451#
452for COMPFILE in `find . -type f -size +0`; do
453  case "${STRICT}" in
454  '' | [Nn][Oo])
455    # Compare CVS $Id's first so if the file hasn't been modified
456    # local changes will be ignored.
457    # If the files have the same $Id, delete the one in temproot so the
458    # user will have less to wade through if files are left to merge by hand.
459    # Take the $Id -> $FreeBSD tag change into account
460    #
461    FREEBSDID1=`grep "[$]FreeBSD:" ${COMPFILE#.} 2>/dev/null`
462    FREEBSDID2=`grep "[$]FreeBSD:" ${COMPFILE} 2>/dev/null`
463
464    if [ -n "${FREEBSDID1}" -a -n "${FREEBSDID2}" ]; then
465      IDTAG='$FreeBSD'
466      compare_ids "${FREEBSDID1}" "${FREEBSDID2}"
467    else
468      CVSID1=`grep "[$]Id:" ${COMPFILE#.} 2>/dev/null`
469      CVSID2=`grep "[$]Id:" ${COMPFILE} 2>/dev/null`
470
471      if [ -n "${CVSID1}" -a -n "${CVSID2}" ]; then
472        IDTAG='$Id'
473        compare_ids "${CVSID1}" "${CVSID2}"
474      fi
475    fi
476    ;;
477  esac
478
479  # If the file is still here either because the $Ids are different, the
480  # file doesn't have an $Id, or we're using STRICT mode; look at the diff.
481  #
482  if [ -f "${COMPFILE}" ]; then
483
484    # Do an absolute diff first to see if the files are actually different.
485    # If they're not different, delete the one in temproot.
486    #
487    if diff -q "${COMPFILE#.}" "${COMPFILE}" > /dev/null 2>&1; then
488      echo " *** Temp ${COMPFILE} and installed are the same, deleting"
489      rm "${COMPFILE}"
490    else
491      # Ok, the files are different, so show the user where they differ. Use user's
492      # choice of diff methods; and user's pager if they have one. Use more if not.
493      # Use unified diffs by default. Context diffs give me a headache. :)
494      #
495      case "${AUTO_RUN}" in
496      '')
497        echo ''
498        if [ -f "${COMPFILE#.}" -a -f "${COMPFILE}" ]; then
499          echo "  *** Displaying differences between ${COMPFILE} and installed version"
500          echo ''
501          diff "${DIFF_FLAG}" "${COMPFILE#.}" "${COMPFILE}" | ${PAGER}
502          echo ''
503        else
504          echo "  *** There is no installed version of ${COMPFILE}"
505          NO_INSTALLED=yes
506        fi
507        echo "  Use 'd' to delete the temporary ${COMPFILE}"
508        echo "  Use 'i' to install the temporary ${COMPFILE}"
509        case "${NO_INSTALLED}" in
510        '')
511          echo "  Use 'm' to merge the old and new versions"
512          ;;
513        esac
514        echo ''
515        echo "  Default is to leave the temporary file to deal with by hand"
516        echo ''
517        read -p "How should I deal with this? [Leave it for later] " HANDLE_COMPFILE
518        case "${HANDLE_COMPFILE}" in
519        [dD]*)
520          rm "${COMPFILE}"
521          echo ''
522          echo "   *** Deleting ${COMPFILE}"
523          ;;
524        [iI]*)
525          echo ''
526          if mm_install "${COMPFILE}"; then
527            echo "   *** ${COMPFILE} installed successfully"
528          else
529            echo "   *** Problem installing ${COMPFILE}, it will remain to merge by hand"
530          fi
531          ;;
532        [mM]*)
533          case "${NO_INSTALLED}" in
534          '')
535            case "${VERBOSE}" in
536            '') ;;
537            *)
538              echo "   *** Type h at the sdiff prompt (%) to get usage help"
539              ;;
540            esac
541            echo ''
542            MERGE_AGAIN=yes
543            while [ "${MERGE_AGAIN}" = "yes" ]; do
544              # Prime file.merged so we don't blat the owner/group id's
545              cp -p "${COMPFILE}" "${COMPFILE}.merged"
546              sdiff -o "${COMPFILE}.merged" --text --suppress-common-lines \
547                --width=${SCREEN_WIDTH:-80} "${COMPFILE#.}" "${COMPFILE}"
548
549              echo ''
550              echo "  Use 'i' to install merged file"
551              echo "  Use 'r' to re-do the merge"
552              echo "  Default is to leave the temporary file to deal with by hand"
553              echo ''
554              read -p "    *** How should I deal with the merged file? [Leave it for later] " INSTALL_MERGED
555
556              case "${INSTALL_MERGED}" in
557              [iI]*)
558                mv "${COMPFILE}.merged" "${COMPFILE}"
559                echo ''
560                if mm_install "${COMPFILE}"; then
561                  echo "     *** Merged version of ${COMPFILE} installed successfully"
562                else 
563                  echo "     *** Problem installing ${COMPFILE}, it will remain to merge by hand later"
564                fi 
565                unset MERGE_AGAIN
566                ;;
567              [rR]*)
568                rm "${COMPFILE}.merged"
569                ;; 
570              *)
571                echo "   *** ${COMPFILE} will remain for your consideration"
572                unset MERGE_AGAIN
573              ;;
574              esac
575            done
576            ;;
577          *)
578            echo ''
579            echo "   *** There is no installed version of ${COMPFILE}"
580            echo "       to merge so it will remain to install by hand later"
581            echo ''
582            echo "   *** Press [Enter] or [Return] key to continue"
583            read ANY_KEY
584            unset ANY_KEY
585            ;;
586          esac # End of "No installed version of file but user selected merge" test
587          ;;
588        *)
589          echo ''
590          echo "   *** ${COMPFILE} will remain for your consideration"
591          ;;
592        esac  # End of "How to handle files that are different"
593        unset NO_INSTALLED
594        echo ''
595        case "${VERBOSE}" in
596        '') ;;
597        *)
598          sleep 3
599          ;;
600        esac
601        ;;
602      *)
603        # If this is an auto run, make it official
604        echo "   *** ${COMPFILE} will remain for your consideration"
605        ;;
606      esac # Auto run test
607    fi # Yes, the files are different
608  fi # Yes, the file still remains to be checked
609done # This is for the do way up there at the beginning of the comparison
610
611echo "*** Comparison complete"
612echo ''
613
614TEST_FOR_FILES=`find ${TEMPROOT} -type f -size +0 2>/dev/null`
615if [ -n "${TEST_FOR_FILES}" ]; then
616  echo "*** Files that remain for you to merge by hand:"
617  find "${TEMPROOT}" -type f -size +0
618fi
619
620case "${AUTO_RUN}" in
621'')
622  echo ''
623  read -p "Do you wish to delete what is left of ${TEMPROOT}? [no] " DEL_TEMPROOT
624  case "${DEL_TEMPROOT}" in
625  [yY]*)
626    if rm -rf "${TEMPROOT}"; then
627      echo " *** ${TEMPROOT} has been deleted"
628    else
629      echo " *** Unable to delete ${TEMPROOT}"
630    fi
631    ;;
632  *)
633    echo " *** ${TEMPROOT} will remain"
634    ;;
635  esac
636  ;;
637*) ;;
638esac
639
640case "${NEED_MAKEDEV}" in
641'') ;;
642*)
643  echo ''
644  echo "*** You installed a new /dev/MAKEDEV script, so make sure that you run"
645  echo "    'cd /dev && /bin/sh MAKEDEV all' to rebuild your devices"
646  ;;
647esac
648
649case "${NEED_NEWALIASES}" in
650'') ;;
651*)
652  echo ''
653  echo "*** You installed a new aliases file, so make sure that you run"
654  echo "    'newaliases' to rebuild your aliases database"
655  ;;
656esac
657
658case "${NEED_CAP_MKDB}" in
659'') ;;
660*)
661  echo ''
662  echo "*** You installed a login.conf file, so make sure that you run"
663  echo "    'cap_mkdb /etc/login.conf' to rebuild your login.conf database"
664  ;;
665esac
666
667case "${NEED_PWD_MKDB}" in
668'') ;;
669*)
670  echo ''
671  echo "*** You installed a new master.passwd file, so make sure that you run"
672  echo "    'pwd_mkdb -p /etc/master.passwd' to rebuild your password files"
673  ;;
674esac
675
676echo ''
677
678exit 0
679