#!/bin/sh
#
# $Id: Customize,v 1.2 97/04/07 08:18:55 abe Exp $
#
# Customize: customize dialect's machine.h header file.
#
# Allows easy modification of some important compile-time definitions for
# lsof, made in the dialect's machine.h header file, including:
#
#	HASSECURITY	the security option
#	HASDCACHE	enabling/disabling the device cache file
#	HASENVDC	enabling/disabling device cache path from environment
#	HASPERSDC	enabling/disabling personal device cache path
#			construction
#	HASPERSDCPATH	enabling/disabling additional personal device cache
#			path component
#	HASSYSDC	enabling/disabling system-wide device cache file path
#	WARNDEVACCESS	enabling inaccessible /dev node warnings
#	WARNINGSTATE	enable/disabling default warning message state

OLD=machine.h
NEW=new_machine.h

# Establish trap and stty handling.

ISIG=":"
trap 'rm -f $NEW; $ISIG; exit 1' 1 2 3 15
stty -a 2>&1 | grep isig > /dev/null
if test $? -eq 0
then
  stty -a 2>&1 | egrep -e -isig > /dev/null
  if test $? -eq 0
  then
    ISIG="stty -isig"
    stty isig
  fi
fi

# Decide how to use echo.

ECHO=`echo -n ""`
if test "X$ECHO" = "X-n "
then
  EC="\c"
  EO=""
else
  EC=""
  EO="-n"
fi

# Display the introduction and basic explanation.

cat << .CAT_MARK

You may now customize the machine.h header file for this UNIX
dialect.  The customizations will take effect when you compile
lsof.  You may also choose to skip customization and proceed to
the compilation of lsof.

If you don't know if you need to customize or want to know more
about what you can customize, consult the 00DCACHE, 00FAQ, 00PORTING,
and 00README files of the lsof distribution.  You might also find
it helpful to examine the machine.h header file for the dialect
you're customizing.

You don't need to use this procedure to customize lsof; you can
edit the machine.h header file directly.  If you later decide you
want to use this procedure to customize machine.h, execute the
./Customize script.
.CAT_MARK

END=0
while test $END = 0
do
  echo ""
  echo $EO "Do you want to customize (y/n)? $EC"
  read ANS EXCESS
  if test "X$ANS" = "Xn" -o "X$ANS" = "XN"
  then
    exit 0
  fi
  if test "X$ANS" = "Xy" -o "X$ANS" = "XY"
  then
    END=1
  else
    echo ""
    echo "Please answer y or n."
  fi
done

# See if $OLD exists.

if test ! -r $OLD
then
  echo ""
  echo "FATAL: The file \"$OLD\" doesn't exist.  Customization can't continue"
  echo "without it.  Customize quits."
  echo ""
  exit 1
fi

# See if $NEW exists.

if test -r $NEW
then
  echo ""
  echo "====================================================================="
  echo ""
  echo "WARNING: \"$NEW\" exists.  Customization will replace it."
  END=0
  while test $END -eq 0
  do
    echo ""
    echo $EO "Do you want to remove $NEW (y/n)? $EC"
    read ANS EXCESS
    if test "X$ANS" = "Xy" -o "X$ANS" = "XY"
    then
      rm -f $NEW
      END=1
    else
      if test "X$ANS" = "Xn" -o "X$ANS" = "XN"
      then
        echo ""
        echo "FATAL: Customize quits; it must be able to create \"$NEW\"."
        echo ""
        exit 1
      else
        echo ""
        echo "Please answer y or n."
      fi
    fi
  done
fi

# Process HASSECURITY.

cat << .CAT_MARK

=====================================================================

When HASSECURITY is enabled, only the root user may use lsof to
examine all open files; other users may examine only the files
belonging to the real user ID of their lsof process.

When HASSECURITY is disabled, anyone may use lsof to examine all
open files.

.CAT_MARK

egrep "^#define[ 	]HASSECURITY" $OLD > /dev/null
if test $? -eq 0
then
  echo "HASSECURITY is enabled."
  SEC=1
