update_GETOPTS='sSrR'

UPDATE_SERIAL=
UPDATE_RELOAD=
UPDATE_INCLUDES=

. $LIB/pvl/hosts/include_cache.sh
. $LIB/pvl/hosts/dhcp.sh
. $LIB/pvl/hosts/zone.sh

function update_help {
    cat <<END
Update:
    -s      force update serials
    -S      do not update serial

    -r      force reload zones/dhcp
    -R      do not reload zones/dhcp
END
}

function update_opt {
    local opt=$1
    local optarg="$2"

    case $opt in
        s)  UPDATE_SERIAL=1 ;;
        S)  UPDATE_SERIAL=0 ;;

        r)  UPDATE_RELOAD=1 ;;
        R)  UPDATE_RELOAD=0 ;;
        
        F)  UPDATE_INCLUDES=1 ;;
        n)
            UPDATE_SERIAL=0
            UPDATE_RELOAD=0
            UPDATE_INCLUDES=0
        ;;
        *)  return 1
    esac
}

function update_setup {
    for dir in etc etc/zones etc/hosts; do
        [ -d $dir ] || die "$dir: missing source directory"
    done
    
    apply_dir       var

    for dir in var/dhcp var/zones var/include-cache var/serials; do
        apply_dir   $dir
    done
    for dir in var/dhcp/hosts; do
        apply_dir   $dir
    done
    for dir in var/zones/includes var/zones/forward var/zones/reverse; do
        apply_dir   $dir
    done
}

function update_commit {
    # TODO: pre-commit test host files

    log "Commit..."
        commit  $SRV
}

function update_update {
    if commit_modified $SRV; then
        serial=$(unix_time)
        log_warn "Using local unix time for uncommited changes: $serial"
    else
        serial=$(commit_time $SRV)
        log "Using commit timestamp: $serial"
    fi

    ## Hosts
    log "Updating forward host zones..."
    for zone in $(list_dirs etc/zones/forward); do
        update_hosts_forward "var/zones/forward/$zone" "etc/zones/forward/$zone"
    done

    log "Updating reverse host zones..."
    for zone in $(list_dirs etc/zones/reverse); do
        update_hosts_reverse "var/zones/reverse/$zone" "etc/zones/reverse/$zone"
    done

    log "Updating DHCP hosts..."
    for hosts in $(list etc/dhcp/hosts); do
        update_hosts_dhcp "var/dhcp/hosts/$hosts.conf" "etc/dhcp/hosts/$hosts"
    done

    ## Zones
    log "Copying zone includes..."
    for zone in $(list_files etc/zones/includes); do
        update_zone_include "var/zones/includes/$zone" "etc/zones/includes/$zone"
    done

    log "Updating zones..."
    for zone in $(list_files etc/zones); do
        update_zone_includes "var/include-cache/$zone" "etc/zones/$zone"

        zone_includes=$(cat "var/include-cache/$zone")

        update_zone_serial "var/serials/$zone" $serial \
            "etc/zones/$zone" $zone_includes

        update_zone "var/zones/$zone" "etc/zones/$zone" "var/serials/$zone" \
            $zone_includes
    done

    log "Updating DHCP confs..."
    for conf in $(list_files etc/dhcp); do
        update_dhcp_conf "var/dhcp/$conf" "etc/dhcp/$conf"
    done
}

function update_reload {
    ## Check
    log "Testing zones..."
    for zone in $(list_files etc/zones); do
        test_zone "var/zones/$zone" $zone
    done

    log "Reload zones..."
        reload_zones

    log "Testing DHCP..."
        test_dhcp

    log "Reload DHCP..."
        reload_dhcp
}

## Main entry point
function update_main {
    ## Setup source/output dirs
    update_setup
   
    ## Commit source dirs
    update_commit

    ## Update output from sources
    update_update

    ## Reload output
    update_reload
}

# entry point
MODULES=(log commit apply service test)
MODULE=hosts
MAIN_MODULE=update

. $LIB/pvl/main.sh && main "$@"
