#!/bin/sh

. caterva-alterator.sh

# args: group_name group_deny
print_access()
{
    local group_name="$1"
    local group_deny="$2"
    local auth="$3"
    if [ -n "$group_name" ]; then
	if [ -z "$MORE_DOMAINS" ]; then
	    print_info "Grant access for the group $group_name"
	    printf "http_access allow $auth %s" "$group_name"
	    MORE_DOMAINS="yes"
	fi
	if [ -n "$group_deny" ] && [ "$group_deny" != "#f" ]; then
	    print_info "Grant access to other than the domain $label for the group $group_name"
	    printf " !$name"
	else
	    print_info "Grant access to the domain $label for the group $group_name"
	    printf " $name"
	fi
	def_val "access_$group_name" "yes"
    fi
}

print_group()
{
    local acltype_group=`read_val "acltype_group"`
    if [ -z "$acltype_group" ] || [ "$acltype_group" != "yes" ]; then
	cat <<EOF
# Define ACL type to test a system group membership
external_acl_type unix_group %LOGIN /usr/lib/squid/squid_unix_group -p
EOF
	print_info "Define ACL type to test a system group membership"
	def_val "acltype_group" "yes"
    fi

    print_info "Adding access for group $label"
    echo "acl $name external unix_group $name    # $label"
    
    alterator-cmdline "/squid/groups/$name/domains" action "list" | \
	on_object print_access "$name" "$deny" `read_val "AUTHENTICATED"`
    
    local finish_ln=`read_val "access_$name"`
    if [ -n "$finish_ln" ] && [ "$finish_ln" = "yes" ]; then
	echo "    # $label"
    fi
}

alterator-cmdline /squid/groups action "list" | on_object print_group
