#!/usr/local/bin/wish -f

# tkvolname copyright 1996 Andrew Tefft

# version 1.0	1/19/96		first public release

# where your volnamedb is
set globalvolnamedb "/usr/local/lib/volnamedb"
set home $env(HOME)
set volnamedb ${home}/.volnamedb 
exec touch $volnamedb

# your default CDROM device(s)

# 	for just one you could use...
# set device "/dev/cdrom"
#	or set a list for multiple devices
#set device { /dev/cdrom /dev/cdrom1 } 
set device [ lsort [ glob /dev/sr? ] ]
#set device { /dev/sr0 /dev/sr1 /dev/sr2 /dev/sr3 /dev/sr4 /dev/sr5 /dev/sr6 }

# your favorite color scheme

# typical background
set bg gray80
# color for background of the button near the pointer
# I recommend a little lighter or darker than the background
set hotbg gray
# typical foreground
set fg black
# color for text of data cd's (this is dark cyan)
set datafg "#009696" 
# color for text of audio cd's (this is dark green)
set audiofg "#008000"
# color for text of empty slots (this is dark red)
set nonefg firebrick

# you probably don't want to change any of these!

set nonewidth 0
set audiowidth 25
set datawidth 25

# now some procedures

proc get_title { id } {
global volnamedb globalvolnamedb
	if { [ file exists $volnamedb ] && \
	     [ file readable $volnamedb ] } {
	set f [ open $volnamedb r ]
	while { [ gets $f line ] >= 0 } {
	if { [ llength $line ] > 0 } {
	   if { [ lindex $line 0 ] == $id } {	   
		close $f
		return [ lrange $line 1  [ llength $line ] ]
	}
	   }
        }
	}
	if { [ file exists $globalvolnamedb ] && \
	     [ file readable $globalvolnamedb ] } {
	set f [ open $volnamedb r ]
	while { [ gets $f line ] >= 0 } {
	if { [ llength $line ] > 0 } {
	   if { [ lindex $line 0 ] == $id } {   
		close $f
		return [ lrange $line 1  [ llength $line ] ]
	 }
	  }
        }
	}
		return "Unknown audio CD"
}

proc updateinfo { a } {
global device type col1_$a col2_$a datafg audiofg nonefg
global closes nonewidth audiowidth datawidth
##   puts stdout $a
    set x [ concat "|volname -id " [ lindex $device $a ] ]
    set f [ open $x r+ ]
	 gets $f line 
    set xx [ concat $line ]

	close $f
# empty
if { $xx == "No CD in drive" } {
    set type "empty"
    set col1_$a "No CD in drive"
    set col2_$a ""
    if { [ winfo exists .main.line_$a.col1 ] } {
	.main.line_$a.col1 configure -fg $nonefg
	.main.line_$a.col2 configure -width $nonewidth -relief flat
       pack forget .main.line_$a.col2
    }
    return
} 

# audio cd
if { [ lindex $xx 0 ] == "Audio" } {
    set type "audio"
    set col1_$a [ get_title [ lindex $xx [ expr [ llength $xx ] -1 ] ] ] 
    set col2_$a [ string trimright \
		[ string trimleft \
	[ lrange $xx 2 [ expr [ llength $xx ] -2 ] ] \
	"(" ] \
	")" ] 
    if { [ winfo exists .main.line_$a.col1 ] } {
	.main.line_$a.col1 configure -fg $audiofg
	.main.line_$a.col2 configure -width $audiowidth -relief sunken
	pack .main.line_$a.col2
    }
    return
} 

# other -- must be data
    set type data
    set col1_$a [ lindex $xx 0 ]
    if { [ expr [ llength $xx ] > 1 ] } {
	set col2_$a [ lindex $xx [ expr [ llength $xx ] - 1 ] ]
    } else {
	set col2_$a "not mounted"
    }
    if { [ winfo exists .main.line_$a.col1 ] } {
	.main.line_$a.col1 configure -fg $datafg 
	.main.line_$a.col2 configure -width $datawidth -relief sunken
	pack .main.line_$a.col2
    }
}


# for each device, we'll have a frame with a couple widgets depending on
# what the cd is. There are four choices: audio, empty, data, error.

frame .main -background $bg
pack .main

set devw 0
foreach x $device {
    if  { [ string length $x ] > $devw } { 
	set devw [ string length $x ]
    }
}
incr devw

set a [ expr [ llength $device  ] -1 ]
while { $a >= 0 } {
updateinfo $a
        frame .main.line_$a -background $bg 
   	button .main.line_$a.dev -text  [ lindex $device $a ] \
		-relief raised -width $devw -background $bg -foreground $fg \
		-command "updateinfo $a" -activebackground $hotbg
        pack .main.line_$a.dev -side left -anchor w
	set gotit 0
# empty
if { $type == "empty" } {
	    label .main.line_$a.col1 -textvariable col1_$a -relief sunken \
		    -background $bg -foreground $nonefg
            label .main.line_$a.col2 -width $nonewidth -textvariable col2_$a \
		    -bg $bg -foreground $fg -relief flat
	    pack .main.line_$a.dev
	    pack .main.line_$a.col1 \
		    -side left -fill x -expand yes -anchor w
            pack .main.line_$a.col2 -side left -anchor w
            pack forget .main.line_$a.col2
	    set gotit 1
	} 
# audio cd
	if { $type == "audio" } {
	    label .main.line_$a.col1 -relief sunken \
		    -textvariable col1_$a \
		    -background $bg -foreground $audiofg -padx 10
	    label .main.line_$a.col2 -relief sunken -width $audiowidth \
		    -background $bg -foreground $fg \
		    -textvariable col2_$a
	    pack  .main.line_$a.col1 -side left -fill x \
		    -expand yes -anchor w 
	    pack .main.line_$a.col2 \
		    -side left -anchor w
	    set gotit 1
	} 
# other -- must be data
	if { $gotit == 0 } {
	    label .main.line_$a.col1 -textvariable col1_$a \
		    -relief sunken -background $bg -foreground $datafg
	    label .main.line_$a.col2 -textvariable col2_$a \
		    -background $bg -foreground $fg -relief sunken -width $datawidth 
	    pack .main.line_$a.col1 -side left -anchor w -fill x -expand yes
	    pack .main.line_$a.col2 \
		    -side left -anchor w
	}
	pack .main.line_$a -side bottom -fill x -expand yes
	incr  a -1
}



