#!/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
  }
if {![file exists greetwonder.conf] && ![file exists greetwonder.help] && ![file exists greetwonder.tcl]} {
  puts "Error: Please run ./greetinstall from the greetwonder1.7/ directory."
  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.7 - 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]} {
  puts "\n  - * Can't find greetwonder.tcl"
  } {
  if {"[pwd]" != "${dir}scripts"} {
    exec cp greetwonder.tcl ${dir}scripts
    }
  }
puts "\n  - ALL DONE, now do a .rehash on the partyline (DCC)\n"
puts "(Report bugs to GreetWondeR@WINGDesign.de)"
