#!/bin/sh
#
# Wrapper for accords commands
#
# Copyright: 
#   Prologue 2011
#   Mandriva 2011
#   Bull 2012
#   CityPassenger 2012
#
# Author: 
#   Jean Parpaillon
#   Jamie Marshall
#   Liliana Cabalantti
#   Vincent Renardias <vincent@renardias.com>
#

#set -e

export PATH=/usr/libexec/accords:/usr/bin:$PATH

confdir=/etc/accords
if test -e ./accords.ini; then
    . ./accords.ini
elif test -e $confdir/accords.ini; then
    . $confdir/accords.ini
fi

# -------------------------------------
# to activate the security using nethsm
# set nethsm to 1
# then specify the security directory,
# and the nagios configuration file
# -------------------------------------
# nethsm=1
# nagios=/usr/local/etc/accords/accords-nagios.cfg
tls=security
runcops=1
# -------------------------------------
# with neither security nor monitoring 
# -------------------------------------
# tls=
nethsm=
nagios=
# runcops=
# -------------------------------------
resthost=127.0.0.1
secdir=$tls
zone="europe"
trace=1
threads=1
verbose=0
debug=0
domain=occi
operator=accords
password=co-system

if [ `basename $0` != "co-status" -a `basename $0` != "co-stop" ]; then

    if [ -z "$accords_workdir" ]; then
         accords_workdir="."
    fi

    if [ ! -d "$accords_workdir" ]; then
         echo "$accords_workdir: no such directory"
         exit 1
    fi

    if [ ! -d "$accords_workdir/rest" ]; then
         echo "$accords_workdir/rest: no such directory"
         exit 1
    fi

fi

cd $accords_workdir

if [ -z "$tls" ]; then
    tempo=1
else
    tempo=2
fi

if [ -z "$target" ]; then
    target=accords.xml
fi

if [ `basename $0` != "accords-config" -a `basename $0` != "co-status" -a `basename $0` != "co-stop" ]; then
    if test ! -e $accords_workdir/$target; then
	echo "Can not find target file: $accords_workdir/$target."
	echo "Please run accords-config first !"
	exit 1
    fi
fi

if [ -z "$resthost" ]; then
    resthost=127.0.0.1
fi

if [ -z "$restport" ]; then
    restport=8086
fi

if [ -z "$publisher" ]; then
	if [ -z "$tls" ]; then
	    publisher=http://$resthost:$restport
	else
	    publisher=https://$resthost:$restport
	fi
fi

if [ -z "$monitor" ]; then
    monitor=$trace
fi

###
### Aliases
###
### The script must be called through one of the following aliases.
###
aliases="co-start co-platform co-provision co-broker co-command co-parser co-provider co-resolver co-status co-stop run-azprocci run-broker run-cops run-coees run-coips run-comons run-conets run-coobas run-conagios run-coss run-ezvm run-fileserver run-onprocci run-osprocci run-paprocci run-parser run-procci run-cosched run-publisher run-osocciprocci run-dcprocci run-slam run-cosacs run-copaas run-paasprocci accords-config run-cool"

_co_run_serv() {
    serv=$1
    name=$2
    if [ -z "$serv" ]; then
        return 0
    fi
    if [ -z "$name" ]; then
        name=$serv
    fi
    which run-$serv 2>/dev/null >/dev/null
    if [ $? -eq 0 ]; then
        echo "Starting $name"
        run-$serv &
        sleep $tempo
    else
        echo "NOT starting $name"
    fi
}

_co_platform() {
    echo "Starting ACCORDS Platform"

# ---------------------------------------------------
#       The basic accords platform components
# ---------------------------------------------------

    _co_run_serv publisher "accords publication services"
    _co_run_serv fileserver "accords file services"
    _co_run_serv coss "accords security services" 
    _co_run_serv comons "accords monitoring services"
    _co_run_serv coobas "accords accounting services"
    _co_run_serv cool "accords elasticity services"
    _co_run_serv cosched "accords scheduling services" 
    _co_run_serv conets "accords networking services"
    _co_run_serv cops "accords placement services"
    _co_run_serv coees "accords energy efficiency services"
    _co_run_serv ezvm "accords image storage services"
    _co_run_serv coips "accords image production services"
    _co_run_serv slam "accords sla services"
    _co_run_serv copaas "accords paas services"
    _co_run_serv conagios "accords nagios services"
    _co_run_serv parser "accords parser services"
    _co_run_serv broker "accords broker services"
    _co_run_serv procci "accords procci services"

# ---------------------------------------------------
#       The provisioning services agreements 
# ---------------------------------------------------
    secs=20
    echo "Please wait $secs seconds while starting the provisioning services agreements"
    for i in $(seq 1 $secs)
    do 
       sleep 1
       echo -n "."
    done
    echo
    co-parser accounts
    co-parser metrics
    co-parser coips

    echo "ACCORDS Platform Started"
}

