#!/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.5

# Conf dir
set CONFDIR "[glob ~/]/.tkcdlayout"

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

# Your ps visualizer. Can be "gv -landscape", "kghostview", "pageview"...
set psviewer "gv -landscape"

# 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 showdirs "yes"
set showfiles "yes"
set Title ""
set rules ""
set subtitle ""
set front_page 1;  # has to be 1
set front_layout_type "taf"
set back_page 1;  # has to be 1
set back_layout_type "btas"

proc TkCdLayout:show_layout {} {
    global config
    global psviewer
    TkCdLayout:output_config
    exec >@stdout 2>@stderr NewCDLayout.pl -config=$config -psviewer=\"$psviewer\"
}

proc TkCdLayout:update_layout {} {
    global config
    global w
    TkCdLayout:output_config
    exec >@stdout 2>@stderr NewCDLayout.pl -config=$config -update
    tk_messageBox -title "Update done..." -parent $w \
	-message "Now hit update in the ps visualizer"
}

# 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 showdirs
    global showfiles
    global front_layout_type
    global back_layout_type
    global front_page
    global back_page
    global config
    global rules
    global CONFDIR
    set f [open $config w]
    puts $f "cdn=\"$cdn\""
    puts $f "cddir=\"$cddir\""
    puts $f "lay=\"$laydir\""
    puts $f "maxdepth=$maxdepth"
    puts $f "exclude=\"$exclude\""
    puts $f "showdirs=\"$showdirs\""
    puts $f "showfiles=\"$showfiles\""
    puts $f "Title=\"$Title\""
    puts $f "rules=\"$rules\""
    puts $f "subtitle=\"$subtitle\""
    puts $f "front_layout_type=\"$front_layout_type\""
    puts $f "back_layout_type=\"$back_layout_type\""
    if {$front_page == 1} {
	if {$back_page == 1} {
	    set page "both"
	} else { set page "front" }
    } else { set page "back" }
    puts $f "page=\"$page\""
    close $f
}

proc TkCdLayout:input_config {} {
    global cdn
    global cddir
    global laydir
    global exclude
    global maxdepth
    global Title
    global rules
    global subtitle
    global showdirs
    global showfiles
    global front_layout_type
    global back_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 }
		    "showdirs" { set showdirs $value }
		    "showfiles" { set showfiles $value }
		    "Title" { set Title $value }
		    "rules" { set rules $value }
		    "subtitle" { set subtitle $value }
		    "front_layout_type" { set front_layout_type $value }
		    "back_layout_type" { set back_layout_type $value }
		    "page" { switch $value {
			"front" { set back_page 0 }
			"back"  { set front_page 0 }
			# both : leave them both to 1
		    }}
		}
	    }
	}  
	close $f
    }
}

# Enable (1) / Disable (0) a textfield + label
proc TkCdLayout:setEnabled {comp label enable} {
    if { $enable == "normal" } { # enable
	$comp configure -state normal -foreground black
	$label configure -foreground black
    } else { # disable
	$comp configure -state disabled -foreground gray75
	$label configure -foreground gray75
    }
}

# Called for enabling/disabling components, after something has changed
proc TkCdLayout:configchanged {} {
    global front_layout_type
    global back_layout_type
    global front_page
    global back_page
    global w
    set subenabled disabled
    set otherenabled disabled
    set buttonsenabled normal
    if { $front_page == 0 } {
	if { $back_page == 0 } { set buttonsenabled disabled }
	$w.front.layout configure -state disabled
    } else {
	$w.front.layout configure -state normal
	if { $front_layout_type == "btas" } { set subenabled normal
	} else {set otherenabled normal}
    }
    if { $back_page == 0 } {
	$w.back.layout configure -state disabled
    } else {
	$w.back.layout configure -state normal
	if { $back_layout_type == "btas" } { set subenabled normal
	} else {set otherenabled normal}
    }
    TkCdLayout:setEnabled $w.fsubtitle.entry1 $w.fsubtitle.label1 $subenabled

    TkCdLayout:setEnabled $w.fdir.entry1 $w.fdir.label1 $otherenabled
    $w.fdir.bbrowse configure -state $otherenabled
    TkCdLayout:setEnabled $w.fmaxdepth.entry1 $w.fmaxdepth.label1 $otherenabled
    $w.fdiro.showfiles configure -state $otherenabled
    $w.fdiro.showdirs configure -state $otherenabled
    TkCdLayout:setEnabled $w.fexcl.entry1 $w.fexcl.label1 $otherenabled

    TkCdLayout:setEnabled $w.ftitle.entry1 $w.ftitle.label1 $buttonsenabled
    $w.ftitle.bgetfromcd configure -state $buttonsenabled
    $w.fb.bsavconf configure -state $buttonsenabled
    $w.fb.bnewlay configure -state $buttonsenabled
    $w.fb.bupdatelay configure -state $buttonsenabled
}

# Called on cd-layout-type change
proc TkCdLayout:layouttypechanged {value} {
    TkCdLayout:configchanged
}

# Generate the components for a page config. Call with "front", then with "back"
proc TkCdLayout:pageconfig {page label} {
    global ${page}_layout_type
    global ${page}_page
    global w
    frame $w.$page
    checkbutton $w.$page.cb -text $label \
	-variable ${page}_page -command "TkCdLayout:configchanged"

    tixOptionMenu $w.$page.layout -label "Layout Type : " \
	-variable ${page}_layout_type -command "TkCdLayout:layouttypechanged" \
	-options { label.width 15 label.anchor e  menubutton.width 18 }

      # We disable callback here, because the components enabled/disabled by
      # the callback function don't exist yet.
    $w.$page.layout configure -disablecallback 1
    $w.$page.layout add command btas -label "Big title and subtitle"
    $w.$page.layout add command taf -label "Title and files"
    $w.$page.layout configure -disablecallback 0

    pack $w.$page.cb $w.$page.layout -side left
}

proc TkCdLayout:gettitlefromcd {} {
    global Title
    set Title [exec GetCDTitle]
}

TkCdLayout:input_config 

set w .tkCL
set ypad 7

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

set front_page_sav $front_page
set front_layout_sav $front_layout_type
set back_page_sav $back_page
set back_layout_sav $back_layout_type

TkCdLayout:pageconfig front "Front page"
TkCdLayout:pageconfig back  "Back page"

frame $w.ftitle
label $w.ftitle.label1 -text "CD Title"
entry $w.ftitle.entry1 -width 30 -textvariable Title
button $w.ftitle.bgetfromcd -command "TkCdLayout:gettitlefromcd" \
    -text "Get from CD"
pack $w.ftitle.label1 $w.ftitle.entry1 $w.ftitle.bgetfromcd -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.showdirs -offvalue no -onvalue yes -variable showdirs \
 -text "Show directories"
checkbutton $w.fdiro.showfiles -offvalue no -onvalue yes -variable showfiles \
 -text "Show files"
pack $w.fdiro.showdirs $w.fdiro.showfiles -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 "Save & Show"
button $w.fb.bupdatelay -command "TkCdLayout:update_layout" -text "Save & Update"
button $w.fb.bexit -command "exit" -text "Exit"
pack $w.fb.bsavconf $w.fb.bnewlay $w.fb.bupdatelay $w.fb.bexit -side left

set front_page $front_page_sav
set front_layout_type $front_layout_sav
set back_page $back_page_sav
set back_layout_type $back_layout_sav

pack $w.front $w.back $w.ftitle $w.fsubtitle $w.fdir $w.fmaxdepth $w.fdiro \
    $w.fexcl $w.fb -side top -pady $ypad

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