#!/bin/sh
# ^-- little trick to restart using tclsh \
exec tclsh "$0" "$@"
# Thanx to ./autobotchk of guppy for the first 3 lines ;)
#
# Usage: ./greetinstall /path/to/your/eggdrop/eggdrop.conf.dist 
# 
# Edits your config file and puts all neccessary files to your scripts dir

set installver "0.5"
if {[llength $argv] < 1} {
  puts "Usage: $argv0 /path/to/your/eggdrop/eggdrop.configfile"
  exit
  }
if {[string match "-*" $argv]} {
  puts "Error: No options allowed."
  exit
  }
if {![string match "/*" $argv]} {
  puts "Error: Please use absolute path (starts with /)."
  exit
  }
set count 0
foreach var [split $argv /] {
  incr count
  if {$count == [llength [split $argv /]]} {
    set filename $var
    } {
    if {![info exists dir]} { set dir / } { append dir "$var/" }
    }
  }
if {[string match "*/" $filename]} { set filename [string trimright $filename /] }
if {![file exists $dir] || ![file isdirectory $dir]} {
  puts "Error: $dir: No such directory."
  exit
  }
if {![file exists ${dir}$filename] || ![file isfile ${dir}$filename]} {
  puts "Error: $filename: No such filename."
  exit
  }
if {![file readable ${dir}$filename]} {
  puts "Error: Can't read $filename, check permissons."
  exit
  }
if {![file writable ${dir}$filename]} {
  puts "Error: Can't write $filename, check permissons."
  exit
  }
if {![file exists ${dir}help] || ![file isdirectory ${dir}help]} {
  puts "Error: Are you sure, you are running an eggdrop bot from $dir?"
  exit
  }
if {![file exists ${dir}scripts] || ![file isdirectory ${dir}scripts]} {
  puts "Error: Are you sure, you are running an eggdrop bot from $dir?"
  exit
  }
if {"[string index $dir 0]" != "/"} {
  puts "Error: use absolut path (e.g. /home/yourdir/eggdrop)."
  exit
  }
set file [open ${dir}$filename r]
while {![eof $file]} { 
  set line [gets $file]
  if {[string match "*set botnet-nick*" $line]} {
    set botnetnick [lindex $line 2]
    }
  if {[string match "*set userfile*" $line]} {
    set userfile 1
    }
  lappend tmp $line
  }
close $file
if {![info exists botnetnick] && ![info exists userfile]} {
  puts "Error: $filename doesn't seem to be an valid eggdrop configfile."
  exit
  }
puts "GreetWondeR 1.5 - Installation Script $installver\n" 
puts "  - Analysing configuration of your bot $botnetnick: ${dir}$filename"
set ccount 0
foreach line $tmp {
  if {[string match "source scripts/greetwonder*.tcl" $line] || [string match "# This line was adde*" $line]} {
    if {![info exists a]} { puts "    * Found old greetwonder.tcl: updated" }
    set a 1
    } { 
    incr ccount
    lappend tmp2 $line
    }
  }  
set file [open ${dir}$filename w]
set bcount 0
foreach line $tmp2 {
  incr bcount
  # to wipe the last empty line
  if {$bcount == $ccount && $line == ""} { 
    } {
    puts $file $line
    }
  }
puts $file "# This line was added by ./greetinstall on [exec date]"
puts $file "source scripts/greetwonder.tcl"
close $file
puts "    * Configfile successfully modified."
puts "\n  - Now copying files to ${dir}scripts..."
if {![file exists greetwonder.tcl] || ![file isfile greetwonder.tcl]} {
  append missingfiles "greetwonder.tcl "
  } {
  if {"[pwd]" != "${dir}scripts"} {
    exec cp greetwonder.tcl ${dir}scripts
    } {
    append warningfiles "greetwonder.tcl "
    }
  }
if {![file exists greetwonder.conf] || ![file isfile greetwonder.conf]} {
  append missingfiles "greetwonder.conf "
  } {
  if {"[pwd]" != "${dir}scripts"} {
    if {[file exists ${dir}scripts/greetwonder.conf]} {
      set file [open ${dir}scripts/greetwonder.conf r]
      while {![eof $file]} {
        set line [gets $file]
	if {[string match "# Version: *" $line]} {
	  set cof 1
	  }
	if {[string match "set greet(gbind) *" $line]} {
	  set cof 2
	  }
	if {[string match "# You didn't edit your config file, as you were told, eh? :)*" $line]} { set cedit 1 }
        }	
      close $file
      }
    if {[info exists cof]} {
      if {$cof == 1} {
        puts "    * Found old .conf file: updated (you have to reedit it)"
        exec cp greetwonder.conf ${dir}scripts
        } {
        if {[info exists cedit]} {
          puts "    * Found old .conf file: updated"
          } {
          puts "    * Found old .conf file: updated (you don't have to reedit it)"
          }
        } 
      } {
      exec cp greetwonder.conf ${dir}scripts
      }
    if {[info exists cedit]} {
      puts "    * WARNING: Don't forget to edit your .conf"
      }
    }
  } {
  append warningfiles "greetwonder.conf "
  }
if {![file exists greetwonder.help] || ![file isfile greetwonder.help]} {
  append missingfiles "greetwonder.help "
  } {
  if {"[pwd]" != "${dir}scripts"} {
    exec cp greetwonder.help ${dir}scripts
    } {
    append warningfiles "greetwonder.help"
    }
  }
if {[info exists missingfiles]} {
  puts "    * Warning: Couldn't copy file(s):"
  foreach file $missingfiles {
    puts "      - $file"
    }
  puts "    * Copy these files to directory: ${dir}scripts"
  } {
  puts "    * Copied greetwonder.tcl, greetwonder.help and greetwonder.conf"
  }
puts "\n  - ALL DONE, now do a .rehash on the partyline (DCC)\n"
puts "(Report bugs to greetwonder@gmx.net)"