_co_run_prov () {
    procci=$1
    oper=$2
    msg=$3
    params="$4 $5 $6 $7 $8" 	
    echo "Starting $msg"
    co-provider $procci accords $params
    co-parser quota-$procci
    co-parser sla-$procci
    run-$procci &
    sleep $tempo
}

_co_provision()
{
# ---------------------------------------------------
#    The accords platform provisioning components
# ---------------------------------------------------
    if [ -z "$runcops" ]; then
         echo "Validate Launch of COPS"
	 read var
         echo "Starting ACCORDS Provisioning"
    else
          echo "Starting ACCORDS Provisioning"
    fi

    _co_run_prov osprocci accords "accords open stack services" 30 30 1000 30 30
    _co_run_prov onprocci accords "accords open nebula services" 20 20 1000 20 20
    _co_run_prov paprocci accords "accords proactive services" 5 5 500 5 5
    _co_run_prov azprocci accords "accords windows azure services" 10 10 1000 10 10
    _co_run_prov dcprocci accords "accords delta cloud services" 1 1 1000 1 1
    _co_run_prov paasprocci accords "accords paas procci services" 10 10 1000 10 10
    _co_run_prov osocciprocci accords "accords os occi procci services" 10 10 1000 10 10

    _co_run_serv cosacs "accords software configuration services"

    echo "ACCORDS Provisioning Started"
}

_co_start()
{
	_co_platform
	_co_provision
	_nagios_start
}

_co_broker() {
    if [ -z "$tls" ]; then
	testcb --publisher "$publisher" --verbose plan_$1.xml
    else
	testcb --tls security/testcbTls.xml --publisher "$publisher" --verbose plan_$1.xml
    fi
} 

_co_command() {
    if [ -z "$tls" ]; then
	/usr/local/bin/command --publisher "$publisher" $*
    else
	/usr/local/bin/command --tls $secdir/commandTls.xml --publisher "$publisher" $*
    fi
}

_co_parser() {
    if [ -z "$tls" ]; then
	testcp --publisher "$publisher" --verbose $2 $1.xml
    else
	testcp --tls $secdir/testcpTls.xml --publisher "$publisher" --verbose $2 $1.xml
    fi
}

_co_provider() {

name=$1
operator=$1
cpu=$3
ram=$4
disk=$5
vm=$6
adr=$7

if [ -z "$cpu" ]; then
cpu=10
fi

if [ -z "$ram" ]; then
ram=10
fi

if [ -z "$disk" ]; then
disk=1000
fi

if [ -z "$vm" ]; then
vm=10
fi

if [ -z "$adr" ]; then
adr=10
fi

echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" > quota-$name.xml
echo "<provider name=\"$name\" operator=\"$name\" zone=\"europe\" " > quota-$name.xml
echo " xmlns=\"http://www.compatibleone.fr/schemes/provision.xsd\">" >> quota-$name.xml 
echo "<quota name=\":cpu\" property=\"occi.compute.cores\" offered=\"$cpu\">" >> quota-$name.xml 
echo "<price name=\":cpu:price\" operator=\"$name\" rate=\"0.04\" currency=\"euro\" period=\"hour\"/>" >> quota-$name.xml
echo "</quota>" >> quota-$name.xml
echo "<quota name=\":ram\" property=\"occi.compute.memory\" offered=\"$ram\">" >> quota-$name.xml 
echo "<price name=\":ram:price\" operator=\"$name\" rate=\"0.01\" currency=\"euro\" period=\"hour\"/>" >> quota-$name.xml
echo "</quota>" >> quota-$name.xml
echo "<quota name=\":disk\" property=\"occi.storage.size\" offered=\"$disk\">" >> quota-$name.xml 
echo "<price name=\":disk:price\" operator=\"$name\" rate=\"0.03\" currency=\"euro\" period=\"hour\"/>" >> quota-$name.xml
echo "</quota>" >> quota-$name.xml
echo "<quota name=\":vm\" property=\"accords.vm.count\" offered=\"$vm\">" >> quota-$name.xml 
echo "<price name=\":vm:price\" operator=\"$name\" rate=\"1.00\" currency=\"euro\" fixed=\"true\"/>" >> quota-$name.xml
echo "</quota>" >> quota-$name.xml
echo "<quota name=\":adr\" property=\"accords.address.count\" offered=\"$adr\">" >> quota-$name.xml 
echo "<price name=\":adr:price\" operator=\"$name\" rate=\"0.05\" currency=\"euro\" period=\"hour\"/>" >> quota-$name.xml
echo "</quota>" >> quota-$name.xml
echo "</provider>" >> quota-$name.xml

echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" > sla-$name.xml
echo "<agreement name=\"$name\" xmlns=\"http://www.compatibleone.fr/schemes/slam.xsd\" initiator=\"$operator\" responder=\"accords\" serviceprovider=\"initiator\" description=\"generated provider SLA\" initiation=\"now\" expiration=\"never\" template=\"none\">" >> sla-$name.xml 
echo "<terms name=\"$name:services\" type=\"services\">" >> sla-$name.xml 
echo "<term name=\"$name:service1\">" >> sla-$name.xml 
echo "<provider name=\"$name\"/>" >> sla-$name.xml 
echo "</term>" >> sla-$name.xml
echo "</terms>" >> sla-$name.xml
echo "<terms name=\"$name:conditions\" type=\"conditions\">" >> sla-$name.xml 
echo "<term name=\"$name:condition1\">" >> sla-$name.xml 
echo "<variable name=\"$name:v1\" property=\"occi.placement.algorithm\" condition=\"eq\" value=\"zone\"/>" >> sla-$name.xml 
echo "<variable name=\"$name:v2\" property=\"occi.placement.zone\" condition=\"eq\" value=\"europe\"/>" >> sla-$name.xml 
echo "</term>" >> sla-$name.xml
echo "</terms>" >> sla-$name.xml
echo "<terms name=\"accords:guarantees\" type=\"guarantees\"/>" >> sla-$name.xml 
echo "</agreement>" >> sla-$name.xml

}


