<commit_before>#!/bin/sh
# this line restarts using wish \
exec wish "$0" "$@"

###############################################################
# Description:  setupconfig.tcl
#               This file, shows existing emc2 configuration
#               and allows copying and naming
#
#  Author: <NAME> & <NAME>
#  License: GPL Version 2
#
#  Copyright (c) 2005 All rights reserved.
#
#  Last change:
# $Revision$
# $Author$
# $Date$
###############################################################
#
# usage:
#    setupconfig [options] 
#
# options:
#  --configs-dir <dir>
#
#   Assume that <dir> is the configs/ directory, and that
#   subdirectories of <dir> contain individual configs.
#   If --configs-dir is not given, the script attempts to
#   find the directory itself, but that works only if the
#   environment variable EMC2_ORIG_CONFIGS_DIR is set, or
#   if the script was run from the configs dir, or one 
#   directly above or below it.
#
#  --run [<config-name>/<ini-name>]
#
#   Validate the config-name/ini-name that was supplied (if
#   any), if not supplied or invalid, go directly to the 
#   screen that asks the user to choose a config.  Once
#   the config is validated (or the user chooses one and 
#   clicks "Run"), invoke the run script to run EMC using
#   that configuration.
#
#  --get-config [<config-name>/<ini-name>]
#
#   Validate the config-name/ini-name that was supplied (if
#   any), if not supplied or invalid, go directly to the 
#   screen that asks the user to choose a config.  Once
#   the config is validated (or the user chooses one and 
#   clicks 'RUN'), do _not_ invoke the run script to run it,
#   instead simply print the chosen <config/ini> string and
#   end.  (This mode is for use by the run script.)
#
#  --new
#
#   Skip the initial menu and go straight to the "create
#   new config" sequence.
#
#  --run, --get-config, and --new are mutually exclusive. 
#  If none of these is specified, setupconfig displays a 
#  sequence of menus so the user can manipulate configurations
#  and/or run emc. 
#
###############################################################

# This logo is used on all the pages
set wizard_image_search "/etc/emc2/emc2-wizard.gif /usr/local/etc/emc2-wizard.gif emc2-wizard.gif"

foreach wizard_image $wizard_image_search {
    if { [file exists $wizard_image] } {
        set logo [image create photo -file $wizard_image]
    }
}

################### PROCEDURE DEFINITIONS #####################

# Internationalisation (i18n)
# in order to use i18n, all the strings will be called [msgcat::mc "string-foo"]
# instead of "string-foo".
# Thus msgcat searches for a translation of the string, and in case one isn't 
# found, the original string is used.
# In order to properly use locale's the env variable LANG is queried.
# If LANG is defined, then the folder src/po is searched for files
# called *.msg, (e.g. en_US.msg).
# That file should contain all the translations regarding tcl scripts 
# (tkemc, mini, setupconfig.tcl, etc)

package require msgcat
if ([info exists env(LANG)]) {
    msgcat::mclocale $env(LANG)
    msgcat::mcload "../../src/po"
}


# a kluge for older systems that don't have 'file normalize'
# (which was introduced with tcl 8.4)
# this version converts to an absolute path and eliminates 
# any references to ./ and ../, but it doesn't do all the 
# other things that normalize can do, such as resolve symlinks.
# it seems good enough for what this program needs to do

proc file_normalize { input } {
    # make path absolute
    if { [ file pathtype $input ] != "absolute" } {
	set input [ file join [ pwd ] $input ]
    }
    # split into component parts
    set path_list [ file split $input ]
    # remove any instances of ./
    set idx [ lsearch $path_list "." ]
    while { $idx > 0 } {
	set path_list [ lreplace $path_list $idx $idx ]
	set idx [ lsearch $path_list "." ]
    }
    # remove any instances of ../ (and the preceding directory name)
    set idx [ lsearch $path_list ".." ]
    while { $idx > 0 } {
	set path_list [ lreplace $path_list [ expr $idx -1 ] $idx ]
	set idx [ lsearch $path_list ".." ]
    }
    set output ""
    foreach name $path_list {
	set output [ file join $output $name ]
    }
    return $output
}

# reads the directory, and fills in "config_list" and "details_list"

proc get_config_list {} {
    global config_list details_list configs_dir

    # clear config and description lists
    set config_list [ list ]
    set details_list [ list ]
    # look at all subdirs
    foreach dir_name [ glob */ ] {
	cd $dir_name
	# is there an ini file (or more than one) inside?
	set inifnames [concat [ glob -nocomplain *.ini ]]
	if { [llength $inifnames]!= "0" } {
	    # yes, this is a viable config
	    # strip trailing / 
	    regsub "/" $dir_name "" config_name
	    # and save it
	    lappend config_list $config_name
	    # look for a README file
	    if { [ file isfile "README" ] } {
		# description found, read it
		set descr [ read -nonewline [ open "README" ]]
		# reformat - remove line breaks, preserve paragraph breaks
		regsub -all {([^\n])\n([^\n])} $descr {\1 \2} descr
		# and save it
		lappend details_list $descr
	    } else {
		# no description, gotta tell the user something
		lappend details_list [msgcat::mc "No details available."]
	    }
	}
	# back to main configs directory
	cd ..
    }
}

