################################
#### gamefaq tcl script v1.10  by Amien ####
################################

# v1.00 first release
# v1.10 adapted to new webpage
# !games <console>
#
# will show upcoming games with retail date
# source is http://www.gamefaqs.com/
#
# If you find any bug, please bug me at amien7049@hotmail.com
# respect to all the people from TCL , PHP and SQL channels on efnet
# for answering all my n00b questions so fast
#
# not the finest lines of tcl but it all works
# feel free to modify, but please send me your scripts or updates


bind pub - !games games

proc games {nick host handle chan arg}  {
    global system 


## parameter settings ##

    set arg [string toupper $arg]
    if {$arg != "PC" && $arg != "GC" && $arg != "PS2" && $arg != "XBOX" && $arg != "GBA"} {  
            puthelp "notice $nick :Missing argument, type !games SECTION, available sections are: GBA GC PC PS2 XBOX"
            return 0
            }
    puthelp "privmsg $chan :UPCOMING $arg GAMES // US RETAiL // v1.10"

    if {$arg == "PC"} { set urlgames "http://www.gamefaqs.com/computer/doswin/" }
    if {$arg == "GC"} { set urlgames "http://www.gamefaqs.com/console/gamecube/" }
    if {$arg == "PS2"} { set urlgames "http://www.gamefaqs.com/console/ps2/" }
    if {$arg == "XBOX"} { set urlgames "http://www.gamefaqs.com/console/xbox/" }
    if {$arg == "GBA"} { set urlgames "http://www.gamefaqs.com/portable/gbadvance/" }
    

## read  http page ##

    package forget http
    package require http 2.3
    set conn [::http::geturl $urlgames]
    set data [::http::data $conn]
    set gameline2 ""

## lets strip! ##

    foreach line [split $data \n] {
      if {[string first "upcomingreleases" $line] != -1} {  

         

           set line [string map  {"</tr><tr><td" "NLP"} $line] 
           set line [string map  {"</font></td><td>" " "} $line]      
           set line [string map  {"</a><br>" " "} $line] 
           set line [string map  {">" " "} $line] 
           set line [string map  {"<font size=" ""} $line] 
           set line [string map  {"<a href=" ""} $line] 
           set line [string map  {"data" ""} $line] 
           set line [string map  {".html" ""} $line] 
           set line [string map  {"&amp;" "&"} $line] 
           set line [string map  {"valign=" " "} $line] 

           set finalline [string first "Japan</th" $line]
           if {$finalline == -1 } { set finalline [string first "Europe</th" $line] }

           set finalline [expr $finalline - 1]
     
           set line [string range $line 0 $finalline]
        
           set line "$line NLP"
           set cutfirst [string first "NLP" $line]
           set cutfirst [expr $cutfirst + 3]
           set line [string range $line $cutfirst end]

           
         
## create final line ##       
          
           while {[string first "NLP" $line] != -1} {
               set dateline [lindex $line 2]
               set dateline [string map {"/" "-"} $dateline]
               set dateline [string map {"<-td" ""} $dateline]
               set newline [string first "NLP" $line]
               set newline [expr $newline - 1]
               set gameline [string range $line 0 $newline]  
               
               

               #regsub -all {[0-9]+} $gameline "" gameline
              
               foreach element $gameline { 
                                      
                                              if {[string first "/" $element] != -1} { set gameline2 "$gameline2\,"} else {set gameline2 "$gameline2 $element"}
                                          }
 
                     
               #puthelp "privmsg $chan :($dateline) [lrange $gameline 5 end]. [llength $gameline]"

               set finalline "[lrange $gameline2 5 end]."
               set finalline [string map {",." "."} $finalline]

               set finalline2 [string first ",,," $finalline]
            
               if {$finalline2 != -1 } { set finalline [string range $finalline 0  [expr $finalline2 + 1]] }
               set finalline [string map {",," "."} $finalline]

            
               puthelp "privmsg $chan :($dateline) $finalline"
               set gameline2 ""


## create new line for new date ###         
                      
               set found [string first "NLP" $line] 
               set found [expr $found + 3]
               set line [string range $line $found end]]
             
           }
      }
    }
 }




putlog "Amiens GAMESFAQS Scripts"