_co_resolver() {
    if [ -z "$tls" ]; then
	testresolver --publisher "$publisher" $*
    else
	testresolver --tls security/testresolverTls.xml --publisher "$publisher" $*
    fi
}

_co_status() {
    echo "-----------------------------"
    echo " Accords Platform Components "
    echo "-----------------------------"
    if [ "$(uname -s)" = 'Linux' ] ; then
	netstat -a -p -n | grep LISTEN | egrep '(publisher|fileserver|coss|comons|conets|coobas|cool|cops|coees|parser|broker|procci|cosched|ezvm|coips|cosacs|conagios|slam|copaas)'
    elif [ "$(uname -s)" = 'Darwin' ] ; then
	lsof -i | grep LISTEN | grep publisher || true
	lsof -i | grep LISTEN | grep fileserver || true
	lsof -i | grep LISTEN | grep coss || true
	lsof -i | grep LISTEN | grep comons || true
	lsof -i | grep LISTEN | grep conets || true
	lsof -i | grep LISTEN | grep coobas || true
	lsof -i | grep LISTEN | grep cool || true
	lsof -i | grep LISTEN | grep cops || true
	lsof -i | grep LISTEN | grep coees || true
	lsof -i | grep LISTEN | grep parser || true
	lsof -i | grep LISTEN | grep broker || true
	lsof -i | grep LISTEN | grep procci || true
	lsof -i | grep LISTEN | grep cosched || true
	lsof -i | grep LISTEN | grep ezvm || true
	lsof -i | grep LISTEN | grep coips || true
	lsof -i | grep LISTEN | grep conagios || true
	lsof -i | grep LISTEN | grep cosacs || true
	lsof -i | grep LISTEN | grep slam || true
	lsof -i | grep LISTEN | grep copaas || true
    else
	echo "Unsupported OS ($(uname -s))"
	exit 1
    fi
}

_co_stop() {
    _nagios_stop
    _stop_service cosacs
    _stop_service slam
    _stop_service paasprocci
    _stop_service onprocci
    _stop_service osocciprocci
    _stop_service dcprocci
    _stop_service osprocci
    _stop_service paprocci
    _stop_service azprocci
    _stop_service cosched
    _stop_service procci
    _stop_service broker
    _stop_service parser
    _stop_service copaas
    _stop_service conagios
    _stop_service ezvm
    _stop_service coips
    _stop_service coees
    _stop_service cops
    _stop_service conets
    _stop_service cool
    _stop_service coobas
    _stop_service comons
    _stop_service coss
    _stop_service fileserver
    _stop_service publisher
}

_run_azprocci() {
    azprocci --config $target CO-AZPROCCI/1.0
} 

_run_broker() {
    broker --config $target CO-BROKER/1.0
} 

