#!/usr/local/bin/tclsh8.6

#
# Choose which type of modification to apply to an IP address.
#
# Called by: net (host list by view or address map)
#
# Parameters (form or url):
#   - addr: IP address (v4 at this time)
#   - idview (optional): view id (or all possible views if not provided)
#
# History
#   2010/10/10 : pda      : specification
#   2010/12/11 : pda      : i18n
#   2010/12/25 : pda      : use cgi-dispatch
#   2012/11/29 : pda/jean : add views
#

#
# Template pages used by this script
#

set conf(page)		edit.html

#
# Next actions
# 

set conf(nextdel)	"del"
set conf(nextmod)	"mod"
set conf(nextlist)	"net"
set conf(nextsearch)	"search"

#
# Netmagis general library
#

source /usr/local/lib/netmagis/libnetmagis.tcl

# ::webapp::cgidebug ; exit

##############################################################################
# Main procedure
##############################################################################

d cgi-register {} {
    {addr	1 1}
    {idview	0 1}
} {
    global conf

    #
    # Check parameters
    #

    set msg [check-ip-syntax $dbfd $addr "inet"]
    if {$msg ne ""} then {
	d error $msg
    }

    #
    # Check that IP address belongs to one of our networks
    #

    set idcor $tabuid(idcor)

    if {! [check-authorized-ip $dbfd $idcor $addr]} then {
	d error [mc "You don't have rights on '%s'" $addr]
    }

    #
    # Get all possible views (if idview is not given) or get
    # only filtered views (if idview is given)
    #

    if {$idview == ""} then {
	set idviews {}
    } else {
	set idviews [list $idview]
    }
    set msg [filter-views $dbfd tabuid "addr" $addr $idviews chkv]
    if {$msg ne ""} then {
	d error $msg
    }

    set idviews $chkv(idviews)
    set nok [llength $chkv(ok)]

    #
    # Display selected actions for all views
    #

    set actions ""
    foreach idv $idviews {
	lassign $chkv($idv) vn msg t

	array unset trr
	array set trr $t

	set fqdn "$trr(name).$trr(domain)"

	if {$msg eq ""} then {
	    set vact ""

	    #
	    # Delete name
	    #
	    d urlset "" $conf(nextdel) [list \
						[list "name" $trr(name)] \
						[list "domain" $trr(domain)] \
						[list "idviews" $idv] \
					    ]
	    d urladdnext ""
	    set a [mc {<a href="%1$s">Remove</a> host '%2$s'} [d urlget ""] $fqdn]
	    append vact [::webapp::helem "li" $a]
	    append vact "\n"

	    #
	    # Delete IP address (if more than one IP address for this host)
	    #

	    set lip [rr-ip-by-view trr $idv]
	    if {[llength $lip] > 1} then {
		d urlset "" $conf(nextdel) [list \
						    [list "addr" $addr] \
						    [list "idviews" $idv] \
						]
		d urladdnext ""
		set a [mc {<a href="%1$s">Remove address '%2$s'</a> from '%3$s' (all other addresses of this host will stay)} [d urlget ""] $addr $fqdn]
		append vact [::webapp::helem "li" $a]
		append vact "\n"
	    }

	    #
	    # Modify informations
	    #

	    d urlset "" $conf(nextmod) [list \
						[list "action" "edit"] \
						[list "name" $trr(name)] \
						[list "domain" $trr(domain)] \
						[list "idview" $idv] \
					    ]
	    d urladdnext ""
	    set a [mc {<a href="%1$s">Edit informations</a> of '%2$s'} [d urlget ""] $fqdn]
	    append vact [::webapp::helem "li" $a]
	    append vact "\n"

	    set vact [::webapp::helem "ul" $vact]

	    #
	    # Display view name only if more than view is available
	    #

	    lassign [display-rr-masked $dbfd trr $idv {}] link desc
	    set title [mc {Address %1$s matches %2$s in view '%3$s'} $addr $link $vn]

	    set html "$title\n$desc\n$vact"
	    if {$nok <= 1} then {
		append actions $html
	    } else {
		append actions [::webapp::helem "li" $html]
	    }
	}
    }

    if {$nok > 1} then {
	set actions [::webapp::helem "ul" $actions]
    }

    #
    # Next action
    #

    switch -- [d nextprog] {
	search {
	    d urlset "%URLBACK%" $conf(nextsearch) [list \
	    						[d nextargs] \
						    ]
	}
	map {
	    d urlset "%URLBACK%" $conf(nextlist) [list \
							[list "domap" "yes"] \
							[d nextargs] \
						    ]
	}
	list -
	default {
	    d urlset "%URLBACK%" $conf(nextlist) [list \
							[list "dolist" "yes"] \
							[d nextargs] \
						    ]
	}
    }

    #
    # End of script: output page and close database
    #

    d result $conf(page) [list \
				[list %ADDR%    $addr] \
				[list %NAME%    $trr(name)] \
				[list %DOMAIN%  $trr(domain)] \
				[list %ACTIONS% $actions] \
			    ]
}

##############################################################################
# Main procedure
##############################################################################

d cgi-dispatch "dns" ""

