mergemaster.sh revision 68507
152400Sbillf#!/bin/sh
252400Sbillf
352400Sbillf# mergemaster
452400Sbillf
552400Sbillf# Compare files created by /usr/src/etc/Makefile (or the directory
652400Sbillf# the user specifies) with the currently installed copies.
752400Sbillf
867859Sdougb# Copyright 1998-2000 Douglas Barton
952400Sbillf# Doug@gorean.org
1052400Sbillf
1152495Sbillf# $FreeBSD: head/usr.sbin/mergemaster/mergemaster.sh 68507 2000-11-09 00:19:21Z dougb $
1252400Sbillf
1368507SdougbPATH=/bin:/usr/bin:/usr/sbin
1452400Sbillf
1552400Sbillfdisplay_usage () {
1652533Sbillf  VERSION_NUMBER=`grep "[$]FreeBSD:" $0 | cut -d ' ' -f 4`
1752400Sbillf  echo "mergemaster version ${VERSION_NUMBER}"
1867949Sdougb  echo 'Usage: mergemaster [-scrvahi] [-m /path]'
1967949Sdougb  echo '         [-t /path] [-d] [-u N] [-w N] [-D /path]'
2052400Sbillf  echo "Options:"
2152400Sbillf  echo "  -s  Strict comparison (diff every pair of files)"
2252400Sbillf  echo "  -c  Use context diff instead of unified diff"
2352400Sbillf  echo "  -r  Re-run on a previously cleaned directory (skip temproot creation)"
2452400Sbillf  echo "  -v  Be more verbose about the process, include additional checks"
2552400Sbillf  echo "  -a  Leave all files that differ to merge by hand"
2652400Sbillf  echo "  -h  Display more complete help"
2767949Sdougb  echo '  -i  Automatically install files that do not exist in destination directory'
2852400Sbillf  echo "  -m /path/directory  Specify location of source to do the make in"
2952400Sbillf  echo "  -t /path/directory  Specify temp root directory"
3052400Sbillf  echo "  -d  Add date and time to directory name (e.g., /var/tmp/temproot.`date +%m%d.%H.%M`)"
3152400Sbillf  echo "  -u N  Specify a numeric umask"
3252400Sbillf  echo "  -w N  Specify a screen width in columns to sdiff"
3367949Sdougb  echo '  -D /path/directory  Specify the destination directory to install files to'
3452400Sbillf  echo ''
3552400Sbillf}
3652400Sbillf
3752400Sbillfdisplay_help () {
3852400Sbillf  echo "* To specify a directory other than /var/tmp/temproot for the"
3952400Sbillf  echo "  temporary root environment, use -t /path/to/temp/root"
4052400Sbillf  echo "* The -w option takes a number as an argument for the column width"
4167859Sdougb  echo "  of the screen.  The default is 80."
4267949Sdougb  echo '* The -a option causes mergemaster to run without prompting.'
4352400Sbillf}
4452400Sbillf
4558910Salfred# Loop allowing the user to use sdiff to merge files and display the merged
4658910Salfred# file.
4758910Salfredmerge_loop () {
4867850Sdougb  case "${VERBOSE}" in
4967850Sdougb  '') ;;
5067850Sdougb  *)
5167850Sdougb      echo "   *** Type h at the sdiff prompt (%) to get usage help"
5267850Sdougb      ;;
5367850Sdougb  esac
5467850Sdougb  echo ''
5567850Sdougb  MERGE_AGAIN=yes
5667850Sdougb  while [ "${MERGE_AGAIN}" = "yes" ]; do
5767850Sdougb    # Prime file.merged so we don't blat the owner/group id's
5867850Sdougb    cp -p "${COMPFILE}" "${COMPFILE}.merged"
5967850Sdougb    sdiff -o "${COMPFILE}.merged" --text --suppress-common-lines \
6067949Sdougb      --width=${SCREEN_WIDTH:-80} "${DESTDIR}${COMPFILE#.}" "${COMPFILE}"
6167850Sdougb    INSTALL_MERGED=V
6267850Sdougb    while [ "${INSTALL_MERGED}" = "v" -o "${INSTALL_MERGED}" = "V" ]; do
6367850Sdougb      echo ''
6467850Sdougb      echo "  Use 'i' to install merged file"
6567850Sdougb      echo "  Use 'r' to re-do the merge"
6667850Sdougb      echo "  Use 'v' to view the merged file"
6767850Sdougb      echo "  Default is to leave the temporary file to deal with by hand"
6867850Sdougb      echo ''
6967859Sdougb      echo -n "    *** How should I deal with the merged file? [Leave it for later] "
7067859Sdougb      read INSTALL_MERGED
7158910Salfred
7267850Sdougb      case "${INSTALL_MERGED}" in
7367850Sdougb      [iI])
7467850Sdougb        mv "${COMPFILE}.merged" "${COMPFILE}"
7567850Sdougb        echo ''
7667850Sdougb        if mm_install "${COMPFILE}"; then
7767850Sdougb          echo "     *** Merged version of ${COMPFILE} installed successfully"
7867859Sdougb        else
7967850Sdougb          echo "     *** Problem installing ${COMPFILE}, it will remain to merge by hand later"
8067859Sdougb        fi
8167850Sdougb        unset MERGE_AGAIN
8267850Sdougb        ;;
8367850Sdougb      [rR])
8467850Sdougb        rm "${COMPFILE}.merged"
8567859Sdougb        ;;
8667850Sdougb      [vV])
8767850Sdougb        ${PAGER} "${COMPFILE}.merged"
8867850Sdougb        ;;
8967850Sdougb      '')
9067850Sdougb        echo "   *** ${COMPFILE} will remain for your consideration"
9167850Sdougb        unset MERGE_AGAIN
9267850Sdougb        ;;
9367850Sdougb      *)
9467850Sdougb        echo "invalid choice: ${INSTALL_MERGED}"
9567850Sdougb        INSTALL_MERGED=V
9667850Sdougb        ;;
9767850Sdougb      esac
9867850Sdougb    done
9967850Sdougb  done
10058910Salfred}
10158910Salfred
10258910Salfred# Loop showing user differences between files, allow merge, skip or install
10358910Salfred# options
10458910Salfreddiff_loop () {
10558910Salfred
10667850Sdougb  HANDLE_COMPFILE=v
10758910Salfred
10867850Sdougb  while [ "${HANDLE_COMPFILE}" = "v" -o "${HANDLE_COMPFILE}" = "V" -o "${HANDLE_COMPFILE}" = "NOT V" ]; do
10967949Sdougb    if [ -f "${DESTDIR}${COMPFILE#.}" -a -f "${COMPFILE}" ]; then
11067850Sdougb      if [ "${HANDLE_COMPFILE}" = "v" -o "${HANDLE_COMPFILE}" = "V" ]; then
11167850Sdougb        (
11267949Sdougb          echo ''
11367850Sdougb          echo "  *** Displaying differences between ${COMPFILE} and installed version:"
11467850Sdougb          echo ''
11567949Sdougb          diff "${DIFF_FLAG}" "${DESTDIR}${COMPFILE#.}" "${COMPFILE}"
11667850Sdougb        ) | ${PAGER}
11767850Sdougb        echo ''
11867850Sdougb      fi
11967850Sdougb    else
12067949Sdougb      echo ''
12167850Sdougb      echo "  *** There is no installed version of ${COMPFILE}"
12267949Sdougb      case "${AUTO_INSTALL}" in
12367949Sdougb      [Yy][Ee][Ss])
12467949Sdougb        echo ''
12567949Sdougb        if mm_install "${COMPFILE}"; then
12667949Sdougb          echo "   *** ${COMPFILE} installed successfully"
12768507Sdougb          echo ''
12867949Sdougb          # Make the list print one file per line
12967949Sdougb          AUTO_INSTALLED_FILES="${AUTO_INSTALLED_FILES}      ${DESTDIR}${COMPFILE#.}
13067949Sdougb"
13167949Sdougb        else
13267949Sdougb          echo "   *** Problem installing ${COMPFILE}, it will remain to merge by hand"
13367949Sdougb        fi
13467949Sdougb        return
13567949Sdougb        ;;
13667949Sdougb      *)
13767949Sdougb        NO_INSTALLED=yes
13867949Sdougb        ;;
13967949Sdougb      esac
14067850Sdougb    fi
14167859Sdougb
14267850Sdougb    echo "  Use 'd' to delete the temporary ${COMPFILE}"
14367850Sdougb    echo "  Use 'i' to install the temporary ${COMPFILE}"
14467850Sdougb    case "${NO_INSTALLED}" in
14567850Sdougb    '')
14667850Sdougb      echo "  Use 'm' to merge the old and new versions"
14767850Sdougb      echo "  Use 'v' to view to differences between the old and new versions again"
14867850Sdougb      ;;
14967850Sdougb    esac
15067850Sdougb    echo ''
15167850Sdougb    echo "  Default is to leave the temporary file to deal with by hand"
15267850Sdougb    echo ''
15367859Sdougb    echo -n "How should I deal with this? [Leave it for later] "
15467859Sdougb    read HANDLE_COMPFILE
15567859Sdougb
15667850Sdougb    case "${HANDLE_COMPFILE}" in
15767850Sdougb    [dD])
15867850Sdougb      rm "${COMPFILE}"
15967850Sdougb      echo ''
16067850Sdougb      echo "   *** Deleting ${COMPFILE}"
16167850Sdougb      ;;
16267850Sdougb    [iI])
16367850Sdougb      echo ''
16467850Sdougb      if mm_install "${COMPFILE}"; then
16567850Sdougb        echo "   *** ${COMPFILE} installed successfully"
16667850Sdougb      else
16767850Sdougb        echo "   *** Problem installing ${COMPFILE}, it will remain to merge by hand"
16867850Sdougb      fi
16967850Sdougb      ;;
17067850Sdougb    [mM])
17167850Sdougb      case "${NO_INSTALLED}" in
17267850Sdougb      '')
17367850Sdougb        # interact with user to merge files
17467850Sdougb        merge_loop
17567850Sdougb        ;;
17667850Sdougb      *)
17767850Sdougb        echo ''
17867850Sdougb        echo "   *** There is no installed version of ${COMPFILE}"
17967850Sdougb        echo ''
18067850Sdougb        HANDLE_COMPFILE="NOT V"
18167850Sdougb        ;;
18267850Sdougb      esac # End of "No installed version of file but user selected merge" test
18367850Sdougb      ;;
18467850Sdougb    [vV])
18567850Sdougb      continue
18667850Sdougb      ;;
18767850Sdougb    '')
18867850Sdougb      echo ''
18967850Sdougb      echo "   *** ${COMPFILE} will remain for your consideration"
19067850Sdougb      ;;
19167850Sdougb    *)
19267850Sdougb      # invalid choice, show menu again.
19367850Sdougb      echo "invalid choice: ${HANDLE_COMPFILE}"
19467850Sdougb      echo ''
19567850Sdougb      HANDLE_COMPFILE="NOT V"
19667850Sdougb      continue
19767850Sdougb      ;;
19867850Sdougb    esac  # End of "How to handle files that are different"
19967859Sdougb  done
20067850Sdougb  unset NO_INSTALLED
20167850Sdougb  echo ''
20267850Sdougb  case "${VERBOSE}" in
20367850Sdougb  '') ;;
20467850Sdougb  *)
20567850Sdougb    sleep 3
20667850Sdougb    ;;
20767850Sdougb  esac
20858910Salfred}
20958910Salfred
21052400Sbillf# Set the default path for the temporary root environment
21152400Sbillf#
21252400SbillfTEMPROOT='/var/tmp/temproot'
21352400Sbillf
21452400Sbillf# Read .mergemasterrc before command line so CLI can override
21552400Sbillf#
21667949Sdougbif [ -r "$HOME/.mergemasterrc" ]; then
21752400Sbillf  . "$HOME/.mergemasterrc"
21852400Sbillffi
21952400Sbillf
22052400Sbillf# Check the command line options
22152400Sbillf#
22267949Sdougbwhile getopts ":ascrvhim:t:du:w:D:" COMMAND_LINE_ARGUMENT ; do
22352400Sbillf  case "${COMMAND_LINE_ARGUMENT}" in
22452400Sbillf  s)
22552400Sbillf    STRICT=yes
22652400Sbillf    ;;
22752400Sbillf  c)
22852400Sbillf    DIFF_FLAG='-c'
22952400Sbillf    ;;
23052400Sbillf  r)
23152400Sbillf    RERUN=yes
23252400Sbillf    ;;
23352400Sbillf  v)
23452400Sbillf    case "${AUTO_RUN}" in
23552400Sbillf    '') VERBOSE=yes ;;
23652400Sbillf    esac
23752400Sbillf    ;;
23852400Sbillf  a)
23952400Sbillf    AUTO_RUN=yes
24052400Sbillf    unset VERBOSE
24152400Sbillf    ;;
24252400Sbillf  h)
24352400Sbillf    display_usage
24452400Sbillf    display_help
24552400Sbillf    exit 0
24652400Sbillf    ;;
24767949Sdougb  i)
24867949Sdougb    AUTO_INSTALL=yes
24967949Sdougb    ;;
25052400Sbillf  m)
25152400Sbillf    SOURCEDIR=${OPTARG}
25252400Sbillf    ;;
25352400Sbillf  t)
25452400Sbillf    TEMPROOT=${OPTARG}
25552400Sbillf    ;;
25652400Sbillf  d)
25752400Sbillf    TEMPROOT=${TEMPROOT}.`date +%m%d.%H.%M`
25852400Sbillf    ;;
25952400Sbillf  u)
26052400Sbillf    NEW_UMASK=${OPTARG}
26152400Sbillf    ;;
26252400Sbillf  w)
26352400Sbillf    SCREEN_WIDTH=${OPTARG}
26452400Sbillf    ;;
26567949Sdougb  D)
26667949Sdougb    DESTDIR=${OPTARG}
26767949Sdougb    ;;
26852400Sbillf  *)
26952400Sbillf    display_usage
27052400Sbillf    exit 1
27152400Sbillf    ;;
27252400Sbillf  esac
27352400Sbillfdone
27452400Sbillf
27552400Sbillfecho ''
27652400Sbillf
27752400Sbillf# If the user has a pager defined, make sure we can run it
27852400Sbillf#
27952400Sbillfcase "${DONT_CHECK_PAGER}" in
28052400Sbillf'')
28164467Sbrian  while ! type "${PAGER%% *}" >/dev/null && [ -n "${PAGER}" ]; do
28252400Sbillf    echo " *** Your PAGER environment variable specifies '${PAGER}', but"
28364467Sbrian    echo "     due to the limited PATH that I use for security reasons,"
28467859Sdougb    echo "     I cannot execute it.  So, what would you like to do?"
28552400Sbillf    echo ''
28652400Sbillf    echo "  Use 'e' to exit mergemaster and fix your PAGER variable"
28764467Sbrian    if [ -x /usr/bin/less -o -x /usr/local/bin/less ]; then
28864467Sbrian    echo "  Use 'l' to set PAGER to 'less' for this run"
28952400Sbillf    fi
29052400Sbillf    echo "  Use 'm' to use plain old 'more' as your PAGER for this run"
29152400Sbillf    echo ''
29252400Sbillf    echo "  Default is to use plain old 'more' "
29352400Sbillf    echo ''
29467859Sdougb    echo -n "What should I do? [Use 'more'] "
29567859Sdougb    read FIXPAGER
29667859Sdougb
29752400Sbillf    case "${FIXPAGER}" in
29858910Salfred    [eE])
29952400Sbillf       exit 0
30052400Sbillf       ;;
30158910Salfred    [lL])
30264467Sbrian       if [ -x /usr/bin/less ]; then
30364467Sbrian         PAGER=/usr/bin/less
30464467Sbrian       elif [ -x /usr/local/bin/less ]; then
30558910Salfred         PAGER=/usr/local/bin/less
30664467Sbrian       else
30764467Sbrian         echo ''
30864467Sbrian         echo " *** Fatal Error:"
30964467Sbrian         echo "     You asked to use 'less' as your pager, but I can't"
31064467Sbrian         echo "     find it in /usr/bin or /usr/local/bin"
31164467Sbrian         exit 1
31258910Salfred       fi
31352400Sbillf       ;;
31460420Sbsd    [mM]|'')
31552400Sbillf       PAGER=more
31652400Sbillf       ;;
31758910Salfred    *)
31858910Salfred       echo ''
31958910Salfred       echo "invalid choice: ${FIXPAGER}"
32052400Sbillf    esac
32152400Sbillf    echo ''
32258910Salfred  done
32352400Sbillf  ;;
32452400Sbillfesac
32552400Sbillf
32652400Sbillf# If user has a pager defined, or got assigned one above, use it.
32752400Sbillf# If not, use more.
32852400Sbillf#
32952400SbillfPAGER=${PAGER:-more}
33052400Sbillf
33152400Sbillfif [ -n "${VERBOSE}" -a ! "${PAGER}" = "more" ]; then
33252400Sbillf  echo " *** You have ${PAGER} defined as your pager so we will use that"
33352400Sbillf  echo ''
33452400Sbillf  sleep 3
33552400Sbillffi
33652400Sbillf
33752400Sbillf# Assign the diff flag once so we will not have to keep testing it
33852400Sbillf#
33952400SbillfDIFF_FLAG=${DIFF_FLAG:--u}
34052400Sbillf
34152400Sbillf# Assign the source directory
34252400Sbillf#
34352400SbillfSOURCEDIR=${SOURCEDIR:-/usr/src/etc}
34452400Sbillf
34552400Sbillfcase "${RERUN}" in
34652400Sbillf'')
34752400Sbillf  # Set up the loop to test for the existence of the
34852400Sbillf  # temp root directory.
34952400Sbillf  #
35052400Sbillf  TEST_TEMP_ROOT=yes
35152400Sbillf  while [ "${TEST_TEMP_ROOT}" = "yes" ]; do
35252400Sbillf    if [ -d "${TEMPROOT}" ]; then
35352400Sbillf      echo "*** The directory specified for the temporary root environment,"
35467859Sdougb      echo "    ${TEMPROOT}, exists.  This can be a security risk if untrusted"
35552400Sbillf      echo "    users have access to the system."
35652400Sbillf      echo ''
35752400Sbillf      case "${AUTO_RUN}" in
35852400Sbillf      '')
35952400Sbillf        echo "  Use 'd' to delete the old ${TEMPROOT} and continue"
36052400Sbillf        echo "  Use 't' to select a new temporary root directory"
36152400Sbillf        echo "  Use 'e' to exit mergemaster"
36252400Sbillf        echo ''
36352400Sbillf        echo "  Default is to use ${TEMPROOT} as is"
36452400Sbillf        echo ''
36567859Sdougb        echo -n "How should I deal with this? [Use the existing ${TEMPROOT}] "
36667859Sdougb        read DELORNOT
36767859Sdougb
36867859Sdougb        case "${DELORNOT}" in
36967859Sdougb        [dD])
37067859Sdougb          echo ''
37167859Sdougb          echo "   *** Deleting the old ${TEMPROOT}"
37267859Sdougb          echo ''
37367859Sdougb          rm -rf "${TEMPROOT}"
37467859Sdougb          unset TEST_TEMP_ROOT
37552400Sbillf          ;;
37667859Sdougb        [tT])
37767859Sdougb          echo "   *** Enter new directory name for temporary root environment"
37867859Sdougb          read TEMPROOT
37967859Sdougb          ;;
38067859Sdougb        [eE])
38167859Sdougb          exit 0
38267859Sdougb          ;;
38367859Sdougb        '')
38467859Sdougb          echo ''
38567859Sdougb          echo "   *** Leaving ${TEMPROOT} intact"
38667859Sdougb          echo ''
38767859Sdougb          unset TEST_TEMP_ROOT
38867859Sdougb          ;;
38967859Sdougb        *)
39067859Sdougb          echo ''
39167859Sdougb          echo "invalid choice: ${DELORNOT}"
39267859Sdougb          echo ''
39367859Sdougb          ;;
39467859Sdougb        esac
39567859Sdougb        ;;
39652400Sbillf      *)
39752400Sbillf        # If this is an auto-run, try a hopefully safe alternative then re-test anyway
39852400Sbillf        TEMPROOT=/var/tmp/temproot.`date +%m%d.%H.%M.%S`
39952400Sbillf        ;;
40052400Sbillf      esac
40152400Sbillf    else
40252400Sbillf      unset TEST_TEMP_ROOT
40352400Sbillf    fi
40452400Sbillf  done
40552400Sbillf
40652400Sbillf  echo "*** Creating the temporary root environment in ${TEMPROOT}"
40752400Sbillf
40852400Sbillf  if mkdir -p "${TEMPROOT}"; then
40952400Sbillf    echo " *** ${TEMPROOT} ready for use"
41052400Sbillf  fi
41152400Sbillf
41252400Sbillf  if [ ! -d "${TEMPROOT}" ]; then
41352400Sbillf    echo ''
41452400Sbillf    echo "  *** FATAL ERROR: Cannot create ${TEMPROOT}"
41552400Sbillf    echo ''
41652400Sbillf    exit 1
41752400Sbillf  fi
41852400Sbillf
41952400Sbillf  echo " *** Creating and populating directory structure in ${TEMPROOT}"
42052400Sbillf  echo ''
42152400Sbillf
42252400Sbillf  case "${VERBOSE}" in
42352400Sbillf  '') ;;
42452400Sbillf  *)
42552400Sbillf    echo " *** Press [Enter] or [Return] key to continue"
42652400Sbillf    read ANY_KEY
42752400Sbillf    unset ANY_KEY
42852400Sbillf    ;;
42952400Sbillf  esac
43052400Sbillf
43152400Sbillf  { cd ${SOURCEDIR} &&
43268507Sdougb    case "${DESTDIR}" in
43368507Sdougb    '') ;;
43468507Sdougb    *)
43568507Sdougb      make DESTDIR=${DESTDIR} distrib-dirs
43668507Sdougb      ;;
43768507Sdougb    esac
43852400Sbillf    make DESTDIR=${TEMPROOT} distrib-dirs &&
43957173Sbillf    make DESTDIR=${TEMPROOT} -DNO_MAKEDEV distribution;} ||
44052400Sbillf  { echo '';
44152400Sbillf    echo "  *** FATAL ERROR: Cannot 'cd' to ${SOURCEDIR} and install files to the";
44252400Sbillf    echo "      temproot environment";
44352400Sbillf    echo '';
44452400Sbillf    exit 1;}
44552400Sbillf
44667949Sdougb  # Doing the inventory and removing files that we don't want to compare only makes
44767949Sdougb  # sense if we are not doing a rerun, since we have no way of knowing what happened
44867949Sdougb  # to the files during previous incarnations.
44967949Sdougb  case "${VERBOSE}" in
45067949Sdougb  '') ;;
45167949Sdougb  *)
45267949Sdougb    echo ''
45367949Sdougb    echo ' *** The following files exist only in the installed version of'
45467949Sdougb    echo "     ${DESTDIR}/etc.  In the vast majority of cases these files"
45567949Sdougb    echo '     are necessary parts of the system and should not be deleted.'
45667949Sdougb    echo '     However because these files are not updated by this process you'
45767949Sdougb    echo '     might want to verify their status before rebooting your system.'
45867949Sdougb    echo ''
45967949Sdougb    echo ' *** Press [Enter] or [Return] key to continue'
46067949Sdougb    read ANY_KEY
46167949Sdougb    unset ANY_KEY
46267949Sdougb    diff -qr ${DESTDIR}/etc ${TEMPROOT}/etc | grep "^Only in /etc" | ${PAGER}
46367949Sdougb    echo ''
46467949Sdougb    echo ' *** Press [Enter] or [Return] key to continue'
46567949Sdougb    read ANY_KEY
46667949Sdougb    unset ANY_KEY
46767949Sdougb    ;;
46867949Sdougb  esac
46967949Sdougb
47052400Sbillf  # We really don't want to have to deal with these files, since
47152400Sbillf  # master.passwd is the real file that should be compared, then
47252400Sbillf  # the user should run pwd_mkdb if necessary.
47352400Sbillf  #
47452400Sbillf  rm ${TEMPROOT}/etc/spwd.db ${TEMPROOT}/etc/passwd ${TEMPROOT}/etc/pwd.db
47552400Sbillf
47652534Sbillf  # Avoid comparing the motd if the user specifies it in .mergemasterrc
47752534Sbillf  case "${IGNORE_MOTD}" in
47852534Sbillf  '') ;;
47952534Sbillf  *) rm ${TEMPROOT}/etc/motd
48052534Sbillf     ;;
48152534Sbillf  esac
48252534Sbillf
48365115Sben  # Avoid trying to update MAKEDEV if /dev is on a devfs
48468507Sdougb  if /sbin/sysctl vfs.devfs.generation > /dev/null 2>&1 ; then
48565115Sben    rm ${TEMPROOT}/dev/MAKEDEV ${TEMPROOT}/dev/MAKEDEV.local
48665115Sben  fi
48765115Sben
48852400Sbillf  ;; # End of the "RERUN" test
48952400Sbillfesac
49052400Sbillf
49152400Sbillf# Get ready to start comparing files
49252400Sbillf
49352400Sbillf# Check umask if not specified on the command line,
49452400Sbillf# and we are not doing an autorun
49552400Sbillf#
49652400Sbillfif [ -z "${NEW_UMASK}" -a -z "${AUTO_RUN}" ]; then
49752400Sbillf  USER_UMASK=`umask`
49852400Sbillf  case "${USER_UMASK}" in
49952400Sbillf  0022) ;;
50052400Sbillf  *)
50152400Sbillf    echo ''
50267859Sdougb    echo " *** Your umask is currently set to ${USER_UMASK}.  By default, this script"
50352400Sbillf    echo "     installs all files with the same user, group and modes that"
50452400Sbillf    echo "     they are created with by ${SOURCEDIR}/Makefile, compared to"
50567859Sdougb    echo "     a umask of 022.  This umask allows world read permission when"
50652400Sbillf    echo "     the file's default permissions have it."
50767859Sdougb    echo "     No world permissions can sometimes cause problems.  A umask of"
50852400Sbillf    echo "     022 will restore the default behavior, but is not mandatory."
50967859Sdougb    echo "     /etc/master.passwd is a special case.  Its file permissions"
51052400Sbillf    echo "     will be 600 (rw-------) if installed."
51152400Sbillf    echo ''
51267859Sdougb    echo -n "What umask should I use? [${USER_UMASK}] "
51367859Sdougb    read NEW_UMASK
51467859Sdougb
51552400Sbillf    NEW_UMASK="${NEW_UMASK:-$USER_UMASK}"
51652400Sbillf    ;;
51752400Sbillf  esac
51852400Sbillf  echo ''
51952400Sbillffi
52052400Sbillf
52152400SbillfCONFIRMED_UMASK=${NEW_UMASK:-0022}
52252400Sbillf
52367949Sdougb# Warn users who still have ${DESTDIR}/etc/sysconfig
52452400Sbillf#
52567949Sdougbif [ -e "${DESTDIR}/etc/sysconfig" ]; then
52652400Sbillf  echo ''
52767949Sdougb  echo " *** There is a sysconfig file on this system in ${DESTDIR}/etc/."
52867949Sdougb  echo ''
52967949Sdougb  echo '     Starting with FreeBSD version 2.2.2 those settings moved from'
53067949Sdougb  echo '     /etc/sysconfig to /etc/rc.conf.  If you are upgrading an older'
53167949Sdougb  echo '     system make sure that you transfer your settings by hand from'
53267949Sdougb  echo '     sysconfig to rc.conf and install the rc.conf file.  If you'
53367949Sdougb  echo '     have already made this transition, you should consider'
53467949Sdougb  echo '     renaming or deleting the sysconfig file.'
53567949Sdougb  echo ''
53652400Sbillf  case "${AUTO_RUN}" in
53752400Sbillf  '')
53867859Sdougb    echo -n "Continue with the merge process? [yes] "
53967859Sdougb    read CONT_OR_NOT
54067859Sdougb
54152400Sbillf    case "${CONT_OR_NOT}" in
54252400Sbillf    [nN]*)
54352400Sbillf      exit 0
54452400Sbillf      ;;
54552400Sbillf    *)
54652400Sbillf      echo "   *** Continuing"
54752400Sbillf      echo ''
54852400Sbillf      ;;
54952400Sbillf    esac
55052400Sbillf    ;;
55152400Sbillf  *) ;;
55252400Sbillf  esac
55352400Sbillffi
55452400Sbillf
55552400Sbillf# Use the umask/mode information to install the files
55652400Sbillf# Create directories as needed
55752400Sbillf#
55852400Sbillfmm_install () {
55952400Sbillf  local INSTALL_DIR
56052400Sbillf  INSTALL_DIR=${1#.}
56152400Sbillf  INSTALL_DIR=${INSTALL_DIR%/*}
56267949Sdougb
56352400Sbillf  case "${INSTALL_DIR}" in
56452400Sbillf  '')
56552400Sbillf    INSTALL_DIR=/
56652400Sbillf    ;;
56752400Sbillf  esac
56852400Sbillf
56967949Sdougb  if [ -n "${DESTDIR}${INSTALL_DIR}" -a ! -d "${DESTDIR}${INSTALL_DIR}" ]; then
57052400Sbillf    DIR_MODE=`perl -e 'printf "%04o\n", (((stat("$ARGV[0]"))[2] & 07777) &~ oct("$ARGV[1]"))' "${TEMPROOT}/${INSTALL_DIR}" "${CONFIRMED_UMASK}"`
57167949Sdougb    install -d -o root -g wheel -m "${DIR_MODE}" "${DESTDIR}${INSTALL_DIR}"
57252400Sbillf  fi
57352400Sbillf
57452400Sbillf  FILE_MODE=`perl -e 'printf "%04o\n", (((stat("$ARGV[0]"))[2] & 07777) &~ oct("$ARGV[1]"))' "${1}" "${CONFIRMED_UMASK}"`
57552400Sbillf
57652400Sbillf  if [ ! -x "${1}" ]; then
57752400Sbillf    case "${1#.}" in
57864625Sgshapiro    /etc/mail/aliases)
57952400Sbillf      NEED_NEWALIASES=yes
58052400Sbillf      ;;
58152400Sbillf    /etc/login.conf)
58252400Sbillf      NEED_CAP_MKDB=yes
58352400Sbillf      ;;
58452400Sbillf    /etc/master.passwd)
58567949Sdougb      install -m 600 "${1}" "${DESTDIR}${INSTALL_DIR}"
58652400Sbillf      NEED_PWD_MKDB=yes
58752400Sbillf      DONT_INSTALL=yes
58852400Sbillf      ;;
58952400Sbillf    /.cshrc | /.profile)
59052400Sbillf      case "${LINK_EXPLAINED}" in
59152400Sbillf      '')
59267859Sdougb        echo "   *** Historically BSD derived systems have had a"
59367859Sdougb        echo "       hard link from /.cshrc and /.profile to"
59467859Sdougb        echo "       their namesakes in /root.  Please indicate"
59567859Sdougb        echo "       your preference below for bringing your"
59667859Sdougb        echo "       installed files up to date."
59752400Sbillf        echo ''
59852400Sbillf        LINK_EXPLAINED=yes
59952400Sbillf        ;;
60052400Sbillf      esac
60152400Sbillf
60252400Sbillf      echo "   Use 'd' to delete the temporary ${COMPFILE}"
60367949Sdougb      echo "   Use 'l' to delete the existing ${DESTDIR}${COMPFILE#.} and create the link"
60452400Sbillf      echo ''
60552400Sbillf      echo "   Default is to leave the temporary file to deal with by hand"
60652400Sbillf      echo ''
60767859Sdougb      echo -n "  How should I handle ${COMPFILE}? [Leave it to install later] "
60867859Sdougb      read HANDLE_LINK
60967859Sdougb
61052400Sbillf      case "${HANDLE_LINK}" in
61152400Sbillf      [dD]*)
61252400Sbillf        rm "${COMPFILE}"
61352400Sbillf        echo ''
61452400Sbillf        echo "   *** Deleting ${COMPFILE}"
61552400Sbillf        ;;
61652400Sbillf      [lL]*)
61752400Sbillf        echo ''
61867949Sdougb        rm -f "${DESTDIR}${COMPFILE#.}"
61967949Sdougb        if ln "${DESTDIR}/root/${COMPFILE##*/}" "${DESTDIR}${COMPFILE#.}"; then
62067949Sdougb          echo "   *** Link from ${DESTDIR}${COMPFILE#.} to ${DESTDIR}/root/${COMPFILE##*/} installed successfully"
62152400Sbillf          rm "${COMPFILE}"
62252400Sbillf        else
62367949Sdougb          echo "   *** Error linking ${DESTDIR}${COMPFILE#.} to ${DESTDIR}/root/${COMPFILE##*/}, ${COMPFILE} will remain to install by hand"
62452400Sbillf        fi
62552400Sbillf        ;;
62652400Sbillf      *)
62752400Sbillf        echo "   *** ${COMPFILE} will remain for your consideration"
62852400Sbillf        ;;
62952400Sbillf      esac
63052400Sbillf      DONT_INSTALL=yes
63152400Sbillf      ;;
63252400Sbillf    esac
63352400Sbillf
63452400Sbillf    case "${DONT_INSTALL}" in
63552400Sbillf    '')
63667949Sdougb      install -m "${FILE_MODE}" "${1}" "${DESTDIR}${INSTALL_DIR}"
63752400Sbillf      ;;
63852400Sbillf    *)
63952400Sbillf      unset DONT_INSTALL
64052400Sbillf      ;;
64152400Sbillf    esac
64252400Sbillf  else
64367949Sdougb    case "${1#.}" in
64467949Sdougb    /dev/MAKEDEV)
64567949Sdougb      NEED_MAKEDEV=yes
64667949Sdougb      ;;
64767949Sdougb    esac
64867949Sdougb    install -m "${FILE_MODE}" "${1}" "${DESTDIR}${INSTALL_DIR}"
64952400Sbillf  fi
65052400Sbillf  return $?
65152400Sbillf}
65252400Sbillf
65367949Sdougbecho ''
65467949Sdougbecho "*** Beginning comparison"
65567949Sdougbecho ''
65652400Sbillf
65767949Sdougbcd "${TEMPROOT}"
65867949Sdougb
65967949Sdougbif [ -r "${MM_PRE_COMPARE_SCRIPT}" ]; then
66067949Sdougb  . "${MM_PRE_COMPARE_SCRIPT}"
66167949Sdougbfi
66267949Sdougb
66352400Sbillf# Using -size +0 avoids uselessly checking the empty log files created
66452400Sbillf# by ${SOURCEDIR}/Makefile and the device entries in ./dev, but does
66567949Sdougb# check the scripts in ./dev, as we'd like (assuming no devfs of course).
66652400Sbillf#
66752400Sbillffor COMPFILE in `find . -type f -size +0`; do
66867949Sdougb
66967949Sdougb  # First, check to see if the file exists in DESTDIR.  If not, the
67067949Sdougb  # diff_loop function knows how to handle it.
67167949Sdougb  #
67267949Sdougb  if [ ! -e "${DESTDIR}${COMPFILE#.}" ]; then
67367949Sdougb    diff_loop
67467949Sdougb    continue
67567949Sdougb  fi
67667949Sdougb
67752400Sbillf  case "${STRICT}" in
67852400Sbillf  '' | [Nn][Oo])
67952400Sbillf    # Compare CVS $Id's first so if the file hasn't been modified
68052400Sbillf    # local changes will be ignored.
68152400Sbillf    # If the files have the same $Id, delete the one in temproot so the
68252400Sbillf    # user will have less to wade through if files are left to merge by hand.
68352400Sbillf    #
68467949Sdougb    # Reduce complexity and improve portability by using ident
68567949Sdougb    #
68667949Sdougb    CVSID1=`ident ${DESTDIR}${COMPFILE#.} 2>&1`
68767949Sdougb    CVSID1="${CVSID1#${DESTDIR}}"
68867949Sdougb    CVSID2=`ident ${COMPFILE} 2>&1`
68952400Sbillf
69067949Sdougb    case "${CVSID2}" in
69167949Sdougb    *'no id keywords'*)
69267949Sdougb      ;;
69367949Sdougb    ."${CVSID1}")
69467949Sdougb      echo " *** Temp ${COMPFILE} and installed have the same CVS Id, deleting"
69567949Sdougb      rm "${COMPFILE}"
69667949Sdougb      ;;
69767949Sdougb    esac
69852400Sbillf    ;;
69952400Sbillf  esac
70052400Sbillf
70152400Sbillf  # If the file is still here either because the $Ids are different, the
70252400Sbillf  # file doesn't have an $Id, or we're using STRICT mode; look at the diff.
70352400Sbillf  #
70452400Sbillf  if [ -f "${COMPFILE}" ]; then
70552400Sbillf
70652400Sbillf    # Do an absolute diff first to see if the files are actually different.
70752400Sbillf    # If they're not different, delete the one in temproot.
70852400Sbillf    #
70967949Sdougb    if diff -q "${DESTDIR}${COMPFILE#.}" "${COMPFILE}" > /dev/null 2>&1; then
71052400Sbillf      echo " *** Temp ${COMPFILE} and installed are the same, deleting"
71152400Sbillf      rm "${COMPFILE}"
71252400Sbillf    else
71367859Sdougb      # Ok, the files are different, so show the user where they differ.  Use user's
71467859Sdougb      # choice of diff methods; and user's pager if they have one.  Use more if not.
71567859Sdougb      # Use unified diffs by default.  Context diffs give me a headache. :)
71652400Sbillf      #
71752400Sbillf      case "${AUTO_RUN}" in
71852400Sbillf      '')
71958910Salfred        # prompt user to install/delete/merge changes
72058910Salfred        diff_loop
72152400Sbillf        ;;
72252400Sbillf      *)
72352400Sbillf        # If this is an auto run, make it official
72452400Sbillf        echo "   *** ${COMPFILE} will remain for your consideration"
72552400Sbillf        ;;
72652400Sbillf      esac # Auto run test
72752400Sbillf    fi # Yes, the files are different
72852400Sbillf  fi # Yes, the file still remains to be checked
72952400Sbillfdone # This is for the do way up there at the beginning of the comparison
73052400Sbillf
73152534Sbillfecho ''
73252400Sbillfecho "*** Comparison complete"
73352400Sbillfecho ''
73452400Sbillf
73552400SbillfTEST_FOR_FILES=`find ${TEMPROOT} -type f -size +0 2>/dev/null`
73652400Sbillfif [ -n "${TEST_FOR_FILES}" ]; then
73752400Sbillf  echo "*** Files that remain for you to merge by hand:"
73852400Sbillf  find "${TEMPROOT}" -type f -size +0
73952400Sbillffi
74052400Sbillf
74152400Sbillfcase "${AUTO_RUN}" in
74252400Sbillf'')
74352400Sbillf  echo ''
74467859Sdougb  echo -n "Do you wish to delete what is left of ${TEMPROOT}? [no] "
74567859Sdougb  read DEL_TEMPROOT
74667859Sdougb
74752400Sbillf  case "${DEL_TEMPROOT}" in
74852400Sbillf  [yY]*)
74952400Sbillf    if rm -rf "${TEMPROOT}"; then
75052400Sbillf      echo " *** ${TEMPROOT} has been deleted"
75152400Sbillf    else
75252400Sbillf      echo " *** Unable to delete ${TEMPROOT}"
75352400Sbillf    fi
75452400Sbillf    ;;
75552400Sbillf  *)
75652400Sbillf    echo " *** ${TEMPROOT} will remain"
75752400Sbillf    ;;
75852400Sbillf  esac
75952400Sbillf  ;;
76052400Sbillf*) ;;
76152400Sbillfesac
76252400Sbillf
76368153Sdougbcase "${AUTO_INSTALLED_FILES}" in
76468153Sdougb'') ;;
76568153Sdougb*)
76668153Sdougb  (
76768153Sdougb    echo ''
76868153Sdougb    echo '*** You chose the automatic install option for files that did not exist'
76968153Sdougb    echo '    on your system.  The following files were installed for you:'
77068153Sdougb    echo "${AUTO_INSTALLED_FILES}"
77168153Sdougb  ) | ${PAGER}
77268153Sdougb  ;;
77368153Sdougbesac
77468153Sdougb
77552400Sbillfcase "${NEED_MAKEDEV}" in
77652400Sbillf'') ;;
77752400Sbillf*)
77852400Sbillf  echo ''
77952400Sbillf  echo "*** You installed a new /dev/MAKEDEV script, so make sure that you run"
78052400Sbillf  echo "    'cd /dev && /bin/sh MAKEDEV all' to rebuild your devices"
78152400Sbillf  ;;
78252400Sbillfesac
78352400Sbillf
78452400Sbillfcase "${NEED_NEWALIASES}" in
78552400Sbillf'') ;;
78652400Sbillf*)
78752400Sbillf  echo ''
78852400Sbillf  echo "*** You installed a new aliases file, so make sure that you run"
78952400Sbillf  echo "    'newaliases' to rebuild your aliases database"
79052400Sbillf  ;;
79152400Sbillfesac
79252400Sbillf
79352400Sbillfcase "${NEED_CAP_MKDB}" in
79452400Sbillf'') ;;
79552400Sbillf*)
79652400Sbillf  echo ''
79752400Sbillf  echo "*** You installed a login.conf file, so make sure that you run"
79852400Sbillf  echo "    'cap_mkdb /etc/login.conf' to rebuild your login.conf database"
79952400Sbillf  ;;
80052400Sbillfesac
80152400Sbillf
80252400Sbillfcase "${NEED_PWD_MKDB}" in
80352400Sbillf'') ;;
80452400Sbillf*)
80552400Sbillf  echo ''
80652400Sbillf  echo "*** You installed a new master.passwd file, so make sure that you run"
80752400Sbillf  echo "    'pwd_mkdb -p /etc/master.passwd' to rebuild your password files"
80852400Sbillf  ;;
80952400Sbillfesac
81052400Sbillf
81152400Sbillfecho ''
81252400Sbillf
81367949Sdougbif [ -r "${MM_EXIT_SCRIPT}" ]; then
81467949Sdougb  . "${MM_EXIT_SCRIPT}"
81567949Sdougbfi
81667949Sdougb
81752400Sbillfexit 0
81867850Sdougb
819