# main button callback, it assigns the button name to 'choice'

proc button_pushed { button_name } {
    global choice

    set choice $button_name
}

# generic popup, displays a message and waits for "OK" click

proc popup { message } {
    global choice top logo

    bind . <Escape> {button_pushed OK}
    bind . <Return> {button_pushed OK}
    wm protocol . WM_DELETE_WINDOW {button_pushed OK}
    set f1 [ frame $top.f1 ]
    set lbl [ label $f1.lbl -text $message -padx 20 -pady 10 ]
    set but [ button $f1.but -text OK -command "button_pushed OK" -default active -width 8]
    bind [winfo toplevel $top] <Return> { button_pushed OK }
    pack $lbl -side top
    pack $but -side bottom -padx 10 -pady 10
    pack $f1 -anchor nw -fill both -expand 1

    wizard_event_loop $f1

    pack forget $f1
    destroy $f1
}

# button_entry_change - Utility function for button_depends_on_entry
proc button_entry_change {b t name1 name2 op} {
    puts stderr [list button_entry_change $b $t $name1 $name2 op]
    upvar \#0 $t v
    if {$v == ""} {
        $b configure -state disabled
    } else {
        $b configure -state normal
    }
}

# button_depends_on_variable - Sometimes, a button should not be enabled until
# text is entered
proc button_depends_on_variable {b v e} {
    global top
    global $v
    set b $top.f1.f2.[string tolower $b]
    set cmd [list button_entry_change $b $v]
    bind $e <Destroy> [list trace remove variable $v write $cmd]
    trace add variable $v write $cmd
    button_entry_change $b $v . . .
}

