#!/bin/csh -f
#Tag 1024

# IncreaseVSwap: This script when executed as root, verifies that the current
#                size of virtual swap space is at least as large as the sum
#                of the size of main memory and physical swap space. If this
#                is not true, it will be made true.
#
#    1) Verify that the IncreaseVSwap script is being run as root:
#
#       If the script is not being run as root, inform the user that the
#       IncreaseVSwap script must be run as root in order to change the
#       size of the virtual swap space. Prompt them for the root password
#       (via su) and execute IncreaseVSwap in a root subshell. If the user
#       fails to specify the root password in 3 attempts, ask them to try
#       again later and exit.
#
#    2) Verify that the machine is running an acceptable version of the
#       operating system:
#
#       Any machine running a variant of IRIX 4 is not supported.
#
#    3) Conditionally enable virtual swap:
#
#       If the machine is running an acceptable operating system (verified
#       above) and the vswap chkconfig option (enable or disable virtual swap)
#       is off, enable it. Maybe, I should ask them if they want vswap enabled,
#       but they ran this script. That's enough of an affirmative response for
#       me.
#
#    4) Determine the optimal size of virtual swap space:
#
#       Add the size of main memory (in blocks) and the total size of phyical
#       swap space (in blocks) to determine an optimum value for the size of
#       virtual swap space. All this to convince the system that we have more
#       swap space than physical resources so that the Alias software can
#       perform safely without depleting system resources.
#
#    5) Verify the existence of the virtual swap configuration file
#       (/etc/config/vswap.options):
#
#       If the config file for virtual swap (/etc/config/vswap.options) does
#       not exist, create one which sets the size of virtual swap space to zero.
#
#    6) Determine how much virtual swap space currently exists by scanning
#       the output of `swap -l'.
#
#    7) Verify that the value of `vswaplen' (size of virtual swap space) in the
#       the virtual swap config file is at least as big as the optimum length of
#       virtual swap:
#
#       If the length of virtual swap space in the virtual swap config file is
#       less than the optimal size, change the value of `vswaplen' in the config
#       file to the optimum value. This change will take effect with the next
#       so that it will automatically be removed by a machine restart. It will
#       also be a previously non-existent file.
#
#    8) Conditionally, set the current size of virtual swap space to the
#       optimal value:
#
#       If the current size of virtual swap space is smaller than the optimum
#       value, set the size to the optimum value. Determine if the file
#       /.swap.virtual is in use as we want to use it to represent the virtual
#       swap space. If it is in use, delete it from the list of swap files so
#       that it can be reused. If the delete fails, we can't use that filename. 
#       In this case, we have to determine a new name for a new virtual swap
#       region whose size is the difference between the optimal value and the
#       size of the virtual swap region defined by /.swap.virtual.
#
#       Note that the name of the new virtual swap parition will be in /tmp
#       so that it will automatically be removed by a machine restart.
#
#       Add the new partition of virtual swap space (/.swap.virtual or other
#       unused name) to the system. This addition of virtual swap space will
#       bring the amount up to the optimal required value.

set path = (/usr/bsd /usr/bin /usr/sbin /usr/lbin /bin /etc /sbin)

set DLR='$'
set vswapfile = /.swap.virtual # MUST be in / or code below will break
set VSWCFG = /etc/config/vswap.options # Virtual Swap config file

#    Verify that the IncreaseVSwap script is being run as root:
#
#    If the script is not being run as root, inform the user that the
#    IncreaseVSwap script must be run as root in order to change the size
#    of the virtual swap space. Prompt them for the root password (via su)
#    and execute IncreaseVSwap in a root subshell. If the user fails to
#    specify the root password in 3 attempts, ask them to try again later
#    and exit.

set whoami = `id | cut -c6-11`
if ("$whoami" != "(root)") then
    echo ""
    echo ""
    echo "In order to increase the size of virtual swap space on this machine, this"
    echo "script must be run as root. At this time, you may specify the root password"
    echo "to continue with the script. If your machine does not have a root password,"
    echo "you will not be prompted and the script will continue. If you don't know"
    echo "the root password, you will have to find someone who does before the size"
    echo "of virtual swap can be increased."
    echo ""

    @ i = 1
    while ($i <= 3)
        su - root -c $0
        if ($status == 0) exit 0
        @ i++
    end
    echo ""
    echo "Three attempts to specify the root password have failed. Please try again"
    echo "later."
    echo ""
    echo -n "Press <ENTER> to continue: "
    set a = $<
    exit 1
    exit 1
endif

#    If the ``-i'' command flag was specified, it indicates that the script
#    is being called from InstallAlias.

