diff --git a/main.mk b/main.mk
index 9d09cefcbb..2a2091088d 100644
--- a/main.mk
+++ b/main.mk
@@ -254,6 +254,9 @@ target_source:	$(SRC)
 
 sqlite3.c:	target_source $(TOP)/tool/mksqlite3c.tcl
 	tclsh $(TOP)/tool/mksqlite3c.tcl
+	cp sqlite3.c tclsqlite3.c
+	cat $(TOP)/src/tclsqlite.c >>tclsqlite3.c
+	tclsh $(TOP)/tool/mksqlite3internalh.tcl
 
 # Rules to build the LEMON compiler generator
 #
diff --git a/manifest b/manifest
index f9187ae042..b0dffb7125 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Add\sa\starget\sto\sthe\sautoconf-generated\smakefile\sfor\samalgamation.\s(CVS\s3816)
-D 2007-04-05T18:34:58
+C Fix\sthe\samalgamation\sso\sthat\sit\sworks\swith\s-DSQLITE_ENABLE_REDEF_IO.\s\sChange\ntclsqlite.c\sso\sthat\sit\scan\sbe\sappended\sto\sthe\samalgamation.\s\sCreate\sa\snew\namalgamation\sof\sheader\sfiles\sfor\suse\sby\sprojects\sthat\swant\sto\sredefine\stheir\nown\sI/O\sinterface\susing\s-DSQLITE_ENABLE_REDEF_IO.\s(CVS\s3817)
+D 2007-04-05T21:58:33
 F Makefile.in a9bd493360a2b17e5214b69d8c32dd6cdaceaf55
 F Makefile.linux-gcc 2d8574d1ba75f129aba2019f0b959db380a90935
 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
@@ -43,7 +43,7 @@ F ext/fts2/fts2_tokenizer.h 4c5ffe31d63622869eb6eec1503df7f6996fd1bd
 F ext/fts2/fts2_tokenizer1.c 6067f2f710bc7e91c0688b7b11be1027777553e0
 F install-sh 9d4de14ab9fb0facae2f48780b874848cbf2f895
 F ltmain.sh 56abb507100ed2d4261f6dd1653dec3cf4066387
-F main.mk 49c1b97b57d2a0091fbecee6feb9a992661ff05a
+F main.mk 2c20b3501860b36115c477a95a7f9355c755cf3b
 F mkdll.sh ed62756baf44babf562a7843588790c02fee2106
 F mkopcodec.awk bd46ad001c98dfbab07b1713cb8e692fa0e5415d
 F mkopcodeh.awk cde995d269aa06c94adbf6455bea0acedb913fa5
@@ -75,8 +75,8 @@ F src/legacy.c 2631df6a861f830d6b1c0fe92b9fdd745b2c0cd6
 F src/loadext.c 146fb9b9dc6133e763888d710205c32ebf8eeca2
 F src/main.c c8915777ae8e50823d01eefe2b674ef68c32bf61
 F src/md5.c c5fdfa5c2593eaee2e32a5ce6c6927c986eaf217
-F src/os.c 5a194b9b9a10dc66543d4ca22bc0db40c5cc5ef1
-F src/os.h 21db538bb674d53baa960ea8ec2332f1af799ed6
+F src/os.c 4650e98aadd27abfe1698ff58edf6893c58d4881
+F src/os.h 9240adf088fd55732f8926f9017601ba3430ead8
 F src/os_common.h 0969285cc1e0b1ccc7a2cd7ce8eb144176ce3369
 F src/os_os2.c 2ce97909b926a598823f97338027dbec1dcf4165
 F src/os_os2.h e5f17dd69333632bbc3112881ea407c37d245eb3
@@ -100,7 +100,7 @@ F src/sqlite.h.in e429f66f9245c7f8675db24b230c950b8672ad1c
 F src/sqlite3ext.h 7d0d363ea7327e817ef0dfe1b7eee1f171b72890
 F src/sqliteInt.h f09f449f266f71b236844e540bfce10e455ba6ab
 F src/table.c 6d0da66dde26ee75614ed8f584a1996467088d06
-F src/tclsqlite.c a8d1166319db5d505b25ac6a9820162afe63fc8a
+F src/tclsqlite.c 58fd67ba2c5a6ca816ad110029b6cb2a44a3c707
 F src/test1.c 6021cbcede9bfab986ade678efa7226180f3edff
 F src/test2.c 24458b17ab2f3c90cbc1c8446bd7ffe69be62f88
 F src/test3.c 65f92247cf8592854e9bf5115b3fb711f8b33280
@@ -385,7 +385,8 @@ F tool/memleak2.awk 9cc20c8e8f3c675efac71ea0721ee6874a1566e8
 F tool/memleak3.tcl 7707006ee908cffff210c98158788d85bb3fcdbf
 F tool/mkkeywordhash.c 520d0016eaac0d783ea8ffb7f91d150d0bf06e81
 F tool/mkopts.tcl 66ac10d240cc6e86abd37dc908d50382f84ff46e x
