#!/usr/bin/env wish
###
###
################################################################
###                                              ###############
###        Alvaro's Messenger - amsn             ###############
###                                              ###############
###       http://amsn.sourceforge.net            ###############
###     amsn-users@lists.sourceforge.net         ###############
###                                              ###############
################################################################
### airadier at users.sourceforge.net (airadier) ###############
### Universidad de Zaragoza                      ###############
### http://aim.homelinux.com                     ###############
################################################################
### grimaldo@panama.iaehv.nl (LordOfScripts)     ###############
### http://www.coralys.com/linux/                ###############
################################################################
### Original ccmsn                               ###############
### http://msn.CompuCreations.com/               ###############
### Dave Mifsud <dave at CompuCreations dot com> ###############
################################################################
###
###
### 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; version 2 of the License
###
### 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.
###
### You should have received a copy of the GNU General Public License
### along with this program; if not, write to the Free Software
### Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
###
###


############################################################
### Some global variables
############################################################
set version "0.83"
set date "10/14/2003"
set weburl "http://amsn.sourceforge.net"
set program_dir ""
set HOME ""
set files_dir ""

#=======================================================================


############################################################
### Look if we are launched from a link
### and set the correct working dir
############################################################

if {[catch {file readlink [info script]} res]!=0} {

   #Error in readlink, so it's not a symbolic link
   set program_dir [file dirname [info script]]

} else {

   #Recursively update $resdir until it's not a link
   set program_dir [file dirname $res]

   while {[catch {file readlink $res} res2]==0} {
      set res $res2

      #Update $program_dir, depending on absolute or relative path
      if { [string range $res2 0 0]=="/" } {
         set program_dir [file dirname $res2]
      } else {
         set program_dir [file join $resdir [file dirname $res2]]
      }

   }
}


############################################################
### Setup other important directory paths
### depending on the platform
############################################################

if {$tcl_platform(platform) == "unix"} {
   set HOME "[file join $env(HOME) .amsn]"
   set files_dir "[file join $env(HOME) amsn_received]"
} elseif {$tcl_platform(platform) == "windows"} {
  if {[info exists env(USERPROFILE)]} {
     set HOME "[file join $env(USERPROFILE) amsn]"
     set files_dir "[file join $env(USERPROFILE) amsn_received]"
  } else {
   set HOME "[file join [pwd] amsn_config]"
   set files_dir "[file join [pwd] amsn_received]"
  }
} else {
   set HOME "[file join [pwd] amsn_config]"
   set files_dir "[file join [pwd] amsn_received]"
}

############################################################
### And setup where to find optional packages
############################################################

lappend auto_path "[file join $program_dir plugins]"
lappend auto_path "[file join ${HOME} plugins]"


set libtls ""

catch { source [file join $HOME tlsconfig.tcl] }

if { $libtls != "" && [lsearch $auto_path $libtls] == -1 } {
    lappend auto_path $libtls
}

#TODO: Move this from here ??
#///////////////////////////////////////////////////////////////////////
#Notebook Pages (Buddies,News,Calendar,etc.)
set pgBuddy ""
set pgNews  ""
#///////////////////////////////////////////////////////////////////////


############################################################
#### Load program modules
############################################################
set initialize_amsn 1



proc reload_files { } {
    foreach glob [info globals] {
	global $glob
    }

    source [file join $program_dir ctthemes.tcl]
    source [file join $program_dir notebook.tcl]	;# Notebook Megawidget
    #source [file join $program_dir notebook1.tcl]	;# Notebook Megawidget
    source [file join $program_dir rnotebook.tcl]	;# Notebook Megawidget
    source [file join $program_dir progressbar.tcl]	;# Progressbar Megawidget
    source [file join $program_dir dkffont.tcl]	;# Font selection megawidget
    source [file join $program_dir migmd5.tcl]
    source [file join $program_dir des.tcl]		;# DES encryption
    source [file join $program_dir sxml.tcl]   	;# Simple XML parser
    source [file join $program_dir lang.tcl]
    source [file join $program_dir automsg.tcl]
    source [file join $program_dir smileys.tcl]
    source [file join $program_dir skins.tcl]       ;# Used for skinning purposes
    source [file join $program_dir config.tcl]
    source [file join $program_dir proxy.tcl]
    source [file join $program_dir protocol.tcl]
    source [file join $program_dir ctadverts.tcl]
    source [file join $program_dir ctdegt.tcl]
    source [file join $program_dir hotmail.tcl]
    source [file join $program_dir groups.tcl]	;# Handle buddy groups
    source [file join $program_dir abook.tcl]	;# Handle buddy address book
    source [file join $program_dir anigif.tcl]	;# Animated GIFS
    source [file join $program_dir gui.tcl]
    source [file join $program_dir alarm.tcl]	;# Alarms code (Burger)
    source [file join $program_dir dock.tcl]	;# Docking routines
    source [file join $program_dir trayicon.tcl]	;# Docking routines for freedesktop system tray compliant docks
    source [file join $program_dir loging.tcl]	;# Euh yeh it's for loging :P
    source [file join $program_dir combobox.tcl]	;# The all mighty combobox is here! (B. Oakley)
    source [file join $program_dir blocking.tcl]    ;# The blocking users feature
    source [file join $program_dir remote.tcl  ]    ;# The remote control procedures
    source [file join $program_dir plugins.tcl]	;# Plugins system
    source [file join $program_dir balloon.tcl]       ;# For the balloons tooltip

    load_lang
}


reload_files

set initialize_amsn 0
#///////////////////////////////////////////////////////////////////////

if { [catch {package require tls}] } {
    # Either tls is not installed, or $auto_path does not point to it.
    # Should now never happen; the check for the presence of tls is made
    # before this point.
#    status_log "Could not find the package tls on this system.\n"
    set tlsinstalled 0
} else {
    set tlsinstalled 1
}


#create_dir $HOME
set log_dir "[file join ${HOME} logs]"
#create_dir $log_dir

sb set ns name ns
sb set ns sock ""
sb set ns data [list]
sb set ns serv [split $config(start_ns_server) ":"]
sb set ns stat "d"

set family [lindex $config(basefont) 0]
set size [lindex $config(basefont) 1]

::themes::Init
degt_Init
::amsn::initLook $family $size $config(backgroundcolor)


if { $config(encoding) != "auto" } {
  set_encoding $config(encoding)
}

if { $config(receiveddir) != "" } {
   set res [create_dir $config(receiveddir)]
   if { $res >= 0} {
      set files_dir $config(receiveddir)
   } else {
      create_dir $files_dir
   }
} else {
	set config(receiveddir) $files_dir
	create_dir $files_dir
}

cmsn_draw_main

bind all <KeyPress> "set idletime 0"

idleCheck

degt_protocol_win
degt_ns_command_win

after 500 proc_ns
after 750 proc_sb

if {$version != $config(last_client_version)} {
   ::amsn::aboutWindow

}

if { $config(autoconnect) == 1 } {
  ::MSN::connect $config(login) password
}

if { $config(autocheckver) == 1 } {
    set new [check_version_silent]
}

# BeginVerifyBlocked
init_remote_DS
init_dock
