# $arg1 - the network we're adding to
# $arg2 - x position of the tile
# $arg3 - y position of the tile
# $arg4 - the output of the new node goes into this node
# $arg5 - this is which input of the above node we go into
# $arg6 - this node connects to our first input
# $arg7 - type of node to add
# $arg8 - is the node an indirect input 
# $arg9 - is the index of the output from $arg6
# arg10 - pass 1 for autolayout, 2 for autolayout & autoscroll

\set noalias = 1
set saved_path = `execute("oppwf")`
opcf $arg1 

# Node $name
set cmd = opadd -v $arg7 `$arg7`1

set name = `run($cmd)`
oplocate -x $arg2 -y $arg3 $name
opset -C on -p on $name

if ($argc >= 5 && "$arg4" != "") then
    set output = $name
    opwire $output -$arg5 $arg4
endif

set outputidx = "0"
if ($argc >= 9 && "$arg9" != "") then
    set outputidx = $arg9
endif

if ($arg5 < -1 && $argc >= 6 && "$arg6" != "") then
    foreach dir (`execute("opdepend -o -b -l 1 -u $outputidx $arg6")`)
	if ($dir = "") then
	    break;
	endif
	set i = `execute("opgetinput -o $arg6 -u $outputidx $dir")`
	while ( $i >= 0 )
	    opwire $name -$i $dir
	    set i = `execute("opgetinput -o $arg6 -u $outputidx $dir")`
	end
    end
endif

if ($argc >= 6 && "$arg6" != "") then
    set input = $name
    if ($arg8) then
	opwire -i $arg6 -0 $input
    else
	opwire -o $outputidx $arg6 -0 $input
    endif
endif

if($argc >= 10) then
    if($arg10 == 1) then
        opautoplace $name
    else
        if($arg10 == 2) then
           opautoplace -i $name
        endif
    endif
endif

opcf $saved_path