-F tool/mksqlite3c.tcl ea7a08c3a87e47031bcefccde4e641914d013a99
+F tool/mksqlite3c.tcl 2d204fc271b2e2a2139e360527dd845385c4dffa
+F tool/mksqlite3internalh.tcl a85bb0c812db1a060e6e6dfab4e4c817f53d194b
 F tool/omittest.tcl e6b3d6a1285f9813bc1dea53bb522b4b72774710
 F tool/opcodeDoc.awk b3a2a3d5d3075b8bd90b7afe24283efdd586659c
 F tool/report1.txt 9eae07f26a8fc53889b45fc833a66a33daa22816
@@ -450,7 +451,7 @@ F www/tclsqlite.tcl bb0d1357328a42b1993d78573e587c6dcbc964b9
 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
 F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b
 F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513
-P 7338e68e0fd9263236f12f8911cb8293747dd1a4
-R 2236cb31ece453878263b0b9238a99b5
+P 204e7d38aeb40d126b80a869958ca79cd0902d83
+R 33250e136d9f0af3693e864db67d12e7
 U drh
-Z a8b4f2b06486c0a181a8a4d0cf0b24c8
+Z d3104f68e1b1c0abe9a9e14724db1ec1
diff --git a/manifest.uuid b/manifest.uuid
index 820bcd8341..99c6c33130 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-204e7d38aeb40d126b80a869958ca79cd0902d83
\ No newline at end of file
+f2caff870c81c08ec85aae72986c231ee986b251
\ No newline at end of file
diff --git a/src/os.c b/src/os.c
index 77f5418645..bc65ae32b2 100644
--- a/src/os.c
+++ b/src/os.c
@@ -16,6 +16,7 @@
 #define _SQLITE_OS_C_ 1
 #include "sqliteInt.h"
 #include "os.h"
+#undef _SQLITE_OS_C_
 
 /*
 ** The following routines are convenience wrappers around methods
diff --git a/src/os.h b/src/os.h
index e7637fbca8..006ed5e131 100644
--- a/src/os.h
+++ b/src/os.h
@@ -447,9 +447,12 @@ struct sqlite3OsVtbl {
 #endif
 
 
-#ifdef _SQLITE_OS_C_
+#if defined(_SQLITE_OS_C_) || defined(SQLITE_AMALGAMATION)
   /*
   ** The os.c file implements the global virtual function table.
+  ** We have to put this file here because the initializers
+  ** (ex: sqlite3OsRandomSeed) are macros that are about to be
+  ** redefined.
   */
   struct sqlite3OsVtbl sqlite3Os = {
     IF_DISKIO( sqlite3OsOpenReadWrite ),
diff --git a/src/tclsqlite.c b/src/tclsqlite.c
index cc1f624f77..e7b2ac4677 100644
--- a/src/tclsqlite.c
+++ b/src/tclsqlite.c
@@ -9,19 +9,25 @@
 **    May you share freely, never taking more than you give.
 **
 *************************************************************************
-** A TCL Interface to SQLite
+** A TCL Interface to SQLite.  Append this file to sqlite3.c and
+** compile the whole thing to build a TCL-enabled version of SQLite.
 **
-** $Id: tclsqlite.c,v 1.177 2007/03/29 12:19:12 danielk1977 Exp $
+** $Id: tclsqlite.c,v 1.178 2007/04/05 21:58:33 drh Exp $
 */
-#ifndef NO_TCL     /* Omit this whole file if TCL is unavailable */
-
-#include "sqliteInt.h"
-#include "hash.h"
 #include "tcl.h"
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-#include <ctype.h>
+
+/*
+** Some additional include files are needed if this file is not
+** appended to the amalgamation.
+*/
+#ifndef SQLITE_AMALGAMATION
+# include "sqliteInt.h"
+# include "hash.h"
+# include <stdlib.h>
+# include <string.h>
+# include <assert.h>
+# include <ctype.h>
+#endif
 
 /*
  * Windows needs to know which symbols to export.  Unix does not.
@@ -2256,5 +2262,3 @@ int TCLSH_MAIN(int argc, char **argv){
   return 0;
 }
 #endif /* TCLSH */
-
-#endif /* !defined(NO_TCL) */
diff --git a/tool/mksqlite3c.tcl b/tool/mksqlite3c.tcl
index d58206bc62..8df131f024 100644
--- a/tool/mksqlite3c.tcl
+++ b/tool/mksqlite3c.tcl
@@ -63,7 +63,8 @@ puts $out [subst \
 ** SQLite library.
 **
 ** This amalgamation was generated on $today.
-*/}]
+*/
+#define SQLITE_AMALGAMATION 1}]
 
 # These are the header files used by SQLite.  The first time any of these 
 # files are seen in a #include statement in the C code, include the complete