else
  echo "HASSECURITY is disabled."
  SEC=0
fi
END=0
NSEC=$SEC
while test $END = 0
do
  echo ""
  if test $NSEC -eq 1
  then
    echo $EO "Disable HASSECURITY (y/n)? $EC"
  else
    echo $EO "Enable HASSECURITY (y/n)? $EC"
  fi
  read ANS EXCESS
  if test "X$ANS" = "Xy" -o "X$ANS" = "XY"
  then
    echo ""
    if test $NSEC -eq 1
    then
      NSEC=0
      echo "HASSECURITY will be disabled."
    else
      NSEC=1
      echo "HASSECURITY will be enabled."
    fi
    END=1
  else
    if test "X$ANS" = "Xn" -o "X$ANS" = "XN"
    then
      END=1
    else
      echo ""
      echo "Please answer y or n."
    fi
  fi
done

# process WARNINGSTATE

cat << .CAT_MARK

=====================================================================

When WARNINGSTATE is enabled, lsof will will issue whatever warning
messages it finds necessary.  When WARNINGSTATE is disabled, lsof
will issue no warning messages.  The lsof -h option output will
indicate what WARNINGSTATE is in effect with the -w option description,
and -w may be used to reverse the WARNINGSTATE.

.CAT_MARK

egrep "^#define	WARNINGSTATE" $OLD > /dev/null
if test $? -eq 0
then
  echo "WARNINGSTATE is disabled."
  WST=0
else
  echo "WARNINGSTATE is enabled."
  WST=1
fi
END=0
NWST=$WST
while test $END = 0
do
  echo ""
  if test $NWST -eq 0
  then
    echo $EO "Enable WARNINGSTATE? (y/n)? $EC"
  else
    echo $EO "Disable WARNINGSTATE? (y/n)? $EC"
  fi
  read ANS EXCESS
  if test "X$ANS" = "Xy" -o "X$ANS" = "XY"
  then
    echo ""
    if test $NWST -eq 1
    then
      echo "WARNINGSTATE will be disabled."
      NWST=0
    else
      echo "WARNINGSTATE will be enabled."
      NWST=1
    fi
    END=1
  else
    if test "X$ANS" = "Xn" -o "X$ANS" = "XN"
    then
      END=1
    else
      echo ""
      echo "Please answer y or n."
    fi
  fi
done

# Process WARNDEVACCESS.

cat << .CAT_MARK

=====================================================================

When WARNDEVACCESS is enabled, lsof will issue warning messages
when it can't access nodes in /dev (or /devices), subject to the
default or explicit (-w) WARNINGSTATE.

When WARNDEVACCESS is disabled, lsof will silently skip nodes in
/dev (or /devices) that it can't access.

.CAT_MARK

egrep "^#define[ 	]WARNDEVACCESS" $OLD > /dev/null
if test $? -eq 0
then
  echo "WARNDEVACCESS is enabled."
  WDA=1
else
  echo "WARNDEVACCESS is disabled."
  WDA=0
fi
END=0
NWDA=$WDA
while test $END = 0
do
  echo ""
  if test $NWDA -eq 1
  then
    echo $EO "Disable WARNDEVACCESS (y/n)? $EC"
  else
    echo $EO "Enable WARNDEVACCESS (y/n)? $EC"
  fi
  read ANS EXCESS
  if test "X$ANS" = "Xy" -o "X$ANS" = "XY"
  then
    echo ""
    if test $NWDA -eq 1
    then
      echo "WARNDEVACCESS will be disabled."
      NWDA=0
    else
      echo "WARNDEVACCESS will be enabled."
      NWDA=1
    fi
    END=1
  else
    if test "X$ANS" = "Xn" -o "X$ANS" = "XN"
    then
      END=1
    else
      echo ""
      echo "Please answer y or n."
    fi
  fi
done

# Process HASDCACHE.

cat << .CAT_MARK

=====================================================================

