#!/home/brian/tcl_core/usr_special/bin/wish8.7

lappend auto_path [pwd]

proc copyfile {dstdir src} {
    set dir [file dirname $src]
    set {src-name} [file tail $src]
    if {[string match {*zipfs*} $dir]} {
	set {src-pl} [lrange [file split $dir] 1 end]
	make-dir $dstdir ${src-pl}
	set dst [file join $dstdir {*}${src-pl} ${src-name}]
	file copy -force $src $dst
    } else {
	error "Expecting zipfs filesystem: $dir"
    }
}

proc make-dir {root-path path-list} {
    set path ${root-path}
    foreach name ${path-list} {
	set path [file join $path $name]
	if {[file isdir $path]} continue
	file mkdir $path
    }
    
}

proc extract_zipfiles {} {
    foreach {mpoint mpath} [zipfs mount] {
	set strlen [string length $mpoint]
	set files [glob -nocomplain [file join $mpoint *]]
	set map [lmap f $files {set d [string range $f $strlen end]; list $f $d}]
	lappend rv {*}$map
    }
    return $rv
}

proc make_mac_icns {outname svgfile} {
    if {$::tcl_platform(os) eq "Darwin"} {
        set inkscape /Applications/Inkscape.app/Contents/MacOS/inkscape
    } else {
        set inkscape inkscape
    }
    
    if {[auto_execok $inkscape] eq ""} {
	error "\"$inkscape\" not found."
    }
    set icon-set-sizes {
	16 32 128 256 512
    }
    set iconset ${outname}.iconset
    file mkdir $iconset
    foreach size ${icon-set-sizes} {
	set cmd [list $inkscape --batch-process \
		     --export-width=$size \
		     --export-height=$size \
                     --export-type=png \
		     --export-filename=[file join $iconset icon_${size}x${size}.png] \
		     $svgfile]
	puts $cmd
	if {[catch {exec {*}$cmd} err]} {
	    puts inkscape-error:$err
	}
    }
    set icns ${outname}.icns
    set cmd [list iconutil -c icns --output $icns $iconset]
    if {[catch {exec {*}$cmd} err]} {
	puts iconutil-error:$err
    }
    return $icns
}

proc main {} {
    set filelist {
        word.tcl
        five_words3.txt
        officialwrds.tcl
        nytofficialwords.tcl
        nyt_march2022.tcl
        main.tcl
    }

    #set iconlist [glob -nocomplain assets/Adwaita/*.svg]
    #lappend iconlist {*}[glob -nocomplain assets/Adwaita/*.png]
    set iconlist {}
    
    set pkglist {
	*tcl_library
	*tk_library
    }
    switch -- $::tcl_platform(os) {
        "Linux" {
            set img-ext .bin
        }
        "Windows NT" {
            set img-ext .exe
            lappend pkglist twapi*
        }
        "Darwin" {
            set img-ext .app
        }
    }
    
    set appDir [file normalize libword.vfs]
    set password "griffintk"

    set infile [file normalize [info nameofexecutable]]

    set img-name words

    # Make a local copy of the Wish
    if {$::tcl_platform(os) eq "Darwin"} {
	set macos-sub-folder Contents/MacOS
	if {[regexp "(.*\.app)/${macos-sub-folder}/Wish\$" $infile -> app-root]} {
	    puts [list file delete -force ${img-name}${img-ext}]
	    file delete -force ${img-name}${img-ext}
	    puts [list file copy -force ${app-root} ${img-name}${img-ext}]
	    file copy -force ${app-root} ${img-name}${img-ext}
	} else {
	    error "Unexpected application path: $infile"
	}
        puts app-root=${app-root}
	set img ${img-name}${img-ext}/${macos-sub-folder}/${img-name}
        puts img=$img
	file rename ${img-name}${img-ext}/${macos-sub-folder}/Wish $img
	set permissions [file attributes $img -permissions]
	file attributes $img -permissions 0o777 ;# Make writable
	make_mac_icns ${img-name}${img-ext}/Contents/Resources/${img-name} \
	    assets/Adwaita/edit-nocopy-symbolic.svg
    } else {
	set img ./${img-name}${img-ext}
	puts [list file delete -force $img]
	file delete -force $img
	puts [list file copy -force $infile $img]
	file copy -force $infile $img
    }

    puts "#\n# Building $img from $infile\n#"

    file delete -force $appDir
    file mkdir $appDir
        
    foreach file $filelist {
	file copy -force $file libword.vfs
    }
    set icondir [lindex $iconlist 0]
    #puts $icondir
    file mkdir [file dirname libword.vfs/$icondir]
    foreach file $iconlist {
	file copy -force $file libword.vfs/$file
    }
    set pkgs {}
    foreach apath $::auto_path {
        if {[string match {*Script*} $apath]} {
            puts apath=$apath,contents=[glob -nocomplain [file join $apath *]]
        }
	lappend pkgs {*}[lmap pat $pkglist {
	    set m [glob -nocomplain [file join $apath $pat]]
	    if {[llength $m]==0} continue
	    list $apath $m
	}]
    }
    if {[llength $pkgs] != [llength $pkglist]} {
	puts "*** Missing required packages! \n\tpkgs=\n\t[join [lmap x $pkgs {lindex $x 1}] \n\t]\n\tpkglist=$pkglist"
    }
    foreach lpkg $pkgs {
	lassign $lpkg dir path
	set dpath [string range $path [string length $dir] end]
	puts "file copy -force $path libword.vfs$dpath"
	file copy -force $path libword.vfs$dpath
    }

    if {$::tcl_platform(os) eq "Darwin"} {
        set permissions [file attributes $img -permissions]
        file attributes $img -permissions 0o777
    }

    set cmd [list zipfs mkimg $img $appDir $appDir $password];# $infile]
    puts $cmd
    {*}$cmd

    if {$::tcl_platform(os) eq "Darwin"} {
	# Put back to oringinal permissions
	file attributes $img -permissions $permissions
    }

    puts [exec file $img]
    puts "file size: [file size $img]"
    if {[auto_execok ldd] ne ""} {
	# Linux
	puts [exec ldd $img]
    } elseif {[auto_execok otool] ne ""} {
	# MacOS
	puts [exec otool -L $img]
    }
    puts \nDone!
}

puts "auto_path="
foreach ape $auto_path {
    puts "->\t$ape =>"
    puts \t\t[join [glob -nocomplain $ape/*] \n\t\t]
}
puts zipfs=[zipfs mount]
main
exit