_run_cops() {
    if [ -z "$runcops" ]; then
	echo inhibited cops
    else
        cops --config $target CO-ES/1.0
    fi
} 

_run_coees() {
    coees --config $target CO-EES/1.0
} 

_run_comons() {
    comons --config $target CO-MONS/1.0
}

_run_conets() {
    conets --config $target CO-NETS/1.0
}

_run_coobas() {
    coobas --config $target CO-BAS/1.0
}

_run_cool() {
    cool --config $target CO-OL/1.0
}

_run_cosacs() {
    cosacs --config $target CO-SACS/1.0
}

_run_coss() {
    coss --config $target CO-SS/1.0
}

_run_ezvm() {
    ezvm --config $target CO-EZVM/1.0
}

_run_coips() {
    coips --config $target CO-IPS/1.0
}

_run_fileserver() {
    fileserver --config $target CO-FS/1.0
}

_run_onprocci() {
    onprocci --config $target CO-ONPROCCI/1.0
}

_run_osprocci() {
    osprocci --config $target CO-OSPROCCI/1.0
}

_run_paprocci() {
    JDK=$(readlink -f /usr/bin/java| sed "s:/jre/bin/java::") LD_LIBRARY_PATH=$JDK/jre/lib/i386/client:$JDK/jre/lib/i386 paprocci --config $target CO-PAPROCCI/1.0
}

_run_osocciprocci() {
    osocciprocci --config $target CO-OSOCCIPROCCI/1.0
}

_run_slam() {
    slam --config $target CO-SLAM/1.0
}

_run_copaas() {
    copaas --config $target CO-PAAS/1.0
}

_run_conagios() {
    conagios --config $target CO-NAGIOS/1.0
}

_run_paasprocci() {
    paasprocci --config $target CO-PAASPROCCI/1.0
}

_run_dcprocci() {
    dcprocci --config $target CO-DCPROCCI/1.0
}

_run_parser() {
    parser --config $target CO-PARSER/1.0
}

_run_procci() {
    procci --config $target CO-PROCCI/1.0
}
_run_cosched() {
    cosched --config $target CO-SCHED/1.0
}
 
_run_publisher() {
    publisher --config $target CO-PUB/1.0
}

_run_fileserver() {
    fileserver --config $target CO-FS/1.0
}

_make_security_files() {
	_mksecs publisher 0
	_mksecs fileserver 0
	_mksecs coss 0
	_mksecs comons 1
	_mksecs cops 1
	_mksecs coees 1
	_mksecs coobas 1
	_mksecs cool 1
	_mksecs conets 1
	_mksecs broker 1
	_mksecs ezvm 1
	_mksecs coips 1
	_mksecs cosacs 1
	_mksecs parser 1
	_mksecs procci 1
	_mksecs cosched 1
	_mksecs osprocci 1
	_mksecs paprocci 1
	_mksecs osocciprocci 1
	_mksecs onprocci 1
	_mksecs azprocci 1
	_mksecs dcprocci 1
	_mksecs slam 1
	_mksecs copaas 1
	_mksecs conagios 1
	_mksecs paasprocci 1
	_mksecs command
    
	_mksect testcp 1
	_mksect testpa 1
	_mksect testcb 1
	_mksect teston 1
	_mksect testos 1
	_mksect testresolver 1
	_mksect command 1
}

