mergemaster.sh revision 67949
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 67949 2000-10-30 10:33:51Z dougb $
1252400Sbillf
1365115SbenPATH=/bin:/usr/bin:/usr/sbin:/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"
12767949Sdougb          # Make the list print one file per line
12867949Sdougb          AUTO_INSTALLED_FILES="${AUTO_INSTALLED_FILES}      ${DESTDIR}${COMPFILE#.}
12967949Sdougb"
13067949Sdougb        else
13167949Sdougb          echo "   *** Problem installing ${COMPFILE}, it will remain to merge by hand"
13267949Sdougb        fi
13367949Sdougb        return
13467949Sdougb        ;;
13567949Sdougb      *)
13667949Sdougb        NO_INSTALLED=yes
13767949Sdougb        ;;
13867949Sdougb      esac
13967850Sdougb    fi
14067859Sdougb
14167850Sdougb    echo "  Use 'd' to delete the temporary ${COMPFILE}"
14267850Sdougb    echo "  Use 'i' to install the temporary ${COMPFILE}"
14367850Sdougb    case "${NO_INSTALLED}" in
14467850Sdougb    '')
14567850Sdougb      echo "  Use 'm' to merge the old and new versions"
14667850Sdougb      echo "  Use 'v' to view to differences between the old and new versions again"
14767850Sdougb      ;;
14867850Sdougb    esac
14967850Sdougb    echo ''
15067850Sdougb    echo "  Default is to leave the temporary file to deal with by hand"
15167850Sdougb    echo ''
15267859Sdougb    echo -n "How should I deal with this? [Leave it for later] "
15367859Sdougb    read HANDLE_COMPFILE
15467859Sdougb
15567850Sdougb    case "${HANDLE_COMPFILE}" in
15667850Sdougb    [dD])
15767850Sdougb      rm "${COMPFILE}"
15867850Sdougb      echo ''
15967850Sdougb      echo "   *** Deleting ${COMPFILE}"
16067850Sdougb      ;;
16167850Sdougb    [iI])
16267850Sdougb      echo ''
16367850Sdougb      if mm_install "${COMPFILE}"; then
16467850Sdougb        echo "   *** ${COMPFILE} installed successfully"
16567850Sdougb      else
16667850Sdougb        echo "   *** Problem installing ${COMPFILE}, it will remain to merge by hand"
16767850Sdougb      fi
16867850Sdougb      ;;
16967850Sdougb    [mM])
17067850Sdougb      case "${NO_INSTALLED}" in
17167850Sdougb      '')
17267850Sdougb        # interact with user to merge files
17367850Sdougb        merge_loop
17467850Sdougb        ;;
17567850Sdougb      *)
17667850Sdougb        echo ''
17767850Sdougb        echo "   *** There is no installed version of ${COMPFILE}"
17867850Sdougb        echo ''
17967850Sdougb        HANDLE_COMPFILE="NOT V"
18067850Sdougb        ;;
18167850Sdougb      esac # End of "No installed version of file but user selected merge" test
18267850Sdougb      ;;
18367850Sdougb    [vV])
18467850Sdougb      continue
18567850Sdougb      ;;
18667850Sdougb    '')
18767850Sdougb      echo ''
18867850Sdougb      echo "   *** ${COMPFILE} will remain for your consideration"
18967850Sdougb      ;;
19067850Sdougb    *)
19167850Sdougb      # invalid choice, show menu again.
19267850Sdougb      echo "invalid choice: ${HANDLE_COMPFILE}"
19367850Sdougb      echo ''
19467850Sdougb      HANDLE_COMPFILE="NOT V"
19567850Sdougb      continue
19667850Sdougb      ;;
19767850Sdougb    esac  # End of "How to handle files that are different"
19867859Sdougb  done
19967850Sdougb  unset NO_INSTALLED
20067850Sdougb  echo ''
20167850Sdougb  case "${VERBOSE}" in
20267850Sdougb  '') ;;
20367850Sdougb  *)
20467850Sdougb    sleep 3
20567850Sdougb    ;;
20667850Sdougb  esac
20758910Salfred}
20858910Salfred
20952400Sbillf# Set the default path for the temporary root environment
21052400Sbillf#
21152400SbillfTEMPROOT='/var/tmp/temproot'
21252400Sbillf
21352400Sbillf# Read .mergemasterrc before command line so CLI can override
21452400Sbillf#
21567949Sdougbif [ -r "$HOME/.mergemasterrc" ]; then
21652400Sbillf  . "$HOME/.mergemasterrc"
21752400Sbillffi
21852400Sbillf
21952400Sbillf# Check the command line options
22052400Sbillf#
22167949Sdougbwhile getopts ":ascrvhim:t:du:w:D:" COMMAND_LINE_ARGUMENT ; do
22252400Sbillf  case "${COMMAND_LINE_ARGUMENT}" in
22352400Sbillf  s)
22452400Sbillf    STRICT=yes
22552400Sbillf    ;;
22652400Sbillf  c)
22752400Sbillf    DIFF_FLAG='-c'
22852400Sbillf    ;;
22952400Sbillf  r)
23052400Sbillf    RERUN=yes
23152400Sbillf    ;;
23252400Sbillf  v)
23352400Sbillf    case "${AUTO_RUN}" in
23452400Sbillf    '') VERBOSE=yes ;;
23552400Sbillf    esac
23652400Sbillf    ;;
23752400Sbillf  a)
23852400Sbillf    AUTO_RUN=yes
23952400Sbillf    unset VERBOSE
24052400Sbillf    ;;
24152400Sbillf  h)
24252400Sbillf    display_usage
24352400Sbillf    display_help
24452400Sbillf    exit 0
24552400Sbillf    ;;
24667949Sdougb  i)
24767949Sdougb    AUTO_INSTALL=yes
24867949Sdougb    ;;
24952400Sbillf  m)
25052400Sbillf    SOURCEDIR=${OPTARG}
25152400Sbillf    ;;
25252400Sbillf  t)
25352400Sbillf    TEMPROOT=${OPTARG}
25452400Sbillf    ;;
25552400Sbillf  d)
25652400Sbillf    TEMPROOT=${TEMPROOT}.`date +%m%d.%H.%M`
25752400Sbillf    ;;
25852400Sbillf  u)
25952400Sbillf    NEW_UMASK=${OPTARG}
26052400Sbillf    ;;
26152400Sbillf  w)
26252400Sbillf    SCREEN_WIDTH=${OPTARG}
26352400Sbillf    ;;
26467949Sdougb  D)
26567949Sdougb    DESTDIR=${OPTARG}
26667949Sdougb    ;;
26752400Sbillf  *)
26852400Sbillf    display_usage
26952400Sbillf    exit 1
27052400Sbillf    ;;
27152400Sbillf  esac
27252400Sbillfdone
27352400Sbillf
27452400Sbillfecho ''
27552400Sbillf
27652400Sbillf# If the user has a pager defined, make sure we can run it
27752400Sbillf#
27852400Sbillfcase "${DONT_CHECK_PAGER}" in
27952400Sbillf'')
28064467Sbrian  while ! type "${PAGER%% *}" >/dev/null && [ -n "${PAGER}" ]; do
28152400Sbillf    echo " *** Your PAGER environment variable specifies '${PAGER}', but"
28264467Sbrian    echo "     due to the limited PATH that I use for security reasons,"
28367859Sdougb    echo "     I cannot execute it.  So, what would you like to do?"
28452400Sbillf    echo ''
28552400Sbillf    echo "  Use 'e' to exit mergemaster and fix your PAGER variable"
28664467Sbrian    if [ -x /usr/bin/less -o -x /usr/local/bin/less ]; then
28764467Sbrian    echo "  Use 'l' to set PAGER to 'less' for this run"
28852400Sbillf    fi
28952400Sbillf    echo "  Use 'm' to use plain old 'more' as your PAGER for this run"
29052400Sbillf    echo ''
29152400Sbillf    echo "  Default is to use plain old 'more' "
29252400Sbillf    echo ''
29367859Sdougb    echo -n "What should I do? [Use 'more'] "
29467859Sdougb    read FIXPAGER
29567859Sdougb
29652400Sbillf    case "${FIXPAGER}" in
29758910Salfred    [eE])
29852400Sbillf       exit 0
29952400Sbillf       ;;
30058910Salfred    [lL])
30164467Sbrian       if [ -x /usr/bin/less ]; then
30264467Sbrian         PAGER=/usr/bin/less
30364467Sbrian       elif [ -x /usr/local/bin/less ]; then
30458910Salfred         PAGER=/usr/local/bin/less
30564467Sbrian       else
30664467Sbrian         echo ''
30764467Sbrian         echo " *** Fatal Error:"
30864467Sbrian         echo "     You asked to use 'less' as your pager, but I can't"
30964467Sbrian         echo "     find it in /usr/bin or /usr/local/bin"
31064467Sbrian         exit 1
31158910Salfred       fi
31252400Sbillf       ;;
31360420Sbsd    [mM]|'')
31452400Sbillf       PAGER=more
31552400Sbillf       ;;
31658910Salfred    *)
31758910Salfred       echo ''
31858910Salfred       echo "invalid choice: ${FIXPAGER}"
31952400Sbillf    esac
32052400Sbillf    echo ''
32158910Salfred  done
32252400Sbillf  ;;
32352400Sbillfesac
32452400Sbillf
32552400Sbillf# If user has a pager defined, or got assigned one above, use it.
32652400Sbillf# If not, use more.
32752400Sbillf#
32852400SbillfPAGER=${PAGER:-more}
32952400Sbillf
33052400Sbillfif [ -n "${VERBOSE}" -a ! "${PAGER}" = "more" ]; then
33152400Sbillf  echo " *** You have ${PAGER} defined as your pager so we will use that"
33252400Sbillf  echo ''
33352400Sbillf  sleep 3
33452400Sbillffi
33552400Sbillf
33652400Sbillf# Assign the diff flag once so we will not have to keep testing it
33752400Sbillf#
33852400SbillfDIFF_FLAG=${DIFF_FLAG:--u}
33952400Sbillf
34052400Sbillf# Assign the source directory
34152400Sbillf#
34252400SbillfSOURCEDIR=${SOURCEDIR:-/usr/src/etc}
34352400Sbillf
34452400Sbillfcase "${RERUN}" in
34552400Sbillf'')
34652400Sbillf  # Set up the loop to test for the existence of the
34752400Sbillf  # temp root directory.
34852400Sbillf  #
34952400Sbillf  TEST_TEMP_ROOT=yes
35052400Sbillf  while [ "${TEST_TEMP_ROOT}" = "yes" ]; do
35152400Sbillf    if [ -d "${TEMPROOT}" ]; then
35252400Sbillf      echo "*** The directory specified for the temporary root environment,"
35367859Sdougb      echo "    ${TEMPROOT}, exists.  This can be a security risk if untrusted"
35452400Sbillf      echo "    users have access to the system."
35552400Sbillf      echo ''
35652400Sbillf      case "${AUTO_RUN}" in
35752400Sbillf      '')
35852400Sbillf        echo "  Use 'd' to delete the old ${TEMPROOT} and continue"
35952400Sbillf        echo "  Use 't' to select a new temporary root directory"
36052400Sbillf        echo "  Use 'e' to exit mergemaster"
36152400Sbillf        echo ''
36252400Sbillf        echo "  Default is to use ${TEMPROOT} as is"
36352400Sbillf        echo ''
36467859Sdougb        echo -n "How should I deal with this? [Use the existing ${TEMPROOT}] "
36567859Sdougb        read DELORNOT
36667859Sdougb
36767859Sdougb        case "${DELORNOT}" in
36867859Sdougb        [dD])
36967859Sdougb          echo ''
37067859Sdougb          echo "   *** Deleting the old ${TEMPROOT}"
37167859Sdougb          echo ''
37267859Sdougb          rm -rf "${TEMPROOT}"
37367859Sdougb          unset TEST_TEMP_ROOT
37452400Sbillf          ;;
37567859Sdougb        [tT])
37667859Sdougb          echo "   *** Enter new directory name for temporary root environment"
37767859Sdougb          read TEMPROOT
37867859Sdougb          ;;
37967859Sdougb        [eE])
38067859Sdougb          exit 0
38167859Sdougb          ;;
38267859Sdougb        '')
38367859Sdougb          echo ''
38467859Sdougb          echo "   *** Leaving ${TEMPROOT} intact"
38567859Sdougb          echo ''
38667859Sdougb          unset TEST_TEMP_ROOT
38767859Sdougb          ;;
38867859Sdougb        *)
38967859Sdougb          echo ''
39067859Sdougb          echo "invalid choice: ${DELORNOT}"
39167859Sdougb          echo ''
39267859Sdougb          ;;
39367859Sdougb        esac
39467859Sdougb        ;;
39552400Sbillf      *)
39652400Sbillf        # If this is an auto-run, try a hopefully safe alternative then re-test anyway
39752400Sbillf        TEMPROOT=/var/tmp/temproot.`date +%m%d.%H.%M.%S`
39852400Sbillf        ;;
39952400Sbillf      esac
40052400Sbillf    else
40152400Sbillf      unset TEST_TEMP_ROOT
40252400Sbillf    fi
40352400Sbillf  done
40452400Sbillf
40552400Sbillf  echo "*** Creating the temporary root environment in ${TEMPROOT}"
40652400Sbillf
40752400Sbillf  if mkdir -p "${TEMPROOT}"; then
40852400Sbillf    echo " *** ${TEMPROOT} ready for use"
40952400Sbillf  fi
41052400Sbillf
41152400Sbillf  if [ ! -d "${TEMPROOT}" ]; then
41252400Sbillf    echo ''
41352400Sbillf    echo "  *** FATAL ERROR: Cannot create ${TEMPROOT}"
41452400Sbillf    echo ''
41552400Sbillf    exit 1
41652400Sbillf  fi
41752400Sbillf
41852400Sbillf  echo " *** Creating and populating directory structure in ${TEMPROOT}"
41952400Sbillf  echo ''
42052400Sbillf
42152400Sbillf  case "${VERBOSE}" in
42252400Sbillf  '') ;;
42352400Sbillf  *)
42452400Sbillf    echo " *** Press [Enter] or [Return] key to continue"
42552400Sbillf    read ANY_KEY
42652400Sbillf    unset ANY_KEY
42752400Sbillf    ;;
42852400Sbillf  esac
42952400Sbillf
43052400Sbillf  { cd ${SOURCEDIR} &&
43167949Sdougb    make DESTDIR=${DESTDIR} distrib-dirs &&
43252400Sbillf    make DESTDIR=${TEMPROOT} distrib-dirs &&
43357173Sbillf    make DESTDIR=${TEMPROOT} -DNO_MAKEDEV distribution;} ||
43452400Sbillf  { echo '';
43552400Sbillf    echo "  *** FATAL ERROR: Cannot 'cd' to ${SOURCEDIR} and install files to the";
43652400Sbillf    echo "      temproot environment";
43752400Sbillf    echo '';
43852400Sbillf    exit 1;}
43952400Sbillf
44067949Sdougb  # Doing the inventory and removing files that we don't want to compare only makes
44167949Sdougb  # sense if we are not doing a rerun, since we have no way of knowing what happened
44267949Sdougb  # to the files during previous incarnations.
44367949Sdougb  case "${VERBOSE}" in
44467949Sdougb  '') ;;
44567949Sdougb  *)
44667949Sdougb    echo ''
44767949Sdougb    echo ' *** The following files exist only in the installed version of'
44867949Sdougb    echo "     ${DESTDIR}/etc.  In the vast majority of cases these files"
44967949Sdougb    echo '     are necessary parts of the system and should not be deleted.'
45067949Sdougb    echo '     However because these files are not updated by this process you'
45167949Sdougb    echo '     might want to verify their status before rebooting your system.'
45267949Sdougb    echo ''
45367949Sdougb    echo ' *** Press [Enter] or [Return] key to continue'
45467949Sdougb    read ANY_KEY
45567949Sdougb    unset ANY_KEY
45667949Sdougb    diff -qr ${DESTDIR}/etc ${TEMPROOT}/etc | grep "^Only in /etc" | ${PAGER}
45767949Sdougb    echo ''
45867949Sdougb    echo ' *** Press [Enter] or [Return] key to continue'
45967949Sdougb    read ANY_KEY
46067949Sdougb    unset ANY_KEY
46167949Sdougb    ;;
46267949Sdougb  esac
46367949Sdougb
46452400Sbillf  # We really don't want to have to deal with these files, since
46552400Sbillf  # master.passwd is the real file that should be compared, then
46652400Sbillf  # the user should run pwd_mkdb if necessary.
46752400Sbillf  #
46852400Sbillf  rm ${TEMPROOT}/etc/spwd.db ${TEMPROOT}/etc/passwd ${TEMPROOT}/etc/pwd.db
46952400Sbillf
47052534Sbillf  # Avoid comparing the motd if the user specifies it in .mergemasterrc
47152534Sbillf  case "${IGNORE_MOTD}" in
47252534Sbillf  '') ;;
47352534Sbillf  *) rm ${TEMPROOT}/etc/motd
47452534Sbillf     ;;
47552534Sbillf  esac
47652534Sbillf
47765115Sben  # Avoid trying to update MAKEDEV if /dev is on a devfs
47867859Sdougb  if sysctl vfs.devfs.generation > /dev/null 2>&1 ; then
47965115Sben    rm ${TEMPROOT}/dev/MAKEDEV ${TEMPROOT}/dev/MAKEDEV.local
48065115Sben  fi
48165115Sben
48252400Sbillf  ;; # End of the "RERUN" test
48352400Sbillfesac
48452400Sbillf
48552400Sbillf# Get ready to start comparing files
48652400Sbillf
48752400Sbillf# Check umask if not specified on the command line,
48852400Sbillf# and we are not doing an autorun
48952400Sbillf#
49052400Sbillfif [ -z "${NEW_UMASK}" -a -z "${AUTO_RUN}" ]; then
49152400Sbillf  USER_UMASK=`umask`
49252400Sbillf  case "${USER_UMASK}" in
49352400Sbillf  0022) ;;
49452400Sbillf  *)
49552400Sbillf    echo ''
49667859Sdougb    echo " *** Your umask is currently set to ${USER_UMASK}.  By default, this script"
49752400Sbillf    echo "     installs all files with the same user, group and modes that"
49852400Sbillf    echo "     they are created with by ${SOURCEDIR}/Makefile, compared to"
49967859Sdougb    echo "     a umask of 022.  This umask allows world read permission when"
50052400Sbillf    echo "     the file's default permissions have it."
50167859Sdougb    echo "     No world permissions can sometimes cause problems.  A umask of"
50252400Sbillf    echo "     022 will restore the default behavior, but is not mandatory."
50367859Sdougb    echo "     /etc/master.passwd is a special case.  Its file permissions"
50452400Sbillf    echo "     will be 600 (rw-------) if installed."
50552400Sbillf    echo ''
50667859Sdougb    echo -n "What umask should I use? [${USER_UMASK}] "
50767859Sdougb    read NEW_UMASK
50867859Sdougb
50952400Sbillf    NEW_UMASK="${NEW_UMASK:-$USER_UMASK}"
51052400Sbillf    ;;
51152400Sbillf  esac
51252400Sbillf  echo ''
51352400Sbillffi
51452400Sbillf
51552400SbillfCONFIRMED_UMASK=${NEW_UMASK:-0022}
51652400Sbillf
51767949Sdougb# Warn users who still have ${DESTDIR}/etc/sysconfig
51852400Sbillf#
51967949Sdougbif [ -e "${DESTDIR}/etc/sysconfig" ]; then
52052400Sbillf  echo ''
52167949Sdougb  echo " *** There is a sysconfig file on this system in ${DESTDIR}/etc/."
52267949Sdougb  echo ''
52367949Sdougb  echo '     Starting with FreeBSD version 2.2.2 those settings moved from'
52467949Sdougb  echo '     /etc/sysconfig to /etc/rc.conf.  If you are upgrading an older'
52567949Sdougb  echo '     system make sure that you transfer your settings by hand from'
52667949Sdougb  echo '     sysconfig to rc.conf and install the rc.conf file.  If you'
52767949Sdougb  echo '     have already made this transition, you should consider'
52867949Sdougb  echo '     renaming or deleting the sysconfig file.'
52967949Sdougb  echo ''
53052400Sbillf  case "${AUTO_RUN}" in
53152400Sbillf  '')
53267859Sdougb    echo -n "Continue with the merge process? [yes] "
53367859Sdougb    read CONT_OR_NOT
53467859Sdougb
53552400Sbillf    case "${CONT_OR_NOT}" in
53652400Sbillf    [nN]*)
53752400Sbillf      exit 0
53852400Sbillf      ;;
53952400Sbillf    *)
54052400Sbillf      echo "   *** Continuing"
54152400Sbillf      echo ''
54252400Sbillf      ;;
54352400Sbillf    esac
54452400Sbillf    ;;
54552400Sbillf  *) ;;
54652400Sbillf  esac
54752400Sbillffi
54852400Sbillf
54952400Sbillf# Use the umask/mode information to install the files
55052400Sbillf# Create directories as needed
55152400Sbillf#
55252400Sbillfmm_install () {
55352400Sbillf  local INSTALL_DIR
55452400Sbillf  INSTALL_DIR=${1#.}
55552400Sbillf  INSTALL_DIR=${INSTALL_DIR%/*}
55667949Sdougb
55752400Sbillf  case "${INSTALL_DIR}" in
55852400Sbillf  '')
55952400Sbillf    INSTALL_DIR=/
56052400Sbillf    ;;
56152400Sbillf  esac
56252400Sbillf
56367949Sdougb  if [ -n "${DESTDIR}${INSTALL_DIR}" -a ! -d "${DESTDIR}${INSTALL_DIR}" ]; then
56452400Sbillf    DIR_MODE=`perl -e 'printf "%04o\n", (((stat("$ARGV[0]"))[2] & 07777) &~ oct("$ARGV[1]"))' "${TEMPROOT}/${INSTALL_DIR}" "${CONFIRMED_UMASK}"`
56567949Sdougb    install -d -o root -g wheel -m "${DIR_MODE}" "${DESTDIR}${INSTALL_DIR}"
56652400Sbillf  fi
56752400Sbillf
56852400Sbillf  FILE_MODE=`perl -e 'printf "%04o\n", (((stat("$ARGV[0]"))[2] & 07777) &~ oct("$ARGV[1]"))' "${1}" "${CONFIRMED_UMASK}"`
56952400Sbillf
57052400Sbillf  if [ ! -x "${1}" ]; then
57152400Sbillf    case "${1#.}" in
57264625Sgshapiro    /etc/mail/aliases)
57352400Sbillf      NEED_NEWALIASES=yes
57452400Sbillf      ;;
57552400Sbillf    /etc/login.conf)
57652400Sbillf      NEED_CAP_MKDB=yes
57752400Sbillf      ;;
57852400Sbillf    /etc/master.passwd)
57967949Sdougb      install -m 600 "${1}" "${DESTDIR}${INSTALL_DIR}"
58052400Sbillf      NEED_PWD_MKDB=yes
58152400Sbillf      DONT_INSTALL=yes
58252400Sbillf      ;;
58352400Sbillf    /.cshrc | /.profile)
58452400Sbillf      case "${LINK_EXPLAINED}" in
58552400Sbillf      '')
58667859Sdougb        echo "   *** Historically BSD derived systems have had a"
58767859Sdougb        echo "       hard link from /.cshrc and /.profile to"
58867859Sdougb        echo "       their namesakes in /root.  Please indicate"
58967859Sdougb        echo "       your preference below for bringing your"
59067859Sdougb        echo "       installed files up to date."
59152400Sbillf        echo ''
59252400Sbillf        LINK_EXPLAINED=yes
59352400Sbillf        ;;
59452400Sbillf      esac
59552400Sbillf
59652400Sbillf      echo "   Use 'd' to delete the temporary ${COMPFILE}"
59767949Sdougb      echo "   Use 'l' to delete the existing ${DESTDIR}${COMPFILE#.} and create the link"
59852400Sbillf      echo ''
59952400Sbillf      echo "   Default is to leave the temporary file to deal with by hand"
60052400Sbillf      echo ''
60167859Sdougb      echo -n "  How should I handle ${COMPFILE}? [Leave it to install later] "
60267859Sdougb      read HANDLE_LINK
60367859Sdougb
60452400Sbillf      case "${HANDLE_LINK}" in
60552400Sbillf      [dD]*)
60652400Sbillf        rm "${COMPFILE}"
60752400Sbillf        echo ''
60852400Sbillf        echo "   *** Deleting ${COMPFILE}"
60952400Sbillf        ;;
61052400Sbillf      [lL]*)
61152400Sbillf        echo ''
61267949Sdougb        rm -f "${DESTDIR}${COMPFILE#.}"
61367949Sdougb        if ln "${DESTDIR}/root/${COMPFILE##*/}" "${DESTDIR}${COMPFILE#.}"; then
61467949Sdougb          echo "   *** Link from ${DESTDIR}${COMPFILE#.} to ${DESTDIR}/root/${COMPFILE##*/} installed successfully"
61552400Sbillf          rm "${COMPFILE}"
61652400Sbillf        else
61767949Sdougb          echo "   *** Error linking ${DESTDIR}${COMPFILE#.} to ${DESTDIR}/root/${COMPFILE##*/}, ${COMPFILE} will remain to install by hand"
61852400Sbillf        fi
61952400Sbillf        ;;
62052400Sbillf      *)
62152400Sbillf        echo "   *** ${COMPFILE} will remain for your consideration"
62252400Sbillf        ;;
62352400Sbillf      esac
62452400Sbillf      DONT_INSTALL=yes
62552400Sbillf      ;;
62652400Sbillf    esac
62752400Sbillf
62852400Sbillf    case "${DONT_INSTALL}" in
62952400Sbillf    '')
63067949Sdougb      install -m "${FILE_MODE}" "${1}" "${DESTDIR}${INSTALL_DIR}"
63152400Sbillf      ;;
63252400Sbillf    *)
63352400Sbillf      unset DONT_INSTALL
63452400Sbillf      ;;
63552400Sbillf    esac
63652400Sbillf  else
63767949Sdougb    case "${1#.}" in
63867949Sdougb    /dev/MAKEDEV)
63967949Sdougb      NEED_MAKEDEV=yes
64067949Sdougb      ;;
64167949Sdougb    esac
64267949Sdougb    install -m "${FILE_MODE}" "${1}" "${DESTDIR}${INSTALL_DIR}"
64352400Sbillf  fi
64452400Sbillf  return $?
64552400Sbillf}
64652400Sbillf
64767949Sdougbecho ''
64867949Sdougbecho "*** Beginning comparison"
64967949Sdougbecho ''
65052400Sbillf
65167949Sdougbcd "${TEMPROOT}"
65267949Sdougb
65367949Sdougbif [ -r "${MM_PRE_COMPARE_SCRIPT}" ]; then
65467949Sdougb  . "${MM_PRE_COMPARE_SCRIPT}"
65567949Sdougbfi
65667949Sdougb
65752400Sbillf# Using -size +0 avoids uselessly checking the empty log files created
65852400Sbillf# by ${SOURCEDIR}/Makefile and the device entries in ./dev, but does
65967949Sdougb# check the scripts in ./dev, as we'd like (assuming no devfs of course).
66052400Sbillf#
66152400Sbillffor COMPFILE in `find . -type f -size +0`; do
66267949Sdougb
66367949Sdougb  # First, check to see if the file exists in DESTDIR.  If not, the
66467949Sdougb  # diff_loop function knows how to handle it.
66567949Sdougb  #
66667949Sdougb  if [ ! -e "${DESTDIR}${COMPFILE#.}" ]; then
66767949Sdougb    diff_loop
66867949Sdougb    continue
66967949Sdougb  fi
67067949Sdougb
67152400Sbillf  case "${STRICT}" in
67252400Sbillf  '' | [Nn][Oo])
67352400Sbillf    # Compare CVS $Id's first so if the file hasn't been modified
67452400Sbillf    # local changes will be ignored.
67552400Sbillf    # If the files have the same $Id, delete the one in temproot so the
67652400Sbillf    # user will have less to wade through if files are left to merge by hand.
67752400Sbillf    #
67867949Sdougb    # Reduce complexity and improve portability by using ident
67967949Sdougb    #
68067949Sdougb    CVSID1=`ident ${DESTDIR}${COMPFILE#.} 2>&1`
68167949Sdougb    CVSID1="${CVSID1#${DESTDIR}}"
68267949Sdougb    CVSID2=`ident ${COMPFILE} 2>&1`
68352400Sbillf
68467949Sdougb    case "${CVSID2}" in
68567949Sdougb    *'no id keywords'*)
68667949Sdougb      ;;
68767949Sdougb    ."${CVSID1}")
68867949Sdougb      echo " *** Temp ${COMPFILE} and installed have the same CVS Id, deleting"
68967949Sdougb      rm "${COMPFILE}"
69067949Sdougb      ;;
69167949Sdougb    esac
69252400Sbillf    ;;
69352400Sbillf  esac
69452400Sbillf
69552400Sbillf  # If the file is still here either because the $Ids are different, the
69652400Sbillf  # file doesn't have an $Id, or we're using STRICT mode; look at the diff.
69752400Sbillf  #
69852400Sbillf  if [ -f "${COMPFILE}" ]; then
69952400Sbillf
70052400Sbillf    # Do an absolute diff first to see if the files are actually different.
70152400Sbillf    # If they're not different, delete the one in temproot.
70252400Sbillf    #
70367949Sdougb    if diff -q "${DESTDIR}${COMPFILE#.}" "${COMPFILE}" > /dev/null 2>&1; then
70452400Sbillf      echo " *** Temp ${COMPFILE} and installed are the same, deleting"
70552400Sbillf      rm "${COMPFILE}"
70652400Sbillf    else
70767859Sdougb      # Ok, the files are different, so show the user where they differ.  Use user's
70867859Sdougb      # choice of diff methods; and user's pager if they have one.  Use more if not.
70967859Sdougb      # Use unified diffs by default.  Context diffs give me a headache. :)
71052400Sbillf      #
71152400Sbillf      case "${AUTO_RUN}" in
71252400Sbillf      '')
71358910Salfred        # prompt user to install/delete/merge changes
71458910Salfred        diff_loop
71552400Sbillf        ;;
71652400Sbillf      *)
71752400Sbillf        # If this is an auto run, make it official
71852400Sbillf        echo "   *** ${COMPFILE} will remain for your consideration"
71952400Sbillf        ;;
72052400Sbillf      esac # Auto run test
72152400Sbillf    fi # Yes, the files are different
72252400Sbillf  fi # Yes, the file still remains to be checked
72352400Sbillfdone # This is for the do way up there at the beginning of the comparison
72452400Sbillf
72552534Sbillfecho ''
72652400Sbillfecho "*** Comparison complete"
72752400Sbillfecho ''
72852400Sbillf
72952400SbillfTEST_FOR_FILES=`find ${TEMPROOT} -type f -size +0 2>/dev/null`
73052400Sbillfif [ -n "${TEST_FOR_FILES}" ]; then
73152400Sbillf  echo "*** Files that remain for you to merge by hand:"
73252400Sbillf  find "${TEMPROOT}" -type f -size +0
73352400Sbillffi
73452400Sbillf
73552400Sbillfcase "${AUTO_RUN}" in
73652400Sbillf'')
73752400Sbillf  echo ''
73867859Sdougb  echo -n "Do you wish to delete what is left of ${TEMPROOT}? [no] "
73967859Sdougb  read DEL_TEMPROOT
74067859Sdougb
74152400Sbillf  case "${DEL_TEMPROOT}" in
74252400Sbillf  [yY]*)
74352400Sbillf    if rm -rf "${TEMPROOT}"; then
74452400Sbillf      echo " *** ${TEMPROOT} has been deleted"
74552400Sbillf    else
74652400Sbillf      echo " *** Unable to delete ${TEMPROOT}"
74752400Sbillf    fi
74852400Sbillf    ;;
74952400Sbillf  *)
75052400Sbillf    echo " *** ${TEMPROOT} will remain"
75152400Sbillf    ;;
75252400Sbillf  esac
75352400Sbillf  ;;
75452400Sbillf*) ;;
75552400Sbillfesac
75652400Sbillf
75752400Sbillfcase "${NEED_MAKEDEV}" in
75852400Sbillf'') ;;
75952400Sbillf*)
76052400Sbillf  echo ''
76152400Sbillf  echo "*** You installed a new /dev/MAKEDEV script, so make sure that you run"
76252400Sbillf  echo "    'cd /dev && /bin/sh MAKEDEV all' to rebuild your devices"
76352400Sbillf  ;;
76452400Sbillfesac
76552400Sbillf
76652400Sbillfcase "${NEED_NEWALIASES}" in
76752400Sbillf'') ;;
76852400Sbillf*)
76952400Sbillf  echo ''
77052400Sbillf  echo "*** You installed a new aliases file, so make sure that you run"
77152400Sbillf  echo "    'newaliases' to rebuild your aliases database"
77252400Sbillf  ;;
77352400Sbillfesac
77452400Sbillf
77552400Sbillfcase "${NEED_CAP_MKDB}" in
77652400Sbillf'') ;;
77752400Sbillf*)
77852400Sbillf  echo ''
77952400Sbillf  echo "*** You installed a login.conf file, so make sure that you run"
78052400Sbillf  echo "    'cap_mkdb /etc/login.conf' to rebuild your login.conf database"
78152400Sbillf  ;;
78252400Sbillfesac
78352400Sbillf
78452400Sbillfcase "${NEED_PWD_MKDB}" in
78552400Sbillf'') ;;
78652400Sbillf*)
78752400Sbillf  echo ''
78852400Sbillf  echo "*** You installed a new master.passwd file, so make sure that you run"
78952400Sbillf  echo "    'pwd_mkdb -p /etc/master.passwd' to rebuild your password files"
79052400Sbillf  ;;
79152400Sbillfesac
79252400Sbillf
79367949Sdougbcase "${AUTO_INSTALLED_FILES}" in
79467949Sdougb'') ;;
79567949Sdougb*)
79667949Sdougb  (
79767949Sdougb    echo ''
79867949Sdougb    echo '*** You chose the automatic install option for files that did not exist'
79967949Sdougb    echo '    on your system.  The following files were installed for you:'
80067949Sdougb    echo "${AUTO_INSTALLED_FILES}"
80167949Sdougb  ) | ${PAGER}
80267949Sdougb  ;;
80367949Sdougbesac
80467949Sdougb
80552400Sbillfecho ''
80652400Sbillf
80767949Sdougbif [ -r "${MM_EXIT_SCRIPT}" ]; then
80867949Sdougb  . "${MM_EXIT_SCRIPT}"
80967949Sdougbfi
81067949Sdougb
81152400Sbillfexit 0
81267850Sdougb
813