package require msgcat

namespace eval pluginmanager {

    ::msgcat::mcload [file join [file dirname [info script]] msgs]

    variable DirPluginsAll [file join [file dirname $::rootdir] plugins]
    variable PluginsAll [lsort [glob -type d -tails \
	-directory $DirPluginsAll -nocomplain *]]
    foreach plugin $PluginsAll {
	variable $plugin 0
    }
    variable DirPluginsInstalled [file join $::configdir plugins]
    if {[file exists $DirPluginsInstalled] == 0} {
        file mkdir $DirPluginsInstalled
    }
    variable PluginsInstalled
    variable winname .plugin_manager

    proc create_menu {} {
	set menu [.mainframe getmenu services]
	set ix [::msgcat::mc "Plugins"]
	$menu insert $ix separator
	$menu insert $ix command \
		-label [::msgcat::mc "Add/remove plugins"] \
		-command [namespace current]::open_window
    }

    hook::add finload_hook [namespace current]::create_menu

    proc open_window {} {
	variable PluginsAll
	variable DirPluginsInstalled
	variable PluginsInstalled [lsort [glob -type d -tails \
		-directory $DirPluginsInstalled -nocomplain *]]
	variable winname

	if {[winfo exists $winname]} {
	    return
	}

	add_win $winname -title [::msgcat::mc "Add/remove plugins"] \
	    -tabtitle [::msgcat::mc "Add/remove plugins"] \
	    -raisecmd [list focus $winname.apply]
	set sw [ScrolledWindow $winname.sw]
	set t [text $winname.pluginlist -wrap word -background [$winname cget -background]]
	$sw setwidget $t
	place $sw -in $winname -relwidth 1 -relheight 1 \
			-x 0 -y 0 -width -1 -height -40
	
	foreach plugin $PluginsAll {
	    if { [lsearch -exact $PluginsInstalled $plugin] >= 0 } {
		variable $plugin 1
	    } else {
		variable $plugin 0
	    }
	    set cb_$plugin [checkbutton $t.cb$plugin -cursor left_ptr\
			-text $plugin \
			-variable [namespace current]::$plugin]
	    $t window create end -window [set cb_$plugin]
	    bindscroll [set cb_$plugin] $t
	    $t insert end "\n"
	    $t insert end [::msgcat::mc $plugin]
	    $t insert end "\n\n"
	}
	set apply_button [button $winname.apply \
			-text [::msgcat::mc "Apply changes"] \
			-command [namespace current]::installdelete] 
	
	place $apply_button -in $winname -relx 1 -rely 1 -x -5 -y -2 -anchor se
	$t configure -state disabled
    }

    proc installdelete {} {
        variable DirPluginsAll
	variable PluginsAll
	variable DirPluginsInstalled
	variable PluginsInstalled [lsort [glob -type d -tails \
		-directory $DirPluginsInstalled -nocomplain *]]
	variable winname
	foreach plugin $PluginsAll {
	    variable $plugin
	    if { [set $plugin] } {
		if { [lsearch -exact $PluginsInstalled $plugin] < 0 } {
		    file copy -force [file join $DirPluginsAll $plugin] $DirPluginsInstalled
		}
	    } else {
		if { [lsearch -exact $PluginsInstalled $plugin] >= 0 } {
		    file delete -force [file join $DirPluginsInstalled $plugin]
		}
	    }
	}
	tk_messageBox -type ok -icon info -title [::msgcat::mc "Operation completed"] \
		-message [::msgcat::mc "You need to restart Tkabber to apply changes"]
    }
}

# vim:ts=8:sw=4:sts=4:noet
