# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

package require tcltest 2
namespace import tcltest::*

set pwd [file dirname [file normalize $argv0]]

source ../package_test_autoconf.tcl
package require macports 1.0

array set ui_options {}
#set ui_options(ports_debug)   yes
#set ui_options(ports_verbose) yes
mportinit ui_options

source ./library.tcl
macports_worker_init

package require portarchivefetch 1.0
namespace eval portutil {
    namespace eval autoconf {
        variable openssl_path "/usr/bin/openssl"
    }
}

test filter_sites {
    Filter sites unit test.
} -setup {
    global os.platform os.major os.arch destpath package.destpath configure.build_arch
    global subport version revision package.flat maintainers description categories
    global supported_archs name porturl portdbpath portpath workpath distname filespath license
    env_init

    set portvariants {var1}
    set porturl http://www.fondu.sourceforge.net/fondu-060102_1var1.darwin_9.noarch.tgz

} -body {
    if {[portarchivefetch::filter_sites] != "http://www.fondu.sourceforge.net/:tgz"} {
        return "FAIL: couldn't fetch file"
    }

    return "Filter sites successful."
} -result "Filter sites successful."


test checkarchivefiles {
    Check archive files unit test.
} -setup {
    global os.platform os.major os.arch destpath package.destpath configure.build_arch
    global subport version revision package.flat maintainers description categories
    global supported_archs name porturl portdbpath portpath workpath distname filespath license
    env_init

    set urls {www.a.com www.b.com}
    set all_archive_files ""
    set archivefetch.fulldestpath $pwd/path
    set archive_sites {site1:zip site2:tgz}

} -body {
    if {[catch {portarchivefetch::checkarchivefiles $urls}] != 0} {
        return "FAIL: cannot run checkarchivefiles"
    }
    if {[lindex $all_archive_files 0] != "fondu-060102_1var1.darwin_9.noarch.zip"} {
        return "FAIL: invalid archive file name"
    }
    if {[lindex $all_archive_files 1] != "fondu-060102_1var1.darwin_9.noarch.tgz"} {
        return "FAIL: invalid archive file name"
    }
    return "Check archive files successful."
} -result "Check archive files successful."


# test get_full_archive_sites_path
# test checkfiles


test fetchfiles {
    Fetch files unit test.
} -constraints {
    root
} -setup {
    global os.platform os.major os.arch destpath package.destpath configure.build_arch
    global subport version revision package.flat maintainers description categories
    global supported_archs name porturl portdbpath portpath workpath distname filespath license
    env_init

    array set ::portfetch::urlmap {
        db_select http://packages.macports.org/db_select/
    }
    set portarchivefetch::archivefetch_urls {db_select db_select-0.1_3.darwin_14.noarch.tbz2}
    set target_state_fd [open $pwd/target w+]

    set mport [mportopen file://.]
    set workername [ditem_key $mport workername]
    set macportsuser macports

    set oldchannels [array get macports::channels]
    set macports::channels(msg)    {}
    set macports::channels(notice) {}
    set macports::channels(warn)   {}
} -body {

    set archivefetch.pubkeys $pwd
    if {[catch {portarchivefetch::fetchfiles}] != 0} {
        return "FAIL: cannot fetch archive"
    }
    if {[file exists ${archivefetch.fulldestpath}/db_select-0.1_3.darwin_14.noarch.tbz2]} {
        return "FAIL: unverified archive not discarded"
    }

    set archivefetch.pubkeys ${macports::prefix}/share/macports/macports-pubkey.pem
    if {[portarchivefetch::fetchfiles] != 0} {
        return "FAIL: cannot fetch archive"
    }
    if {![file exists ${archivefetch.fulldestpath}/db_select-0.1_3.darwin_14.noarch.tbz2]} {
        return "FAIL: verified archive deleted"
    }

    return "Fetch files successful."

} -cleanup {
    array set macports::channels $oldchannels

    close $target_state_fd
    file delete -force $pwd/target
    mportclose $mport
    file delete -force $portdbpath
} -result "Fetch files successful."


# test archivefetch_start
# test archivefetch_main


cleanupTests
