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

set RATE 22050
set STEREO MONO
set PUBLIC 0 
set LOGIN 0
set SOUNDCARD 0
set ENCODER LAME3
set PLAYLIST playlist
set NAME "LiveIce Radio"
set GENRE "Live"
set URL "http://www.icecast.org"
set SERVER "localhost"
set PORT 8000
set PASSWORD letmein
set BITRATE 32000
set VBR_QUALITY 1

proc write_file {} {
global RATE STEREO  PUBLIC  LOGIN SOUNDCARD DUPLEX ENCODER MIXER CONTROL 

set FILENAME [ .f14.cfile get ] 
set O_FILE [ open $FILENAME w ]
puts $O_FILE "# liveice configuration file"
puts $O_FILE "# Automatically generated"
puts $O_FILE "SERVER [.f2.server get ]"
puts $O_FILE "PORT [.f2.port get ]"
puts $O_FILE "NAME [.f3.name get ]"
puts $O_FILE "GENRE [.f4.genre get ]"
puts $O_FILE "URL [.f5.url get ]"
puts $O_FILE "PUBLIC $PUBLIC"
puts $O_FILE "$LOGIN"
puts $O_FILE "PASSWORD [ .f6.password get ]"
puts $O_FILE "SAMPLE_RATE $RATE"
puts $O_FILE "$STEREO"
puts $O_FILE "$SOUNDCARD"
puts $O_FILE "$DUPLEX"
puts $O_FILE "USE_$ENCODER [.f11.enc get]"
puts $O_FILE "BITRATE [ .f9.br get ]"
puts $O_FILE "VBR_QUALITY [ .f9.qual get ]"
puts $O_FILE "$MIXER"
puts $O_FILE "PLAYLIST [.f10.playlist get ]"
puts $O_FILE "DECODER_COMMAND [.f11.dec get ]"
puts $O_FILE "MIX_CONTROL_$CONTROL"
puts $O_FILE "CONTROL_FILE [.f13.c_log get]"
puts $O_FILE "TRACK_LOGFILE [.f13.t_log get]"

puts $O_FILE ""

close $O_FILE
}

proc set_encoder { name1 name2 op } {
global ENCODER

.f11.enc delete 0 end 

switch -exact $ENCODER {
    AJ_ENCODER { .f11.enc insert 0 "encoder" }
    L3ENC { .f11.enc insert 0 "l3enc" }
    MP3ENC { .f11.enc insert 0 "mp3enc" }
    SCREAMER { .f11.enc insert 0 "lamer" }
    LAME3 { .f11.enc insert 0 "lame" }
    XING { .f11.enc insert 0 "xingmp3enc" }
    XING_VBR { .f11.enc insert 0 "xingmp3enc" }
    default { .f11.enc insert 0 "encoder" }
}

}


wm title . "LiveIce Configuration"

frame .f1 -borderwidth 4


label .f1.title  -text "LiveIce Configuration File Generator"
pack .f1.title -side left -fill both 

frame .f2 -borderwidth 4

label .f2.label -text "Server" -width 10
entry .f2.server -width 67 -relief sunken

.f2.server insert 0 $SERVER

label .f2.plabel -text " : "
entry .f2.port -width 9 -relief sunken

.f2.port insert 0 $PORT

pack .f2.label .f2.server .f2.plabel .f2.port  -side left -fill both -expand TRUE

frame .f3 -borderwidth 4
label .f3.label -text "Name:" -width 10
entry .f3.name -width 80 -relief sunken
.f3.name insert 0 $NAME

pack .f3.label .f3.name -side left -fill both -expand TRUE



frame .f4 -borderwidth 4
label .f4.label -text "Genre:" -width 10
entry .f4.genre -width 80 -relief sunken
.f4.genre insert 0 $GENRE

pack .f4.label .f4.genre -side left -fill both -expand TRUE



frame .f5 -borderwidth 4
label .f5.label -text "URL:" -width 10
entry .f5.url -width 80 -relief sunken
.f5.url insert 0 $URL

pack .f5.label .f5.url -side left -fill both -expand TRUE


frame .f6 -borderwidth 4
label .f6.l1 -text "Directory" 
radiobutton .f6.private -variable PUBLIC -text "Private" -value 0
radiobutton .f6.public -variable PUBLIC -text "Public" -value 1
.f6.private invoke

label .f6.l2 -text "     Login Type"
radiobutton .f6.icy -variable LOGIN -text "icy " -value ICY_LOGIN
radiobutton .f6.x_audiocast -variable LOGIN -text "x-audiocast" -value X_AUDIOCAST_LOGIN
.f6.icy invoke


label .f6.l3 -text "   Password" 
entry .f6.password -width 16 -relief sunken

.f6.password insert 0 $PASSWORD

pack .f6.l1 .f6.private .f6.public .f6.l2 .f6.icy .f6.x_audiocast .f6.l3 .f6.password -side left -fill both -expand TRUE


frame .f7 -borderwidth 4

label .f7.l1 -text "PCM Audio Format"
menubutton .f7.mn1 -textvariable RATE -relief raised -menu .f7.mn1.m -width 20

menu .f7.mn1.m
.f7.mn1.m add radiobutton -variable RATE -label "8000 " -value 8000
.f7.mn1.m add radiobutton -variable RATE -label "11050" -value 11050
.f7.mn1.m add radiobutton -variable RATE -label "16000" -value 16000
.f7.mn1.m add radiobutton -variable RATE -label "22050" -value 22050
.f7.mn1.m add radiobutton -variable RATE -label "32000" -value 32000
.f7.mn1.m add radiobutton -variable RATE -label "44100" -value 44100
.f7.mn1.m add radiobutton -variable RATE -label "48000" -value 48000

