# -*- 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 ../port_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

package require portload 1.0
source ../port_autoconf.tcl


test load_main {
    Load main unit test.
} -constraints {
    root

} -setup {
    set path /Library/test/files
    file mkdir $path

    # Create dummy plist
    set fd [open $path/file.plist w+]
    puts $fd "\<?xml version=\"1.0\" encoding=\"UTF-8\"?\>"
    puts $fd "\<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"\>"
    puts $fd "\<plist version=\"1.0\"\>"
    puts $fd "\<dict\>"
    puts $fd "\<key\>Label\</key\>"
    puts $fd "\<string\>org.test.load\</string\>"
    puts $fd "\<key\>ProgramArguments\</key\>"
    puts $fd "\<array\>\<string\>/bin/ls\</string\>\</array\>"
    puts $fd "\</dict\>"
    puts $fd "\</plist\>"
    close $fd

} -body {
    set launchctl_path ${portutil::autoconf::launchctl_path}
    set startupitem.location test
    set startupitem.plist files/file.plist

    if {[portload::load_main] != ""} {
        return "FAIL: unable to load plist"
    }
    return "Load successful."

} -cleanup {
    exec -ignorestderr $launchctl_path unload -w $path
    file delete -force /Library/test

} -result "Load successful."


cleanupTests
