#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.
#http://www.gnu.org/licenses/gpl.txt

#Variables

#HLTV channel, ops in that channel are allowed to control the bot
set hltv_channel "#cs.gather.nl"
#Text that has to prefix all commands
set hltv_command "!hltv"
#Space seperated list of available HLTV screens
set hltv_screens {hltv1 hltv2 hltv3 hltv4 hltv5}

#DON'T EDIT BELOW THIS LINE IF YOU DON'T KNOW EGGDROP TCL

#Bind !hltv command
bind pub - $hltv_command hltv_main

#Define method that does all the exciting stuff
proc hltv_main { nick uhost hand chan args } {
	global hltv_channel hltv_command hltv_screens
	set args [join $args]
	
        #Check if user is opped in $hltv_channel
	set hltv_hasaccess 0
        if {[isop $nick $hltv_channel]} {
		#if opped, set access variable to one
	        set hltv_hasaccess 1	
	}
	
	#If you want allow another channel's ops to control the bot, uncomment the following bit:
	
	#if {[isop $nick #channelname]} {
	#	set hltv_hasaccess 1
	#}

	#exit if user doesn't have ops in correct channel
	if {$hltv_hasaccess!=1} { return 0 }

	#If user hasn't given any arguments, send notice with info
	if {[lindex $args 0]==""} { 
		putquick "NOTICE $nick :For help: $hltv_command commands"
		return 0
	}


	if {[lindex $args 0]=="commands"} {
	#Show user possible commands if he asks for it
               putquick "NOTICE $nick :$hltv_command status - Detailed statusreport, $hltv_command stat - Basic statusreport, $hltv_command <hltv#> connect ip:port password (optional) - Connect <hltv#> to server, takes optional password argument"
	       putquick "NOTICE $nick :$hltv_command <hltv#> record demoname - Starts demorecord with name <demoname>, $hltv_command <hltv#> stop - Disconnects <hltv#> from server, $hltv_command <hltv#> stoprecord - Stops demorecording on <hltv#>, $hltv_command <hltv#> status - Detailed statusreport for <hltv#>"
	       return 0	       
	}

	if {[lindex $args 0]=="stat"} {
                set hltv_next 0
		set hltv_lastline ""
		set hltv_usedlist ""
		set hltv_freelist ""
		#Tell channel were command was used what we're doing.
		putquick "PRIVMSG $chan : Fetching HLTV status..."

		while {[lindex $hltv_screens $hltv_next]!=""} {
			set hltv_statusname [lindex $hltv_screens $hltv_next]
			set hltv_next [expr $hltv_next + 1]
			#Send status check command
			if {[file exists /tmp/$hltv_statusname]} { eval [exec rm /tmp/$hltv_statusname] }
			#Ask HLTV for its status
			eval [exec screen -S $hltv_statusname -X stuff "status "]
			#Delete old file
			#Write the info in the screen to a temp file
			sleep 500
			eval [exec screen -S $hltv_statusname -X hardcopy /tmp/$hltv_statusname]
			sleep 500
			#Reading tmp file into eggdrop
			set fs [open /tmp/$hltv_statusname r]
			while {![eof $fs]} {
			gets $fs line
			if {[string match *Connected* $line]} { set hltv_lastline $hltv_statusname }
			if {[string match *Not*connected* $line]} { set hltv_lastline ""}
			if {[string match *,*Players* $line]} { set hltv_players [string rang $line [expr [string length $line] - 2] [expr [string length $line] - 1]] } 
			}
			close $fs
			if {$hltv_lastline!=""} { 
	                if {$hltv_players!=""} { set hltv_usedlist "$hltv_usedlist $hltv_statusname ([expr $hltv_players - 1])" }
	                if {$hltv_players==""} { set hltv_usedlist "hltv_usedlist $hltv_statusname (0)" }
			}				
			if {$hltv_lastline==""} { set hltv_freelist "$hltv_freelist $hltv_statusname" }
			set hltv_lastline ""
			}
			putquick "PRIVMSG $chan :\002\037HLTV servers in use:\037\002 \0034 $hltv_usedlist"
			putquick "PRIVMSG $chan :\002\037HLTV servers available:\037\002 \0033 $hltv_freelist"
			return 0
	}
	if {[lindex $args 0]=="status"} {
		set hltv_next 0
		set hltv_lastline ""
		while {[lindex $hltv_screens $hltv_next]!=""} {
			set hltv_statusname [lindex $hltv_screens $hltv_next]
			set hltv_next [expr $hltv_next + 1]
			#Send status check command
		        if {[file exists /tmp/$hltv_statusname]} { eval [exec rm /tmp/$hltv_statusname] }
			#Ask HLTV for its status
			eval [exec screen -S $hltv_statusname -X stuff "status "]
			#Delete old file
			#Write the info in the screen to a temp file
			sleep 500
			eval [exec screen -S $hltv_statusname -X hardcopy /tmp/$hltv_statusname]
			sleep 500
			#Reading tmp file into eggdrop
			set fs [open /tmp/$hltv_statusname r]
			while {![eof $fs]} {
				gets $fs line
				if {[string match *Connected* $line]} { set hltv_lastline $line }
				if {[string match *Name* $line]} { set hltv_lastname $line }
				if {[string match *Not*connected* $line]} { set hltv_lastline ""}
				if {[string match *,*Players* $line]} { set hltv_players [string rang $line [expr [string length $line] - 2] [expr [string length $line] - 1]] }}
			close $fs
			if {$hltv_lastline!=""} { 
	                if {$hltv_players!=""} { putquick "PRIVMSG $chan :$hltv_statusname $hltv_lastline, $hltv_lastname, players: [expr $hltv_players - 1]" }
	                if {$hltv_players==""} { putquick "PRIVMSG $chan :$hltv_statusname $hltv_lastline, $hltv_lastname" }
			}
			if {$hltv_lastline==""} { putquick "PRIVMSG $chan :$hltv_statusname is not connected" }
			set hltv_lastline ""
		}
		return 0
	}
	
	set hltv_name ""
	set hltv_next 0
	while {[lindex $hltv_screens $hltv_next]!=""} {
	        set hltv_tempname [lindex $hltv_screens $hltv_next]
		set hltv_next [expr $hltv_next + 1]
		if {$hltv_tempname==[lindex $args 0]} { set hltv_name $hltv_tempname }
	}						
	
	if {$hltv_name==""} { 
		putquick "PRIVMSG $chan :There is no HLTV server called '[lindex $args 0]'"
		return 0
	}

	if {[lindex $args 1]=="connect"} {
		if {[lindex $args 2]==""} {
			#User forgot IP:PORT PASSW
			putquick "NOTICE $nick :You forgot to enter IP:PORT and/or password"
			return 0
		}
		
		putquick "PRIVMSG $chan :Connecting '[lindex $args 0]' to [lindex $args 2]..."
		if {[lindex $args 3]!=""} { eval [exec screen -S $hltv_name -X stuff "serverpassword [lindex $args 3] "] }
		eval [exec screen -S $hltv_name -X stuff "connect [lindex $args 2] "]
		return 0
	}

	if {[lindex $args 1]=="record"} {
		
		if {[lindex $args 2]==""} {
			#If demoname is forgotten, notice user
			putquick "NOTICE $nick :You forgot to enter the name of the demo you want to record"
			#And stop
			return 0
		}
		set hltv_lastline ""
		if {[file exists /tmp/$hltv_name]} { eval [exec rm /tmp/$hltv_name] }
		#Ask HLTV for its status
		eval [exec screen -S $hltv_name -X stuff "status "]
		#Delete old file
		#Write the info in the screen to a temp file
		sleep 500
		eval [exec screen -S $hltv_name -X hardcopy /tmp/$hltv_name]
		sleep 500
		#Reading tmp file into eggdrop
		set fs [open /tmp/$hltv_name r]
		while {![eof $fs]} {
		gets $fs line
		if {[string match *Connected* $line]} { set hltv_lastline $line }
		if {[string match *Not*connected* $line]} { set hltv_lastline ""}
		}
		close $fs
		if {$hltv_lastline==""} { 
			putquick "PRIVMSG $chan :'$hltv_name' is not connected, unable to record"
			return 0
		}
		putquick "PRIVMSG $chan :Starting demorecord on '$hltv_name'"
		eval [exec screen -S $hltv_name -X stuff "stoprecording "]
		eval [exec screen -S $hltv_name -X stuff "record [lindex $args 2] "]
		return 0
	}	
	if {[lindex $args 1]=="stop"} {
        	putquick "PRIVMSG $chan :Disconnecting '$hltv_name' from server"
		eval [exec screen -S $hltv_name -X stuff "stop "]
		return 0
	}
        if {[lindex $args 1]=="stoprecord"} {
		putquick "PRIVMSG $chan :Terminating demo recording on '$hltv_name'"
                eval [exec screen -S $hltv_name -X stuff "stoprecording "]
		return 0
        }

        if {[lindex $args 1]=="status"} {
		set hltv_lastline ""
		if {[file exists /tmp/$hltv_name]} { eval [exec rm /tmp/$hltv_name] }
		#Ask HLTV for its status
		eval [exec screen -S $hltv_name -X stuff "status "]
		#Write the info in the screen to a temp file
		sleep 500
		eval [exec screen -S $hltv_name -X hardcopy /tmp/$hltv_name]
		sleep 500
		#Reading tmp file into eggdrop
		set fs [open /tmp/$hltv_name r]
		while {![eof $fs]} {
			gets $fs line
			if {[string match *Connected* $line]} { set hltv_lastline $line }
			if {[string match *Name* $line]} { set hltv_lastname $line }			
			if {[string match *Not*connected* $line]} { set hltv_lastline ""}
			if {[string match *,*Players* $line]} { set hltv_players [string rang $line [expr [string length $line] - 2] [expr [string length $line] - 1]] }
		}
		close $fs
		if {$hltv_lastline!=""} { 
		putquick "PRIVMSG $chan :$hltv_lastline"
		if {$hltv_players!=""} { putquick "PRIVMSG $chan :$hltv_lastname, players: [expr $hltv_players - 1]" }
		if {$hltv_players==""} { putquick "PRIVMSG $chan :$hltv_lastname" }
		}
                if {$hltv_lastline==""} { putquick "PRIVMSG $chan :'$hltv_name' is not connected" }
                set hltv_lastline ""								
		}
}
proc sleep { val } {
     set x 0
     after $val {set x 1}
     vwait x
}