diff --git a/tool/mksqlite3internalh.tcl b/tool/mksqlite3internalh.tcl
new file mode 100644
index 0000000000..6bc2e5bbac
--- /dev/null
+++ b/tool/mksqlite3internalh.tcl
@@ -0,0 +1,143 @@
+#!/usr/bin/tclsh
+#
+# To build a single huge source file holding all of SQLite (or at
+# least the core components - the test harness, shell, and TCL 
+# interface are omitted.) first do
+#
+#      make target_source
+#
+# The make target above moves all of the source code files into
+# a subdirectory named "tsrc".  (This script expects to find the files
+# there and will not work if they are not found.)  There are a few
+# generated C code files that are also added to the tsrc directory.
+# For example, the "parse.c" and "parse.h" files to implement the
+# the parser are derived from "parse.y" using lemon.  And the 
+# "keywordhash.h" files is generated by a program named "mkkeywordhash".
+#
+# After the "tsrc" directory has been created and populated, run
+# this script:
+#
+#      tclsh mksqlite3c.tcl
+#
+# The amalgamated SQLite code will be written into sqlite3.c
+#
+
+# Begin by reading the "sqlite3.h" header file.  Count the number of lines
+# in this file and extract the version number.  That information will be
+# needed in order to generate the header of the amalgamation.
+#
+set in [open tsrc/sqlite3.h]
+set cnt 0
+set VERSION ?????
+while {![eof $in]} {
+  set line [gets $in]
+  if {$line=="" && [eof $in]} break
+  incr cnt
+  regexp {#define\s+SQLITE_VERSION\s+"(.*)"} $line all VERSION
+}
+close $in
+
+# Open the output file and write a header comment at the beginning
+# of the file.
+#
+set out [open sqlite3internal.h w]
+set today [clock format [clock seconds] -format "%Y-%m-%d %H:%M:%S UTC" -gmt 1]
+puts $out [subst \
+{/******************************************************************************
+** This file is an amalgamation of many private header files from SQLite
+** version $VERSION. 
+*/}]
+
+# These are the header files used by SQLite.  The first time any of these 
+# files are seen in a #include statement in the C code, include the complete
+# text of the file in-line.  The file only needs to be included once.
+#
+foreach hdr {
+   btree.h
+   hash.h
+   keywordhash.h
+   opcodes.h
+   os_common.h
+   os.h
+   os_os2.h
+   pager.h
+   parse.h
+   sqlite3ext.h
+   sqlite3.h
+   sqliteInt.h
+   vdbe.h
+   vdbeInt.h
+} {
+  set available_hdr($hdr) 1
+}
+
+# 78 stars used for comment formatting.
+set s78 \
+{*****************************************************************************}
+
+# Insert a comment into the code
+#
+proc section_comment {text} {
+  global out s78
+  set n [string length $text]
+  set nstar [expr {60 - $n}]
+  set stars [string range $s78 0 $nstar]
+  puts $out "/************** $text $stars/"
+}
+
+# Read the source file named $filename and write it into the
+# sqlite3.c output file.  If any #include statements are seen,
+# process them approprately.
+#
+proc copy_file {filename} {
+  global seen_hdr available_hdr out
+  set tail [file tail $filename]
+  section_comment "Begin file $tail"
+  set in [open $filename r]
+  while {![eof $in]} {
+    set line [gets $in]
+    if {[regexp {^#\s*include\s+["<]([^">]+)[">]} $line all hdr]} {
+      if {[info exists available_hdr($hdr)]} {
+        if {$available_hdr($hdr)} {
+          section_comment "Include $hdr in the middle of $tail"
+          copy_file tsrc/$hdr
+          section_comment "Continuing where we left off in $tail"
+        }
+      } elseif {![info exists seen_hdr($hdr)]} {
+        set seen_hdr($hdr) 1
+        puts $out $line
+      }
+    } elseif {[regexp {^#ifdef __cplusplus} $line]} {
+      puts $out "#if 0"
+    } elseif {[regexp {^#line} $line]} {
+      # Skip #line directives.
+    } else {
+      puts $out $line
+    }
+  }
+  close $in
+  section_comment "End of $tail"
+}
+
+
+# Process the source files.  Process files containing commonly
+# used subroutines first in order to help the compiler find
+# inlining opportunities.
+#
+foreach file {
+   sqliteInt.h
+   sqlite3.h
+   btree.h
+   hash.h
+   os.h
+   pager.h
+   parse.h
+   sqlite3ext.h
+   vdbe.h
+} {
+  if {$available_hdr($file)} {
+    copy_file tsrc/$file
+  }
+}
+
+close $out