#!/usr/bin/wish

set PACKAGEDIR  /usr/lib/ppxp/tcl
set TKPPXPDIR   /usr/lib/ppxp/tkppxp

set SCRIPTDIR   [file join $TKPPXPDIR scripts]
set WIDGETDIR   [file join $TKPPXPDIR widgets]
set IMAGEDIR    [file join $TKPPXPDIR images]
set RESOURCEDIR [file join $TKPPXPDIR resources]

set PPXPLOG     /var/log/ppxp

set env(PATH) /usr/bin:/bin:/sbin:/usr/sbin

catch {
    set auto_path [concat $PACKAGEDIR $WIDGETDIR $auto_path]
}

set sources {
    tclutil.tcl
    toplevel.tcl
    popup.tcl
    ppxplink.tcl
    log.tcl
    terminal.tcl
    conf.tcl
    modem.tcl
    indicator.tcl
    command.tcl
    setup.tcl
    panel.tcl
    qdial.tcl
    icon.tcl
}

foreach f $sources {
    set file [file join $SCRIPTDIR $f]
    if [file readable $file] {
	source $file
    }
}

source [file join $WIDGETDIR widgetutil.tcl]

option readfile [file join $RESOURCEDIR default] userDefault

wm command . [concat $argv0 $argv]

set start_hide 0
set mini_window 0
set icon_relief ""
set icon_bd ""

set start_qdial 0
set start_icon 0
set start_log 0
set start_setup 0
set DEBUG 0

switch -regexp $argv0 {
    {qdial$}     {set start_qdial 1}
    {tkppxpm$}   {set stat_icon   1}
    {ppxplog$}   {set start_log   1}
    {ppxpsetup$} {set start_setup 1}
}

set av1 $argv
set av2 {}
set av  {}

while {[llength $av1] > 0} {
    set o [shift av1]
    if {$o != "-style"} {
	lappend av2 $o
    } else {
	switch [shift av1] {
	    dock {
		set start_hide 1
		set icon_relief sunken
	    }
	    wharf {
		set start_hide 1
		set mini_window 1
		set icon_relief sunken
	    }
	    goodstuff {
		set start_hide 1
		set mini_window 1
		set icon_bd 0
	    }
	    fvwmbuttons {
		set start_hide 1
		set mini_window 1
	    }
	}
    }
}

while {[llength $av2] > 0} {
    set o [shift av2]
    switch -- $o {
	-iconic     {set start_icon 1}
	-withdrawn  {set start_hide 1}
	-iconrelief {set icon_relief [shift av2]}
	-iconborder {set icon_bd [shift av2]}
	-mini       {set mini_window 1}
	-qdial      {set start_qdial 1}
	-style      {}
	-debug      {set DEBUG 1}
	default     {lappend av $o}
    }
}

PPxP:Start $av

set subdirs {conf chat log rc tkppxp}

if [info exists PPxP_UsrPath] {
    if ![file exists $PPxP_UsrPath] {
	catch {
	    file mkdir $PPxP_UsrPath
	    file attributes $PPxP_UsrPath -permissions 0700
	}
    }
    foreach d $subdirs {
	set dir [file join $PPxP_UsrPath $d]
	if ![file exists $dir] {
	    catch {
		file mkdir $dir
		file attributes $dir -permissions 0700
	    }
	}
    }
    set passwd [file join $PPxP_UsrPath passwd]
    if ![file exists $passwd] {
	set f [open $passwd w 0600]
	close $f
    }
}

if {$icon_relief != ""} {
    option add *tkppxpIcon.relief $icon_relief
}

if {$icon_bd != ""} {
    option add *tkppxpIcon.borderWidth $icon_bd
    option add *tkppxpIcon.width  [expr 56 + $icon_bd]
    option add *tkppxpIcon.height [expr 56 + $icon_bd]
}

wm group . .

proc start_main {flag} {
    global start_qdial start_log start_setup

    if $start_qdial {
	Qdial:Start $flag
    } elseif $start_log {
	Log:Start $flag
    } elseif $start_setup {
	Setup:Start $flag
    } else {
	Panel:Start $flag
    }
}

if $mini_window {
    start_main 0
    Icon:Start 1 .tkppxpPanel
    if {$start_hide || $start_icon} {
#	tkwait visibility .tkppxpPanel
	wm withdraw .tkppxpPanel
    }
    if $start_icon {
#	tkwait visibility .
	wm iconify .
    }
} else {
    Icon:Start 0 .
    wm iconwindow . .tkppxpIcon
    start_main 1
    if $start_hide {
	tkwait visibility .
	wm withdraw .
    } elseif $start_icon {
#	tkwait visibility .
	wm iconify .
    }
}

# Local Variables:
# mode: tcl
# End:
