#! /usr/bin/tclsh
# This file is part of secnet.
# See LICENCE and this file CREDITS for full list of copyright holders.
# SPDX-License-Identifier: GPL-3.0-or-later
# There is NO WARRANTY.

source mtest/common.tcl

#----- success test -----

set good [list Uuser Ginside -u \
          mtest/header.sites $tmp/groupfiles $tmp/out.sites Ginside \
	  < mtest/Ginside.sites]

eval run-mss-userv $good

diff-output e-userv out .sites

#----- argument parser does not look for args beyond header -----

set env(LC_MESSAGES) C

set try [lreplace $good 4 4 --misparse-test]

if {![catch {
    eval run-mss-userv $try
} emsg]} {
    error "should have failed"
} else {
    switch -glob $emsg {
	{*unrecognized arguments: --misparse-test*} {
	    error "misparsed!"
	}
	{*No such file or directory: '--misparse-test/TGinside'*} {
	}
	* {
	    error "huh ? $emsg"
	}
    }
}

#----- wrong group is rejected -----

set bad [lreplace $good 1 1 Gwrong]

if {![catch {
    eval run-mss-userv $bad
} emsg]} {
    error "should have failed"
} else {
    switch -glob $emsg {
	{*caller not in group Ginside*} { }
	* { error "huh ? $emsg" }
    }
}

#----- resulting sites file is processable -----

eval run-mss $tmp/out.sites >$tmp/out.conf

diff-mss-sites-conf mtest/e-userv.conf $tmp/out.conf

#----- works without group in `location' line -----

set testgf $tmp/Ginside.no-group

exec sed < mtest/Ginside.sites > $testgf {/^location / s/ root$//}

set testargs [lreplace $good end end $testgf]
eval run-mss-userv $testargs