# button_listbox_change - Utility function for button_depends_on_listbox
proc button_listbox_change {b lb} {
    global top<commit_msg>remove debugging message<commit_after>#!/bin/sh
# this line restarts using wish \
exec wish "$0" "$@"

###############################################################
# Description:  setupconfig.tcl
#               This file, shows existing emc2 configuration
#               and allows copying and naming
#
#  Author: <NAME> & <NAME>
#  License: GPL Version 2
#
#  Copyright (c) 2005 All rights reserved.
#
#  Last change:
# $Revision$
# $Author$
# $Date$
###############################################################
#
# usage:
#    setupconfig [options] 
#
# options:
#  --configs-dir <dir>
#
#   Assume that <dir> is the configs/ directory, and that
#   subdirectories of <dir> contain individual configs.
#   If --configs-dir is not given, the script attempts to
#   find the directory itself, but that works only if the
#   environment variable EMC2_ORIG_CONFIGS_DIR is set, or
#   if the script was run from the configs dir, or one 
#   directly above or below it.
#
#  --run [<config-name>/<ini-name>]
#
#   Validate the config-name/ini-name that was supplied (if
#   any), if not supplied or invalid, go directly to the 
#   screen that asks the user to choose a config.  Once
#   the config is validated (or the user chooses one and 
#   clicks "Run"), invoke the run script to run EMC using
#   that configuration.
#
#  --get-config [<config-name>/<ini-name>]
#
#   Validate the config-name/ini-name that was supplied (if
#   any), if not supplied or invalid, go directly to the 
#   screen that asks the user to choose a config.  Once
#   the config is validated (or the user chooses one and 
#   clicks 'RUN'), do _not_ invoke the run script to run it,
#   instead simply print the chosen <config/ini> string and
#   end.  (This mode is for use by the run script.)
#
#  --new
#
#   Skip the initial menu and go straight to the "create
#   new config" sequence.
#
#  --run, --get-config, and --new are mutually exclusive. 
#  If none of these is specified, setupconfig displays a 
#  sequence of menus so the user can manipulate configurations
#  and/or run emc. 
#
###############################################################

# This logo is used on all the pages
set wizard_image_search "/etc/emc2/emc2-wizard.gif /usr/local/etc/emc2-wizard.gif emc2-wizard.gif"

foreach wizard_image $wizard_image_search {
    if { [file exists $wizard_image] } {
        set logo [image create photo -file $wizard_image]
    }
}

################### PROCEDURE DEFINITIONS #####################

# Internationalisation (i18n)
# in order to use i18n, all the strings will be called [msgcat::mc "string-foo"]
# instead of "string-foo".
# Thus msgcat searches for a translation of the string, and in case one isn't 
# found, the original string is used.
# In order to properly use locale's the env variable LANG is queried.
# If LANG is defined, then the folder src/po is searched for files
# called *.msg, (e.g. en_US.msg).
# That file should contain all the translations regarding tcl scripts 
# (tkemc, mini, setupconfig.tcl, etc)

package require msgcat
if ([info exists env(LANG)]) {
    msgcat::mclocale $env(LANG)
    msgcat::mcload "../../src/po"
}


# a kluge for older systems that don't have 'file normalize'
# (which was introduced with tcl 8.4)
# this version converts to an absolute path and eliminates 
# any references to ./ and ../, but it doesn't do all the 
# other things that normalize can do, such as resolve symlinks.
# it seems good enough for what this program needs to do

proc file_normalize { input } {
    # make path absolute
    if { [ file pathtype $input ] != "absolute" } {
	set input [ file join [ pwd ] $input ]
    }
    # split into component parts
    set path_list [ file split $input ]
    # remove any instances of ./
    set idx [ lsearch $path_list "." ]
    while { $idx > 0 } {
	set path_list [ lreplace $path_list $idx $idx ]
	set idx [ lsearch $path_list "." ]
    }
    # remove any instances of ../ (and the preceding directory name)
    set idx [ lsearch $path_list ".." ]
    while { $idx > 0 } {
	set path_list [ lreplace $path_list [ expr $idx -1 ] $idx ]
	set idx [ lsearch $path_list ".." ]
    }
    set output ""
    foreach name $path_list {
	set output [ file join $output $name ]
    }
    return $output
}

# reads the directory, and fills in "config_list" and "details_list"

proc get_config_list {} {
    global config_list details_list configs_dir

    # clear config and description lists
    set config_list [ list ]
    set details_list [ list ]
    # look at all subdirs
    foreach dir_name [ glob */ ] {
	cd $dir_name
	# is there an ini file (or more than one) inside?
	set inifnames [concat [ glob -nocomplain *.ini ]]
	if { [llength $inifnames]!= "0" } {
	    # yes, this is a viable config
	    # strip trailing / 
	    regsub "/" $dir_name "" config_name
	    # and save it
	    lappend config_list $config_name
	    # look for a README file
	    if { [ file isfile "README" ] } {
		# description found, read it
		set descr [ read -nonewline [ open "README" ]]
		# reformat - remove line breaks, preserve paragraph breaks
		regsub -all {([^\n])\n([^\n])} $descr {\1 \2} descr
		# and save it
		lappend details_list $descr
	    } else {
		# no description, gotta tell the user something
		lappend details_list [msgcat::mc "No details available."]
	    }
	}
	# back to main configs directory
	cd ..
    }
}

# main button callback, it assigns the button name to 'choice'

proc button_pushed { button_name } {
    global choice

    set choice $button_name
}

# generic popup, displays a message and waits for "OK" click

proc popup { message } {
    global choice top logo

    bind . <Escape> {button_pushed OK}
    bind . <Return> {button_pushed OK}
    wm protocol . WM_DELETE_WINDOW {button_pushed OK}
    set f1 [ frame $top.f1 ]
    set lbl [ label $f1.lbl -text $message -padx 20 -pady 10 ]
    set but [ button $f1.but -text OK -command "button_pushed OK" -default active -width 8]
    bind [winfo toplevel $top] <Return> { button_pushed OK }
    pack $lbl -side top
    pack $but -side bottom -padx 10 -pady 10
    pack $f1 -anchor nw -fill both -expand 1

    wizard_event_loop $f1

    pack forget $f1
    destroy $f1
}

# button_entry_change - Utility function for button_depends_on_entry
proc button_entry_change {b t name1 name2 op} {
    upvar \#0 $t v
    if {$v == ""} {
        $b configure -state disabled
    } else {
        $b configure -state normal
    }
}

# button_depends_on_variable - Sometimes, a button should not be enabled until
# text is entered
proc button_depends_on_variable {b v e} {
    global top
    global $v
    set b $top.f1.f2.[string tolower $b]
    set cmd [list button_entry_change $b $v]
    bind $e <Destroy> [list trace remove variable $v write $cmd]
    trace add variable $v write $cmd
    button_entry_change $b $v . . .
}

# button_listbox_change - Utility function for button_depends_on_listbox
proc button_listbox_change {b lb} {
    global top
{'fraction_non_alphanumeric': 0.12741947003785445, 'fraction_numerical': 0.0068566530962074134, 'mean_word_length': 3.118235294117647, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1516375', 'n_tokens_mistral': 4554, 'n_tokens_neox': 4375, 'n_words': 1724}