####################################
### The #Homicide Efnet Trivia Bot #
####################### Ophmisu ####
############# by WSergio ###########


set scupgen "$tgpath/w"
set ftpuploader "$tgpath/tools/upload"
set scup "$tgpath/scup"
set head "\002\0031\[$tgchan\]"
set ftpcmd "ftp"
set CMDDoUpload "upload"


bind pubm N|N "% $nick-$ftpcmd*" ftp
bind pubm N|N "% !$ftpcmd*" ftp
proc callback {input} {
	foreach line [split $input "\n"] {
	  puthelp "PRIVMSG $::team(chan) :$line"
	}
}
proc DoUpload {chan what} {
	if {![file exists $::ftpuploader]} {
		puthelp "privmsg $chan :FTP uploading binary not found."
		exit
	}
	if {[GetSet ftpuser]==0||[GetSet ftppass]==0||[GetSet ftphost]==0||[GetSet ftpdir]==0} {
		say $chan "$::head FTP error:\002 detaliile de conectare nu au fost introduse..."
		return
	}	
	if {[llength $what]>1} {
		foreach single_file $what {
			DoUpload $chan $single_file
		}
		return
	} else {
		set _msg "$::head\002 Uploading:"
		if {[file exists $what]} {
			bgexec "$::ftpuploader [GetSet ftpuser] [GetSet ftppass] [GetSet ftphost] [GetSet ftpdir] $what" callback 20
			append _msg "\002 $what \002to \002[GetSet ftphost]"
		} else { append _msg "\002 skipped\002 $what - inexistent" }
		puthelp "privmsg $chan :$_msg"
	}
}



proc del {mask} {
	set _tmp [glob -nocomplain $mask]
	foreach _file $_tmp { file delete -force $_file }
}



proc GenScup {chan} {
	bgexec "$::scupgen [GetSet ftpuser] [GetSet ftppass] [GetSet ftphost] [GetSet ftpdir] blabla" callback 20
	bgexec "$::scup" callback 20
	puthelp "privmsg $chan :$::head Scup\002 generated & uploaded (Score Updater for website)"
}


proc ftp {nick uhost hand chan args} {
	global head iup
	set args [lindex $args 0]
	set args [split $args " "]
	set do ""
	switch [lindex $args 1] {
		set {
			if {[lindex $args 2]=="ftpuser"||[lindex $args 2]=="ftppass"||[lindex $args 2]=="ftphost"||[lindex $args 2]=="ftpfile"||[lindex $args 2]=="ftpdir"} {
				if {[lindex $args 3]!=""} {
				PutSet [lindex $args 2] [lindex $args 3]
				puthelp "privmsg $chan :$head \002Set \002[lindex $args 2]\002 to \002[lindex $args 3]\002"
				} else {
				puthelp "privmsg $chan :$head Error!\002 Missing [lindex $args 2]'s value"
				}
			} else {
				set do "get"
			}
			return
		}
		upload {
			if {[llength $args]>2} {
				set lfil [lrange $args 2 end]
				DoUpload $chan $lfil
			} else { 
				Stats:Gen
				DoUpload $chan "$::tgpath/.backups/lastbackup.tgz $::tgpath/index.html $::tgpath/greseli.txt $::tgpath/tools/homicide.jpg $::tgpath/tools/style.css"
			}
			return
		}
		clean {
			del "$tgpath/.backups/*"
			del "$tgpath/*.index.*"
			del "$tgpath/greseli.txt"
			del "$tgpath/txt/topics.log"
			puthelp "privmsg $chan :$head\002 Cleaned backups/errors/topics files..."
			return
		}
		backup {
			bgexec "$::tgpath/tools/backup" callbackdump 1
		}
		scup {
			Stats:Gen
			set do ""
			GenScup $chan
			return
		}
		default { set do "get" }	
	}

	if {$do=="get"} {
		puthelp "notice $nick :$head FTP - \002ftpuser:\002 [GetSet ftpuser]\002 ftppass:\002 [GetSet ftppass]\002 ftphost: \002[GetSet ftphost]\002 ftpfile: \002[GetSet ftpfile]\002 ftpdir: \002[GetSet ftpdir]\002"
		return
	}

}


proc linii {ifile} {
	set iq [open $ifile r]
	set ilist "[split [read -nonewline $iq] "\n"]"
	set ilines "[llength $ilist]"
	close $iq
	return $ilines
}
