# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $


#package require http

bind pub - !rdep pub:rindex
bind pub - !ddep pub:dindex

bind msg - !rdep msg:rindex
bind msg - !ddep msg:dindex

bind msg - rdep msg:rindex
bind msg - ddep msg:dindex

# old dont use me.. I can't make up my mind.
bind pub - !rrdep pub:rindex

set agent "Mozilla"

proc pub:rindex { nick uhost handle chan arg } {
	set stripmagic "/>=-_1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	if {[string trim "[string toupper $arg]" "${stripmagic}" ] != ""} { return 1 }

	catch { exec ~/grabrdeps.sh "$arg" } rdeps

	if {$rdeps == "child process exited abnormally"} {
		putserv "PRIVMSG $chan :no reverse rdepends info for $arg"
		return 0
	}

	if {$rdeps == ""} { return 0 }

	if {([ string length $rdeps ] > 400)} {
		set rdeps "yikes [llength ${rdeps}] pkgs reverse rdepend on ${arg}! Try digging around here instead. http://tinderbox.dev.gentoo.org/misc/rindex/"
	}
	putserv "PRIVMSG $chan :$rdeps"
	return 0
}

proc pub:dindex { nick uhost handle chan arg } {
	set stripmagic "/>=-_1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	if {[string trim "[string toupper $arg]" "${stripmagic}" ] == ""} {
		catch {exec ~/grabddeps.sh "$arg" } ddeps
		if {$ddeps == "child process exited abnormally"} {
			putserv "PRIVMSG $chan :no reverse depends info for $arg"
			return 0
		}
		if {$ddeps != ""} {
			if {([ string length $ddeps ] > 400)} {
				set ddeps "yikes [llength ${ddeps}] pkgs reverse depend on ${arg}! Try digging around here instead. http://tinderbox.dev.gentoo.org/misc/dindex/"
			}
			putserv "PRIVMSG $chan :$ddeps"
			return 0
		}
	}
}

proc msg:rindex { nick uhost handle arg } {
	pub:rindex "${nick}" "${uhost}" "${handle}" "${nick}" "${arg}"
}

proc msg:dindex { nick uhost handle arg } {
	pub:dindex "${nick}" "${uhost}" "${handle}" "${nick}" "${arg}"
}
