#!/bin/sh

#turn off auto expansion
set -f


. /usr/share/alterator/build/backend3.sh

on_message()
{
        echo "#$in_action#" >&2
        case "$in_action" in
                #information for renderer
                template)
                        case "$in__objects" in
		       generate)
		           
			   echo '(template "form" url "/var/www/html/fbi/results.xhtml")'
		       ;;
		       import)
			   echo -n '(template "form" url "/var/www/html/fbi/ok.xhtml" '
 			   echo -n 'redirect "/oldfw")'
		       ;;
		       "export")
			   echo -n '(template "form" url "/var/www/html/fbi/ok.xhtml" '
 			   echo -n 'redirect "/oldfw")'
		       ;;
		       install)
			   echo -n '(template "form" url "/var/www/html/fbi/ok.xhtml"  '
 			   echo -n 'redirect "/oldfw")'
		       ;;
		       try)
			   echo -n '(template "form" url "/var/www/html/fbi/ok.xhtml" '
 			   echo -n 'redirect "/oldfw")'
		        ;;
		       default)
			   echo -n '(template "form" url "/var/www/html/fbi/ok.xhtml" '
 			   echo -n 'redirect "/oldfw/")'
		        ;;
		       v1/*/chains)
                                echo -n '('
                                echo -n 'template "form" '
				echo -n 'url "/var/www/html/fbi/chains.xhtml" '
                                echo ')'
		       ;;     
                       
                        
		      v1/*/*/rules)
                      set -x
		      #figure out if is there any rules in that chain?
		      tm1=${in__objects%/rules}
		      tm2=${tm1#v1/}
		      ZON=${tm2%%/*}
		      CHAIN=${tm2##*/}
		      if [ "`ls /etc/dwall-alt/expert/v1/$ZON/$CHAIN`" == "" ]; then

                                echo -n '('
                                echo -n 'template "form" '
                                echo -n 'url "/var/www/html/fbi/rulesnd.xhtml" )'
			else
                       		
                                echo -n '('
                                echo -n 'template "form" '
                                echo -n 'url "/var/www/html/fbi/rules.xhtml" )'
			fi
			;;

			v1/*/*/rules/*new)
			                    echo -n '('
								echo -n 'template "form"'
								echo -n  'url "/var/www/html/fbi/newrule.xhtml"'
								echo -n  "redirect \"/oldfw/${in__objects%/new}\""
								echo ')'
						;;
			   v1/*/*/rules/*)
			   	echo  '(template "form" url "/var/www/html/fbi/rule.xhtml"  '
				echo -n "redirect \"/oldfw/${in__objects%/*}\""
                                echo ')'
					  ;;
			   *)
			   echo '(template "form" url "/var/www/html/fbi/oldfw-main.html")'
			   ;;
                        esac
                        ;;
	 	info)
			echo '('
			printf ' title "advanced firewall" '
			printf ' description "Cетевой экран с детальным редактированием правил"'
			printf ' group "Firewall"'
			echo ')'
			;;
                *)
                        echo "#f"
                ;;
        esac
}

message_loop
