#!/usr/bin/env tclsh

# 2026/05
# - add underscores to the rp-program name to avoid
#   collision with existing tcl commands (e.g. "panel")

# 2025/05
# - adapted to handle 'pipexe':
# - args are now: creator creator_arg
# - see luc.tcl
#
# 2025/03 -> compatibility with lurette etc.
# - NO LONGER USE stdout, i.e.:
#     no pipe to tclsh
#     no dump to stout
# - INSTEAD:
#     generates an actual luc file
#     whose name is a parameter

# DEPRECATED
# usage: rp2luc.luc  prgfile [-dump]
# - prgfile is a file accepted by rpexe (.ec, .dro)
# - without '-save':
#   simply runs a default luciole gui for prgfile
# - with '-save':
#   outputs the corresponding luc on stdout
#

# 2026 -> load lustubs more complex, put in separate file
# load lustubs (provides droexe/ecexe)
#load $::env(LUSTRE_INSTALL)/tcl/lustubs.so
source $::env(LUSTRE_INSTALL)/tcl/luciole/luc-load.tcl

source $::env(LUSTRE_INSTALL)/tcl/luciole/pipe.tcl

set Param(tool) [file tail $argv0]

proc usage { } {
	global Param
	puts stderr "Usage: $Param(tool) \[-v\] <desc> <file.luc>"
	puts stderr "  where <rp-desc> := droexe <file.dro> | ecexe <file.ec> | pipexe <command>"
}
proc _error { msg } {
	puts stderr "Error: $msg"
	exit 1
}
proc verbose { msg } {
	global Param
	if { $Param(verbose) } {
		puts stderr "#$msg"
	}
}

set Param(verbose) 0
set Param(creator) ""
set Param(creator_arg) ""
set Param(outfile) ""

set Param(others) {}

proc parse_args { argv } {
	global Param
#puts "parse_args $argv"
#puts "nb=[llength $argv]"
	foreach arg $argv {
		switch -glob $arg {
			"-v" { set Param(verbose) 1; lappend Param(others) $arg }
			"-*" { lappend Param(others) $arg }
			default {
				if { $Param(creator) == "" } {
					set Param(creator) $arg
				} elseif { $Param(creator_arg) == "" } {
					set Param(creator_arg) $arg
				} elseif { $Param(outfile) == "" } {
					set Param(outfile) $arg
				} else {
					usage; _error "unexpected args ($arg)"
				}
			}
		}
	}
}

# implicit current folder not allowed
# must add a './'
proc unimplicit_file { f } {
	if { [file pathtype $f] == "relative" } {
		set cl [file split $f]
		set hd [lindex $cl 0]
		set hddir [file dirname $hd]
		if { $hd != $hddir } {
			return [file join $hddir $f]
		}
		return $f
	}
	return $f
}

# can't get desired behavior with 'open |tclsh'
# use 'chan pipe' instead,
# WARNING tcl > 8.5 necessary

proc do_luc { loadcmd execmd exearg outfile } {
	global Param
	verbose "$Param(tool): generate '$outfile' for '$execmd' \"$exearg\""
	if [ catch { $loadcmd zeprg $exearg } errmsg ] {
		_error $errmsg
	}
	# add underscores to prevent mismatch with other tcl commands (e.g. "panel")
	set zeprgname "_[zeprg name]_"
	if { $zeprgname == "" } {
		set zeprgname "__unnamed__"
	}
	set outc [open $outfile w]

	proc dump { args } {
		upvar outc outc
		puts $outc {*}$args
	}
	proc dump_nnl { args } {
		upvar outc outc
		puts -nonewline $outc {*}$args
	}
	dump \
"#!/usr/bin/env tclsh
# generated by: $Param(tool)

lappend auto_path  \$::env(LUSTRE_INSTALL)/tcl
package require Luciole 2.0
namespace import luc::*

# accepts default luciole opts
luc::parse_args

# change font magnification (%)
ezfont::scale 20
"

	dump "$execmd $zeprgname \"$exearg\" \{"
	set nbi [zeprg nb_ins]
	for {set i 0} {$i<$nbi} {incr i} {
		set n [zeprg in_name $i]
		set t [zeprg in_type $i]
		dump "   \{ \"$n\" \"$t\" \}"
	}
	dump "\} \{"
	set nbo [zeprg nb_outs]
	for {set i 0} {$i<$nbo} {incr i} {
		set n [zeprg out_name $i]
		set t [zeprg out_type $i]
		dump "   \{ \"$n\" \"$t\" \}"
	}
	dump "\}
"

dump "panel inputs col -text \"Inputs\" \{"
	for {set i 0} {$i<$nbi} {incr i} {
		set n [zeprg in_name $i]
		dump_nnl " \$$n"
	}
dump "
\}
"

dump "panel outputs col -text \"Outputs\" \{"
	for {set i 0} {$i<$nbo} {incr i} {
		set n [zeprg out_name $i]
		dump_nnl " \$$n"
	}
dump "
\}

stdgui line \{
   \$inputs
   \$outputs
\}
"
}

#set chan [open "|tclsh" r+]

proc main { argv } {
	global Param
	parse_args $argv
	set execmd $Param(creator)
	set exearg $Param(creator_arg)
	set outfile $Param(outfile)
	# load command can be deduced from execmd
	if { $execmd == "" } { usage; exit 0 }
	if { $exearg == "" } { usage; exit 0 }
	if { $outfile == "" } { usage; exit 0 }
	switch $execmd {
		"droexe" { set loadcmd "droload" }
		"ecexe" { set loadcmd "ecload" }
		"pipexe" { set loadcmd "pipecreate" }
		default {
			_error "unexpected creator '$execmd'"
		}
	}
#puts stderr "do_luc $loadcmd $execmd $exearg $outfile"
	do_luc $loadcmd $execmd $exearg $outfile
	file attributes $outfile -permissions +x
}

main $::argv