_accords_config() {

    if [ -z "$tls" ]; then
	echo "Accords Security Not Required"
    else
	echo "Creating Accords Security Files"
    
	export calist=Certificates.crt
	if [ -f /etc/accords/Certificates.crt ]; then
		export calist=/etc/accords/Certificates.crt
	fi
	export pass=Accords

	if [ -z "$nethsm" ]; then 
		export options=13

	else
		export options=525
	fi
    

	if test -d $secdir; then
	   if [ -z "$nethsm" ]; then
	    	echo "ACCORDS security dir exists ($secdir): please remove it first !"
	    	exit 1
	   else
	    	echo "ACCORDS security dir exists ($secdir): certificates have not been regenerated !"
		_make_security_files
	   fi
	else
	    	mkdir -p $secdir
		_make_security_files

		echo "Creating Accords Certificats"
	
		export bits=2048
		export days=365
		export sslconfig=./openssl.cnf

		_mkcert coss
		_mkcert cops
		_mkcert coees
		_mkcert coobas
		_mkcert cool
		_mkcert comons
		_mkcert conets
		_mkcert broker
		_mkcert parser
		_mkcert ezvm
		_mkcert coips
		_mkcert publisher
		_mkcert fileserver
		_mkcert procci
		_mkcert cosched
		_mkcert osprocci
		_mkcert osocciprocci
		_mkcert onprocci
		_mkcert paprocci
		_mkcert azprocci
		_mkcert dcprocci
		_mkcert slam
		_mkcert copaas
		_mkcert conagios
		_mkcert paasprocci
		_mkcert cosacs
	
		_mkcert testcp
		_mkcert testcb
		_mkcert testos
		_mkcert testpa
		_mkcert teston
		_mkcert testresolver
		_mkcert command
	fi
    fi

    echo "Creating Accords Port Configuration : ports.xml"
    echo "Creating Accords Nagios Configuration : " $nagios

    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > ports.xml
    echo "<import name=\"accords-ports\" xmlns=\"http://www.compatibleone.fr/schemes/cords.xsd\">" >> ports.xml
    echo "<network name=\"accords-platform\">" >> ports.xml

    echo "Creating Accords Global Configuration File: $target"
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > $target
    echo "<accords>" >> $target

    _initnag

    _mkpub publisher
    _mkmon coss 
    _mkmon comons 
    _mkcfg cops 
    _mkcfg conets 
    _mkcfg coobas 
    _mkcfg cool
    _mkcfg parser 
    _mkcfg broker 
    _mkcfg procci 
    _mkpro osprocci  
    _mkpro paprocci  
    _mkpro osocciprocci  
    _mkpro onprocci
    _mkpro azprocci
    _mkpro dcprocci
    _mkcfg fileserver
    _mkcfg ezvm
    _mkcfg coips
    _mkcfg coees 
    _mkcfg cosched
    _mkcfg slam
    _mkcfg copaas
    _mkcfg conagios
    _mkpro paasprocci
    _mkcosacs cosacs

    _termnag

    echo "</accords>" >> $target
    echo "</network>" >> ports.xml
    echo "</import>" >> ports.xml
}

###
### Helper functions
###
_usage() {
    echo "Usage: this script must be called through one of the following name:"
    for alias in $aliases; do
	echo " * $alias"
    done
}

_stop_service() {
    service=$1
    echo "Stopping service: $service"
    if [ "$(uname -s)" = 'Linux' ] ; then
	killall -u $(whoami) -q $service || true
	killall -u $(whoami) -q lt-$service || true
    elif [ "$(uname -s)" = 'Darwin' ] ; then
	killall $service >/dev/null 2>&1 || true
	killall lt-$service >/dev/null 2>&1 || true
    else
	echo "Unsupported OS ($(uname -s))"
	exit 1
    fi
    sleep $tempo
}

_mksecs() {
    module=$1
    auth=$2
    echo "<tls key='$secdir/"${module}"PrivateKey.pem'" > $secdir/${module}Tls.xml
    echo "     certificate='$secdir/"${module}"Certificate.pem'" >> $secdir/${module}Tls.xml
    echo "     passphrase='"$pass"'" >> $secdir/${module}Tls.xml
    echo "     authority='"$calist"'" >> $secdir/${module}Tls.xml
    echo "     authenticate='"$auth"'" >> $secdir/${module}Tls.xml
    echo "     option='"$options"' />" >> $secdir/${module}Tls.xml
    chmod 0640 $secdir/${module}Tls.xml
}

_mksect() {
    module=$1
    auth=$2
    echo "<tls key='$secdir/"${module}"PrivateKey.pem'" > $secdir/${module}Tls.xml
    echo "     certificate='$secdir/"${module}"Certificate.pem'" >> $secdir/${module}Tls.xml
    echo "     passphrase='"$pass"'" >> $secdir/${module}Tls.xml
    echo "     authority='"$calist"'" >> $secdir/${module}Tls.xml
    echo "     authenticate='"$auth"'" >> $secdir/${module}Tls.xml
    echo "     option='"$optiont"' />" >> $secdir/${module}Tls.xml
    chmod 0640 $secdir/${module}Tls.xml
}

_mkcert() {
    echo "Creating certificate for module $1"
    module=$1
    if test ! -e $sslconfig; then
	echo "Can not find openSSL config file: $sslconfig"
	exit 1
    fi

    if [ -z "$nethsm" ]; then

	openssl req -config $sslconfig \
	    -newkey $bits -x509 -days $days -sha1 \
	    -keyout ${secdir}/${module}PrivateKey.pem -out ${secdir}/${module}Certificate.pem 2> /dev/null
    else
	openssl req -engine pkcs11 -keyform e -config $sslconfig \
	    -newkey $bits -x509 -days $days -sha1 \
	    -keyout ${secdir}/${module}PrivateKey.pem -out ${secdir}/${module}Certificate.pem
    fi
    chmod 0400 ${secdir}/${module}PrivateKey.pem
    chmod 0444 ${secdir}/${module}Certificate.pem
}

