#!/bin/sh
#
# This script is (C) 1999-2000 Riccardo Facchetti.
#
# A big thank you to SuSE people that send me, as a gift, their distribution.
# This enabled me to test this script with SuSE 6.0 and will test with 6.2
# ASAP (just arrived on my desk, I need time to upgrade 6.0 to 6.2).
#
# Modified by Kaspar P. Klingholz for handling `exit 0' at the end of
# halt.local

MAGICWORD="XXX APCUPSD - DO NOT EDIT XXX"
prefix=@prefix@                         # $MAGICWORD
exec_prefix=@exec_prefix@               # $MAGICWORD
APCCONTROL="@sysconfdir@/apccontrol killpower"

case "$1" in
install)

if [ -f /etc/rc.d/halt.local ]
then
# Check if it is not already installed.
        RESULT=`grep "$MAGICWORD" /etc/rc.d/halt.local`
        if [ -z "$RESULT" ]
        then
                # check for last line is "exit 0" as in SuSE 5.2
                RESULT=`tail -1 /etc/rc.d/halt.local`
                if [ "$RESULT" = "exit 0" ]
                then
                        echo "Last line in /etc/rc.d/halt.local is \"exit 0\""
                        NUM=`grep -c "" /etc/rc.d/halt.local`
                        NUM="$(echo $NUM | \
                        awk '{print $1 -1}')"
                        head -$NUM /etc/rc.d/halt.local > /etc/rc.d/halt.local.tmp
                        mv /etc/rc.d/halt.local.tmp /etc/rc.d/halt.local
                        chmod u+x /etc/rc.d/halt.local
                        echo "... removed"
                fi
                echo "Adding apcupsd routines to local halt script..."
                cat >> /etc/rc.d/halt.local <<EOF
# Added automatically by apcupsd installation       # $MAGICWORD
if [ -f @PWRFAILDIR@/powerfail ]; then              # $MAGICWORD
    echo "Power down detected while shutting down." # $MAGICWORD
    echo "Sending all processes the TERM signal..." # $MAGICWORD
    killall5 -15                                    # $MAGICWORD
#   Do a fast kill                                  # $MAGICWORD
    sleep 1                                         # $MAGICWORD
    echo "Sending all processes the KILL signal..." # $MAGICWORD
    killall5 -9                                     # $MAGICWORD
    echo "Remounting filesystems read-only."        # $MAGICWORD
    mounts=/etc/fstab                               # $MAGICWORD
    test -r /proc/mounts && mounts=/proc/mounts     # $MAGICWORD
    while read des fs rest; do                      # $MAGICWORD
        mount -v -n -o remount,ro \$fs              # $MAGICWORD
    done < \$mounts                                 # $MAGICWORD
    echo "Preparing UPS to power shutdown."         # $MAGICWORD
    $APCCONTROL                                     # $MAGICWORD
fi                                                  # $MAGICWORD
EOF
        else
                echo "No need to update local halt script."
        fi
else
        if [ -f /etc/rc.d/halt.O ]
        then
                echo "Halt script alredy backed up..."
        else
                echo "Saving old halt script..."
                cp /etc/rc.d/halt /etc/rc.d/halt.O
        fi
        echo "Installing apcupsd halt routines..."
        cp halt /etc/rc.d/halt
fi
;;
uninstall)
if [ -f /etc/rc.d/halt.local ]
then
        RESULT=`grep "$MAGICWORD" /etc/rc.d/halt.local`
        if [ -z "$RESULT" ]
        then
                echo "apcupsd halt routines not installed."
        else
                echo "Removing apcupsd routines from local halt script..."
                grep -v "$MAGICWORD" /etc/rc.d/halt.local > /etc/rc.d/halt.local.tmp
                mv /etc/rc.d/halt.local.tmp /etc/rc.d/halt.local
        fi
else
        if test -f /etc/rc.d/halt.O
        then
                echo "Restoring default halt script..."
                cp /etc/rc.d/halt.O /etc/rc.d/halt
        else
                echo "Can not find original halt script: leaving apcupsd halt in place."
        fi
fi
;;
esac


syntax highlighted by Code2HTML, v. 0.9.1