When HASDCACHE is enabled, lsof will write a device cache file that
contains information about the nodes in /dev (or /devices).  The
options HASENVDC, HASPERSDC, HASPERSDCPATH, and HASSYSDC define
the device cache file path.

When HASDCACHE is disabled, lsof won't write a device cache file.

Consult the 00DCACHE and 00FAQ files of the lsof distribution for
more information.

.CAT_MARK

egrep "^#define[ 	]HASDCACHE" $OLD > /dev/null
if test $? -eq 0
then
  echo "HASDCACHE is enabled."
  DC=1
else
  echo "HASDCACHE is disabled."
  DC=0
fi
END=0
NDC=$DC
while test $END = 0
do
  echo ""
  if test $NDC -eq 1
  then
    echo $EO "Disable HASDCACHE (y/n)? $EC"
  else
    echo $EO "Enable HASDCACHE (y/n)? $EC"
  fi
  read ANS EXCESS
  if test "X$ANS" = "Xy" -o "X$ANS" = "XY"
  then
    echo ""
    if test $NDC -eq 1
    then
      echo "HASDCACHE will be disabled."
      NDC=0
    else
      echo "HASDCACHE will be enabled."
      NDC=1
    fi
    END=1
  else
    if test "X$ANS" = "Xn" -o "X$ANS" = "XN"
    then
      END=1
    else
      echo ""
      echo "Please answer y or n."
    fi
  fi
done

# See if other device cache options need to be declared.

if test $DC -eq 1 -a $NDC -eq 1
then
  cat << .CAT_MARK

=====================================================================

You have decided to leave HASDCACHE defined.  There are a number
of other definitions associated with HASDCACHE that specify options
for the formation of the device cache file path.  You may change
them.

Consult the 00DCACHE and 00FAQ files of the lsof distribution for
more information.

The current path options are:
  
.CAT_MARK

  egrep "^#define[ 	]HASENVDC[ 	]" $OLD
  egrep "^#define[ 	]HASPERSDC[ 	]" $OLD
  egrep "^#define[ 	]HASPERSDCPATH[ 	]" $OLD
  egrep "^#define[ 	]HASSYSDC[ 	]" $OLD
  END=0
  while test $END -eq 0
  do
    echo ""
    echo $EO "Do you want to change path options (y/n)? $EC"
    read ANS EXCESS
    if test "X$ANS" = "Xy" -o "X$ANS" = "XY"
    then
      CDC=1
      END=1
    else
      if test "X$ANS" = "Xn" -o "X$ANS" = "XN"
      then
	CDC=0
	END=1
      else
	echo ""
	echo "Please answer y or n."
      fi
    fi
  done
else
  CDC=0
fi
if test \( $NDC -eq 1 -a $DC -eq 0 \) -o \( $DC -eq 1 -a $CDC -eq 1 \)
then
  cat << .CAT_MARK

=====================================================================

Specify for HASENVDC the name of the environment variable from
which lsof should take the device cache file path for non-root
users.
.CAT_MARK

  END=0
  ENV=""
  while test $END = 0
  do
    echo ""
    echo $EO "Do you want to define a name for HASENVDC (y/n)? $EC"
    read ANS EXCESS
    if test "X$ANS" = "Xn" -o "X$ANS" = "XN"
    then
      ENV=""
      END=1
    else
      if test "X$ANS" = "Xy" -o "X$ANS" = "XY"
      then
	ENV="1"
	END=1
      else
	echo ""
	echo "Please answer y or n."
      fi
    fi
  done
  if test "X$ENV" = "X1"
  then
    echo ""
    echo $EO "Please enter the HASENVDC name: $EC"
    read ENVV EXCESS
  fi
  cat << .CAT_MARK

=====================================================================

