mergemaster.sh revision 153604
11541Srgrimes#!/bin/sh
21541Srgrimes
31541Srgrimes# mergemaster
41541Srgrimes
51541Srgrimes# Compare files created by /usr/src/etc/Makefile (or the directory
61541Srgrimes# the user specifies) with the currently installed copies.
71541Srgrimes
81541Srgrimes# Copyright 1998-2004 Douglas Barton
91541Srgrimes# DougB@FreeBSD.org
101541Srgrimes
111541Srgrimes# $FreeBSD: head/usr.sbin/mergemaster/mergemaster.sh 153604 2005-12-21 08:59:22Z dougb $
121541Srgrimes
131541SrgrimesPATH=/bin:/usr/bin:/usr/sbin
141541Srgrimes
151541Srgrimesdisplay_usage () {
161541Srgrimes  VERSION_NUMBER=`grep "[$]FreeBSD:" $0 | cut -d ' ' -f 4`
171541Srgrimes  echo "mergemaster version ${VERSION_NUMBER}"
181541Srgrimes  echo 'Usage: mergemaster [-scrvahipCP] [-m /path]'
191541Srgrimes  echo '         [-t /path] [-d] [-u N] [-w N] [-D /path]'
201541Srgrimes  echo "Options:"
211541Srgrimes  echo "  -s  Strict comparison (diff every pair of files)"
221541Srgrimes  echo "  -c  Use context diff instead of unified diff"
231541Srgrimes  echo "  -r  Re-run on a previously cleaned directory (skip temproot creation)"
241541Srgrimes  echo "  -v  Be more verbose about the process, include additional checks"
251541Srgrimes  echo "  -a  Leave all files that differ to merge by hand"
261541Srgrimes  echo "  -h  Display more complete help"
271541Srgrimes  echo '  -i  Automatically install files that do not exist in destination directory'
281541Srgrimes  echo '  -p  Pre-buildworld mode, only compares crucial files'
291541Srgrimes  echo '  -C  Compare local rc.conf variables to the defaults'
301541Srgrimes  echo '  -P  Preserve files that are overwritten'
311541Srgrimes  echo "  -m /path/directory  Specify location of source to do the make in"
321541Srgrimes  echo "  -t /path/directory  Specify temp root directory"
331541Srgrimes  echo "  -d  Add date and time to directory name (e.g., /var/tmp/temproot.`date +%m%d.%H.%M`)"
341541Srgrimes  echo "  -u N  Specify a numeric umask"
351541Srgrimes  echo "  -w N  Specify a screen width in columns to sdiff"
361541Srgrimes  echo '  -D /path/directory  Specify the destination directory to install files to'
3750477Speter  echo ''
381541Srgrimes}
391541Srgrimes
401541Srgrimesdisplay_help () {
411541Srgrimes  echo "* To specify a directory other than /var/tmp/temproot for the"
421541Srgrimes  echo "  temporary root environment, use -t /path/to/temp/root"
4334925Sdufault  echo "* The -w option takes a number as an argument for the column width"
4434925Sdufault  echo "  of the screen.  The default is 80."
451541Srgrimes  echo '* The -a option causes mergemaster to run without prompting.'
461541Srgrimes}
471541Srgrimes
481541Srgrimes# Loop allowing the user to use sdiff to merge files and display the merged
491541Srgrimes# file.
501541Srgrimesmerge_loop () {
511541Srgrimes  case "${VERBOSE}" in
521541Srgrimes  '') ;;
531541Srgrimes  *)
541541Srgrimes      echo "   *** Type h at the sdiff prompt (%) to get usage help"
551541Srgrimes      ;;
561541Srgrimes  esac
571541Srgrimes  echo ''
588876Srgrimes  MERGE_AGAIN=yes
591541Srgrimes  while [ "${MERGE_AGAIN}" = "yes" ]; do
601541Srgrimes    # Prime file.merged so we don't blat the owner/group id's
611541Srgrimes    cp -p "${COMPFILE}" "${COMPFILE}.merged"
621541Srgrimes    sdiff -o "${COMPFILE}.merged" --text --suppress-common-lines \
631541Srgrimes      --width=${SCREEN_WIDTH:-80} "${DESTDIR}${COMPFILE#.}" "${COMPFILE}"
641541Srgrimes    INSTALL_MERGED=V
651541Srgrimes    while [ "${INSTALL_MERGED}" = "v" -o "${INSTALL_MERGED}" = "V" ]; do
661541Srgrimes      echo ''
6711863Sphk      echo "  Use 'i' to install merged file"
6811865Sphk      echo "  Use 'r' to re-do the merge"
691541Srgrimes      echo "  Use 'v' to view the merged file"
701541Srgrimes      echo "  Default is to leave the temporary file to deal with by hand"
711541Srgrimes      echo ''
721541Srgrimes      echo -n "    *** How should I deal with the merged file? [Leave it for later] "
7311863Sphk      read INSTALL_MERGED
7411863Sphk
751541Srgrimes      case "${INSTALL_MERGED}" in
7611863Sphk      [iI])
7711863Sphk        mv "${COMPFILE}.merged" "${COMPFILE}"
7811863Sphk        echo ''
7911865Sphk        if mm_install "${COMPFILE}"; then
8012910Sphk          echo "     *** Merged version of ${COMPFILE} installed successfully"
8141728Struckman        else
8246155Sphk          echo "     *** Problem installing ${COMPFILE}, it will remain to merge by hand later"
8311863Sphk        fi
8419268Sjulian        unset MERGE_AGAIN
8519268Sjulian        ;;
8619268Sjulian      [rR])
8719268Sjulian        rm "${COMPFILE}.merged"
8834925Sdufault        ;;
8919268Sjulian      [vV])
9019268Sjulian        ${PAGER} "${COMPFILE}.merged"
9112582Sphk        ;;
9212582Sphk      '')
9311865Sphk        echo "   *** ${COMPFILE} will remain for your consideration"
9412131Sphk        unset MERGE_AGAIN
9512243Sphk        ;;
9611863Sphk      *)
9712243Sphk        echo "invalid choice: ${INSTALL_MERGED}"
9812243Sphk        INSTALL_MERGED=V
9912243Sphk        ;;
10012243Sphk      esac
10112243Sphk    done
10212243Sphk  done
10312429Sphk}
10412243Sphk
10538517Sdfr# Loop showing user differences between files, allow merge, skip or install
10638517Sdfr# options
10738517Sdfrdiff_loop () {
10812243Sphk
10938517Sdfr  HANDLE_COMPFILE=v
11038517Sdfr
11138517Sdfr  while [ "${HANDLE_COMPFILE}" = "v" -o "${HANDLE_COMPFILE}" = "V" -o \
11212243Sphk    "${HANDLE_COMPFILE}" = "NOT V" ]; do
11312243Sphk    if [ -f "${DESTDIR}${COMPFILE#.}" -a -f "${COMPFILE}" ]; then
11444078Sdfr      if [ "${HANDLE_COMPFILE}" = "v" -o "${HANDLE_COMPFILE}" = "V" ]; then
11544078Sdfr	echo ''
11612243Sphk	echo '   ======================================================================   '
11712243Sphk	echo ''
11812243Sphk        (
11912243Sphk          echo "  *** Displaying differences between ${COMPFILE} and installed version:"
12011863Sphk          echo ''
12144078Sdfr          diff ${DIFF_FLAG} ${DIFF_OPTIONS} "${DESTDIR}${COMPFILE#.}" "${COMPFILE}"
12244078Sdfr        ) | ${PAGER}
12311863Sphk        echo ''
12411863Sphk      fi
12511863Sphk    else
12611863Sphk      echo ''
12712623Sphk      echo "  *** There is no installed version of ${COMPFILE}"
12811865Sphk      echo ''
12912623Sphk      case "${AUTO_INSTALL}" in
13011863Sphk      [Yy][Ee][Ss])
13111863Sphk        echo ''
13212243Sphk        if mm_install "${COMPFILE}"; then
13312243Sphk          echo "   *** ${COMPFILE} installed successfully"
13412243Sphk          echo ''
13511865Sphk          # Make the list print one file per line
13638517Sdfr          AUTO_INSTALLED_FILES="${AUTO_INSTALLED_FILES}      ${DESTDIR}${COMPFILE#.}
13738517Sdfr"
13811865Sphk        else
13911865Sphk          echo "   *** Problem installing ${COMPFILE}, it will remain to merge by hand"
14011865Sphk        fi
14144078Sdfr        return
14244078Sdfr        ;;
14344078Sdfr      *)
14444078Sdfr        NO_INSTALLED=yes
14544078Sdfr        ;;
14644078Sdfr      esac
14744078Sdfr    fi
14844078Sdfr
14944078Sdfr    echo "  Use 'd' to delete the temporary ${COMPFILE}"
15044078Sdfr    echo "  Use 'i' to install the temporary ${COMPFILE}"
15138859Sbde    case "${NO_INSTALLED}" in
15212623Sphk    '')
15344078Sdfr      echo "  Use 'm' to merge the temporary and installed versions"
15444078Sdfr      echo "  Use 'v' to view the diff results again"
15544078Sdfr      ;;
15644078Sdfr    esac
15711863Sphk    echo ''
15838859Sbde    echo "  Default is to leave the temporary file to deal with by hand"
15944078Sdfr    echo ''
16044078Sdfr    echo -n "How should I deal with this? [Leave it for later] "
16144078Sdfr    read HANDLE_COMPFILE
16244078Sdfr
16344078Sdfr    case "${HANDLE_COMPFILE}" in
16411863Sphk    [dD])
16538859Sbde      rm "${COMPFILE}"
16612623Sphk      echo ''
16712623Sphk      echo "   *** Deleting ${COMPFILE}"
16829210Sbde      ;;
16911863Sphk    [iI])
17038859Sbde      echo ''
17112623Sphk      if mm_install "${COMPFILE}"; then
17212623Sphk        echo "   *** ${COMPFILE} installed successfully"
17329210Sbde      else
17411863Sphk        echo "   *** Problem installing ${COMPFILE}, it will remain to merge by hand"
17542095Sdfr      fi
17642095Sdfr      ;;
17738517Sdfr    [mM])
17842095Sdfr      case "${NO_INSTALLED}" in
17938517Sdfr      '')
18038859Sbde        # interact with user to merge files
18112705Sphk        merge_loop
18212623Sphk        ;;
18329210Sbde      *)
18411863Sphk        echo ''
18538859Sbde        echo "   *** There is no installed version of ${COMPFILE}"
18612623Sphk        echo ''
18712623Sphk        HANDLE_COMPFILE="NOT V"
18812623Sphk        ;;
18929210Sbde      esac # End of "No installed version of file but user selected merge" test
19011863Sphk      ;;
19138859Sbde    [vV])
19212623Sphk      continue
19312623Sphk      ;;
19429210Sbde    '')
19511865Sphk      echo ''
19611863Sphk      echo "   *** ${COMPFILE} will remain for your consideration"
1971541Srgrimes      ;;
1981541Srgrimes    *)
1991541Srgrimes      # invalid choice, show menu again.
2001541Srgrimes      echo "invalid choice: ${HANDLE_COMPFILE}"
2011541Srgrimes      echo ''
2021541Srgrimes      HANDLE_COMPFILE="NOT V"
20314498Shsu      continue
2041541Srgrimes      ;;
2051541Srgrimes    esac  # End of "How to handle files that are different"
2061541Srgrimes  done
2071541Srgrimes  unset NO_INSTALLED
2081541Srgrimes  echo ''
20934925Sdufault  case "${VERBOSE}" in
21034030Sdufault  '') ;;
21134030Sdufault  *)
2121541Srgrimes    sleep 3
2131541Srgrimes    ;;
2141541Srgrimes  esac
2151541Srgrimes}
21614498Shsu
2171541Srgrimespress_to_continue () {
2181541Srgrimes  local DISCARD
2191541Srgrimes  echo -n ' *** Press the [Enter] or [Return] key to continue '
2201541Srgrimes  read DISCARD
2211541Srgrimes}
22234925Sdufault
2231541Srgrimes# Set the default path for the temporary root environment
2241541Srgrimes#
2251541SrgrimesTEMPROOT='/var/tmp/temproot'
2261541Srgrimes
2271541Srgrimes# Read /etc/mergemaster.rc first so the one in $HOME can override
2281541Srgrimes#
2291541Srgrimesif [ -r /etc/mergemaster.rc ]; then
2301541Srgrimes  . /etc/mergemaster.rc
2311541Srgrimesfi
2321541Srgrimes
2331541Srgrimes# Read .mergemasterrc before command line so CLI can override
2341541Srgrimes#
2351541Srgrimesif [ -r "$HOME/.mergemasterrc" ]; then
2361541Srgrimes  . "$HOME/.mergemasterrc"
2371541Srgrimesfi
2381541Srgrimes
2391541Srgrimes# Check the command line options
2401541Srgrimes#
2411541Srgrimeswhile getopts ":ascrvhipCPm:t:du:w:D:" COMMAND_LINE_ARGUMENT ; do
2421541Srgrimes  case "${COMMAND_LINE_ARGUMENT}" in
2431541Srgrimes  s)
2441541Srgrimes    STRICT=yes
2451541Srgrimes    unset DIFF_OPTIONS
2461541Srgrimes    ;;
2471541Srgrimes  c)
2481541Srgrimes    DIFF_FLAG='-c'
24917281Swollman    ;;
2501952Swollman  r)
2512004Swollman    RERUN=yes
2522858Swollman    ;;
2533038Swollman  v)
2546577Sguido    case "${AUTO_RUN}" in
2556577Sguido    '') VERBOSE=yes ;;
2568481Swollman    esac
25723083Swollman    ;;
25823083Swollman  a)
25914235Speter    AUTO_RUN=yes
26014235Speter    unset VERBOSE
26137931Sjoerg    ;;
26237931Sjoerg  h)
2631541Srgrimes    display_usage
2641541Srgrimes    display_help
2651541Srgrimes    exit 0
2661541Srgrimes    ;;
2671541Srgrimes  i)
2681541Srgrimes    AUTO_INSTALL=yes
2691541Srgrimes    ;;
2701541Srgrimes  C)
2711541Srgrimes    COMP_CONFS=yes
2721541Srgrimes    ;;
2731541Srgrimes  P)
2741541Srgrimes    PRESERVE_FILES=yes
2751541Srgrimes    ;;
2761541Srgrimes  p)
2771541Srgrimes    PRE_WORLD=yes
2781541Srgrimes    unset COMP_CONFS
2791541Srgrimes    unset AUTO_RUN
2801541Srgrimes    ;;
2811541Srgrimes  m)
2821541Srgrimes    SOURCEDIR=${OPTARG}
2831541Srgrimes    ;;
2841541Srgrimes  t)
2851541Srgrimes    TEMPROOT=${OPTARG}
2861541Srgrimes    ;;
28717281Swollman  d)
2881952Swollman    TEMPROOT=${TEMPROOT}.`date +%m%d.%H.%M`
2892004Swollman    ;;
2902858Swollman  u)
2913038Swollman    NEW_UMASK=${OPTARG}
2926577Sguido    ;;
2936577Sguido  w)
2948481Swollman    SCREEN_WIDTH=${OPTARG}
29523083Swollman    ;;
29623083Swollman  D)
29714235Speter    DESTDIR=${OPTARG}
29814235Speter    ;;
29937931Sjoerg  *)
3001541Srgrimes    display_usage
3011541Srgrimes    exit 1
3022946Swollman    ;;
30314498Shsu  esac
3042946Swollmandone
30514498Shsu
3062946Swollman# Don't force the user to set this in the mergemaster rc file
3072946Swollmanif [ -n "${PRESERVE_FILES}" -a -z "${PRESERVE_FILES_DIR}" ]; then
3082946Swollman  PRESERVE_FILES_DIR=/var/tmp/mergemaster/preserved-files-`date +%y%m%d-%H%M%S`
3098876Srgrimesfi
3101541Srgrimes
3111541Srgrimesecho ''
3121541Srgrimes
3131541Srgrimes# If the user has a pager defined, make sure we can run it
3141541Srgrimes#
3151541Srgrimescase "${DONT_CHECK_PAGER}" in
3161541Srgrimes'')
3171541Srgrimes  while ! type "${PAGER%% *}" >/dev/null && [ -n "${PAGER}" ]; do
3181541Srgrimes    echo " *** Your PAGER environment variable specifies '${PAGER}', but"
3191541Srgrimes    echo "     due to the limited PATH that I use for security reasons,"
3208876Srgrimes    echo "     I cannot execute it.  So, what would you like to do?"
32123083Swollman    echo ''
32223083Swollman    echo "  Use 'e' to exit mergemaster and fix your PAGER variable"
32323083Swollman    if [ -x /usr/bin/less -o -x /usr/local/bin/less ]; then
32423083Swollman    echo "  Use 'l' to set PAGER to 'less' for this run"
32523083Swollman    fi
32623083Swollman    echo "  Use 'm' to use plain old 'more' as your PAGER for this run"
32723083Swollman    echo ''
32823083Swollman    echo "  Default is to use plain old 'more' "
32923083Swollman    echo ''
33023083Swollman    echo -n "What should I do? [Use 'more'] "
33135413Sdg    read FIXPAGER
33223083Swollman
33323083Swollman    case "${FIXPAGER}" in
3341541Srgrimes    [eE])
3351541Srgrimes       exit 0
3361541Srgrimes       ;;
3371541Srgrimes    [lL])
3381541Srgrimes       if [ -x /usr/bin/less ]; then
3391541Srgrimes         PAGER=/usr/bin/less
3401541Srgrimes       elif [ -x /usr/local/bin/less ]; then
3411541Srgrimes         PAGER=/usr/local/bin/less
3421541Srgrimes       else
3431541Srgrimes         echo ''
3441541Srgrimes         echo " *** Fatal Error:"
3452631Swollman         echo "     You asked to use 'less' as your pager, but I can't"
34628885Skato         echo "     find it in /usr/bin or /usr/local/bin"
34728885Skato         exit 1
3481541Srgrimes       fi
3491541Srgrimes       ;;
3501541Srgrimes    [mM]|'')
3511541Srgrimes       PAGER=more
3521541Srgrimes       ;;
3531541Srgrimes    *)
3541541Srgrimes       echo ''
3551541Srgrimes       echo "invalid choice: ${FIXPAGER}"
3561541Srgrimes    esac
3571541Srgrimes    echo ''
3581541Srgrimes  done
3591541Srgrimes  ;;
3602631Swollmanesac
3611541Srgrimes
3621541Srgrimes# If user has a pager defined, or got assigned one above, use it.
3631541Srgrimes# If not, use more.
3641541Srgrimes#
3651541SrgrimesPAGER=${PAGER:-more}
3661541Srgrimes
3671541Srgrimesif [ -n "${VERBOSE}" -a ! "${PAGER}" = "more" ]; then
3681541Srgrimes  echo " *** You have ${PAGER} defined as your pager so we will use that"
3691541Srgrimes  echo ''
3701541Srgrimes  sleep 3
3711541Srgrimesfi
3721541Srgrimes
3731541Srgrimes# Assign the diff flag once so we will not have to keep testing it
3741541Srgrimes#
3751541SrgrimesDIFF_FLAG=${DIFF_FLAG:--u}
3761541Srgrimes
3771541Srgrimes# Assign the source directory
3781541Srgrimes#
3791541SrgrimesSOURCEDIR=${SOURCEDIR:-/usr/src/etc}
3801541Srgrimes
3811541Srgrimes# Check the width of the user's terminal
3821541Srgrimes#
3831541Srgrimesif [ -t 0 ]; then
3841541Srgrimes  w=`tput columns`
3851541Srgrimes  case "${w}" in
3861541Srgrimes  0|'') ;; # No-op, since the input is not valid
3871541Srgrimes  *)
3881541Srgrimes    case "${SCREEN_WIDTH}" in
3891541Srgrimes    '') SCREEN_WIDTH="${w}" ;;
3901541Srgrimes    "${w}") ;; # No-op, since they are the same
3911541Srgrimes    *)
3921541Srgrimes      echo -n "*** You entered ${SCREEN_WIDTH} as your screen width, but stty "
3931541Srgrimes      echo "thinks it is ${w}."
3941541Srgrimes      echo ''
3951541Srgrimes      echo -n "What would you like to use? [${w}] "
3961541Srgrimes      read SCREEN_WIDTH
3971541Srgrimes      case "${SCREEN_WIDTH}" in
3981541Srgrimes      '') SCREEN_WIDTH="${w}" ;;
3991541Srgrimes      esac
4001541Srgrimes      ;;
4011541Srgrimes    esac
4021541Srgrimes  esac
4031541Srgrimesfi
4041541Srgrimes
4051541Srgrimes# Define what CVS $Id tag to look for to aid portability.
4061541Srgrimes#
4071541SrgrimesCVS_ID_TAG=FreeBSD
4081541Srgrimes
4091541Srgrimesdelete_temproot () {
4101541Srgrimes  rm -rf "${TEMPROOT}" 2>/dev/null
4111541Srgrimes  chflags -R 0 "${TEMPROOT}" 2>/dev/null
41234925Sdufault  rm -rf "${TEMPROOT}" || exit 1
41334925Sdufault}
41434925Sdufault
41534925Sdufaultcase "${RERUN}" in
41634925Sdufault'')
41734925Sdufault  # Set up the loop to test for the existence of the
41834925Sdufault  # temp root directory.
41934925Sdufault  #
42034925Sdufault  TEST_TEMP_ROOT=yes
42134925Sdufault  while [ "${TEST_TEMP_ROOT}" = "yes" ]; do
42234925Sdufault    if [ -d "${TEMPROOT}" ]; then
42334925Sdufault      echo "*** The directory specified for the temporary root environment,"
42434925Sdufault      echo "    ${TEMPROOT}, exists.  This can be a security risk if untrusted"
42534925Sdufault      echo "    users have access to the system."
42634925Sdufault      echo ''
42734925Sdufault      case "${AUTO_RUN}" in
42834925Sdufault      '')
42934925Sdufault        echo "  Use 'd' to delete the old ${TEMPROOT} and continue"
43034925Sdufault        echo "  Use 't' to select a new temporary root directory"
43134925Sdufault        echo "  Use 'e' to exit mergemaster"
43234925Sdufault        echo ''
43334925Sdufault        echo "  Default is to use ${TEMPROOT} as is"
43434925Sdufault        echo ''
43534925Sdufault        echo -n "How should I deal with this? [Use the existing ${TEMPROOT}] "
43634925Sdufault        read DELORNOT
43734925Sdufault
43834925Sdufault        case "${DELORNOT}" in
43934925Sdufault        [dD])
44034925Sdufault          echo ''
44134925Sdufault          echo "   *** Deleting the old ${TEMPROOT}"
44234925Sdufault          echo ''
44334925Sdufault          delete_temproot || exit 1
44434925Sdufault          unset TEST_TEMP_ROOT
44534925Sdufault          ;;
44634925Sdufault        [tT])
44734925Sdufault          echo "   *** Enter new directory name for temporary root environment"
44834925Sdufault          read TEMPROOT
44934925Sdufault          ;;
45034925Sdufault        [eE])
45134925Sdufault          exit 0
45234925Sdufault          ;;
45334925Sdufault        '')
45434925Sdufault          echo ''
45534925Sdufault          echo "   *** Leaving ${TEMPROOT} intact"
45634925Sdufault          echo ''
45734925Sdufault          unset TEST_TEMP_ROOT
45834925Sdufault          ;;
45934925Sdufault        *)
46034925Sdufault          echo ''
46134925Sdufault          echo "invalid choice: ${DELORNOT}"
46234925Sdufault          echo ''
46334925Sdufault          ;;
46434925Sdufault        esac
46534925Sdufault        ;;
46634925Sdufault      *)
46734925Sdufault        # If this is an auto-run, try a hopefully safe alternative then
46834925Sdufault        # re-test anyway.
46912289Sphk        TEMPROOT=/var/tmp/temproot.`date +%m%d.%H.%M.%S`
4701541Srgrimes        ;;
47144078Sdfr      esac
47244078Sdfr    else
47344078Sdfr      unset TEST_TEMP_ROOT
47444078Sdfr    fi
47544078Sdfr  done
47644078Sdfr
47744078Sdfr  echo "*** Creating the temporary root environment in ${TEMPROOT}"
47844078Sdfr
47944078Sdfr  if mkdir -p "${TEMPROOT}"; then
48044078Sdfr    echo " *** ${TEMPROOT} ready for use"
48144078Sdfr  fi
48244078Sdfr
48344078Sdfr  if [ ! -d "${TEMPROOT}" ]; then
48450465Smarcel    echo ''
48544078Sdfr    echo "  *** FATAL ERROR: Cannot create ${TEMPROOT}"
4867090Sbde    echo ''
4877090Sbde    exit 1
4887090Sbde  fi
4897090Sbde
49044331Sdt  echo " *** Creating and populating directory structure in ${TEMPROOT}"
49144331Sdt  echo ''
49244078Sdfr
49344078Sdfr  case "${VERBOSE}" in
49438859Sbde  '') ;;
49538859Sbde  *)
49638859Sbde    press_to_continue
49738859Sbde    ;;
49838859Sbde  esac
49938859Sbde
5003421Sphk  case "${PRE_WORLD}" in
5011541Srgrimes  '')
5021541Srgrimes    { cd ${SOURCEDIR} &&
5031541Srgrimes      case "${DESTDIR}" in
5041541Srgrimes      '') ;;
5051541Srgrimes      *)
50627342Speter      make DESTDIR=${DESTDIR} distrib-dirs
5071541Srgrimes        ;;
5081541Srgrimes      esac
5094468Sbde      make DESTDIR=${TEMPROOT} distrib-dirs &&
5101541Srgrimes      MAKEOBJDIRPREFIX=${TEMPROOT}/usr/obj make obj &&
511      MAKEOBJDIRPREFIX=${TEMPROOT}/usr/obj make all &&
512      MAKEOBJDIRPREFIX=${TEMPROOT}/usr/obj make DESTDIR=${TEMPROOT} \
513          distribution;} ||
514    { echo '';
515     echo "  *** FATAL ERROR: Cannot 'cd' to ${SOURCEDIR} and install files to";
516      echo "      the temproot environment";
517      echo '';
518      exit 1;}
519    ;;
520  *)
521    # Only set up files that are crucial to {build|install}world
522    { mkdir -p ${TEMPROOT}/etc &&
523      cp -p ${SOURCEDIR}/master.passwd ${TEMPROOT}/etc &&
524      cp -p ${SOURCEDIR}/group ${TEMPROOT}/etc;} ||
525    { echo '';
526      echo '  *** FATAL ERROR: Cannot copy files to the temproot environment';
527      echo '';
528      exit 1;}
529    ;;
530  esac
531
532  # Doing the inventory and removing files that we don't want to compare only
533  # makes sense if we are not doing a rerun, since we have no way of knowing
534  # what happened to the files during previous incarnations.
535  case "${VERBOSE}" in
536  '') ;;
537  *)
538    echo ''
539    echo ' *** The following files exist only in the installed version of'
540    echo "     ${DESTDIR}/etc.  In the vast majority of cases these files"
541    echo '     are necessary parts of the system and should not be deleted.'
542    echo '     However because these files are not updated by this process you'
543    echo '     might want to verify their status before rebooting your system.'
544    echo ''
545    press_to_continue
546    diff -qr ${DESTDIR}/etc ${TEMPROOT}/etc | grep "^Only in ${DESTDIR}/etc" | ${PAGER}
547    echo ''
548    press_to_continue
549    ;;
550  esac
551
552  # Avoid comparing the motd if the user specifies it in .mergemasterrc
553  case "${IGNORE_MOTD}" in
554  '') ;;
555  *) rm -f ${TEMPROOT}/etc/motd
556     ;;
557  esac
558
559  # Avoid trying to update MAKEDEV if /dev is on a devfs
560  if /sbin/sysctl vfs.devfs.generation > /dev/null 2>&1 ; then
561    rm -f ${TEMPROOT}/dev/MAKEDEV ${TEMPROOT}/dev/MAKEDEV.local
562  fi
563
564  ;; # End of the "RERUN" test
565esac
566
567# We really don't want to have to deal with files like login.conf.db, pwd.db,
568# or spwd.db.  Instead, we want to compare the text versions, and run *_mkdb.
569# Prompt the user to do so below, as needed.
570#
571rm -f ${TEMPROOT}/etc/*.db ${TEMPROOT}/etc/passwd
572
573# We only need to compare things like freebsd.cf once
574find ${TEMPROOT}/usr/obj -type f -delete 2>/dev/null
575
576# Get ready to start comparing files
577
578# Check umask if not specified on the command line,
579# and we are not doing an autorun
580#
581if [ -z "${NEW_UMASK}" -a -z "${AUTO_RUN}" ]; then
582  USER_UMASK=`umask`
583  case "${USER_UMASK}" in
584  0022|022) ;;
585  *)
586    echo ''
587    echo " *** Your umask is currently set to ${USER_UMASK}.  By default, this script"
588    echo "     installs all files with the same user, group and modes that"
589    echo "     they are created with by ${SOURCEDIR}/Makefile, compared to"
590    echo "     a umask of 022.  This umask allows world read permission when"
591    echo "     the file's default permissions have it."
592    echo ''
593    echo "     No world permissions can sometimes cause problems.  A umask of"
594    echo "     022 will restore the default behavior, but is not mandatory."
595    echo "     /etc/master.passwd is a special case.  Its file permissions"
596    echo "     will be 600 (rw-------) if installed."
597    echo ''
598    echo -n "What umask should I use? [${USER_UMASK}] "
599    read NEW_UMASK
600
601    NEW_UMASK="${NEW_UMASK:-$USER_UMASK}"
602    ;;
603  esac
604  echo ''
605fi
606
607CONFIRMED_UMASK=${NEW_UMASK:-0022}
608
609#
610# Warn users who still have old rc files
611#
612for file in atm devfs diskless1 diskless2 isdn network network6 pccard \
613  serial syscons sysctl alpha amd64 i386 ia64 sparc64; do
614  if [ -f "${DESTDIR}/etc/rc.${file}" ]; then
615    OLD_RC_PRESENT=1
616    break
617  fi
618done
619
620case "${OLD_RC_PRESENT}" in
6211)
622  echo ''
623  echo " *** There are elements of the old rc system in ${DESTDIR}/etc/."
624  echo ''
625  echo '     While these scripts will not hurt anything, they are not'
626  echo '     functional on an up to date system, and can be removed.'
627  echo ''
628
629  case "${AUTO_RUN}" in
630  '')
631    echo -n 'Move these files to /var/tmp/mergemaster/old_rc? [yes] '
632    read MOVE_OLD_RC
633
634    case "${MOVE_OLD_RC}" in
635    [nN]*) ;;
636    *)
637      mkdir -p /var/tmp/mergemaster/old_rc
638        for file in atm devfs diskless1 diskless2 isdn network network6 pccard \
639          serial syscons sysctl alpha amd64 i386 ia64 sparc64; do
640          if [ -f "${DESTDIR}/etc/rc.${file}" ]; then
641            mv ${DESTDIR}/etc/rc.${file} /var/tmp/mergemaster/old_rc/
642          fi
643        done
644      echo '  The files have been moved'
645      press_to_continue
646      ;;
647    esac
648    ;;
649  *) ;;
650  esac
651esac
652
653# Use the umask/mode information to install the files
654# Create directories as needed
655#
656do_install_and_rm () {
657  case "${PRESERVE_FILES}" in
658  [Yy][Ee][Ss])
659    if [ -f "${3}/${2##*/}" ]; then
660      mkdir -p ${PRESERVE_FILES_DIR}/${2%/*}
661      cp ${3}/${2##*/} ${PRESERVE_FILES_DIR}/${2%/*}
662    fi
663    ;;
664  esac
665
666  install -m "${1}" "${2}" "${3}" &&
667  rm -f "${2}"
668}
669
670# 4095 = "obase=10;ibase=8;07777" | bc
671find_mode () {
672  local OCTAL
673  OCTAL=$(( ~$(echo "obase=10; ibase=8; ${CONFIRMED_UMASK}" | bc) & 4095 &
674    $(echo "obase=10; ibase=8; $(stat -f "%OMp%OLp" ${1})" | bc) ))
675  printf "%04o\n" ${OCTAL}
676}
677
678mm_install () {
679  local INSTALL_DIR
680  INSTALL_DIR=${1#.}
681  INSTALL_DIR=${INSTALL_DIR%/*}
682
683  case "${INSTALL_DIR}" in
684  '')
685    INSTALL_DIR=/
686    ;;
687  esac
688
689  if [ -n "${DESTDIR}${INSTALL_DIR}" -a ! -d "${DESTDIR}${INSTALL_DIR}" ]; then
690    DIR_MODE=`find_mode "${TEMPROOT}/${INSTALL_DIR}"`
691    install -d -o root -g wheel -m "${DIR_MODE}" "${DESTDIR}${INSTALL_DIR}"
692  fi
693
694  FILE_MODE=`find_mode "${1}"`
695
696  if [ ! -x "${1}" ]; then
697    case "${1#.}" in
698    /etc/mail/aliases)
699      NEED_NEWALIASES=yes
700      ;;
701    /etc/login.conf)
702      NEED_CAP_MKDB=yes
703      ;;
704    /etc/master.passwd)
705      do_install_and_rm 600 "${1}" "${DESTDIR}${INSTALL_DIR}"
706      NEED_PWD_MKDB=yes
707      DONT_INSTALL=yes
708      ;;
709    /.cshrc | /.profile)
710    case "${AUTO_INSTALL}" in
711    '')
712      case "${LINK_EXPLAINED}" in
713      '')
714        echo "   *** Historically BSD derived systems have had a"
715        echo "       hard link from /.cshrc and /.profile to"
716        echo "       their namesakes in /root.  Please indicate"
717        echo "       your preference below for bringing your"
718        echo "       installed files up to date."
719        echo ''
720        LINK_EXPLAINED=yes
721        ;;
722      esac
723
724      echo "   Use 'd' to delete the temporary ${COMPFILE}"
725      echo "   Use 'l' to delete the existing ${DESTDIR}${COMPFILE#.} and create the link"
726      echo ''
727      echo "   Default is to leave the temporary file to deal with by hand"
728      echo ''
729      echo -n "  How should I handle ${COMPFILE}? [Leave it to install later] "
730      read HANDLE_LINK
731      ;;
732    *)  # Part of AUTO_INSTALL
733      HANDLE_LINK=l
734      ;;
735    esac
736
737      case "${HANDLE_LINK}" in
738      [dD]*)
739        rm "${COMPFILE}"
740        echo ''
741        echo "   *** Deleting ${COMPFILE}"
742        ;;
743      [lL]*)
744        echo ''
745        rm -f "${DESTDIR}${COMPFILE#.}"
746        if ln "${DESTDIR}/root/${COMPFILE##*/}" "${DESTDIR}${COMPFILE#.}"; then
747          echo "   *** Link from ${DESTDIR}${COMPFILE#.} to ${DESTDIR}/root/${COMPFILE##*/} installed successfully"
748          rm "${COMPFILE}"
749        else
750          echo "   *** Error linking ${DESTDIR}${COMPFILE#.} to ${DESTDIR}/root/${COMPFILE##*/}, ${COMPFILE} will remain to install by hand"
751        fi
752        ;;
753      *)
754        echo "   *** ${COMPFILE} will remain for your consideration"
755        ;;
756      esac
757      DONT_INSTALL=yes
758      ;;
759    esac
760
761    case "${DONT_INSTALL}" in
762    '')
763      do_install_and_rm "${FILE_MODE}" "${1}" "${DESTDIR}${INSTALL_DIR}"
764      ;;
765    *)
766      unset DONT_INSTALL
767      ;;
768    esac
769  else	# File matched -x
770    case "${1#.}" in
771    /dev/MAKEDEV)
772      NEED_MAKEDEV=yes
773      ;;
774    esac
775    do_install_and_rm "${FILE_MODE}" "${1}" "${DESTDIR}${INSTALL_DIR}"
776  fi
777  return $?
778}
779
780echo ''
781echo "*** Beginning comparison"
782echo ''
783
784# Pre-world does not populate /etc/rc.d.
785# It is very possible that a previous run would have deleted files in
786# ${TEMPROOT}/etc/rc.d, thus creating a lot of false positives.
787if [ -z "${PRE_WORLD}" -a -z "${RERUN}" ]; then
788  echo "   *** Checking ${DESTDIR}/etc/rc.d for stale files"
789  echo ''
790  cd "${DESTDIR}/etc/rc.d" &&
791  for file in *; do
792    if [ ! -e "${TEMPROOT}/etc/rc.d/${file}" ]; then
793      STALE_RC_FILES="${STALE_RC_FILES} ${file}"
794    fi
795  done
796  case "${STALE_RC_FILES}" in
797  ''|' *')
798    echo '   *** No stale files found'
799    ;;
800  *)
801    echo "   *** The following files exist in ${DESTDIR}/etc/rc.d but not in"
802    echo "       ${TEMPROOT}/etc/rc.d/:"
803    echo ''
804    echo "${STALE_RC_FILES}"
805    echo ''
806    echo '       The presence of stale files in this directory can cause the'
807    echo '       dreaded unpredictable results, and therefore it is highly'
808    echo '       recommended that you delete them.'
809    case "${AUTO_RUN}" in
810    '')
811      echo ''
812      echo -n '   *** Delete them now? [n] '
813      read DELETE_STALE_RC_FILES
814      case "${DELETE_STALE_RC_FILES}" in
815      [yY])
816        echo '      *** Deleting ... '
817        rm ${STALE_RC_FILES}
818        echo '                       done.'
819        ;;
820      *)
821        echo '      *** Files will not be deleted'
822        ;;
823      esac
824      sleep 2
825      ;;
826    esac
827    ;;
828  esac
829  echo ''
830fi
831
832cd "${TEMPROOT}"
833
834if [ -r "${MM_PRE_COMPARE_SCRIPT}" ]; then
835  . "${MM_PRE_COMPARE_SCRIPT}"
836fi
837
838# Using -size +0 avoids uselessly checking the empty log files created
839# by ${SOURCEDIR}/Makefile and the device entries in ./dev, but does
840# check the scripts in ./dev, as we'd like (assuming no devfs of course).
841#
842for COMPFILE in `find . -type f -size +0`; do
843
844  # First, check to see if the file exists in DESTDIR.  If not, the
845  # diff_loop function knows how to handle it.
846  #
847  if [ ! -e "${DESTDIR}${COMPFILE#.}" ]; then
848    case "${AUTO_RUN}" in
849      '')
850        diff_loop
851        ;;
852      *)
853        case "${AUTO_INSTALL}" in
854        '')
855          # If this is an auto run, make it official
856          echo "   *** ${COMPFILE} will remain for your consideration"
857          ;;
858        *)
859          diff_loop
860          ;;
861        esac
862        ;;
863    esac # Auto run test
864    continue
865  fi
866
867  case "${STRICT}" in
868  '' | [Nn][Oo])
869    # Compare CVS $Id's first so if the file hasn't been modified
870    # local changes will be ignored.
871    # If the files have the same $Id, delete the one in temproot so the
872    # user will have less to wade through if files are left to merge by hand.
873    #
874    CVSID1=`grep "[$]${CVS_ID_TAG}:" ${DESTDIR}${COMPFILE#.} 2>/dev/null`
875    CVSID2=`grep "[$]${CVS_ID_TAG}:" ${COMPFILE} 2>/dev/null` || CVSID2=none
876
877    case "${CVSID2}" in
878    "${CVSID1}")
879      echo " *** Temp ${COMPFILE} and installed have the same CVS Id, deleting"
880      rm "${COMPFILE}"
881      ;;
882    esac
883    ;;
884  esac
885
886  # If the file is still here either because the $Ids are different, the
887  # file doesn't have an $Id, or we're using STRICT mode; look at the diff.
888  #
889  if [ -f "${COMPFILE}" ]; then
890
891    # Do an absolute diff first to see if the files are actually different.
892    # If they're not different, delete the one in temproot.
893    #
894    if diff -q ${DIFF_OPTIONS} "${DESTDIR}${COMPFILE#.}" "${COMPFILE}" > \
895      /dev/null 2>&1; then
896      echo " *** Temp ${COMPFILE} and installed are the same, deleting"
897      rm "${COMPFILE}"
898    else
899      # Ok, the files are different, so show the user where they differ.
900      # Use user's choice of diff methods; and user's pager if they have one.
901      # Use more if not.
902      # Use unified diffs by default.  Context diffs give me a headache. :)
903      #
904      case "${AUTO_RUN}" in
905      '')
906        # prompt user to install/delete/merge changes
907        diff_loop
908        ;;
909      *)
910        # If this is an auto run, make it official
911        echo "   *** ${COMPFILE} will remain for your consideration"
912        ;;
913      esac # Auto run test
914    fi # Yes, the files are different
915  fi # Yes, the file still remains to be checked
916done # This is for the do way up there at the beginning of the comparison
917
918echo ''
919echo "*** Comparison complete"
920echo ''
921
922TEST_FOR_FILES=`find ${TEMPROOT} -type f -size +0 2>/dev/null`
923if [ -n "${TEST_FOR_FILES}" ]; then
924  echo "*** Files that remain for you to merge by hand:"
925  find "${TEMPROOT}" -type f -size +0
926  echo ''
927fi
928
929case "${AUTO_RUN}" in
930'')
931  echo -n "Do you wish to delete what is left of ${TEMPROOT}? [no] "
932  read DEL_TEMPROOT
933
934  case "${DEL_TEMPROOT}" in
935  [yY]*)
936    if delete_temproot; then
937      echo " *** ${TEMPROOT} has been deleted"
938    else
939      echo " *** Unable to delete ${TEMPROOT}"
940    fi
941    ;;
942  *)
943    echo " *** ${TEMPROOT} will remain"
944    ;;
945  esac
946  ;;
947*) ;;
948esac
949
950case "${AUTO_INSTALLED_FILES}" in
951'') ;;
952*)
953  case "${AUTO_RUN}" in
954  '')
955    (
956      echo ''
957      echo '*** You chose the automatic install option for files that did not'
958      echo '    exist on your system.  The following were installed for you:'
959      echo "${AUTO_INSTALLED_FILES}"
960    ) | ${PAGER}
961    ;;
962  *)
963    echo ''
964    echo '*** You chose the automatic install option for files that did not'
965    echo '    exist on your system.  The following were installed for you:'
966    echo "${AUTO_INSTALLED_FILES}"
967    ;;
968  esac
969  ;;
970esac
971
972run_it_now () {
973  case "${AUTO_RUN}" in
974  '')
975    unset YES_OR_NO
976    echo ''
977    echo -n '    Would you like to run it now? y or n [n] '
978    read YES_OR_NO
979
980    case "${YES_OR_NO}" in
981    y)
982      echo "    Running ${1}"
983      echo ''
984      eval "${1}"
985      ;;
986    ''|n)
987      echo ''
988      echo "       *** Cancelled"
989      echo ''
990      echo "    Make sure to run ${1} yourself"
991      ;;
992    *)
993      echo ''
994      echo "       *** Sorry, I do not understand your answer (${YES_OR_NO})"
995      echo ''
996      echo "    Make sure to run ${1} yourself"
997    esac
998    ;;
999  *) ;;
1000  esac
1001}
1002
1003case "${NEED_MAKEDEV}" in
1004'') ;;
1005*)
1006  echo ''
1007  echo "*** You installed a new ${DESTDIR}/dev/MAKEDEV script, so make sure that you run"
1008  echo "    'cd ${DESTDIR}/dev && /bin/sh MAKEDEV all' to rebuild your devices"
1009  run_it_now "cd ${DESTDIR}/dev && /bin/sh MAKEDEV all"
1010  ;;
1011esac
1012
1013case "${NEED_NEWALIASES}" in
1014'') ;;
1015*)
1016  echo ''
1017  if [ -n "${DESTDIR}" ]; then
1018    echo "*** You installed a new aliases file into ${DESTDIR}/etc/mail, but"
1019    echo "    the newaliases command is limited to the directories configured"
1020    echo "    in sendmail.cf.  Make sure to create your aliases database by"
1021    echo "    hand when your sendmail configuration is done."
1022  else
1023    echo "*** You installed a new aliases file, so make sure that you run"
1024    echo "    '/usr/bin/newaliases' to rebuild your aliases database"
1025    run_it_now '/usr/bin/newaliases'
1026  fi
1027  ;;
1028esac
1029
1030case "${NEED_CAP_MKDB}" in
1031'') ;;
1032*)
1033  echo ''
1034  echo "*** You installed a login.conf file, so make sure that you run"
1035  echo "    '/usr/bin/cap_mkdb ${DESTDIR}/etc/login.conf'"
1036  echo "     to rebuild your login.conf database"
1037  run_it_now "/usr/bin/cap_mkdb ${DESTDIR}/etc/login.conf"
1038  ;;
1039esac
1040
1041case "${NEED_PWD_MKDB}" in
1042'') ;;
1043*)
1044  echo ''
1045  echo "*** You installed a new master.passwd file, so make sure that you run"
1046  if [ -n "${DESTDIR}" ]; then
1047    echo "    '/usr/sbin/pwd_mkdb -d ${DESTDIR}/etc -p ${DESTDIR}/etc/master.passwd'"
1048    echo "    to rebuild your password files"
1049    run_it_now "/usr/sbin/pwd_mkdb -d ${DESTDIR}/etc -p ${DESTDIR}/etc/master.passwd"
1050  else
1051    echo "    '/usr/sbin/pwd_mkdb -p /etc/master.passwd'"
1052    echo "     to rebuild your password files"
1053    run_it_now '/usr/sbin/pwd_mkdb -p /etc/master.passwd'
1054  fi
1055  ;;
1056esac
1057
1058echo ''
1059
1060if [ -r "${MM_EXIT_SCRIPT}" ]; then
1061  . "${MM_EXIT_SCRIPT}"
1062fi
1063
1064case "${COMP_CONFS}" in
1065'') ;;
1066*)
1067  . ${DESTDIR}/etc/defaults/rc.conf
1068
1069  (echo ''
1070  echo "*** Comparing conf files: ${rc_conf_files}"
1071
1072  for CONF_FILE in ${rc_conf_files}; do
1073    if [ -r "${DESTDIR}${CONF_FILE}" ]; then
1074      echo ''
1075      echo "*** From ${DESTDIR}${CONF_FILE}"
1076      echo "*** From ${DESTDIR}/etc/defaults/rc.conf"
1077
1078      for RC_CONF_VAR in `grep -i ^[a-z] ${DESTDIR}${CONF_FILE} |
1079        cut -d '=' -f 1`; do
1080        echo ''
1081        grep -w ^${RC_CONF_VAR} ${DESTDIR}${CONF_FILE}
1082        grep -w ^${RC_CONF_VAR} ${DESTDIR}/etc/defaults/rc.conf ||
1083          echo ' * No default variable with this name'
1084      done
1085    fi
1086  done) | ${PAGER}
1087  echo ''
1088  ;;
1089esac
1090
1091case "${PRE_WORLD}" in
1092'') ;;
1093*)
1094  MAKE_CONF="${SOURCEDIR%etc}share/examples/etc/make.conf"
1095
1096  (echo ''
1097  echo '*** Comparing make variables'
1098  echo ''
1099  echo "*** From ${DESTDIR}/etc/make.conf"
1100  echo "*** From ${MAKE_CONF}"
1101
1102  for MAKE_VAR in `grep -i ^[a-z] ${DESTDIR}/etc/make.conf | cut -d '=' -f 1`; do
1103    echo ''
1104    grep -w ^${MAKE_VAR} ${DESTDIR}/etc/make.conf
1105    grep -w ^#${MAKE_VAR} ${MAKE_CONF} ||
1106      echo ' * No example variable with this name'
1107  done) | ${PAGER}
1108  ;;
1109esac
1110
1111exit 0
1112
1113