# sqlite.sh

spf_template gnu &&
spf_url home "http://www.sqlite.org/" &&
spf_url watch "http://www.sqlite.org/download.html" &&
spf_url src "http://www.sqlite.org/sqlite-${spf_version?}.tar.gz" &&
spf_tested_version 3.5.4 &&
spf_tested_version 3.5.1 &&
spf_tested_version 3.5.0 &&
spf_tested_version 3.3.17 &&
spf_tested_version 3.3.8 &&
spf_depend code.dogmap.org:readline degree optional &&
spf_depend code.dogmap.org:ncurses degree optional &&
spf_depend tcl degree optional &&

# Patch up the configure script to correct linkage to optional dependencies.
# Modify the Makefile to enable loadable modules and fts3.
# http://www.sqlite.org/cvstrac/wiki?p=CompilingFts
spf_template_gnu_do_before_configure() {
  # Ensure that the SPF_BEGIN/SPF_END markers exist.
  prj_sedfile configure '/^# Generate the output files\.$/c\
# SPF_BEGIN\
# SPF_END\
\
#########\
# Generate the output files.  (This comment modified by spftools.)' &&
  prj_sedfile Makefile.in '/^# Object files for the SQLite library\.$/c\
# SPF_BEGIN\
# SPF_END\
\
# Object files for the SQLite library.  (This comment modified by spftools.)' &&
  prj_sedfile tool/mksqlite3c.tcl '/^close \$out$/c\
# SPF_BEGIN\
# SPF_END\
\
close $out ;# This comment added by spftools.' &&

  # Wipe any text appearing between the SPF_BEGIN/SPF_END markers.
  prj_sedfile configure '/^# SPF_BEGIN$/,/^# SPF_END$/c\
# SPF_BEGIN\
# SPF_END' &&
  prj_sedfile Makefile.in '/^# SPF_BEGIN$/,/^# SPF_END$/c\
# SPF_BEGIN\
# SPF_END' &&
  prj_sedfile tool/mksqlite3c.tcl '/^# SPF_BEGIN$/,/^# SPF_END$/c\
# SPF_BEGIN\
# SPF_END' &&

  # Handle the optional readline dependency.
  if test -d "${spf_path_conf?}"/readline/.; then
    # Allow the readline test to pass without resorting to /usr.
    prj_sedfile configure 's:<readline\.h>:<readline/readline.h>:' &&

    # Look in the correct place for the readline headers.
    prj_sedfile configure "/^# SPF_END$/i\\
TARGET_READLINE_INC='-I${spf_path_conf?}/readline/include'\\
TARGET_READLINE_LIBS='-L${spf_path_conf?}/readline/library -Xlinker -R -Xlinker ${spf_path_conf?}/readline/library -lreadline -L${spf_path_conf?}/ncurses/library -Xlinker -R -Xlinker ${spf_path_conf?}/ncurses/library -lncurses'"
  else :; fi &&

  # Handle the optional Tcl dependency.
  if test -d "${spf_path_conf?}"/tcl/.; then
    # Look in the right place for the tclConfig.sh script.
    spf_args configure --with-tcl="${spf_path_conf?}"/tcl/library &&

    # Determine the Tcl version.
    eval `grep '^TCL_VERSION=' "${spf_path_conf?}"/tcl/library/tclConfig.sh` &&

    # Correct the Tcl linkage.
    prj_sedfile configure "/^# SPF_END$/i\\
TCL_BIN_DIR='${spf_path_conf?}/tcl/library'\\
TCL_LIB_SPEC='-L${spf_path_conf?}/tcl/library -ltcl${TCL_VERSION?}'\\
TCL_STUB_LIB_SPEC='-L${spf_path_conf?}/tcl/library -ltclstub${TCL_VERSION?}'"
  else :; fi &&

  # Enable fts3.
  prj_sedfile Makefile.in '/^# SPF_END$/i\
LTLINK += -ldl\
TCC := $(filter-out -DSQLITE_OMIT_LOAD_EXTENSION=1,$(TCC))\
TCC += -DSQLITE_CORE=1 -DSQLITE_ENABLE_FTS3=1 -DSQLITE_SOUNDEX=1 -Iext/fts3\
LIBOBJ := $(patsubst %.c,%.lo,$(notdir $(wildcard $(TOP)/ext/fts3/*.c)))\
SRC := $(wildcard $(TOP)/ext/fts3/*.c) $(wildcard $(TOP)/ext/fts3/*.h)\
HDR := $(wildcard $(TOP)/ext/fts3/*.h)\
fts%.lo: $(TOP)/ext/fts3/fts%.c $(HDR)\
	$(LTCOMPILE) -c $<' &&
  prj_sedfile Makefile.in '/^\(LIBOBJ\|SRC\|HDR\) =/s/=/+=/' &&
  prj_sedfile tool/mksqlite3c.tcl '/^# SPF_END$/i\
foreach file [glob -nocomplain tsrc/fts3*.h] {\
  set available_hdr([file tail $file]) 1\
}\
foreach file [glob -nocomplain tsrc/fts3*.c] {\
  copy_file $file\
}' &&

  # Get rid of fts1 leftovers.
  prj_sedfile Makefile.in '/^# Source code for extensions$/,/^$/d' &&
  prj_sedfile Makefile.in '/^# Header files used by extensions$/,/^$/d'
} &&

# Correct a bug (?) preventing sqlite3 from being a loadable Tcl package.
spf_template_gnu_do_before_install() {
  prj_sedfile Makefile '/tclinstaller/s/VERSION/RELEASE/'
} &&

# Install the Tcl sqlite library to the correct location.
spf_template_gnu_do_install() {
  make install TCLLIBPATH="${spf_path_prefix?}"/lib
} &&

spf_no_check

# vim: set ts=2 sts=2 sw=2 tw=80 et ft=sh:
