#!/bin/bash

########################################################################
#
#PROJECT:      Unified Sessions Manager
#AUTHOR:       Arno-Can Uestuensoez - acue@UnifiedSessionsManager.org
#MAINTAINER:   Arno-Can Uestuensoez - acue_sf1@sourceforge.net
#SHORT:        ctys
#CALLFULLNAME: Commutate To Your Session
#LICENCE:      GPL3
#VERSION:      01_02_007a17
#
########################################################################
#
# Copyright (C) 2007 Arno-Can Uestuensoez (UnifiedSessionsManager.org)
#
########################################################################

_myPKGNAME_XEN_ENUMERATE="${BASH_SOURCE}"
_myPKGVERS_XEN_ENUMERATE="01.01.001a01"
hookInfoAdd $_myPKGNAME_XEN_ENUMERATE $_myPKGVERS_XEN_ENUMERATE
_myPKGBASE_XEN_ENUMERATE="`dirname ${_myPKGNAME_XEN_ENUMERATE}`"


#FUNCBEG###############################################################
#NAME:
#  enumerateMySessionsXEN
#
#TYPE:
#  bash-function
#
#DESCRIPTION:
#  Enumerates all XEN sessions, therefore the vmx-files will be scanned
#  and the matched attributes displayed.
#
#  For detailed interface descritpion refer to genric dispatcher.
#
#EXAMPLE:
#
#PARAMETERS:
#  $1: For detailed interface descritpion refer to genric dispatcher.
#
#GLOBALS:
#  CTRL_TERSE
#    Where <dname> is literally equal to the result of call 
#    "fetchLabel4ID", and <filename/ID> is literally equal to 
#    the result of call "fetchID4Label".
#
#    off   Formatted display-output, format:
#
#          Label       =>  ID/vmx-file  
#          --------------------------------
#          "<dname>    =>  <filename/ID>"
#
#    on    Formatted machine-output, format:
#
#          "<dname>:<filename/ID>"
#
#
#OUTPUT:
#  RETURN:
#
#  VALUES:
#
#
#FUNCEND###############################################################
function enumerateMySessionsXEN () {
    printVerboseDebug ${DBG_LVL_UID} $LINENO $BASH_SOURCE "$FUNCNAME:\$@=${@}"

    #avoids of multiple rescans when MAPDB enabled.
    local _curMACCache=;

    #UUID
    local _base="`echo ${1}|sed 's/[uU][uU][iI][dD]//'`"
    if [ "${1}" != "${_base}" ];then local _uuid=1;fi

    #IP
    local _base1="`echo ${_base}|sed 's/[iI][pP]//'`"
    if [ "${_base1}" != "${_base}" ];then local _ip=1;fi

    #MAC
    _base="`echo ${_base1}|sed 's/[mM][aA][cC][^hH]//'`"
    if [ "${_base1}" != "${_base}" ];then local _mac=1;fi

    #TERSE
    _terse=$CTRL_TERSE;
    _base1="`echo ${_base}|sed 's/[tT][eE][rR][sS][eE]//'`"
    if [ "${_base1}" != "${_base}" ];then local _terse=1;fi

    #LABEL
    _base="`echo ${_base1}|sed 's/[lL][aA][bB][eE][lL]//'`"
    if [ "${_base1}" != "${_base}" ];then local _label=1;fi

    #PNAME
    _base1="`echo ${_base}|sed 's/[pP][nN][aA][mM][eE]//'`"
    if [ "${_base}" != "${_base1}" ];then local _pname=1;fi

    #TYPE
    _base2="`echo ${_base1}|sed 's/[tT][yY][pP][eE]//'`"
    if [ "${_base2}" != "${_base1}" ];then local _type=1;fi

    #ALL
    local _base1="`echo ${_base2}|sed 's/[aA][lL][lL]//'`"
    if [ "${_base2}" != "${_base1}" ];then local _all=1;fi

    #MACHINE
    local _base2="`echo ${_base1}|sed 's/[mM][aA][cC][hH][iI][nN][eE]//'`"
    if [ "${_base2}" != "${_base1}" ];then local _all=1;local _terse=1;fi

    #DISTRO
    local _base1="`echo ${_base2}|sed 's/[dD][iI][sS][tT][rR][oO]//'`"
    if [ "${_base2}" != "${_base1}" ];then local _distro=1;fi

    #OS
    local _base2="`echo ${_base1}|sed 's/[oO][sS]//'`"
    if [ "${_base2}" != "${_base1}" ];then local _os=1;fi

    #VERNO
    local _base1="`echo ${_base2}|sed 's/[vV][eE][rR][nN][oO]//'`"
    if [ "${_base2}" != "${_base1}" ];then local _verno=1;fi

    #SERNO
    local _base2="`echo ${_base1}|sed 's/[sS][eE][rR][nN][oO]//'`"
    if [ "${_base2}" != "${_base1}" ];then local _serno=1;fi

    #CATEGORY
    local _base1="`echo ${_base2}|sed 's/[cC][aA][tT][eE][gG][oO][rR][yY]//'`"
    if [ "${_base2}" != "${_base1}" ];then local _category=1;fi

    #VNCPORT
    local _base2="`echo ${_base1}|sed 's/[vV][nN][cC][pP][oO][rR][tT]//'`"
    if [ "${_base2}" != "${_base1}" ];then local _vncport=1;fi

    #VNCBASE
    local _base1="`echo ${_base2}|sed 's/[vV][nN][cC][bB][aA][sS][eE]//'`"
    if [ "${_base2}" != "${_base1}" ];then local _vncbase=1;fi

    #VNCDISPLAY
    local _base2="`echo ${_base1}|sed 's/[vV][nN][cC][dD][iI][sS][pP][lL][aA][yY]//'`"
    if [ "${_base2}" != "${_base1}" ];then local _vncdisp=1;fi

    #ID
    local _base1="`echo ${_base2}|sed 's/[iI][dD]//'`"
    if [ "${_base2}" != "${_base1}" ];then local _id=1;fi

    #HOST
    local _base2="`echo ${_base1}|sed 's/[hH][oO][sS][tT]//'`"
    if [ "${_base2}" != "${_base1}" ];then local _host=1;fi

    #MAPDB
    local _base1="`echo ${_base2}|sed 's/[mM][aA][pP][dD][bB]//'`"
    if [ "${_base2}" != "${_base1}" ];then local _mapdb=1;fi

    #MAXKEY
    local _base2="`echo ${_base1}|sed 's/[mM][aA][xX][kK][eE][yY]//'`"
    if [ "${_base2}" != "${_base1}" ];then local _maxkey=1;local _terse=1;CTRL_TERSE=$_terse;fi


    #update these two _smartXXX when extending above list!
    local _smartLnk=$_base2
    local _smartSet=$_base1

    #quick-shot too for basepath, and nothing else
    local _iba="";
    _smartSet=${_smartLnk//,/ };
    _base="";


    if [ -n "$_all" ];then
	_uuid=1;_ip=1;_mac=1;_label=1;_pname=1;_type=1;
        _distro=1;_os=1;_verno=1;_serno=1;_category=1;
        _vncport=1;_vncbase=1;_vncdisp=1;_id=1;_host=1;
    fi

    if [ -n "$_maxkey" ];then
        _host=1;_type=1;_label=1;_id=1;_uuid=1;_mac=1;

	_ip=0;_pname=0;
        _distro=0;_os=0;_verno=0;_serno=0;_category=0;
        _vncport=0;_vncbase=0;_vncdisp=0;
    fi

    for _iba in ${_smartSet};do
	case ${_iba} in
	    [bB]:*|[bB][aA][sS][eE]:*|[bB][aA][sS][eE][pP][aA][tT][hH]:*)   
		_base="${_base} ${_iba#*:}";
		;;
            [tT][iI][tT][lL][eE]);;#temp workaround
            *)
 		ABORT=1
 		printERR $LINENO $BASH_SOURCE ${ABORT} "Unknown constraint for <vm-name> element.";
 		printERR $LINENO $BASH_SOURCE ${ABORT} "  Given systax    :<${_iba}>";
		gotoHell ${ABORT}
		;;
	esac
    done

    _base=${_base/,*};_base=${_base//\%/ };
    _base=${_base:-$DEFAULT_ENUM_BASE}
    printVerboseDebug ${DBG_LVL_UID} $LINENO $BASH_SOURCE "$FUNCNAME:_base=${_base}"

    #just for sureness ...
    if [ -z "${_base}" ];then
 	ABORT=1
 	printERR $LINENO $BASH_SOURCE ${ABORT} "Missing base for search: Check your path/file call-parameters.";
	gotoHell ${ABORT}
    fi

    #ID/PNAME should be shown as unique and unambiguos ID, ready to be used
    #Make each base absolut, if not yet
    local _i2=;
    local X=;
    local _baseabs=;
    for _i2 in ${_base};do
	if [ "${_i2#/}" == "${_i2}" ];then
	    _baseabs="${_baseabs} ${PWD}/${_i2}"
	else
	    _baseabs="${_baseabs} ${_i2}"
	fi
    done
    _baseabs="${_baseabs## }"
    printVerboseDebug ${DBG_LVL_UID} $LINENO $BASH_SOURCE "$FUNCNAME:_baseabs=${_baseabs}"
    if [ ! -f "${_myPKGBASE_XEN_ENUMERATE}/enumfilter.awk" ];then
 	ABORT=1
 	printERR $LINENO $BASH_SOURCE ${ABORT} "Missing:${_myPKGBASE_XEN_ENUMERATE}/enumfilter.awk";
	gotoHell ${ABORT}
    fi
    printVerboseDebug ${DBG_LVL_MAINT} $LINENO $BASH_SOURCE "$FUNCNAME:MATCH-FILTER=${_myPKGBASE_XEN_ENUMERATE}/enumfilter.awk";
    local _out=;
    {
	find ${_baseabs} -type f -name '*.conf' \
            -exec awk -F'=' -v matchMin=4 -f ${_myPKGBASE_XEN_ENUMERATE}/enumfilter.awk {} \; \
            -print
    }|\
    while read X;do
        _curMACCache="";
	_out=;
    	printVerboseDebug ${DBG_LVL_UI} $LINENO $BASH_SOURCE "$FUNCNAME:MATCH=${X}"
 	if [ -f "${X}" ];then

            #host
	    if [ -n "${_terse}" ];then
		if [ -n "${_host}" ];    then _out="${MYHOST};"; fi
            fi

            #mandatory parts, which are known in any case
	    if [ -z "${_terse}" ];then
		printf "PNAME = %s\n" "${X}";
		if [ -n "${_type}" ]; then printf "   TYPE       = XEN\n"; fi
	    else
		_out="${_out}XEN";
            fi

            #optional parts
	    if [ ! -r "${X}" ];then
		if [ -z "${_terse}" ];then
		    if [ -n "${_host}" ];     then printf "   HOST       = %s\n" "${NOREAD}"; fi
		    if [ -n "${_label}" ];    then printf "   LABEL      = %s\n" "${NOREAD}"; fi
		    if [ -n "${_id}" ];       then printf "   ID         = %s\n" "${NOREAD}"; fi
		    if [ -n "${_uuid}" ];     then printf "   UUID       = %s\n" "${NOREAD}"; fi
		    if [ -n "${_mac}" ];      then printf "   MAC        = %s\n" "${NOREAD}"; fi
		    if [ -n "${_ip}" ];       then printf "   IP         = %s\n" "${NOREAD}"; fi
		    if [ -n "${_vncport}" ];  then printf "   VNCPORT    = %s\n" "${NOREAD}"; fi
		    if [ -n "${_vncbase}" ];  then printf "   VNCBASE    = %s\n" "${NOREAD}"; fi
		    if [ -n "${_vncdisp}" ];  then printf "   VNCDISPLAY = %s\n" "${NOREAD}"; fi
		    if [ -n "${_distro}" ];   then printf "   DISTRO     = %s\n" "${NOREAD}"; fi
		    if [ -n "${_os}" ];       then printf "   OS         = %s\n" "${NOREAD}"; fi
		    if [ -n "${_verno}" ];    then printf "   VERNO      = %s\n" "${NOREAD}"; fi
		    if [ -n "${_serno}" ];    then printf "   SERNO      = %s\n" "${NOREAD}"; fi
		    if [ -n "${_category}" ]; then printf "   CATEGORY   = %s\n" "${NOREAD}"; fi
		else
		    if [ -n "${_label}" ];    then _out="${_out};";  fi
		    if [ -n "${_id}" ];       then _out="${_out};";  fi
		    if [ -n "${_uuid}" ];     then _out="${_out};";  fi
		    if [ -n "${_mac}" ];      then _out="${_out};";  fi
		    if [ -n "${_ip}" ];       then _out="${_out};";  fi
		    if [ -n "${_vncport}" ];  then _out="${_out};";  fi
		    if [ -n "${_vncbase}" ];  then _out="${_out};";  fi
		    if [ -n "${_vncdisp}" ];  then _out="${_out};";  fi
		    if [ -n "${_distro}" ];   then _out="${_out};";  fi
		    if [ -n "${_os}" ];       then _out="${_out};";  fi
		    if [ -n "${_verno}" ];    then _out="${_out};";  fi
		    if [ -n "${_serno}" ];    then _out="${_out};";  fi
		    if [ -n "${_category}" ]; then _out="${_out};";  fi
                    echo "${_out}"
                fi
	    else
		if [ -z "${_terse}" ];then
		    if [ -n "${_host}" ];     then printf "   HOST       = ${MYHOST}\n";          fi
		    if [ -n "${_label}" ];    then printf "   LABEL      = `getLABEL_XEN ${X}`\n"; fi
		    if [ -n "${_id}" ];       then printf "   ID         = ${NOAPP}          \n"; fi
		    if [ -n "${_uuid}" ];     then printf "   UUID       = `getUUID_XEN     ${X}`\n"; fi
		    if [ -n "${_mac}" ];      then printf "   MAC        = `getMAC_XEN      ${X}`\n"; fi
		    if [ -n "${_ip}" ];       then printf "   IP         = `getIP       ${X}`\n"; fi
		    if [ -n "${_vncport}" ];  then printf "   VNCPORT    = ${NOAPP}\n"; fi
		    if [ -n "${_vncbase}" ];  then printf "   VNCBASE    = ${NOAPP}          \n"; fi
		    if [ -n "${_vncdisp}" ];  then printf "   VNCDISPLAY = ${NOAPP}          \n"; fi
		    if [ -n "${_distro}" ];   then printf "   DISTRO     = `getDIST     ${X}`\n"; fi
		    if [ -n "${_os}" ];       then printf "   OS         = `getOS       ${X}`\n"; fi
		    if [ -n "${_verno}" ];    then printf "   VERNO      = `getVERNO    ${X}`\n"; fi
		    if [ -n "${_serno}" ];    then printf "   SERNO      = `getSERNO    ${X}`\n"; fi
		    if [ -n "${_category}" ]; then printf "   CATEGORY   = `getCATEGORY ${X}`\n"; fi
		else
		    if [ -n "${_label}" ];    then _out="${_out};`getLABEL_XEN    ${X}`";  fi
		    if [ -n "${_id}" ];       then _out="${_out};${X}";  fi
		    if [ -n "${_uuid}" ];     then _out="${_out};`getUUID_XEN     ${X}`";  fi
		    if [ -n "${_mac}" ];      then _out="${_out};`getMAC_XEN      ${X}`";  fi
		    if [ -n "${_ip}" ];       then _out="${_out};`getIP       ${X}`";  fi
		    if [ -n "${_vncport}" ];  then _out="${_out};";  fi
		    if [ -n "${_vncbase}" ];  then _out="${_out};";  fi
		    if [ -n "${_vncdisp}" ];  then _out="${_out};";  fi
		    if [ -n "${_distro}" ];   then _out="${_out};`getDIST     ${X}`"; fi
		    if [ -n "${_os}" ];       then _out="${_out};`getOS       ${X}`"; fi
		    if [ -n "${_verno}" ];    then _out="${_out};`getVERNO    ${X}`"; fi
		    if [ -n "${_serno}" ];    then _out="${_out};`getSERNO    ${X}`"; fi
		    if [ -n "${_category}" ]; then _out="${_out};`getCATEGORY ${X}`"; fi
                    echo "${_out}"
		fi
	    fi
	fi
    done
}
