set FILE "sp_wizard.txt"


proc read {file} {
	global list i
	set list {}
	set tmp_list {}
	set in [open $file]
	puts "Leggo file $in"
	while {[gets $in row] >=0} {
		regsub "\n" $row "" row
		lappend list $row
	}
	puts "analisi"
	for {set i 0} {$i<[llength $list]} {incr i} {
		set val [lindex $list $i]
### allineamento caratteristiche spell
		if {[string first "Range:" $val] != -1 ||
			[string first "Duration:" $val] != -1 ||
			[string first "Area of Effect:" $val] != -1} {
			regsub "\t" $val "\n" val
		}
		lappend tmp_list $val
	}
	puts "end"
	return $tmp_list
}

proc is_school {} {
        global list i
##        if {[lindex $list [expr $i + 1]]=="" &&
##            [lindex $list [expr $i + 2]]=="" &&
##            [lindex $list [expr $i + 3]]==""} 
	if {[string first "-Level" [lindex $list $i]]!=-1} {
                return 1
        }
        return 0
}


proc is_spell {} {
        global list i
        if {[string index [lindex $list [expr $i + 1]] 0]=="("} {
                return 1
        }
        return 0
}


proc save_spell {} {
        global curdir start end list

        set nome "$curdir\\[string trim [lindex $list $start]]"
	regsub "/" $nome "-" nome
        while [info exists $nome.txt] { set nome "$nome_" }
        set id [open $nome.txt w]
        puts "\t\tSpell = $nome"
        for {set x $start} {$x<=$end} {incr x} {
		if {[string index [lindex $list $x] 0]=="(" &&
			[lindex $list [expr $x + 1]]=="" &&
			[string first "Sphere:" [lindex $list [expr $x + 2]]]!=-1} {
			set row_tmp [lindex $list $x]
			incr x +2
			append row_tmp "([string trim [string range [lindex $list $x] 7 end]])\n"
			puts $id $row_tmp
		} elseif {[string index [lindex $list $x] 0]=="(" &&
			[string trim [lindex $list [expr $x + 1]]]=="Reversible" &&
			[lindex $list [expr $x + 2]]=="" &&
			[string first "Sphere:" [lindex $list [expr $x + 3]]]!=-1} {
			set row_tmp [lindex $list $x]
			incr x +3
			append row_tmp "([string trim [string range [lindex $list $x] 7 end]])"
			puts $id $row_tmp
			puts $id "[string trim [lindex $list [expr $x - 2]]]\n"
		} else {
	                puts $id [lindex $list $x]
		}
        }
        close $id
}

global curdir start end list i
####set total [read_file "PHBBK_wizard.txt"]
####set list [split $total "\n"]
puts "*** Analisi $FILE ***"
set lista [read $FILE]
set list $lista
set spell 0
set i 0
set max [llength $list]
while {$i<$max} {
        if {[lindex $list $i]=={}} {
                incr i
        } elseif [is_school] {
                if $spell {
                        save_spell
                        set spell 0
                }
                set newdir [string trim [lindex $list $i]]
                file mkdir $newdir
                set curdir $newdir
                puts "School = $newdir"
                incr i
        } elseif [is_spell] {
                if $spell {
                        save_spell
                }
                set spell 1
                set start $i
                incr i
        } else {
                if $spell {
                        set end $i
                }
                incr i
        }
}
if $spell {
	save_spell
}