#!/bin/sh

chroot="<%= chroot %>"
confdir="<%= confdir %>"
operatingsystem="<%= operatingsystem %>"

printview() {
    view=$1
    case $view in
        default)
          viewdir="${confdir}"
          acl="any"
          ;;
        *)
          viewdir="${confdir}/${view}"
          acl="${view}"
          ;;
    esac

    echo "view ${view} {"
    echo "    match-clients { ${acl}; };"

    case $operatingsystem in
        OpenBSD)
            cat <<EOF
    zone "." {
        type hint;
        file "/etc/root.hint";
    };
    zone "localhost" {
        type master;
        file "/standard/localhost";
    };
    zone "127.in-addr.arpa" {
        type master;
        file "/standard/loopback";
    };
    zone "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa" {
        type master;
        file "/standard/loopback6.arpa";
    };
EOF
            ;;
        Ubuntu)
            echo "    include \"${confdir}/named.conf.default-zones\";"
            ;;
        *)  
            cat <<EOF

    zone "." {
        type hint;
        file "/var/named/named.ca";
    };

EOF
            cat ${chroot}/etc/named.rfc1912.zones | \
                sed -e 's%file "%file "/var/named/%' -e 's/^/    /'
            ;;
    esac

    for name in ${chroot}${viewdir}/zone.* ; do
        [ -f ${name} ] || continue
        echo "    include \"${viewdir}/`basename ${name}`\";"
    done    
    echo "};"
    echo ""
}


cat <<EOF
include "${confdir}/named.conf.local";
include "${confdir}/named.conf.options";

EOF

for name in ${chroot}/${confdir}/* ; do
    [ -d ${name} ] || continue
    name=`basename ${name}`
    case ${name} in
        db.*)
            continue
            ;;
        *)
            printview ${name}
            ;;
    esac
done
printview default