_mktls() {
    module=$1
    if test -z "$tls"; then
	echo "         tls=''/>" >> $target
    else
	echo "         tls='"$tls"/"${module}"Tls.xml'/>" >> $target
    fi
}

_mkpub() {
    module=$1
    echo "<"${module} >> $target
    echo "    publisher=''"           >> $target
    echo "    operator='"$operator"'"           >> $target
    echo "    identity=''" >> $target
    echo "    verbose='"$verbose"'"   >> $target
    echo "    debug='"$debug"'"       >> $target
    echo "    threads='"$threads"'>"  >> $target
    echo "    <rest host='"$resthost"' port='"$restport"'/>" >> $target
    echo "    <security" >> $target
    echo "         user='service-"${module}"'" >> $target
    echo "         password='"$password"'" >> $target
    echo "         monitor='"$trace"'" >> $target
    _mktls ${module}
    echo "    <domain name='"$domain"' zone='"$zone"'/>" >> $target
    echo "</"${module}">" >> $target
    echo "<port name=\"${module}\" protocol=\"tcp\" range=\"0.0.0.0/0\" from=\""$restport"\" to=\""$restport"\"/>" >> ports.xml
    _mknag $module
}

_mkmon() {
    module=$1
    export restport=$(($restport+1))
    echo "<"${module} >> $target
    echo "    publisher='"$publisher"'"           >> $target
    echo "    operator='"$operator"'"           >> $target
    echo "    identity='"$resthost":"$restport"'" >> $target
    echo "    verbose='"$verbose"'"   >> $target
    echo "    debug='"$debug"'"       >> $target
    echo "    threads='"$threads"'>"  >> $target
    echo "    <rest host='"$resthost"' port='"$restport"'/>" >> $target
    echo "    <security" >> $target
    echo "         user='service-"${module}"'" >> $target
    echo "         password='"$password"'" >> $target
    echo "         monitor='"$trace"'" >> $target
    _mktls ${module}
    echo "    <domain name='"$domain"' zone='"$zone"'/>" >> $target
    echo "</"${module}">" >> $target
    echo "<port name=\""${module}"\" protocol=\"tcp\" range=\"0.0.0.0/0\" from=\""$restport"\" to=\""$restport"\"/>" >> ports.xml
    _mknag $module
}

_mkcosacs() {
    module=$1
    echo "<"${module} >> $target
    echo "    publisher='"$publisher"'"           >> $target
    echo "    operator='"$operator"'"           >> $target
    echo "    identity='"$resthost":8286'" >> $target
    echo "    verbose='"$verbose"'"   >> $target
    echo "    debug='"$debug"'"       >> $target
    echo "    threads='"$threads"'>"  >> $target
    echo "    <rest host='"$resthost"' port='8286'/>" >> $target
    echo "    <security" >> $target
    echo "         user='service-"${module}"'" >> $target
    echo "         password='"$password"'" >> $target
    echo "         monitor='"$trace"'" >> $target
    _mktls ${module}
    echo "    <domain name='"$domain"' zone='"$zone"'/>" >> $target
    echo "</"${module}">" >> $target
    echo "<port name=\""${module}"\" protocol=\"tcp\" range=\"0.0.0.0/0\" from=\"8286\" to=\"8286\"/>" >> ports.xml
}

# -------------------------------------------
# addition of accords configuration to nagios
# -------------------------------------------
_nagios_stop() {
	if [ -z "$nagios" ]; then
		echo "nagios is not required"
	else
		if [ -f "/etc/nagios/nagios.cfg" ]; then
			echo "restarting nagios for the accords platform"	
			echo "# accords platform shutdown" > $nagios.active
			service nagios restart
		else
			echo "nogios is not installed"
		fi
	fi
}

# -------------------------------------------
# addition of accords configuration to nagios
# -------------------------------------------
_nagios_start() {
	if [ -z "$nagios" ]; then
		echo "nagios is not required"
	else
		if [ -f "/etc/nagios/nagios.cfg" ]; then
			var=`grep accords /etc/nagios/nagios.cfg`
			if [ -z "$var" ]; then
				echo "configuration of nagios for accords platform"
				echo "# ------------------------------------ " >> /etc/nagios/nagios.cfg	
				echo "# CompatibleOne Small Accords Platform " >> /etc/nagios/nagios.cfg
				echo "# ------------------------------------ " >> /etc/nagios/nagios.cfg
				echo "cfg_file=$nagios.active" >> /etc/nagios/nagios.cfg
			else
				echo "nagios already configured for accords platform"
			fi	
			echo "restarting nagios for the accords platform"	
			cp $nagios $nagios.active
			service nagios restart
		else
			echo "nogios is not installed"
		fi
	fi
}

