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