#include <stdio.h>
#include <stdlib.h>
#include "../inc/config.h"

#include "tk.h"

#include "vtkToolkits.h"

#ifdef __cplusplus
extern "C" {
#endif
extern int Vtkcommontcl_Init(Tcl_Interp *interp);
extern int Vtkiotcl_Init(Tcl_Interp *interp);
extern int Vtkfilteringtcl_Init(Tcl_Interp *interp);
extern int Vtkgraphicstcl_Init(Tcl_Interp *interp);
extern int Vtkrenderingtcl_Init(Tcl_Interp *interp);
extern int Vtktkrenderwidget_Init(Tcl_Interp *interp);
extern int Vtkimagingtcl_Init(Tcl_Interp *interp);
extern int Vtktkimagewindowwidget_Init(Tcl_Interp *interp);
extern int Vtktkimageviewerwidget_Init(Tcl_Interp *interp);
extern int Vtkpatentedtcl_Init(Tcl_Interp *interp);
extern int Vtkcontribtcl_Init(Tcl_Interp *interp);
extern int Vtklocaltcl_Init(Tcl_Interp *interp);
extern int Vtkparalleltcl_Init(Tcl_Interp *interp);

#ifdef __cplusplus
};
#endif

int Vtktcl_Init(Tcl_Interp *interp)
{
static char script[] = "\
foreach dir [list \"/net/hp01/workC25/Eos/util/srcI386LINUX/TclTk/VTK/Wrapping/Tcl\" \"/home/people/tacyas/Eos/
util/I386LINUX/lib/vtk/tcl\" [file join [file join [file dirname [file dirname [info nameofexecutable]]] Wrapping] Tcl] \"C:/Program Files/vtk40/Wrapping/Tcl\"] { \n\
    if {[file isdirectory $dir]} { \n\
        lappend auto_path $dir \n\
    } \n\
} \n\
rename package package.orig;\n\
proc package {args} {\n\
    if {[catch {set package_res [eval package.orig $args]} catch_res]} {\n\
        global errorInfo env;\n\
        if {[lindex $args 0] == \"require\"} {\n\
            set expecting \"can't find package vtk\";\n\
            if {![string compare -length [string length $expecting] $catch_res $expecting]} {\n\
                set msg \"The Tcl interpreter was probably not able to find the VTK packages. Please check that your TCLLIBPATH environment variable includes the path to your VTK Wrapping/Tcl directory (example: C:/Program Files/vtk40/Wrapping/Tcl). You might find it under your VTK binary or source installation directory, or under your site-specific {CMAKE_INSTALL_PREFIX}/lib/vtk/tcl directory.\";\n\
                if {[info exists env(TCLLIBPATH)]} {\n\
                  append msg \" The TCLLIBPATH current value is: $env(TCLLIBPATH))\";\n\
                } \n\
                set errorInfo \"$msg. The TCLLIBPATH variable is a set of space separated paths (hence, path containing spaces should be surrounded by quotes first). Windows users should use forward (Unix-style) slashes '/' instead of the usual backward slashes. More informations can be found in the Wrapping/Tcl/README source file (also available online at http://public.kitware.com/cgi-bin/cvsweb.cgi/~checkout~/VTK/Wrapping/Tcl/README).\n$errorInfo\";\n\
            }\n\
        }\n\
        error $catch_res $errorInfo;\n\
    }\n\
    return $package_res;\n\
}";

  /* init the core vtk stuff */
  if (Vtkcommontcl_Init(interp) == TCL_ERROR)
    {
    return TCL_ERROR;
    }

  if (Vtkfilteringtcl_Init(interp) == TCL_ERROR)
    {
    return TCL_ERROR;
    }

  if (Vtkiotcl_Init(interp) == TCL_ERROR)
    {
    return TCL_ERROR;
    }

  if (Vtkgraphicstcl_Init(interp) == TCL_ERROR)
    {
    return TCL_ERROR;
    }

  if (Vtktkrenderwidget_Init(interp) == TCL_ERROR) 
  	{
	return TCL_ERROR;
    }

  if (Vtkrenderingtcl_Init(interp) == TCL_ERROR) 
  	{
	return TCL_ERROR;
    }

  if (Vtkimagingtcl_Init(interp) == TCL_ERROR)
    {
    return TCL_ERROR;
    }
  if (Vtktkimagewindowwidget_Init(interp) == TCL_ERROR)
    {
    return TCL_ERROR;
    }
  if (Vtktkimageviewerwidget_Init(interp) == TCL_ERROR)
    {
    return TCL_ERROR;
    }

#ifdef PARALLEL
  if (Vtkpatentedtcl_Init(interp) == TCL_ERROR)
    {
    return TCL_ERROR;
    }
	
  if (Vtkcontribtcl_Init(interp) == TCL_ERROR)
    {
    return TCL_ERROR;
    }

  if (Vtklocaltcl_Init(interp) == TCL_ERROR)
    {
    return TCL_ERROR;
    }
  if (Vtkparalleltcl_Init(interp) == TCL_ERROR)
    {
    return TCL_ERROR;
    }
#endif

  Tcl_Eval(interp, script);

  return TCL_OK;
}

int Vtktcl_SafeInit(Tcl_Interp *interp)
{
  return Vtktcl_Init(interp);
}