# -------------------------
# nagios configuration file
# -------------------------
_initnag() {
	if [ -z "$nagios" ]; then
		return;
	else
		echo "# ---------------------- " 		>  $nagios
		echo "# small accords platform " 		>> $nagios
		echo "# ---------------------- " 		>> $nagios
		echo "#  nagios configuration  " 		>> $nagios
		echo "# ---------------------- " 		>> $nagios

# ----------------------------------
#	nagios period definition
# ----------------------------------
		echo "define timeperiod { " 	  		>> $nagios
		echo "  timeperiod_name  whenever " 		>> $nagios
		echo "  alias 'no specific time' " 		>> $nagios
		echo "} " 			 		>> $nagios

# ----------------------------------
#	nagios command definitions
# ----------------------------------
		echo "define command { " 	  		>> $nagios
		echo "  command_name  check_occi " 		>> $nagios
		if [ -z "$tls" ]; then
			echo "  command_line  /usr/libexec/nagios/check_http -I \$HOSTADDRESS\$ -u /-/ -p \$ARG1\$ -e 'Bad Request Expected Host' " >> $nagios
		else
			echo "  command_line  /usr/libexec/nagios/check_http -I \$HOSTADDRESS\$ --ssl -u /-/ -p \$ARG1\$ -e 'Bad Request Expected Host' " >> $nagios
		fi
		echo "} " 			 		>> $nagios

		echo "define command { " 	  		>> $nagios
		echo "  command_name  nothing " 		>> $nagios
		echo "  command_line  test    "			>> $nagios
		echo "} " 			 		>> $nagios

# ----------------------------------
#	nagios contact definition
# ----------------------------------
		echo "define contact { " 	  		>> $nagios
		echo "  contact_name  supervisor " 		>> $nagios
		echo "  host_notifications_enabled 1" 		>> $nagios
		echo "  service_notifications_enabled 1" 	>> $nagios
		echo "  host_notification_options	n " 	>> $nagios
		echo "  service_notification_options	n" 	>> $nagios
		echo "  host_notification_commands nothing" 	>> $nagios
		echo "  service_notification_commands nothing" 	>> $nagios
		echo "} " 			 		>> $nagios

# --------------------------------------
#	nagios contact group definition
# --------------------------------------
		echo "define contactgroup{ "			>> $nagios
		echo " contactgroup_name accords "		>> $nagios
		echo " alias 'AccordsPlatform ContactGroup' "	>> $nagios
		echo "} " 			 		>> $nagios

# --------------------------------------
#	nagios service group definition
# --------------------------------------
		echo "define servicegroup { " 			>> $nagios
		echo "  servicegroup_name accords "		>> $nagios
		echo "  alias 'AccordsPlatform ServiceGroup' "	>> $nagios
		echo "} " 			 		>> $nagios

# ----------------------------------
#	nagios host definition
# ----------------------------------
		echo "define host { " 		 		>> $nagios
		echo "  host_name  accords" 			>> $nagios
		echo "  alias      'Small Accords Platform' " 	>> $nagios
		echo "  address " $resthost 			>> $nagios
		echo "  max_check_attempts  2 "			>> $nagios
		echo "  active_checks_enabled 1 "		>> $nagios
		echo "  contacts supervisor "			>> $nagios
		echo "  normal_check_interval 5 "		>> $nagios
		echo "  retry_check_interval 1 "		>> $nagios
		echo "  contact_groups	accords "		>> $nagios		
		echo "} " 			 		>> $nagios
	fi
}

_mknag() {
	if [ -z "$nagios" ]; then
		return;
	else
		module=$1
		echo "define service { " 			>> $nagios
		echo "  use generic-service" 			>> $nagios
		echo "  servicegroups accords "			>> $nagios
		echo "  host_name accords " 			>> $nagios
		echo "  service_description accords:$module "   >> $nagios
		echo "  check_command check_occi!$restport "    >> $nagios
		echo "  active_checks_enabled 1 "		>> $nagios
		echo "  max_check_attempts  4 "			>> $nagios
		echo "  normal_check_interval 5 "		>> $nagios
		echo "  retry_check_interval 1 "		>> $nagios
		echo "  contacts supervisor "			>> $nagios
		echo "  contact_groups	accords "		>> $nagios		
		echo "}" 					>> $nagios
	fi
}

