#!/bin/bash
#
# Guralp Configuration System
#   Copyright (c) 2008-2009 Guralp Systems Ltd. All rights reserved.
#
#   Services configuration second level selection

# Handle navbar without loading anything else
if [ "X$1" == "X--navbar" ]
then
	echo "servicestop	Services"
	echo "servicessub/$3	$3"
	exit 0
fi

# Load support functions
script_dir=$(dirname $0)
. $script_dir/functions.sh


# Get and check the service name from the supplied subitem
name="$3"

SERVINFO="$SERVDIR/$name/info"

if [ ! -r $SERVINFO ]
then
	gcs_err fatal "Unknown service identifier $name"
	exit 0
fi


# Get and export our service name, description and other info

desc=$(gcs_get_varf DESC $SERVINFO)
[ -z "$desc" ] && desc="$name"

allow_multi=$(gcs_get_varf ALLOW_MULTI $SERVINFO)
[ -z "$allow_multi" ] && allow_multi=True

gcs_var name "$name"
gcs_var desc "$desc"
gcs_var allow_multi "$allow_multi"


# Create the config directory if it's missing
CFDIR=$CONFIGDIR/$name
group_mkdir $CFDIR


# List the services available for editing
declare -i in=0

dofile() {
	local sel=$1
	local file=$CFDIR/${sel}.local
	local ctlfile=$CFDIR/${sel}.ctl.local
	local enb
	local dsc
	local ret=0

	[ ! -r "$file" ] && return 1

	# Get (possibly setting) the user description
	dsc=""
	[ -r "$ctlfile" ] && dsc=$(gcs_get_varf DESC $ctlfile)
	if [ -z "$dsc" ]
	then
	    if [ "$sel" == "default" ]
	    then
		dsc="${desc}. Default instance"
	    else
		dsc="${desc}. Instance $(expr $sel + 1)"
	    fi
	    echo "DESC=$dsc" >> $ctlfile
	    ret=1
	fi

	if [ "$sel" == "default" ] && grep -q "^$name" $INITDIR/autostart
	then
	    enb=True
	    gcs_var change_enable False
	elif [ "${sel}" == "default" ] && grep -q "^${name}\$" "${INITDIR2}/autostart"
	then
	    enb=True
            gcs_var change_enable True
	elif grep "^$name.$sel" $INITDIR2/autostart > /dev/null
	then
	    enb=True
	    gcs_var change_enable True
	else
	    enb=False
	    gcs_var change_enable True
	fi

	if gcs_truefalse "${enb:-false}"
	then
	    echo "select_service${in}=$name/$sel $dsc - starts automatically"
	else
	    echo "select_service${in}=$name/$sel $dsc - does not start automatically"
	fi
	((in++))
	return $ret
}

# Show default service entry
dofile default

for i in $CFDIR/*[0-9].ctl.local
do
	# Show all other numbered instances
	file=$(basename $i ".ctl.local")
	dofile $file
done

if [ $in -gt 0 ]
then
	gcs_var has_entries True
else
	gcs_var has_entries False
fi

# Start numbering of new services at 0
in=0
while [ -f "$CFDIR/$in.ctl.local" ]
do
	# Find free slot
	((in++))
done


# Now the creation options
# User needs to be root or in the group conf to create services
if [ "$(id -ur)" == "0" ] || ( id | grep "\(conf\)" ) > /dev/null 2>&1
then
    if gcs_truefalse "${allow_multi:-false}"
    then
	echo "select_create0=$name/$in Create new service instance"
	gcs_var can_create True

    elif [ \( ! -f $CFDIR/default \) -a \( ! -f $CFDIR/default.local \) ]
    then
	echo "select_create0=$name/default Create default service instance"
	gcs_var can_create True
    else
	gcs_var can_create False
    fi
else
    gcs_var can_create False
fi
exit 0
