#!/bin/sh
# -*- tcl -*-
#               TkCdLayout - David.Faure@insa-lyon.fr
#
# The next line restarts using tixwish. Leave this backslash : \
exec tixwish4.1.8.0 "$0" "$@"

set VERSION 0.2

# Lib dir (could be /usr/local/lib/xcdroast.../ in the future)
set LIBDIR "/usr/local/lib/tkcdlayout-$VERSION"

if { [file isdirectory $LIBDIR] == 0 } {
    puts "TkCdLayout isn't properly installed, $LIBDIR\
doesn't exist"
    exit 1
}
# Conf dir (could be ~/.xcdroast in the future)
set CONFDIR "[glob ~/]/.tkcdlayout"

# Config file name
set config "$CONFDIR/config"

# CHANGE the file "config" if CONFDIR or/and config are changed

# Layout directory
set laydir "$CONFDIR/layouts"

# (Taken from xcdroast)
# Check if CONFDIR is found
if { [file isdirectory $CONFDIR] == 0 } {
        # create confdir if it does not exists
        exec mkdir $CONFDIR
        #chownfile $CONFDIR
} 

if { [file isdirectory $laydir] == 0 } {
        exec mkdir $laydir
} 

# Filename base. Change to 'tmp' if you don't want to archive layouts
#  or to something more precise if you want to archive more than one
#   layout a day.
set cdn "lay[exec date +%y%m%d]"

set cddir ""
set exclude ""
set maxdepth ""
set dironly "yes"
set Title ""
set subtitle ""
set layout_type "taf"
set page "front"

proc TkCdLayout:show_layout {} {
    global LIBDIR
    TkCdLayout:output_config
    exec >@stdout 2>@stderr "$LIBDIR/NewLayout"
}

# Pop up a directory selection dialog
proc TkCdLayout:browse {} {
    set dialog .dirdlg_popup
    if ![winfo exists $dialog] {
        tixDirSelectDialog $dialog
    }
    $dialog config -command TkCdLayout:select_dir
    $dialog popup
}

proc TkCdLayout:select_dir {dir} {
    global cddir
    set cddir $dir
}

proc TkCdLayout:output_config {} {
    global cdn
    global cddir
    global laydir
    global exclude
    global maxdepth
    global Title
    global subtitle
    global dironly
    global layout_type
    global page
    global config
    global CONFDIR
    set f [open $config w]
    puts $f "cdn=\"$cdn\""
    puts $f "cddir=\"$cddir\""
    puts $f "lay=\"$laydir\""
    puts $f "mid=\"\${lay}/$cdn.latex.middle\""
    puts $f "lines=$CONFDIR/lines"
    puts $f "maxdepth=$maxdepth"
    puts $f "exclude=\"$exclude\""
    puts $f "dironly=$dironly"
    puts $f "Title=\"$Title\""
    puts $f "subtitle=\"$subtitle\""
    puts $f "Warn=no"  ; # no warning for overwriting $cdn file
    puts $f "layout_type=$layout_type"
    puts $f "page=$page"
    close $f
}

proc TkCdLayout:input_config {} {
    global cdn
    global cddir
    global laydir
    global exclude
    global maxdepth
    global Title
    global subtitle
    global dironly
    global layout_type
    global page
    global config
    catch {
	set f [open $config r]
	while {[gets $f line] >= 0} {
	    # split line into "keyword=value"
	    set eqpos [string first = $line]
	    if {$eqpos > 0} {
		set keyword [string range $line 0 [expr $eqpos-1]]
		set value [string range $line [expr $eqpos+1] end]
		set value [string trim $value \"]
		# puts $keyword
		# puts $value
		switch $keyword {
		    "cddir" { set cddir $value }
		    "lay" { set laydir $value }
		    "maxdepth" { set maxdepth $value }
		    "exclude" { set exclude $value }
		    "dironly" { set dironly $value }
		    "Title" { set Title $value }
		    "subtitle" { set subtitle $value }
		    "layout_type" { set layout_type $value }
		    "page" { set page $value }
		}
	    }
	}  
	close $f
    }
}

# Called on cd-layout-type change
proc TkCdLayout:layouttype {value} {
    global ypad
    global w
    # Forget packing
    pack forget $w.opt $w.ftitle $w.fdir $w.fmaxdepth $w.fdiro \
		$w.fexcl $w.fb $w.fsubtitle $w.page
    # Pack what we want
    switch $value {
	"btas" { # Big Title and Subtitle
	    pack $w.opt $w.page $w.ftitle $w.fsubtitle $w.fb \
		-side top -pady $ypad
	}
	"taf"  { # Title and files
	    pack $w.opt $w.page $w.ftitle $w.fdir $w.fmaxdepth $w.fdiro \
		$w.fexcl $w.fb -side top -pady $ypad 
	}
    }
}

TkCdLayout:input_config 

set w .tkCL
set ypad 7

catch "destroy $w"
toplevel $w
wm title $w "Tk CD Layout"

set sav $layout_type
tixOptionMenu $w.opt -label "Select CD-Layout-Type : " \
                -variable layout_type -command "TkCdLayout:layouttype" \
                -options { label.width 25 label.anchor e
		    menubutton.width 25 }

        # We disable callback here, because the command connected to
        # the Options pack things that don't exist yet.
$w.opt configure -disablecallback 1
$w.opt add command btas -label "Big title and subtitle"
$w.opt add command taf -label "Title and files"
$w.opt configure -disablecallback 0

frame $w.page
radiobutton $w.page.front -text "Front page" -variable page -value front
radiobutton $w.page.back -text "Back page" -variable page -value back
pack $w.page.front $w.page.back -side left

frame $w.ftitle
label $w.ftitle.label1 -text "CD Title"
entry $w.ftitle.entry1 -width 30 -textvariable Title
pack $w.ftitle.label1 $w.ftitle.entry1 -side left

frame $w.fsubtitle
label $w.fsubtitle.label1 -text "SubTitle"
entry $w.fsubtitle.entry1 -width 40 -textvariable subtitle
pack $w.fsubtitle.label1 $w.fsubtitle.entry1 -side left

frame $w.fdir
label $w.fdir.label1 -text "Files Directory" 
entry $w.fdir.entry1 -width 30 -textvariable cddir
button $w.fdir.bbrowse -command "TkCdLayout:browse" -text ...
pack $w.fdir.label1 $w.fdir.entry1 $w.fdir.bbrowse -side left

frame $w.fmaxdepth
label $w.fmaxdepth.label1 -text "Maxdepth for listing files"
entry $w.fmaxdepth.entry1 -width 20 -textvariable maxdepth
pack $w.fmaxdepth.label1 $w.fmaxdepth.entry1 -side left

frame $w.fdiro
checkbutton $w.fdiro.dironly -offvalue no -onvalue yes -variable dironly \
 -text "Only directories"
pack $w.fdiro.dironly -side left

frame $w.fexcl
label $w.fexcl.label1 -text "Exclude file names containing"
entry $w.fexcl.entry1 -width 20 -textvariable exclude 
pack $w.fexcl.label1 $w.fexcl.entry1 -side left

frame $w.fb
button $w.fb.bsavconf -command "TkCdLayout:output_config" -text "Save config"
button $w.fb.bnewlay -command "TkCdLayout:show_layout" -text "Show Layout"
button $w.fb.bexit -command "exit" -text "Exit"
pack $w.fb.bsavconf $w.fb.bnewlay $w.fb.bexit -side left

set layout_type $sav
unset sav

# Hide main window (while TkCdLayout runs on its own)
wm withdraw .