if ($#argv == 1) then
    if ("$1" == "-i") set installing
endif

#    Verify that the machine is running an acceptable version of the
#    operating system. Any machine running a variant of IRIX 4 is
#    not supported.

set fdig = `uname -r | cut -c1`
if ($fdig == "4") then
    echo ""
    echo ""
    echo "The size of virtual swap space cannot be increased on this machine as all"
    echo "versions of IRIX 4 do not support the concept of virtual swap space."
    echo ""
    echo -n "Press <ENTER> to continue: "
    set a = $<
    exit
endif

#    Conditionally enable virtual swap:
#
#    If the machine is running an acceptable operating system (verified
#    above) and the vswap chkconfig option (enable or disable virtual swap)
#    is off, enable it. Maybe, I should ask them if they want vswap enabled,
#    but they ran this script. That's enough of an affirmative response for me.

chkconfig vswap
if ($status != 0) then
    chkconfig -f vswap on
endif

#    Add the size of main memory (in blocks) and the total size of phyical swap
#    space (in blocks) to determine an optimum value for the size of virtual
#    swap space.

set mmemMb = `hinv -t memory | awk '{print $4}'`
@ mmemBl = $mmemMb * 2000
set swspBl = `swap -l | awk 'BEGIN {sw = 0} {sw += $4} END {print sw}'`
@ totBl = $mmemBl + $swspBl

#    Round total Blocks up to the nearest Megabyte
#
#    There are 1000 * 2 (blocks per kilobyte) blocks in a megabyte.

@ MbBl = 1000 * 2
@ totBl = (($totBl + ($MbBl - 1)) / $MbBl) * $MbBl

#    Now that we have the optimum size for virtual swap space, we may decide
#    to check for any mounted swap partitions which are acting as virtual swap
#    space. We elect not to do this as there is no harm having too much virtual
#    swap space.

#    If the options file for virtual swap (/etc/config/vswap.options) does
#    not exist, create one which sets the size of virtual swap space to zero.

if (! -f $VSWCFG) then
    cat >> $VSWCFG << EOF
#
# This file is used to the startup script /etc/init.d/swap
# to determine the amount of virtual swap to add. You can
# totally disable the addition of virtual swap by
# 'chkconfig vswap off'
# See swap(1M) for a discussion on virtual swap
#
vswaplen=0
EOF
endif

#    Determine how much virtual swap space currently exists by scanning
#    the output of `swap -l'.

set currvs = `swap -l | awk 'BEGIN {vsw = 0} {vsw += $7} END {print vsw}'`

#    Verify that the value of `vswaplen' in the the vswap config file is
#    at least as big as the optimum length of virtual swap.

set vslen = `grep "^vswaplen=" $VSWCFG | awk -F= '{print $2}'`
if ($#vslen == 0) then
    set vslen = 0
else if ($#vslen > 1) then
    set vslen = $vslen[$#vslen]
endif

#    If the length of virtual swap space in the vswap config file is less
#    than the optimal size, change the value of `vswaplen' in the config
#    file to the optimum value. This change will take effect with the
#    next system restart.

if ($vslen < $totBl) then
    ex - $VSWCFG << EOF
    ${DLR}c
vswaplen=$totBl
.
    w!
    q
EOF
endif

#    If the current size of virtual swap space is smaller than the optimum
#    value, set the size to the optimum value.

echo ""
if ("$currvs" < "$totBl") then

#    Determine if the file /.swap.virtual is in use as we want to use it
#    to represent the virtual swap space.
#
#    If it is in use, delete it from the list of swap files so that it can
#    be reused. If the delete fails, we can't use that filename. In this
#    case, we have to determine a new name for a new virtual swap region
#    whose size is the difference between the optimal value and the size
#    of the virtual swap region defined by /.swap.virtual.
#
#    Note that the name of the new virtual swap parition will be in /tmp
#    so that it will automatically be removed by a machine restart. It will
#    also be a previously non-existent file.

    swap -l | grep -s " $vswapfile${DLR}" >& /dev/null
    if ($status == 0) then
        swap -d $vswapfile >& /dev/null
        if ($status != 0) then
            @ cvs = `swap -lnb | grep " $vswapfile " | awk '{print $7}'`
            @ vsincr = $totBl - $cvs
    	    @ vsi = 0
            while (1)
                set nvsp = /tmp${vswapfile}${vsi}
                swap -l | grep -s " ${nvsp}${DLR}" >& /dev/null
                if ($status != 0) then
                    if (! -e $nvsp) then
                        set vswapfile = $nvsp
                        break
                    endif
                endif
                @ vsi++
            end
        else
            @ vsincr = $totBl
        endif
    else
        @ vsincr = $totBl
    endif
    @ newsz = $totBl / 2000
    if ("$currvs" == 0) then
        echo "Setting size of virtual swap space to $totBl blocks ($newsz megabytes)."
    else
        @ oldsz = $currvs / 2000
        echo "Increasing size of virtual swap space from $currvs blocks to $totBl blocks"
        echo "($newsz megabytes)."
    endif

#    Add the new partition of virtual swap space (/.swap.virtual or other
#    unused name) to the system. This addition of virtual swap space will
#    bring the amount up to the optimal required value.

    touch $vswapfile
    swap -a -v${vsincr} ${vswapfile} 0 0
else
    echo "The size of virtual swap space is already at (or above) the optimal value"
    echo "($totBl blocks)."
endif
echo ""

#    If this script is not being invoked from InstallAlias, prompt the user.
#    This is necessary when this script is invoked from Indigo Magic and
#    prevents the resulting window from closing before the user has a chance
#    to read the information. It is not necessary when called from InstallAlias.

if (! $?installing) then
    echo -n "Press <ENTER> to continue: "
    set a = $<
endif