label .f7.l2 -text "Hz  "

menubutton .f7.mn2 -textvariable STEREO -relief raised -menu .f7.mn2.m -width 20

menu .f7.mn2.m
.f7.mn2.m add radiobutton -variable STEREO -label " Mono " -value MONO
.f7.mn2.m add radiobutton -variable STEREO -label "Stereo" -value STEREO

pack .f7.l1 .f7.mn1 .f7.l2 .f7.mn2 -side left -fill both -expand TRUE

frame .f8 -borderwidth 4
label .f8.l1 -text "Soundcard: "
radiobutton .f8.soundcard -variable SOUNDCARD -text "Enabled" -value SOUNDCARD
radiobutton .f8.nosoundcard -variable SOUNDCARD -text "Disabled" -value NO_SOUNDCARD
.f8.soundcard invoke

radiobutton .f8.halfduplex -variable DUPLEX -text "Half-Duplex" -value HALF_DUPLEX
radiobutton .f8.fullduplex -variable DUPLEX -text "Full-Duplex" -value FULL_DUPLEX

.f8.halfduplex invoke

pack .f8.l1 .f8.soundcard .f8.nosoundcard  .f8.halfduplex .f8.fullduplex  -side left -fill both -expand TRUE


frame .f9 -borderwidth 4

label .f9.label -text "Encoder"
menubutton .f9.enc -textvariable ENCODER -relief raised -menu .f9.enc.m -width 20

menu .f9.enc.m 
.f9.enc.m add radiobutton -variable ENCODER -label "Andreas Johannson\'s Encoder" -value AJ_ENCODER
.f9.enc.m add radiobutton -variable ENCODER -label "Fraunhofer\'s l3enc" -value L3ENC
.f9.enc.m add radiobutton -variable ENCODER -label "Fraunhofer\'s mp3enc" -value MP3ENC
.f9.enc.m add radiobutton -variable ENCODER -label "Isaac Richard\'s scrEamer" -value SCREAMER
.f9.enc.m add radiobutton -variable ENCODER -label "LAME 3.x" -value LAME3
.f9.enc.m add radiobutton -variable ENCODER -label "XingTechnologies Beta xingmp3enc" -value XING
.f9.enc.m add radiobutton -variable ENCODER -label "xingmp3enc in VBR mode" -value XING_VBR


label .f9.l2 -text "Bitrate"
entry .f9.br -width 10 -relief sunken

.f9.br insert 0 $BITRATE

label .f9.l3 -text "VBR Quality"
entry .f9.qual -width 10 -relief sunken

.f9.qual insert 0 $VBR_QUALITY

pack .f9.label .f9.enc .f9.l2 .f9.br .f9.l3 .f9.qual -side left -fill both -expand TRUE

frame .f10 -borderwidth 4

radiobutton .f10.simple -variable MIXER -text "Soundcard Only" -value NO_MIXER
radiobutton .f10.mixer -variable MIXER -text "Mp3Mixer Mode" -value MIXER
.f10.simple invoke


label .f10.l1 -text "Playlist File"
entry .f10.playlist -width 30 -relief sunken
.f10.playlist insert 0 "$PLAYLIST"

button .f10.mp -text "Create" -command "exec find / -name \"*.mp3\" > [.f10.playlist get ]"

pack .f10.simple .f10.mixer .f10.l1 .f10.playlist .f10.mp -side left -fill both -expand TRUE

frame .f11 -borderwidth 4
label .f11.l1 -text "Executables - Encoder:" 
entry .f11.enc -width 20 -relief sunken

label .f11.l2 -text "MPG123 Decoder:"
entry .f11.dec -width 20 -relief sunken
.f11.dec insert 0 "mpg123"
pack .f11.l1 .f11.enc .f11.l2  .f11.dec -side left -fill both -expand TRUE


frame .f12 -borderwidth 4
label .f12.l1 -text "Mixer Control"
radiobutton .f12.manual -variable CONTROL -text "Manual" -value MANUAL
radiobutton .f12.logged -variable CONTROL -text "Logged" -value LOGGED
radiobutton .f12.auto -variable CONTROL -text "Automatic" -value AUTO
.f12.manual invoke

pack .f12.l1 .f12.manual .f12.logged .f12.auto -side left -fill both -expand TRUE

frame .f13 -borderwidth 4
label .f13.l1 -text "Command Log"
entry .f13.c_log -width 30 -relief sunken
.f13.c_log insert 0 "mix_command"

label .f13.l2 -text "Track Log"
entry .f13.t_log -width 30 -relief sunken
.f13.t_log insert 0 "track.log"

pack .f13.l1 .f13.c_log .f13.l2 .f13.t_log  -side left -fill both -expand TRUE


frame .f14 -borderwidth 4
button .f14.button -text "Save Config File As:" -command "write_file"
entry .f14.cfile -width 30 -relief sunken
.f14.cfile  insert 0 "liveice.cfg"

button .f14.exit -text "Exit!" -command "exit"

pack .f14.button .f14.cfile .f14.exit  -side left -fill both -expand TRUE


pack .f1 .f2 .f3 .f4 .f5 .f6 .f7 .f8 .f9 .f10 .f11 .f12 .f13 .f14 -side top -fill both
 
update

trace variable ENCODER w set_encoder


