#!/bin/sh
##
##  soe.sh -- Systems of Engagement (SoE) utility
##

if [ $# -eq 0 ]; then
    echo "USAGE: soe config"
    echo "USAGE: soe start"
    echo "USAGE: soe stop"
    echo "USAGE: soe suspend"
    echo "USAGE: soe resume"
    exit 0
fi

nodeinfo () {
    nodetype="unknown"
    nodeaddr=""
    hostname=`hostname`
    i=0
    while [ $i -lt 100 ]; do
        h=`@l_prefix@/bin/yaml get @l_prefix@/etc/soe/soe.yaml "platform.nodes.$i.host"`
        if [ ".h" = ".$hostname" ]; then
            nodetype="platform"
            a1=`@l_prefix@/bin/yaml get @l_prefix@/etc/soe/soe.yaml "platform.nodes.$i.addr.ext"`
            a2=`@l_prefix@/bin/yaml get @l_prefix@/etc/soe/soe.yaml "platform.nodes.$i.addr.int"`
            nodeaddr="$a1,$a2"
            break
        fi
        i=`expr $i + 1`
    done
    if [ ".$type" = ".unknown" ]; then
        i=0
        while [ $i -lt 100 ]; do
            h=`@l_prefix@/bin/yaml get @l_prefix@/etc/soe/soe.yaml "farm.nodes.$i.host"`
            if [ ".h" = ".$hostname" ]; then
                nodetype="farm"
                a1=`@l_prefix@/bin/yaml get @l_prefix@/etc/soe/soe.yaml "farm.nodes.$i.addr.ext"`
                a2=`@l_prefix@/bin/yaml get @l_prefix@/etc/soe/soe.yaml "farm.nodes.$i.addr.int"`
                nodeaddr="$a1,$a2"
                break
            fi
            i=`expr $i + 1`
        done
    fi
    echo "$nodetype,$nodeaddr"
}

cmd="$1"
shift
case "$cmd" in
    status )
        #   show host status
        echo "++ SoE: host status"
        nodeinfo=`nodeinfo`
        nodetype=`echo "$nodeinfo" | sed -e 's;,.*$;;'`
        nodeaddrext=`echo "$nodeinfo" | sed -e 's;^[^,]*,;;' -e 's;,.*$;;'`
        nodeaddrint=`echo "$nodeinfo" | sed -e 's;^[^,]*,[^,]*,;;'`
        echo "-- hostname: `hostname`"
        echo "-- nodetype: $nodetype"
        echo "-- address:  $nodeaddrext (external)"
        echo "-- address:  $nodeaddrint (internal)"
        ;;

    config )
        #   configure all services
        echo "++ SoE: edit configutation"
        ${EDITOR-vi} @l_prefix@/etc/soe/soe.yaml
        nodeinfo=`nodeinfo`
        nodetype=`echo "$nodeinfo" | sed -e 's;,.*$;;'`
        nodeaddrext=`echo "$nodeinfo" | sed -e 's;^[^,]*,;;' -e 's;,.*$;;'`
        nodeaddrint=`echo "$nodeinfo" | sed -e 's;^[^,]*,[^,]*,;;'`
        if [ ".$type" = .unknown ]; then
            echo "soe: ERROR: unable to determine SoE type of current host" 1>&2
            exit 1
        fi
        tmpfile1=@l_prefix@/var/soe/tmp/soe.$$.1
        tmpfile2=@l_prefix@/var/soe/tmp/soe.$$.2
        (    @l_prefix@/bin/yaml2json <@l_prefix@/etc/soe/soe.yaml
             echo ""
             echo "host:"
             echo "    arch:     `uname -m`"
             echo "    os:       `uname -s`"
             echo "    nodetype: $nodetype"
             echo "    hostname: `hostname`"
             echo "    addr:"
             echo "        ext: $nodeaddrext"
             echo "        int: $nodeaddrint"
        ) >$tmpfile1
        echo "++ SoE: (re)configuring all services"
        for name in `grep "^<file" @l_prefix@/etc/soe/soe.tmpl | sed -e 's;^.*name=";;' -e 's;".*$;;'`; do
            name_escaped=`echo "$name" | sed -e 's;/;\\\\/;g'`
            (echo ""; cat @l_prefix@/etc/soe/soe.tmpl; echo "") |\
                sed -e "1,/^<file name=\"$name_escaped\"/d" -e "/<\/file>/,\$d" >$tmpfile2
            nt=`grep "^<file name=\"$name_escaped\"" @l_prefix@/etc/soe/soe.tmpl | sed -e 's;^.*nodetype=";;' -e 's;".*$;;'`
            if [ ".$nt" = ".$nodetype" ]; then
                echo "-- generating: $name"
                case "$name" in
                    @l_prefix@/etc/* ) ;;
                    * ) echo "soe: ERROR: invalid configuration file path \"$name\"" 1>&2; exit 1 ;;
                esac
                @l_prefix@/bin/njx -d $tmpfile1 -t $tmpfile2 >$name
            fi
        done
        rm -f $tmpfile1
        rm -f $tmpfile2
        ;;

    start )
        #   start all services
        echo "++ SoE: starting all services"
        @l_prefix@/bin/openpkg rc all start
        ;;

    suspend )
        #   suspend all services
        echo "++ SoE: suspending all services"
        #   FIXME
        ;;

    resume )
        #   resume all services
        echo "++ SoE: resuming all services"
        #   FIXME
        ;;

    stop )
        #   stop all services
        echo "++ SoE: stopping all services"
        @l_prefix@/bin/openpkg rc all stop
        ;;
esac