_termnag() {
	if [ -z "$nagios" ]; then
		echo "nagios is not required"
	else
		echo "#------------ " 				>> $nagios
		echo "# end of file " 				>> $nagios
		echo "#------------ " 				>> $nagios
	fi
}

_mkcfg() {
    module=$1
    export restport=$(($restport+1))
    echo "<"${module} >> $target
    echo "    publisher='"$publisher"'"           >> $target
    echo "    operator='"$operator"'"           >> $target
    echo "    identity='"$resthost":"$restport"'" >> $target
    echo "    verbose='"$verbose"'"   >> $target
    echo "    debug='"$debug"'"       >> $target
    echo "    threads='"$threads"'>"  >> $target
    echo "    <rest host='"$resthost"' port='"$restport"'/>" >> $target
    echo "    <security" >> $target
    echo "         user='service-"${module}"'" >> $target
    echo "         password='"$password"'" >> $target
    echo "         monitor='"$monitor"'" >> $target
    _mktls ${module}
    echo "    <domain name='"$domain"' zone='"$zone"'/>" >> $target
    echo "</"${module}">" >> $target
    echo "<port name=\""${module}"\" protocol=\"tcp\" range=\"0.0.0.0/0\" from=\""$restport"\" to=\""$restport"\"/>" >> ports.xml
	if [ "$module" != "cool" ]; then
    	_mknag $module
	fi
}

_mkpro() {
    module=$1
    export restport=$(($restport+1))
    echo "<"${module} >> $target
    echo "    publisher='"$publisher"'"           >> $target
    echo "    operator='"${module}"'"               >> $target
    echo "    identity='"$resthost":"$restport"'" >> $target
    echo "    verbose='"$verbose"'"   >> $target
    echo "    debug='"$debug"'"       >> $target
    echo "    threads='"$threads"'>"  >> $target
    echo "    <rest host='"$resthost"' port='"$restport"'/>" >> $target
    echo "    <security" >> $target
    echo "         user='service-"${module}"'" >> $target
    echo "         password='"$password"'" >> $target
    echo "         monitor='"$monitor"'" >> $target
    _mktls ${module}
    echo "    <domain name='"$domain"' zone='"$zone"'/>" >> $target
    echo "</"${module}">" >> $target
    echo "<port name=\""${module}"\" protocol=\"tcp\" range=\"0.0.0.0/0\" from=\""$restport"\" to=\""$restport"\"/>" >> ports.xml
    _mknag $module
}

case `basename $0` in
    co-start) 
	_co_start $*
	;;
    co-provision) 
	_co_provision $*
	;;
    co-platform) 
	_co_platform $*
	;;
    co-broker) 
	_co_broker $*
	;;
    co-command) 
	_co_command $*
	;;
    co-parser) 
	_co_parser $*
	;;
    co-provider) 
	_co_provider $*
	;;
    co-resolver)
	_co_resolver $*
	;;
    co-status)
	_co_status $*
	;;
    co-stop)
	_co_stop $*
	;;
    run-paprocci) 
	_run_paprocci $*
	;;
    run-azprocci) 
	_run_azprocci $*
	;;
    run-broker)
	_run_broker $*
	;;
    run-cops)
	_run_cops $*
	;;
    run-coees)
	_run_coees $*
	;;
    run-comons)
	_run_comons $*
	;;
    run-conets)
	_run_conets $*
	;;
    run-coobas)
	_run_coobas $*
	;;
    run-cool)
	_run_cool $*
	;;
    run-coss)
	_run_coss $*
	;;
    run-ezvm)
	_run_ezvm $*
	;;
    run-coips)
	_run_coips $*
	;;
    run-fileserver)
	_run_fileserver $*
	;;
    run-onprocci)
	_run_onprocci $*
	;;
    run-osprocci)
	_run_osprocci $*
	;;
    run-osocciprocci)
	_run_osocciprocci $*
	;;
    run-slam)
	_run_slam $*
	;;
    run-copaas)
	_run_copaas $*
	;;
    run-conagios)
	_run_conagios $*
	;;
    run-paasprocci)
	_run_paasprocci $*
	;;
    run-dcprocci)
	_run_dcprocci $*
	;;
    run-parser)
	_run_parser $*
	;;
    run-procci)
	_run_procci $*
	;;
    run-cosched)
	_run_cosched $*
	;;
    run-cosacs)
	_run_cosacs $*
	;;
    run-publisher)
	_run_publisher $*
	;;
    accords-config)
	_accords_config $*
	;;
    *)
	_usage
	exit 1
	;;
esac

exit 0
