#!/bin/sh
# the next line restarts using wish \
exec wish "$0" "$@"
#
#   Paul Millar - OTC 1.2
#   Copyright (C) 2001  Paul Millar
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#   dazzlepm@lineone.net
#

# Change this if your theme directory is located elsewhere
set prefix "/usr/share"
set rev 0

wm title . "OTC"
wm resizable . 0 0

#menubutton .mb -text File -menu .mb.menu
#. config -menu .mb -side left
#set m [menu .mb.menu -tearoff true]
#$m add command -label "Install Theme.."
#$m add command -label "About.."
frame .frame1
set theme_list [listbox .frame1.f -background white -selectmode single -width 22 -height 10 -yscrollcommand {.frame1.yscroll set}]
scrollbar .frame1.yscroll -command {.frame1.f yview} -orient vertical
frame .frame3
button .frame3.apply -text "TRY" -command try
button .frame3.quit -text "OK" -command ok
button .frame3.cancel -text "CANCEL" -command cancel
#pack .mb -side top
pack .frame1  -side top -fill both
pack .frame1.f -side left -fill both
pack .frame1.yscroll -side right -fill both
pack .frame3 -side bottom -fill both
pack .frame3.apply .frame3.quit .frame3.cancel -side left

set orc [file exists ~/.oroborusrc]
if {$orc<1} {
	set orc1 [file exists $prefix/oroborus/defaults]
		if {$orc1>=1} {
			set default "defaults"
		} else {
			set default "oroborusrc"
		}
	file copy -force $prefix/oroborus/$default ~/.oroborusrc
		tk_dialog {.confirm} {Info} {.oroborusrc created in home directory} {info} 0 OK
}

set rc1  [open ~/.oroborusrc r]

if {$rev == 0} {
	while {[gets $rc1 line]>=0} {
		set auto [string match "theme=*" $line]
			if {$auto>0} {
				set boolean [string range $line 6 end]
			}
	}
}

close $rc1

proc click {} {
	global theme_list prefix rc boolean rev
		set rc [open ~/.oroborusrc r]
		foreach back [$theme_list curselection] {
			set i [$theme_list get $back]
		}
	set theme $i
		set lines [split [read -nonewline $rc] ]
		close $rc
		set lstring [lsearch $lines "theme=*"]
		set lines [lreplace $lines $lstring $lstring]
		if {$rev == 1} {
			lappend lines "theme=$boolean"
		} else {
			lappend lines "theme=$prefix/oroborus/themes/$theme"
		}
	set rj [join $lines \n]
		set rc2 [open ~/.oroborusrc w]
		puts $rc2 $rj 
		flush stdout
		close $rc2
		set kill1 "-1"
		set wm "oroborus"
		exec killall $kill1 $wm
}

proc dirlist {} {
	global a prefix theme_list  
		$theme_list delete 0 end
		set a [exec ls $prefix/oroborus/themes]
		foreach item $a {
			$theme_list insert end $item
		}
	$theme_list selection set 0
}

proc try {} {
global rev
set rev 0 
click
}

proc ok {} {
click
exit
}

proc cancel {} {
global rev f
set rev 1
click
}

dirlist 
