5 # Compare files created by /usr/src/etc/Makefile (or the directory
6 # the user specifies) with the currently installed copies.
8 # Copyright 1998, 1999 Douglas Barton
13 PATH=/bin:/usr/bin:/usr/sbin
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]"
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"
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"
45 # Set the default path for the temporary root environment
47 TEMPROOT='/var/tmp/temproot'
49 # Read .mergemasterrc before command line so CLI can override
51 if [ -f "$HOME/.mergemasterrc" ]; then
52 . "$HOME/.mergemasterrc"
55 # Check the command line options
57 while getopts ":ascrvhm:t:du:w:" COMMAND_LINE_ARGUMENT ; do
58 case "${COMMAND_LINE_ARGUMENT}" in
89 TEMPROOT=${TEMPROOT}.`date +%m%d.%H.%M`
95 SCREEN_WIDTH=${OPTARG}
106 # If the user has a pager defined, make sure we can run it
108 case "${DONT_CHECK_PAGER}" in
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?"
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"
120 echo " Use 'm' to use plain old 'more' as your PAGER for this run"
122 echo " Default is to use plain old 'more' "
124 read -p "What should I do? [Use 'more'] " FIXPAGER
125 case "${FIXPAGER}" in
130 PAGER=/usr/local/bin/less
141 # If user has a pager defined, or got assigned one above, use it.
146 if [ -n "${VERBOSE}" -a ! "${PAGER}" = "more" ]; then
147 echo " *** You have ${PAGER} defined as your pager so we will use that"
152 # Assign the diff flag once so we will not have to keep testing it
154 DIFF_FLAG=${DIFF_FLAG:--u}
156 # Assign the source directory
158 SOURCEDIR=${SOURCEDIR:-/usr/src/etc}
162 # Set up the loop to test for the existence of the
163 # temp root directory.
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."
172 case "${AUTO_RUN}" in
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"
178 echo " Default is to use ${TEMPROOT} as is"
180 read -p "How should I deal with this? [Use the existing ${TEMPROOT}] " DELORNOT
181 case "${DELORNOT}" in
184 echo " *** Deleting the old ${TEMPROOT}"
190 echo " *** Enter new directory name for temporary root environment"
198 echo " *** Leaving ${TEMPROOT} intact"
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`
214 echo "*** Creating the temporary root environment in ${TEMPROOT}"
216 if mkdir -p "${TEMPROOT}"; then
217 echo " *** ${TEMPROOT} ready for use"
220 if [ ! -d "${TEMPROOT}" ]; then
222 echo " *** FATAL ERROR: Cannot create ${TEMPROOT}"
227 echo " *** Creating and populating directory structure in ${TEMPROOT}"
233 echo " *** Press [Enter] or [Return] key to continue"
240 make DESTDIR=${TEMPROOT} distrib-dirs &&
241 make DESTDIR=${TEMPROOT} -DNO_MAKEDEV distribution;} ||
243 echo " *** FATAL ERROR: Cannot 'cd' to ${SOURCEDIR} and install files to the";
244 echo " temproot environment";
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.
254 rm ${TEMPROOT}/etc/spwd.db ${TEMPROOT}/etc/passwd ${TEMPROOT}/etc/pwd.db
256 # Avoid comparing the motd if the user specifies it in .mergemasterrc
257 case "${IGNORE_MOTD}" in
259 *) rm ${TEMPROOT}/etc/motd
263 ;; # End of the "RERUN" test
266 # Get ready to start comparing files
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."
278 echo " *** Press [Enter] or [Return] key to continue"
281 diff -qr /etc ${TEMPROOT}/etc | grep "^Only in /etc" | ${PAGER}
283 echo " *** Press [Enter] or [Return] key to continue"
289 # Check umask if not specified on the command line,
290 # and we are not doing an autorun
292 if [ -z "${NEW_UMASK}" -a -z "${AUTO_RUN}" ]; then
294 case "${USER_UMASK}" in
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."
308 read -p "What umask should I use? [${USER_UMASK}] " NEW_UMASK
309 NEW_UMASK="${NEW_UMASK:-$USER_UMASK}"
315 CONFIRMED_UMASK=${NEW_UMASK:-0022}
317 # Warn users who have an /etc/sysconfig file
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."
327 case "${AUTO_RUN}" in
329 read -p "Continue with the merge process? [yes] " CONT_OR_NOT
330 case "${CONT_OR_NOT}" in
335 echo " *** Continuing"
345 echo "*** Beginning comparison"
350 # Use the umask/mode information to install the files
351 # Create directories as needed
356 INSTALL_DIR=${INSTALL_DIR%/*}
357 case "${INSTALL_DIR}" in
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}"
368 FILE_MODE=`perl -e 'printf "%04o\n", (((stat("$ARGV[0]"))[2] & 07777) &~ oct("$ARGV[1]"))' "${1}" "${CONFIRMED_UMASK}"`
370 if [ ! -x "${1}" ]; then
382 install -m 600 "${1}" "${INSTALL_DIR}"
387 case "${LINK_EXPLAINED}" in
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."
398 echo " Use 'd' to delete the temporary ${COMPFILE}"
399 echo " Use 'l' to delete the existing ${COMPFILE#.} and create the link"
401 echo " Default is to leave the temporary file to deal with by hand"
403 read -p " How should I handle ${COMPFILE}? [Leave it to install later] " HANDLE_LINK
404 case "${HANDLE_LINK}" in
408 echo " *** Deleting ${COMPFILE}"
412 if [ -e "${COMPFILE#.}" ]; then
415 if ln "/root/${COMPFILE##*/}" "${COMPFILE#.}"; then
416 echo " *** Link from ${COMPFILE#.} to /root/${COMPFILE##*/} installed successfully"
419 echo " *** Error linking ${COMPFILE#.} to /root/${COMPFILE##*/}, ${COMPFILE} will remain to install by hand"
423 echo " *** ${COMPFILE} will remain for your consideration"
430 case "${DONT_INSTALL}" in
432 install -m "${FILE_MODE}" "${1}" "${INSTALL_DIR}"
440 install -m "${FILE_MODE}" "${1}" "${INSTALL_DIR}"
448 echo " *** Temp ${COMPFILE} and installed have the same ${IDTAG}, deleting"
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.
458 for COMPFILE in `find . -type f -size +0`; do
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
467 FREEBSDID1=`grep "[$]FreeBSD:" ${COMPFILE#.} 2>/dev/null`
468 FREEBSDID2=`grep "[$]FreeBSD:" ${COMPFILE} 2>/dev/null`
470 if [ -n "${FREEBSDID1}" -a -n "${FREEBSDID2}" ]; then
472 compare_ids "${FREEBSDID1}" "${FREEBSDID2}"
474 CVSID1=`grep "[$]Id:" ${COMPFILE#.} 2>/dev/null`
475 CVSID2=`grep "[$]Id:" ${COMPFILE} 2>/dev/null`
477 if [ -n "${CVSID1}" -a -n "${CVSID2}" ]; then
479 compare_ids "${CVSID1}" "${CVSID2}"
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.
488 if [ -f "${COMPFILE}" ]; then
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.
493 if diff -q "${COMPFILE#.}" "${COMPFILE}" > /dev/null 2>&1; then
494 echo " *** Temp ${COMPFILE} and installed are the same, deleting"
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. :)
501 case "${AUTO_RUN}" in
504 if [ -f "${COMPFILE#.}" -a -f "${COMPFILE}" ]; then
505 echo " *** Displaying differences between ${COMPFILE} and installed version"
507 diff "${DIFF_FLAG}" "${COMPFILE#.}" "${COMPFILE}" | ${PAGER}
510 echo " *** There is no installed version of ${COMPFILE}"
513 echo " Use 'd' to delete the temporary ${COMPFILE}"
514 echo " Use 'i' to install the temporary ${COMPFILE}"
515 case "${NO_INSTALLED}" in
517 echo " Use 'm' to merge the old and new versions"
521 echo " Default is to leave the temporary file to deal with by hand"
523 read -p "How should I deal with this? [Leave it for later] " HANDLE_COMPFILE
524 case "${HANDLE_COMPFILE}" in
528 echo " *** Deleting ${COMPFILE}"
532 if mm_install "${COMPFILE}"; then
533 echo " *** ${COMPFILE} installed successfully"
535 echo " *** Problem installing ${COMPFILE}, it will remain to merge by hand"
539 case "${NO_INSTALLED}" in
544 echo " *** Type h at the sdiff prompt (%) to get usage help"
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}"
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"
560 read -p " *** How should I deal with the merged file? [Leave it for later] " INSTALL_MERGED
562 case "${INSTALL_MERGED}" in
564 mv "${COMPFILE}.merged" "${COMPFILE}"
566 if mm_install "${COMPFILE}"; then
567 echo " *** Merged version of ${COMPFILE} installed successfully"
569 echo " *** Problem installing ${COMPFILE}, it will remain to merge by hand later"
574 rm "${COMPFILE}.merged"
577 echo " *** ${COMPFILE} will remain for your consideration"
585 echo " *** There is no installed version of ${COMPFILE}"
586 echo " to merge so it will remain to install by hand later"
588 echo " *** Press [Enter] or [Return] key to continue"
592 esac # End of "No installed version of file but user selected merge" test
596 echo " *** ${COMPFILE} will remain for your consideration"
598 esac # End of "How to handle files that are different"
609 # If this is an auto run, make it official
610 echo " *** ${COMPFILE} will remain for your consideration"
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
618 echo "*** Comparison complete"
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
627 case "${AUTO_RUN}" in
630 read -p "Do you wish to delete what is left of ${TEMPROOT}? [no] " DEL_TEMPROOT
631 case "${DEL_TEMPROOT}" in
633 if rm -rf "${TEMPROOT}"; then
634 echo " *** ${TEMPROOT} has been deleted"
636 echo " *** Unable to delete ${TEMPROOT}"
640 echo " *** ${TEMPROOT} will remain"
647 case "${NEED_MAKEDEV}" in
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"
656 case "${NEED_NEWALIASES}" in
660 echo "*** You installed a new aliases file, so make sure that you run"
661 echo " 'newaliases' to rebuild your aliases database"
665 case "${NEED_CAP_MKDB}" in
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"
674 case "${NEED_PWD_MKDB}" in
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"