#! /bin/sh

##  bmc-autoconfig: BMC Autoconfiguration Utility
##  authors: Anand Avati <avati@zresearch.com>
##  Copyright 2005 FreeIPMI Core Team 
## 
##  This program is free software; you can redistribute it and/or
##  modify it under the terms of the GNU General Public License as
##  published by the Free Software Foundation; either version 2, or (at
##  your option) any later version.
##  
##  This program is distributed in the hope that it will be useful, but
##  WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
##  General Public License for more details.
##  
##  You should have received a copy of the GNU General Public License
##  along with this program; if not, write to the Free Software
##  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA


_init ()
{
    # autotool'ize these two?
    TEMPLATE_DIR="/var/lib/freeipmi";
    TEMPLATE="${TEMPLATE_DIR}/bmc-config-template";
    VERSION="0.2.0";

    ME=$(basename ${0});

    PROG=$(which dialog 2>/dev/null || which whiptail 2>/dev/null);

    BMC_CONFIG=$(which bmc-config 2>/dev/null) || {
	echo "bmc-config not found, fatal!";
	return 1;
    }
    
    if [ ! -f ${TEMPLATE} ]; then
	echo "${ME}: ${TEMPLATE} file not found"
	return 1;
    fi
    
    exec 3>&1
    
    ip="0.0.0.0";
    nm="0.0.0.0";
    gw="0.0.0.0";

    return 0;
}

bmc_autoconfig_display_usage ()
{
    cat <<EOF
Usage: ${ME} [-?V] [-h IPMIHOST] [-u USERNAME] [-p PASSWORD]
                      [-a AUTHTYPE] [-l PRIVILEGE-LEVEL]
                      [--driver-poll-interval=USEC]
                      [--sms-io-base=SMS-IO-BASE] [--host=IPMIHOST]
                      [--username=USERNAME] [--password=PASSWORD]
                      [--auth-type=AUTHTYPE]
                      [--priv-level=PRIVILEGE-LEVEL] [--help]
                      [--usage] [--version]
EOF
}

bmc_autoconfig_display_help ()
{
    cat <<EOF
Usage: ${ME} [OPTION...] 
${ME} is an autoconfiguration tool and wrapper around bmc-config.

      --driver-poll-interval=USEC
                             User USEC driver poll interval.
      --sms-io-base=SMS-IO-BASE   SMS IO Base address.
  -h, --host=IPMIHOST        Connect to IPMIHOST.
  -u, --username=USERNAME    Use USERNAME instead of NULL.  Maximum USERNAME
                             length is 16.
  -p, --password=PASSWORD    Use PASSWORD instead of NULL.  Maximum PASSWORD
                             length is 16.
  -a, --auth-type=AUTHTYPE   Use AUTHTYPE instead of NONE.  Allowed values are
                             NONE, MD2, MD5, PLAIN and OEM.
  -l, --priv-level=PRIVILEGE-LEVEL
                             Use this PRIVILEGE-LEVEL instead of USER.  Allowed
                             values are CALLBACK, USER, OPERATOR, ADMIN and
                             OEM.
  -?, --help                 Give this help list.
      --usage                Give a short usage message.
  -V, --version              Print program version.

Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.

Report bugs to <freeipmi-devel@gnu.org>.
EOF
}

bmc_autoconfig_display_version ()
{
    cat <<EOF
BMC Auto Configurator [${ME}-${VERSION}]
Copyright (C) 2003-2005 FreeIPMI Core Team
This program is free software; you may redistribute it under the terms of
the GNU General Public License.  This program has absolutely no warranty.
EOF
}

show_message ()
{
    [ -z "${PROG}" ] && {
	echo "${1}";
	read;
	return;
    }
    ${PROG} --title "BMC Auto Config" --msgbox "${1}" 0 0 >&3;
    
    return;
}

get_input ()
{
    local input;
    [ -z "${PROG}" ] && {
	echo -n "${1}";
	read input;
	echo "${input}";
        return;
    }
    ${PROG} --title "BMC Auto Config" --inputbox "$1" 0 0 "$2" 2>&1 >&3;
}

validate_ip_address ()
{
    local quad;
    local oldIFS;

    quad=${1}
    
    echo "${quad}" | grep -Eq "^[0-9\.]+$" || {
	echo "ERROR: Input cannot have non-numericals";
	return 1;
    }

    oldIFS=${IFS};
    IFS=.
    set -- ${quad};
    IFS=${oldIFS};
    
    if [ "$#" -ne "4" ]; then
	echo "ERROR: IP Address needs 4 octets";
	return 1;
    fi
    
    for oct in $*;  
    do
      if [ "${oct}" -lt "0" -o "${oct}" -gt "255" ]; then
	  echo "ERROR: Input octets should be between 0 - 255";
	  return 1
      fi
    done
    
    return 0
}


get_ip_address ()
{
    get_input "Enter BMC IP Address: " "${ip}";
}


get_netmask ()
{
    get_input "Enter BMC Netmask: " "${nm}";
}


get_gateway_ip_address ()
{
    get_input "Enter BMC Gateway: " "${gw}";
}

accept_input ()
{
    iput_func=$1;
    valid_func=$2;
    local err;
    local input;

    input=$(${iput_func}) || return 1;
    while ! err=$(${valid_func} ${input})
    do
      show_message "${err}";
      input=$(${iput_func}) || return 1;
    done

    echo ${input};

    return 0;
}

main ()
{
    case "${1}" in 
	'--usage')
	    bmc_autoconfig_display_usage
	    ;;
	'--help'|'-?')
	    bmc_autoconfig_display_help
	    ;;
	'--version'|'-V')
	    bmc_autoconfig_display_version
	    ;;
	'')
	    ip=$(accept_input get_ip_address validate_ip_address) &&
	    nm=$(accept_input get_netmask validate_ip_address) &&
	    gw=$(accept_input get_gateway_ip_address validate_ip_address) && {
		echo "Configuring BMC now.  This may take awhile..."
		${BMC_CONFIG} --commit -f "${TEMPLATE}";
		${BMC_CONFIG} --commit -k "Lan_Conf:IP_Address=${ip}";
		${BMC_CONFIG} --commit -k "Lan_Conf:Subnet_Mask=${nm}";
		${BMC_CONFIG} --commit -k "Lan_Conf:Default_Gateway_IP_Address=${gw}";
	    }
	    cat <<EOF


BMC configuration is done.

Note:-
Passwords are not set or cleared.  If you want to set password for BMC
user, run

bmc-config --commit -f 'User<N>.Password=<PASSWORD>'

here,

<N>        -- BMC user id.  Normally this is 1 to 4.
<PASSWORD> -- Your new password for the user.

EOF
	    ;;
	*)
	    cat <<EOF
${ME}: invalid option -- ${1}
Try \`${ME} --help' or \`${ME} --usage' for more information.
EOF
	
    esac

}

_init "$@" && main "$@";
