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