::tcltest::configure {*}$::argv

set output_file "output"
set work_dir "work"

# constraint for tests that should only run on Darwin
::tcltest::testConstraint darwin [expr {"@OS_PLATFORM@" eq "darwin"}]

# constraint indicating whether the platform supports trace mode
::tcltest::testConstraint tracemode_support [expr {@TRACEMODE_SUPPORT@ != 0}]

# Set of procs used for testing.

# Sets $bindir variable from autoconf
proc load_variables {pwd} {
    global bindir datadir portsrc cpwd

    set cpwd [file dirname [file dirname $pwd]]

    set prefix "@prefix@"
    set exec_prefix "@exec_prefix@"
    set bindir "@bindir@"
    set datadir "@datarootdir@"
    set portsrc ${cpwd}/test-macports.conf

}

proc cleanup {} {
    global cpwd

    file delete -force /tmp/macports-tests
    file delete -force ${cpwd}/PortIndex ${cpwd}/PortIndex.quick
}

# Sets initial directories
proc set_dir {} {
    global datadir cpwd

    cleanup

    file mkdir /tmp/macports-tests/ports \
               /tmp/macports-tests/opt/local/etc/macports \
               /tmp/macports-tests/opt/local/share \
               /tmp/macports-tests/opt/local/var/macports/receipts \
               /tmp/macports-tests/opt/local/var/macports/registry \
               /tmp/macports-tests/opt/local/var/macports/build

    file link -symbolic /tmp/macports-tests/opt/local/share/macports $datadir/macports
    file link -symbolic /tmp/macports-tests/ports/test $cpwd/test
}

# Run portindex
proc port_index {} {
    global bindir cpwd

    # Move up 2 level to run portindex.
    set path [pwd]
    cd ../..
    # Avoid warning about ports tree being old
    exec -ignorestderr sh -c {touch */*/Portfile}

    exec -ignorestderr ${bindir}/portindex 2>@1

    file copy ${cpwd}/sources.conf /tmp/macports-tests/opt/local/etc/macports/
    file copy ${cpwd}/PortIndex ${cpwd}/PortIndex.quick /tmp/macports-tests/ports/

    cd $path
}

# Executes port clean.
proc port_clean {pwd} {
    global bindir portsrc

    set back [pwd]
    cd $pwd

    catch {exec -ignorestderr env PORTSRC=${portsrc} ${bindir}/port clean 2>@1}
    cd $back
}

# Runs the portfile.
proc port_run {pwd} {
    global bindir portsrc

    set back [pwd]
    cd $pwd

    set result [catch {exec -ignorestderr env PORTSRC=${portsrc} ${bindir}/port -d -N test >&output} ]
    cd $back
    return $result
}

# Runs port trace.
proc port_trace {pwd} {
    global bindir portsrc

    set back [pwd]
    cd $pwd

    set result [catch {exec -ignorestderr env PORTSRC=${portsrc} ${bindir}/port -d -N -t test >&output 2>@1} ]
    cd $back
    return $result
}

# Installs new portfile.
proc port_install {{name "current"}} {
    global bindir portsrc

    set result [catch {exec -ignorestderr env PORTSRC=${portsrc} ${bindir}/port install $name > output 2>@1} ]
    return $result
}

# Run configure command.
proc port_config {pwd} {
    global bindir portsrc

    set result [catch {exec -ignorestderr env PORTSRC=${portsrc} ${bindir}/port configure 2>@1} ]
    return $result
}

# Run destroot command.
proc port_destroot {pwd} {
    global path bindir portsrc work_dir output_file

    file copy -force ${path}/statefile ${work_dir}/.macports.statefile-unknown-version.state
    if { [exec -ignorestderr id -u] == 0 } {
        exec -ignorestderr chown macports ${work_dir}/.macports.statefile-unknown-version.state
    }
    set result [catch {exec -ignorestderr env PORTSRC=${portsrc} ${bindir}/port destroot >$output_file 2>@1} ]
    return $result
}

# Uninstalls portfile.
proc port_uninstall {{name "current"}} {
    global bindir portsrc

    set result [catch {exec -ignorestderr env PORTSRC=${portsrc} ${bindir}/port uninstall $name > output 2>@1} ]
    return $result
}

# Returns the line containing a given string
# from a given file, or -1 if nothing is found.
proc get_line {filename lookup} {
    set fp [open $filename r]
    set lookup [string tolower $lookup]

    while {[gets $fp line] != -1} {
        set line [string tolower $line]

        if {[string match $lookup $line] != 0} {
            close $fp
            return $line
        }
    }
    close $fp
    return -1
}

# This proc contains all the steps necessary
# to install a port and save the output to a file.
# Needed for the majority of regression tests.
proc initial_setup {} {
    global output_file work_dir path

    makeFile "" $output_file
    makeDirectory $work_dir

    load_variables $path
    set_dir
    port_index
    port_clean $path
    port_run $path
}
