]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/mergemaster/mergemaster.sh
This commit was generated by cvs2svn to compensate for changes in r58650,
[FreeBSD/FreeBSD.git] / usr.sbin / mergemaster / mergemaster.sh
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$
12
13 PATH=/bin:/usr/bin:/usr/sbin
14
15 display_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
34 display_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 #
47 TEMPROOT='/var/tmp/temproot'
48
49 # Read .mergemasterrc before command line so CLI can override
50 #
51 if [ -f "$HOME/.mergemasterrc" ]; then
52   . "$HOME/.mergemasterrc"
53 fi
54
55 # Check the command line options
56 #
57 while 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
102 done
103
104 echo ''
105
106 # If the user has a pager defined, make sure we can run it
107 #
108 case "${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   ;;
139 esac
140
141 # If user has a pager defined, or got assigned one above, use it.
142 # If not, use more.
143 #
144 PAGER=${PAGER:-more}
145
146 if [ -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
150 fi
151
152 # Assign the diff flag once so we will not have to keep testing it
153 #
154 DIFF_FLAG=${DIFF_FLAG:--u}
155
156 # Assign the source directory
157 #
158 SOURCEDIR=${SOURCEDIR:-/usr/src/etc}
159
160 case "${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} -DNO_MAKEDEV 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   # Avoid comparing the motd if the user specifies it in .mergemasterrc
257   case "${IGNORE_MOTD}" in
258   '') ;;
259   *) rm ${TEMPROOT}/etc/motd
260      ;;
261   esac
262
263   ;; # End of the "RERUN" test
264 esac
265
266 # Get ready to start comparing files
267
268 case "${VERBOSE}" in
269 '') ;;
270 *)
271   echo ''
272   echo " *** The following files exist only in the installed version"
273   echo "     of /etc. In the far majority of cases these files are"
274   echo "     necessary parts of the system and should not be deleted,"
275   echo "     however because these files are not updated by this process"
276   echo "     you might want to verify their status before rebooting your system."
277   echo ''
278   echo " *** Press [Enter] or [Return] key to continue"
279   read ANY_KEY
280   unset ANY_KEY
281   diff -qr /etc ${TEMPROOT}/etc | grep "^Only in /etc" | ${PAGER}
282   echo ''
283   echo " *** Press [Enter] or [Return] key to continue"
284   read ANY_KEY
285   unset ANY_KEY
286   ;;
287 esac
288
289 # Check umask if not specified on the command line,
290 # and we are not doing an autorun
291 #
292 if [ -z "${NEW_UMASK}" -a -z "${AUTO_RUN}" ]; then
293   USER_UMASK=`umask`
294   case "${USER_UMASK}" in
295   0022) ;;
296   *)
297     echo ''
298     echo " *** Your umask is currently set to ${USER_UMASK}. By default, this script"
299     echo "     installs all files with the same user, group and modes that"
300     echo "     they are created with by ${SOURCEDIR}/Makefile, compared to"
301     echo "     a umask of 022. This umask allows world read permission when"
302     echo "     the file's default permissions have it."
303     echo "     No world permissions can sometimes cause problems. A umask of"
304     echo "     022 will restore the default behavior, but is not mandatory."
305     echo "     /etc/master.passwd is a special case. Its file permissions"
306     echo "     will be 600 (rw-------) if installed."
307     echo ''
308     read -p "What umask should I use? [${USER_UMASK}] " NEW_UMASK
309     NEW_UMASK="${NEW_UMASK:-$USER_UMASK}"
310     ;;
311   esac
312   echo ''
313 fi
314
315 CONFIRMED_UMASK=${NEW_UMASK:-0022}
316
317 # Warn users who have an /etc/sysconfig file
318 #
319 if [ -f /etc/sysconfig ]; then
320   echo " *** There is an /etc/sysconfig file on this system. Starting with"
321   echo "     FreeBSD version 2.2.2 those settings have moved from /etc/sysconfig"
322   echo "     to /etc/rc.conf. If you are upgrading an older system make sure"
323   echo "     that you transfer your settings by hand from sysconfig to rc.conf and"
324   echo "     install the rc.conf file. If you have already made this transition,"
325   echo "     you should consider renaming or deleting the /etc/sysconfig file."
326   echo ''
327   case "${AUTO_RUN}" in
328   '')
329     read -p "Continue with the merge process? [yes] " CONT_OR_NOT
330     case "${CONT_OR_NOT}" in
331     [nN]*)
332       exit 0
333       ;;
334     *)
335       echo "   *** Continuing"
336       echo ''
337       ;;
338     esac
339     ;;
340   *) ;;
341   esac
342 fi
343
344 echo ''
345 echo "*** Beginning comparison"
346 echo ''
347
348 cd "${TEMPROOT}"
349
350 # Use the umask/mode information to install the files
351 # Create directories as needed
352 #
353 mm_install () {
354   local INSTALL_DIR
355   INSTALL_DIR=${1#.}
356   INSTALL_DIR=${INSTALL_DIR%/*}
357   case "${INSTALL_DIR}" in
358   '')
359     INSTALL_DIR=/
360     ;;
361   esac
362
363   if [ -n "${INSTALL_DIR}" -a ! -d "${INSTALL_DIR}" ]; then
364     DIR_MODE=`perl -e 'printf "%04o\n", (((stat("$ARGV[0]"))[2] & 07777) &~ oct("$ARGV[1]"))' "${TEMPROOT}/${INSTALL_DIR}" "${CONFIRMED_UMASK}"`
365     install -d -o root -g wheel -m "${DIR_MODE}" "${INSTALL_DIR}"
366   fi
367
368   FILE_MODE=`perl -e 'printf "%04o\n", (((stat("$ARGV[0]"))[2] & 07777) &~ oct("$ARGV[1]"))' "${1}" "${CONFIRMED_UMASK}"`
369
370   if [ ! -x "${1}" ]; then
371     case "${1#.}" in
372     /dev/MAKEDEV)
373       NEED_MAKEDEV=yes
374       ;;
375     /etc/aliases)
376       NEED_NEWALIASES=yes
377       ;;
378     /etc/login.conf)
379       NEED_CAP_MKDB=yes
380       ;;
381     /etc/master.passwd)
382       install -m 600 "${1}" "${INSTALL_DIR}"
383       NEED_PWD_MKDB=yes
384       DONT_INSTALL=yes
385       ;;
386     /.cshrc | /.profile)
387       case "${LINK_EXPLAINED}" in
388       '')
389         echo "   *** Historically FreeBSD has had a hard link from"
390         echo "       /.cshrc and /.profile to their namesakes in"
391         echo "       /root. Please indicate your preference below"
392         echo "       for bringing your installed files up to date."
393         echo ''
394         LINK_EXPLAINED=yes
395         ;;
396       esac
397
398       echo "   Use 'd' to delete the temporary ${COMPFILE}"
399       echo "   Use 'l' to delete the existing ${COMPFILE#.} and create the link"
400       echo ''
401       echo "   Default is to leave the temporary file to deal with by hand"
402       echo ''
403       read -p "  How should I handle ${COMPFILE}? [Leave it to install later] " HANDLE_LINK
404       case "${HANDLE_LINK}" in
405       [dD]*)
406         rm "${COMPFILE}"
407         echo ''
408         echo "   *** Deleting ${COMPFILE}"
409         ;;
410       [lL]*)
411         echo ''
412         if [ -e "${COMPFILE#.}" ]; then
413           rm "${COMPFILE#.}"
414         fi
415         if ln "/root/${COMPFILE##*/}" "${COMPFILE#.}"; then
416           echo "   *** Link from ${COMPFILE#.} to /root/${COMPFILE##*/} installed successfully"
417           rm "${COMPFILE}"
418         else
419           echo "   *** Error linking ${COMPFILE#.} to /root/${COMPFILE##*/}, ${COMPFILE} will remain to install by hand"
420         fi
421         ;;
422       *)
423         echo "   *** ${COMPFILE} will remain for your consideration"
424         ;;
425       esac
426       DONT_INSTALL=yes
427       ;;
428     esac
429
430     case "${DONT_INSTALL}" in
431     '')
432       install -m "${FILE_MODE}" "${1}" "${INSTALL_DIR}"
433       ;;
434     *)
435       unset DONT_INSTALL
436       ;;
437     esac
438
439   else
440     install -m "${FILE_MODE}" "${1}" "${INSTALL_DIR}"
441   fi
442   return $?
443 }
444
445 compare_ids () {
446   case "${1}" in
447  "${2}")
448     echo " *** Temp ${COMPFILE} and installed have the same ${IDTAG}, deleting"
449     rm "${COMPFILE}"
450     ;;
451   esac
452 }
453
454 # Using -size +0 avoids uselessly checking the empty log files created
455 # by ${SOURCEDIR}/Makefile and the device entries in ./dev, but does
456 # check the scripts in ./dev, as we'd like.
457 #
458 for COMPFILE in `find . -type f -size +0`; do
459   case "${STRICT}" in
460   '' | [Nn][Oo])
461     # Compare CVS $Id's first so if the file hasn't been modified
462     # local changes will be ignored.
463     # If the files have the same $Id, delete the one in temproot so the
464     # user will have less to wade through if files are left to merge by hand.
465     # Take the $Id -> $FreeBSD tag change into account
466     #
467     FREEBSDID1=`grep "[$]FreeBSD:" ${COMPFILE#.} 2>/dev/null`
468     FREEBSDID2=`grep "[$]FreeBSD:" ${COMPFILE} 2>/dev/null`
469
470     if [ -n "${FREEBSDID1}" -a -n "${FREEBSDID2}" ]; then
471       IDTAG='$FreeBSD'
472       compare_ids "${FREEBSDID1}" "${FREEBSDID2}"
473     else
474       CVSID1=`grep "[$]Id:" ${COMPFILE#.} 2>/dev/null`
475       CVSID2=`grep "[$]Id:" ${COMPFILE} 2>/dev/null`
476
477       if [ -n "${CVSID1}" -a -n "${CVSID2}" ]; then
478         IDTAG='$Id'
479         compare_ids "${CVSID1}" "${CVSID2}"
480       fi
481     fi
482     ;;
483   esac
484
485   # If the file is still here either because the $Ids are different, the
486   # file doesn't have an $Id, or we're using STRICT mode; look at the diff.
487   #
488   if [ -f "${COMPFILE}" ]; then
489
490     # Do an absolute diff first to see if the files are actually different.
491     # If they're not different, delete the one in temproot.
492     #
493     if diff -q "${COMPFILE#.}" "${COMPFILE}" > /dev/null 2>&1; then
494       echo " *** Temp ${COMPFILE} and installed are the same, deleting"
495       rm "${COMPFILE}"
496     else
497       # Ok, the files are different, so show the user where they differ. Use user's
498       # choice of diff methods; and user's pager if they have one. Use more if not.
499       # Use unified diffs by default. Context diffs give me a headache. :)
500       #
501       case "${AUTO_RUN}" in
502       '')
503         echo ''
504         if [ -f "${COMPFILE#.}" -a -f "${COMPFILE}" ]; then
505           echo "  *** Displaying differences between ${COMPFILE} and installed version"
506           echo ''
507           diff "${DIFF_FLAG}" "${COMPFILE#.}" "${COMPFILE}" | ${PAGER}
508           echo ''
509         else
510           echo "  *** There is no installed version of ${COMPFILE}"
511           NO_INSTALLED=yes
512         fi
513         echo "  Use 'd' to delete the temporary ${COMPFILE}"
514         echo "  Use 'i' to install the temporary ${COMPFILE}"
515         case "${NO_INSTALLED}" in
516         '')
517           echo "  Use 'm' to merge the old and new versions"
518           ;;
519         esac
520         echo ''
521         echo "  Default is to leave the temporary file to deal with by hand"
522         echo ''
523         read -p "How should I deal with this? [Leave it for later] " HANDLE_COMPFILE
524         case "${HANDLE_COMPFILE}" in
525         [dD]*)
526           rm "${COMPFILE}"
527           echo ''
528           echo "   *** Deleting ${COMPFILE}"
529           ;;
530         [iI]*)
531           echo ''
532           if mm_install "${COMPFILE}"; then
533             echo "   *** ${COMPFILE} installed successfully"
534           else
535             echo "   *** Problem installing ${COMPFILE}, it will remain to merge by hand"
536           fi
537           ;;
538         [mM]*)
539           case "${NO_INSTALLED}" in
540           '')
541             case "${VERBOSE}" in
542             '') ;;
543             *)
544               echo "   *** Type h at the sdiff prompt (%) to get usage help"
545               ;;
546             esac
547             echo ''
548             MERGE_AGAIN=yes
549             while [ "${MERGE_AGAIN}" = "yes" ]; do
550               # Prime file.merged so we don't blat the owner/group id's
551               cp -p "${COMPFILE}" "${COMPFILE}.merged"
552               sdiff -o "${COMPFILE}.merged" --text --suppress-common-lines \
553                 --width=${SCREEN_WIDTH:-80} "${COMPFILE#.}" "${COMPFILE}"
554
555               echo ''
556               echo "  Use 'i' to install merged file"
557               echo "  Use 'r' to re-do the merge"
558               echo "  Default is to leave the temporary file to deal with by hand"
559               echo ''
560               read -p "    *** How should I deal with the merged file? [Leave it for later] " INSTALL_MERGED
561
562               case "${INSTALL_MERGED}" in
563               [iI]*)
564                 mv "${COMPFILE}.merged" "${COMPFILE}"
565                 echo ''
566                 if mm_install "${COMPFILE}"; then
567                   echo "     *** Merged version of ${COMPFILE} installed successfully"
568                 else 
569                   echo "     *** Problem installing ${COMPFILE}, it will remain to merge by hand later"
570                 fi 
571                 unset MERGE_AGAIN
572                 ;;
573               [rR]*)
574                 rm "${COMPFILE}.merged"
575                 ;; 
576               *)
577                 echo "   *** ${COMPFILE} will remain for your consideration"
578                 unset MERGE_AGAIN
579               ;;
580               esac
581             done
582             ;;
583           *)
584             echo ''
585             echo "   *** There is no installed version of ${COMPFILE}"
586             echo "       to merge so it will remain to install by hand later"
587             echo ''
588             echo "   *** Press [Enter] or [Return] key to continue"
589             read ANY_KEY
590             unset ANY_KEY
591             ;;
592           esac # End of "No installed version of file but user selected merge" test
593           ;;
594         *)
595           echo ''
596           echo "   *** ${COMPFILE} will remain for your consideration"
597           ;;
598         esac  # End of "How to handle files that are different"
599         unset NO_INSTALLED
600         echo ''
601         case "${VERBOSE}" in
602         '') ;;
603         *)
604           sleep 3
605           ;;
606         esac
607         ;;
608       *)
609         # If this is an auto run, make it official
610         echo "   *** ${COMPFILE} will remain for your consideration"
611         ;;
612       esac # Auto run test
613     fi # Yes, the files are different
614   fi # Yes, the file still remains to be checked
615 done # This is for the do way up there at the beginning of the comparison
616
617 echo ''
618 echo "*** Comparison complete"
619 echo ''
620
621 TEST_FOR_FILES=`find ${TEMPROOT} -type f -size +0 2>/dev/null`
622 if [ -n "${TEST_FOR_FILES}" ]; then
623   echo "*** Files that remain for you to merge by hand:"
624   find "${TEMPROOT}" -type f -size +0
625 fi
626
627 case "${AUTO_RUN}" in
628 '')
629   echo ''
630   read -p "Do you wish to delete what is left of ${TEMPROOT}? [no] " DEL_TEMPROOT
631   case "${DEL_TEMPROOT}" in
632   [yY]*)
633     if rm -rf "${TEMPROOT}"; then
634       echo " *** ${TEMPROOT} has been deleted"
635     else
636       echo " *** Unable to delete ${TEMPROOT}"
637     fi
638     ;;
639   *)
640     echo " *** ${TEMPROOT} will remain"
641     ;;
642   esac
643   ;;
644 *) ;;
645 esac
646
647 case "${NEED_MAKEDEV}" in
648 '') ;;
649 *)
650   echo ''
651   echo "*** You installed a new /dev/MAKEDEV script, so make sure that you run"
652   echo "    'cd /dev && /bin/sh MAKEDEV all' to rebuild your devices"
653   ;;
654 esac
655
656 case "${NEED_NEWALIASES}" in
657 '') ;;
658 *)
659   echo ''
660   echo "*** You installed a new aliases file, so make sure that you run"
661   echo "    'newaliases' to rebuild your aliases database"
662   ;;
663 esac
664
665 case "${NEED_CAP_MKDB}" in
666 '') ;;
667 *)
668   echo ''
669   echo "*** You installed a login.conf file, so make sure that you run"
670   echo "    'cap_mkdb /etc/login.conf' to rebuild your login.conf database"
671   ;;
672 esac
673
674 case "${NEED_PWD_MKDB}" in
675 '') ;;
676 *)
677   echo ''
678   echo "*** You installed a new master.passwd file, so make sure that you run"
679   echo "    'pwd_mkdb -p /etc/master.passwd' to rebuild your password files"
680   ;;
681 esac
682
683 echo ''
684
685 exit 0