#!/bin/sh
#   /***************************************************************************
#   **                                                                        **
#   **  License                                                               **
#   **  =======                                                               **
#   **                                                                        **
#   **  This software program is released under the terms of a license        **
#   **  agreement between you ('Licensee') and Intel. Do not use or load this **
#   **  software or any associated materials (collectively, the 'Software')   **
#   **  until you have carefully read the full terms and conditions of the    **
#   **  LICENSE located in this software package. By loading or using the     **
#   **  Software, you agree to the terms of this Agreement. If you do not     **
#   **  agree with the terms of this Agreement, do not install or use the     **
#   **  Software.                                                             **
#   ***************************************************************************/
#
#   /***************************************************************************
#   **                                                                                                                                                                 **
#   ** INTEL CORPORATION                                                                                                                 **
#   **                                                                                                                                                                 **
#   ** This software is supplied under the terms of the license included                                          **
#   ** above.  All use of this software must be in accordance with the terms                                  **
#   ** of that license.                                                                                                                                      **
#   **                                                                                                                                                                  **
#   **  Abstract:                                                                                                                                              **
#   **    Boot Installation script for PROCfgd                                                                                          **
#   **                                                                                                                                                                  **
#   ***************************************************************************/
#
# Install procfgd to load on boot. The exact boot script that is to be installed
# and the boot scripts' directory are chosen according to the Linux distribution.
#
# Usage: INSTALL_BOOT install [overall] - installs procfgd to load on boot
#        INSTALL_BOOT uninstall - remove the procfgd boot script from the system
#
# If the optional "overall" parameter exists (i.e. non-zero in length), it means
# that when copying files existing versions should always be overwritten.

BOOT_SCRIPT="procfgd_init_def"
SCRIPT_DIR="/etc/rc.d/init.d"
RC_DIR="/etc/rc.d"
ret=""
overall="$2"

prompt_yesno() {
    echo -n "$1 [$2] "
    read ret

    if [ -z "$ret" ]; then
        ret="$2"
    else
	flag=0
	while [ $flag -ne 1 ]; do 
            case $ret in
            y* | Y*)
                ret="y"
	        flag=1
                ;;
            n* | N*)
                ret="n"
                flag=1
                ;;
            *)
                echo -n "$1 [$2] "
                read ret
                ;;
            esac
        done 
    fi
}

find_distrib() {
    if [ -f /etc/redhat-release -o -f /etc/turbolinux-release ]; then
        BOOT_SCRIPT="procfgd_init"
        return 0
    fi

    grep "SuSE" /etc/issue > /dev/null 2>&1
    if [ "$?" -eq 0 ]; then
        BOOT_SCRIPT="procfgd_init_suse"
        SCRIPT_DIR="/etc/init.d"
        return 0
    fi

    grep "Caldera" /etc/issue > /dev/null 2>&1
    if [ "$?" -eq 0 ]; then
        BOOT_SCRIPT="procfgd_init_caldera"
        return 0
    fi

    return 1
}

uninstall() {
    for file in $RC_DIR/rc?.d/*procfgd ; do
        /bin/rm -f $file
    done

    /bin/rm -f $SCRIPT_DIR/procfgd

    return 0
}

install() {
    if [ ! -f $BOOT_SCRIPT ]; then
        echo "Boot script '${BOOT_SCRIPT}' not found"
        exit 1
    fi

    if [ -f $SCRIPT_DIR/procfgd -a -z "$overall" ]; then
        prompt_yesno "Procfgd boot script already installed, overwrite (y/n) ?" "y"
        if [ "$ret" != "y" ]; then
            exit 0
        fi
    fi

    uninstall
    /bin/cp -f $BOOT_SCRIPT $SCRIPT_DIR/procfgd

    for file in $RC_DIR/rc?.d/[SK]*network ; do
        act=`basename $file | sed 's/\([SK]\)[0-9][0-9]network/\1/'`
        i=`basename $file | sed 's/[SK]\([0-9][0-9]\)network/\1/'`

        # force base 10 arithmetics or we might get octal
        i="10#$i"
        if [ "$act" = "S" ]; then
            let i+=1
        else
            let i-=1
        fi

        if [ "$i" -lt 10 ]; then
            i="0$i"
        fi

        link=`dirname $file`
        link="$link/${act}${i}procfgd"
        ln -s $SCRIPT_DIR/procfgd $link
    done

    return 0
}

find_distrib

if [ ! -d $SCRIPT_DIR ]; then
    echo "Script directory $SCRIPT_DIR does not exist"
    exit 1
fi

case "$1" in
    install)
        install
        ;;
    uninstall)
        uninstall
        ;;
    *)
        echo "Usage $0 {install|uninstall}"
        exit 1
esac

