#!/usr/bin/tclsh
proc get_input {txt} {
 set var ""
  while {[llength $var] < 1} {
   puts -nonewline stdout $txt
   flush stdout
   gets stdin var
  }
 return $var
}
 proc switchit {var2 choicelist} {
  set clength [llength $choicelist]
   for {set i 0} {$i < $clength} {incr i} {
     if {$i == [expr $var2 - 1]} {
      return [lindex $choicelist $i]
     }
   }
     puts stdout "\nSorry, that entry is invalid.. Using default (#1).."
     return [lindex $choicelist 0]
 }
proc get_menu_input {txt menu} {
 set var ""
  while {[llength $var] < 1} {
   set loop 0
    foreach menuentry $menu { 
     incr loop
     puts stdout "$loop. $menuentry"
    }
   puts -nonewline stdout $txt
   flush stdout
   gets stdin var
   return $var
  }
}
proc get_yesno {txt default} {
 puts -nonewline stdout "$txt "
 flush stdout
 gets stdin text
 set text [split [string tolower $text] ""] 
  if {[lindex $text 0] == "y"} {
   return 1
  }
  if {[lindex $text 0] == "n"} {
   return 0
  }
 puts stdout "Invalid entry. Defaulting to: [return_yesno $default]."
 return $default
}
proc return_yesno {num} {
 switch $num {
  0 {
   return "No"
  }
  1 {
   return "Yes"
  }
 }
}

puts stdout "EMulti install utility..\n"
puts stdout "Ok.. First we're going to ask you some simple yes or no questions..\nYou can always change this setup in emulti.config if you need to.\n"
set var2 [get_yesno "Is this bot going to be the hub (The bot that overrides other bots)?" 0]
 if {$var2 == "0"} {
  set var3 [get_yesno "Do you want the bot to join the channels of the Hub bot?" 1]
 } {
  set var3 "0"
 }
  set globowner [get_input "Enter the person you want *ALWAYS* to be owner..:\n"]
puts stdout "Ok.. That ends basic setup.. Now we'll deal with channels..\n"
puts stdout "Enter the channels you want your bot on, seperated by commas."
puts stdout "(For example, \"#eggdrop, #portal, #twilight_zone\")"
set var4 [get_input "Channels: "]
puts stdout "Ok, we're going to try to put the bot on: $var4"
 foreach chan $var4 {
  set chan [string tolower [string trimright $chan ,]]
  set chanoptions($chan) ""
  set var5($chan) [get_yesno "On $chan, do you want users added as autooped users to be autooped?" 1] 
    if {$var5($chan) == "1"} {
     append chanoptions($chan) "+autoop "
    }
  set var6($chan) [get_yesno "Do you want the bot to join $chan on startup?" 1]
    if {$var6($chan) == "1"} {
     append chanoptions($chan) "+joinonstart "
    }
  set var7($chan) [get_input "Enter the users, seperated by commas, that you want to be owners on $chan:\n"]
  puts stdout "Ok! The settings for $chan are now: AutoOp: ([return_yesno $var5($chan)]) Join On Start: ([return_yesno $var6($chan)])"
  puts stdout "Owners for $chan: $var7($chan)"
 }
puts stdout "Remember you can always edit emulti.config\n"
puts stdout "Now for the main installation stuff.."
set var8 "whatever"
  set emulti [open "emulti.config" w]
  puts $emulti "## Changes autoop-on-add flag.. This allows you to add autoop to the"
  puts $emulti "## flags of all users when they're added.. if you set this to 0 you can safely "
  puts $emulti "## leave channel autoop on and .autoop <nick> individually"
  puts $emulti "set autoop $var2\n"
  puts $emulti "## Should I join channels the hub joins ?"
  puts $emulti "set joinbotchans $var3\n"
  puts $emulti "## Who should be a owner on all chans added?"
  puts $emulti "set chanowners(global) \"$globowner\"\n"
  puts $emulti "proc handchans {} {"
    puts $emulti "## This field is for adding the channel. Addchan is called"
    puts $emulti "## with <channel name> and <channel mode>\n" 
    puts $emulti "## EX: addchan #lamest +nst"
    puts $emulti "## Chset is called with <channel name> and <channel options>"
    puts $emulti "## Channel options being joinonstart or autoop"
    puts $emulti "## EX: chset #lamest +autoop +joinonstart\n"
    puts $emulti "## chanowners(#channel) is the channel owners for that chan.\n"
   foreach chan [array names chanoptions] {
    puts $emulti "addchan $chan +nst"
    puts $emulti "chset $chan \"$chanoptions($chan)\""
    puts $emulti "set chanowners($chan) \"$var7($chan)\""
   } 
   puts $emulti "}"
  close $emulti
  while ![file exists $var8] {
   puts -nonewline stdout "Enter the bots full directory: "
   flush stdout
   gets stdin var8
    if ![file exists $var8] {
     puts stdout "Invalid directory! Try again.."
    } {
     break
    }
  }
  if ![file exists $var8/scripts] {
   puts stdout "No scripts directory! Making one.."
   exec mkdir $var8/scripts
  }
puts stdout "Ok, copying multi.tcl to $var8/scripts/"
catch {exec cp multi.tcl $var8/scripts}
puts stdout "Copying help files to $var8/help"
  if ![file exists $var8/help] {
   puts stdout "No help directory! Making one.."
   catch {exec mkdir $var8/help} caught
    if [info exists caught] {
     puts stdout $caught
    unset caught
    }
  }
 foreach helpfile [exec ls help/] {
  catch {exec cp help/$helpfile $var8/help/} caught
   if [info exists caught] {
    puts stdout $caught
    unset caught
   }
 }
#exec rmdir help
puts stdout "Copying emulti.config to $var8"
catch {exec cp emulti.config $var8} caught
 if [info exists caught] {
  puts stdout $caught
  unset caught
 }
  if ![file exists $var8/multi] {
   puts stdout "Making $var8/multi"
   catch {exec mkdir $var8/multi} caught
    if [info exists caught] {
     puts stdout $caught
     unset caught
    }
  }
puts stdout "Copying UPDATE, etc. to $var8/multi"
catch {exec cp UPDATES $var8/multi/} caught
  if [info exists caught] {
   puts stdout $caught
   unset caught
  }
catch {exec cp INSTALL $var8/multi/} caught
  if [info exists caught] {
   puts stdout $caught
   unset caught
  }
catch {exec cp eminstall $var8/multi/} caught
  if [info exists caught] {
   puts stdout $caught
   unset caught
  }
puts stdout "You'll need to add source scripts/multi.tcl to the end of your bot config file"

