# -*- 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

source ./library.tcl
macports_worker_init

package require port 1.0
package require portstartupitem 1.0


test startupitems_executable {
    Check that setting the startupitem.executable option behaves the\
    same as setting executable via the startupitems option.
} -setup {
    set system_options(startupitem_install) yes
} -body {
    startupitem.executable  ${prefix}/sbin/mydaemon -flag1 "quoted string"
    portstartupitem::foreach_startupitem {
        set single_result [list {*}$si_executable]
    }
    startupitems \
                name        testname \
                executable  "${prefix}/sbin/mydaemon -flag1 \"quoted string\""
    portstartupitem::foreach_startupitem {
        set multi_result [list {*}$si_executable]
    }
    if {$single_result ne $multi_result} {
        return "ERROR: single_result = '$single_result' but multi_result = '$multi_result'"
    }
    return "startupitems_executable successful."
} -cleanup {
    startupitems
} -result "startupitems_executable successful."


test startupitems_name {
    Check that setting the startupitem.name option behaves the\
    same as setting name via the startupitems option.
} -setup {
    set system_options(startupitem_install) yes
} -body {
    startupitem.name        testname
    portstartupitem::foreach_startupitem {
        set single_result [list {*}$si_name]
    }
    startupitems \
                name        testname
    portstartupitem::foreach_startupitem {
        set multi_result [list {*}$si_name]
    }
    if {$single_result ne $multi_result} {
        return "ERROR: single_result = '$single_result' but multi_result = '$multi_result'"
    }
    return "startupitems_name successful."
} -cleanup {
    startupitems
} -result "startupitems_name successful."


test startupitems_start {
    Check that setting the startupitem.start option behaves the\
    same as setting start via the startupitems option.
} -setup {
    set system_options(startupitem_install) yes
} -body {
    startupitem.start       "${prefix}/sbin/daemon1 start -flag1 \"quoted string\"" \
                            "${prefix}/sbin/daemon2 start -flag2 -flag3"
    portstartupitem::foreach_startupitem {
        set single_result [list {*}$si_start]
    }
    startupitems \
                name        testname \
                start       "\"${prefix}/sbin/daemon1 start -flag1 \\\"quoted string\\\"\" \
                            \"${prefix}/sbin/daemon2 start -flag2 -flag3\""
    portstartupitem::foreach_startupitem {
        set multi_result [list {*}$si_start]
    }
    if {$single_result ne $multi_result} {
        return "ERROR: single_result = '$single_result' but multi_result = '$multi_result'"
    }
    return "startupitems_start successful."
} -cleanup {
    startupitems
} -result "startupitems_start successful."


cleanupTests