HASPERSDC is a format that specifies how the personal device cache
path is constructed.  Consult the 00DCACHE and 00FAQ files of the
lsof distribution for information on the conversions supported in
HASPERSDC.
.CAT_MARK

  END=0
  while test $END = 0
  do
    echo ""
    echo $EO "Do you want to define a format for HASPERSDC (y/n)? $EC"
    read ANS EXCESS
    if test "X$ANS" = "Xn" -o "X$ANS" = "XN"
    then
      PDC=""
      END=1
    else
      if test "X$ANS" = "Xy" -o "X$ANS" = "XY"
      then
	PDC="1"
	END=1
      else
	echo ""
	echo "Please answer y or n."
      fi
    fi
  done
  if test "X$PDC" = "X1"
  then
    echo ""
    echo $EO "Please enter the HASPERSDC format: $EC"
    read PDCV EXCESS
  fi
  echo "X$PDCV" | grep "%p" > /dev/null
  if test $? -eq 0
  then
    HASPCTP=1
  else
    HASPCTP=0
  fi
  if test "X$PDC" = "X1" -a $HASPCTP -eq 1
  then
    cat << .CAT_MARK

=====================================================================

Specify for HASPERSDCPATH the name of the environment variable from
which lsof should take a path name component to insert at the %p
conversion in the HASPERSDC format.

Consult the 00FAQ and 00DCACHE files of the lsof distribution for
more information on HASPERSDCPATH usage.

.CAT_MARK

    echo $EO "Please enter the HASPERSDCPATH name: $EC"
    read PDCPV EXCESS
    PDCP="1"
  else
    PDCP=""
  fi
  cat << .CAT_MARK

=====================================================================

Specify for HASSYSDC the system-wide device cache file path.
.CAT_MARK

  END=0
  SDC=""
  while test $END = 0
  do
    echo ""
    echo $EO "Do you want to define a system-device path (y/n)? $EC"
    read ANS EXCESS
  if test "X$ANS" = "Xn" -o "X$ANS" = "XN"
    then
      SDC=""
      END=1
    else
      if test "X$ANS" = "Xy" -o "X$ANS" = "XY"
      then
	SDC="1"
	END=1
      else
	echo ""
	echo "Please answer y or n."
      fi
    fi
  done
  if test "X$SDC" = "X1"
  then
    echo ""
    echo $EO "Please enter the system-wide path: $EC"
    read SDCV EXCESS
  fi
fi

# If HASXOPT is defined, and HASXOPT_ROOT is mentioned,
# ask about changing HASXOPT_ROOT.

HXRC=0
egrep "^#define[ 	]HASXOPT[ 	]" $OLD > /dev/null
if test $? -eq 0
then
  grep HASXOPT_ROOT $OLD > /dev/null
  if test $? -eq 0
  then
    cat << .CAT_MARK

=====================================================================

HASXOPT is defined.  If the dialect for which you are customizing
appears in the following list, you may want to change the definition
of HASXOPT_ROOT to restrict the use of the X option to lsof processes
whose real user ID is root, or enable use of it by all user IDs.

  AIX           the -X option enables the risky operation of letting
		lsof read library entry structures with readx().
		If HASXOPT_ROOT is defined, only processes whose
		real user ID is root will be allowed to use -X.
		If HASXOPT_ROOT is undefined, any process will be
		allowed to use -X.  Consult the 00FAQ file of the
		lsof distribution for more information on why
		readx() may be risky.

.CAT_MARK

    egrep "^#define[ 	]HASXOPT_ROOT[ 	]" $OLD > /dev/null
    if test $? -eq 0
    then
      echo "HASXOPT_ROOT is defined."
      HXR="undefine"
      HXRS=1
    else
      echo "HASXOPT_ROOT is not defined."
      HXR="define"
      HXRS=0
    fi
    END=0
    while test $END -eq 0
    do
      echo ""
      echo $EO "Do you want to $HXR HASXOPT_ROOT (y/n)? $EC"
      read ANS EXCESS
      if test "X$ANS" = "Xy" -o "X$ANS" = "XY"
      then
        HXRA=1
        END=1
      else
        if test "X$ANS" = "Xn" -o "X$ANS" = "XN"
        then
          HXRA=0
          END=1
        else
          echo ""
          echo "Please answer y or n."
        fi
      fi
    done
    if test $HXRA -eq 1
    then
      HXRC=1
    fi
  fi
