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

#
# Read zone prologue
#
# Syntaxe :
#   dnsreadprol <zone> <view>
#
# History
#   2004/05/25 : pda      : design with sh
#   2005/04/11 : pda      : rewriting with Tcl
#   2010/12/18 : pda      : use new install system
#   2013/04/05 : pda/jean : add views
#

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

##############################################################################
# Small utility functions
##############################################################################

proc syntax-error {argv0} {
    regsub {.*/} $argv0 {} argv0
    return "usage: $argv0 zone view"
}

##############################################################################
# Main program
##############################################################################

proc main {argv0 argv} {
    global conf

    #
    # Initialization
    #

    set msg [d init-script dbfd $argv0 false tabcor]
    if {$msg ne ""} then {
	d error "$msg"
    }

    #
    # Argument checking
    #

    if {[llength $argv] != 2} then {
	d error [syntax-error $argv0]
    }
    lassign $argv zone view

    #
    # Check access to view
    #

    set idview [u viewid $view]
    if {$idview == -1} then {
	d error [mc "Invalid view '%s'" $view]
    }
    if {! [u isallowedview $idview]} then {
	d error [mc "Access denied to view '%s'" $view]
    }

    #
    # Get zone contents
    #

    set qzone [::pgsql::quote $zone]
    set sql "SELECT prologue FROM dns.zone
    				WHERE name = '$qzone' AND idview = $idview"

    set found 0
    pg_select $dbfd $sql tab {
	set found 1
	puts -nonewline stdout $tab(prologue)
    }

    if {! $found} then {
	d error [mc "Zone '%s' not found" $zone]
    }

    #
    # End
    #

    d end
    return 0
}

exit [main $argv0 $argv]
