if {$::tcl_platform(platform) ne "windows"} {
    return
}

package ifneeded twapi_base @PACKAGE_VERSION@ \
    [list apply [list {dir} {
        package require platform
        set packageVer [string map {. {}} @PACKAGE_VERSION@]
        if {[package vsatisfies [package require Tcl] 9]} {
            set baseDllName "@PKG_LIB_FILE9@"
        } else {
            set baseDllName "@PKG_LIB_FILE8@"
        }
        set package "@PACKAGE_NAME@"
        set package_ns ::$package
        namespace eval $package_ns {}
        set package_init_name [string totitle $package]

        # Try to load from current directory and if that fails try from
        # platform-specific directories. Note on failure to load when the DLL
        # exists, we do not try to load from other locations as twapi modules
        # may have been partially set up.

        set dllFound false
        foreach platform [linsert [::platform::patterns [platform::identify]] 0 .] {
            if {$platform eq "tcl"} continue
            set path [file join $dir $platform $baseDllName]
            if {[file exists $path]} {
                uplevel #0 [list load $path $package_init_name]
                set dllFound true
                break
            }
        }

        if {!$dllFound} {
            error "Could not locate TWAPI dll."
        }

        # Load was successful
        set ${package_ns}::dllPath [file normalize $path]
        set ${package_ns}::packageDir $dir
        source [file join $dir twapi.tcl]
        package provide twapi_base @PACKAGE_VERSION@
    }] $dir]

set __twapimods {
    com
    msi
    power
    printer
    synch
    security
    account
    apputil
    clipboard
    console
    crypto
    device
    etw
    eventlog
    mstask
    multimedia
    namedpipe
    network
    nls
    os
    pdh
    process
    rds
    registry
    resource
    service
    share
    shell
    storage
    ui
    input
    winsta
    wmi
}
foreach __twapimod $__twapimods {
    package ifneeded twapi_$__twapimod @PACKAGE_VERSION@ \
        [list apply [list {dir mod} {
            package require twapi_base @PACKAGE_VERSION@
            source [file join $dir $mod.tcl]
            package provide twapi_$mod @PACKAGE_VERSION@
        }] $dir $__twapimod]
}

package ifneeded twapi @PACKAGE_VERSION@ \
    [list apply [list {dir mods} {
        package require twapi_base @PACKAGE_VERSION@
        foreach mod $mods {
            package require twapi_$mod @PACKAGE_VERSION@
        }
        package provide twapi @PACKAGE_VERSION@
    }] $dir $__twapimods]

unset __twapimod
unset __twapimods