fi

# Initialize new machine.h.

rm -f $NEW
cp $OLD $NEW
chmod 0644 $NEW
echo "" >> $NEW
echo "/*" >> $NEW
echo $EO " * Added by Customize on $EC" >> $NEW
date >> $NEW
echo " */" >> $NEW
echo "" >> $NEW

# Change HASSECURITY, as required.

echo "#undef	HASSECURITY" >> $NEW
if test $NSEC -eq 1
then
  echo "#define	HASSECURITY 1" >> $NEW
fi

# Change WARNDEVACCESS, as required.

echo "#undef	WARNDEVACCESS" >> $NEW
if test $NWDA -eq 1
then
  echo "#define	WARNDEVACCESS 1" >> $NEW
fi

# Change WARNINGSTATE, as required.

echo "#undef	WARNINGSTATE" >> $NEW
if test $NWST -eq 0
then
  echo "#define	WARNINGSTATE	1" >> $NEW
fi

# Change device cache definitions, as required.

if test \( $NDC -eq 1 -a $DC -eq 0 \) -o \( $DC -eq 1 -a $CDC -eq 1 \)
then
  if test "X$ENV" = "X" -a "X$PDC" = "X" -a "X$SDC" = "X"
  then
    cat << .CAT_MARK

FATAL: HASDCACHE is defined, but there is no definition for
       HASENVDC, or HASPERSDC, or HASSYSDC

       No new machine.h has been created.

       Customize quits.

       Restart Customize and define at least one of HASENVDC,
       HASPERSDC, or HASSYSDC.

.CAT_MARK

    rm -f $NEW
    exit 1
  fi
fi
if test $NDC -eq 0 -o \( $NDC -eq 1 -a $DC -eq 1 -a $CDC -eq 1 \)
then
  echo "#undef	HASDCACHE" >> $NEW
  echo "#undef	HASENVDC" >> $NEW
  echo "#undef	HASPERSDC" >> $NEW
  echo "#undef	HASPERSDCPATH" >> $NEW
  echo "#undef	HASSYSDC" >> $NEW
fi
if test $NDC -eq 1
then
  echo "#undef	HASDCACHE" >> $NEW
  echo "#define	HASDCACHE 1" >> $NEW
  if test "X$ENV" = "X1"
  then
    echo "#define	HASENVDC \"$ENVV\"" >> $NEW
  fi
  if test "X$PDC" = "X1"
  then
    echo "#define	HASPERSDC \"$PDCV\"" >> $NEW
  fi
  if test "X$PDCP" = "X1"
  then
    echo "#define	HASPERSDCPATH \"$PDCPV\"" >> $NEW
  fi
  if test "X$SDC" = "X1"
  then
    echo "#define	HASSYSDC \"$SDCV\"" >> $NEW
  fi
fi

# Change HASXOPT_ROOT, as required.

if test $HXRC -eq 1
then
  if test $HXRS -eq 1
  then
    echo "#undef	HASXOPT_ROOT" >> $NEW
  else
    echo "#define	HASXOPT_ROOT	1" >> $NEW
  fi
fi

# Replace the current machine.h with the new one, as requested.

echo ""
echo "====================================================================="
echo ""
echo "A new $OLD file has been created in \"$NEW\"."
END=0
while test $END = 0
do
  echo ""
  echo "Do you want to rename $OLD to ${OLD}.old and replace it with"
  echo $EO "$NEW (y/n)? $EC"
  read ANS EXCESS
  if test "X$ANS" = "Xn" -o "X$ANS" = "XN"
  then
    END=1
  else
    if test "X$ANS" = "Xy" -o "X$ANS" = "XY"
    then
      rm -f ${OLD}.old
      mv $OLD ${OLD}.old
      mv $NEW $OLD
      END=1
    else
      echo ""
      echo "Please answer y or n."
    fi
  fi
done
exit 0
