{% set version = "3.33.0" %}
{% set year = "2020" %}
{% set version_split = version.split(".") %}
{% set major = version_split[0] %}
{% set minor = version_split[1]|int * 10 %}
{% set bugfix = version_split[2]|int * 100 %}
{% set version_coded=(major ~ (("%03d" % minor)|string) ~ (("%03d" % bugfix)|string)) %}

# If you use this option, then this recipe proceeds exactly as per-usual, except
# we get to use the new CVE patches that are generated if you ..
{% set patchset = 'amalgamated' %}

# .. use this option instead and run the build on a unix machine (preferably Linux)
# {% set patchset = 'rebased' %}  # [unix]
# {% set patchset = 'amalgamated' %}  # [not unix]
#
# I am sorry about the complexity of this recipe. We had been following the upstream
# developer's recommendations about using the amalgamated sources but that's proven
# to be impractical from a CVE-patching perspective as it leaves us exposed to risk
# due to the length of time between releases. This switches to a fully-from-source
# build on Unix which generates a patch that can be used on an amalgamated build
# on Windows (or any other OS). Typicaly it is expected that patch will contain any
# CVE and build system changes we've made.
#
# build.sh writes patches/applicable-to-amalgamated/0000-sqlite3-c-amalgamated.patch.tmp
# It is expected the maintainer checks this and if it looks good, overwrites
# patches/applicable-to-amalgamated/0000-sqlite3-c-amalgamated.patch with it and
# adds it as part of each update PR.
#
# The alternative is that we employ MSYS2 packages so we can do everything on Windows.
# That is probably moderately difficult, but if anyone is really concerned I (Ray) will
# try to find time.
#
# To make things worse, upstream uses fossil for their source control, and their
# documentation on how to export that to a git repo causes git to crash. Someone
# created a project to work around that crash, but it also does not work:
# https://repo.or.cz/sqlite-export.git
# .. we still get a segfault after a promising start.
# Luckily the upstream devs do provide a source mirror on github so that's what is
# used for any build that doesn't set patchset = 'amalgamated'. The pachset = 'fossil'
# stuff is not something I expect to maintian and I am happy to delete it. The only
# value it has is incase the upstream developers stop providing a github mirror.
#

package:
  name: sqlite
  version: {{ version }}

source:
  # - url: https://www.sqlite.org/2020/sqlite-src-3310100.zip
  #   sha256: f2dc2382855d99a960c363c1e5ae72b49da4c55d49154aa6d100e5970a1fee58
  # - url: https://www.sqlite.org/{{ year }}/sqlite-autoconf-{{ version_coded }}.tar.gz
  # sha256: 62284efebc05a76f909c580ffa5c008a7d22a1287285d68b7825a2b6b51949ae
{% if patchset != 'amalgamated' %}
  - git_url: https://github.com/sqlite/sqlite
    git_tag: version-{{ version }}
    folder: src
{% endif %}
{% if patchset == 'rebased' %}
    patches:
{% endif %}
{% if patchset == 'github' %}
    patches:
{% endif %}
{% if patchset == 'fossil' %}
    patches:
{% endif %}
{% if patchset == 'amalgamated' or patchset == 'rebased' %}
  - url: https://www.sqlite.org/{{ year }}/sqlite-autoconf-{{ version_coded }}.tar.gz
    sha256: 106a2c48c7f75a298a7557bcc0d5f4f454e5b43811cc738b7ca294d6956bbb15
    folder: amalgamated
{% if patchset == 'amalgamated' %}
    patches:
      - patches/applicable-to-amalgamated/0001-expose_symbols.patch
{% endif %}
{% endif %}

build:
  number: 0
  run_exports:
    # sometimes adds new symbols.  Default behavior is OK.
    #    https://abi-laboratory.pro/tracker/timeline/sqlite/
    - {{ pin_subpackage('sqlite') }}

requirements:
  build:
    - {{ compiler('c') }}
    # Not entirely sure this is needed.
    - tk  # [not win]
    - autoconf  # [not win]
    - libtool  # [not win]
    - make  # [not win]
    - patch  # [not win]
    - m2-patch  # [win]
    - m2-gcc-libs  # [win]
  host:
    - ncurses  # [not win]
    - libedit  # [not win]
    - zlib

test:
  commands:
    - sqlite3 --version
    - if not exist %PREFIX%\\Library\bin\sqlite3.dll exit 1    # [win]
    - if not exist %PREFIX%\\Library\lib\sqlite3.lib exit 1    # [win]
    - test -f $PREFIX/lib/libsqlite3${SHLIB_EXT}  # [not win]
    - if not exist %PREFIX%\\Library\include\sqlite3.h exit 1  # [win]
    - test -f $PREFIX/include/sqlite3.h                        # [not win]

about:
  home: http://www.sqlite.org/
  license: Public-Domain (http://www.sqlite.org/copyright.html)
  license_url: http://www.sqlite.org/copyright.html
  summary: 'Implements a self-contained, zero-configuration, SQL database engine'
  description: |
    SQLite is a self-contained, high-reliability, embedded, full-featured,
    public-domain, SQL database engine.It is the most used database engine
    in the world.
  doc_url: http://www.sqlite.org/docs.html
  doc_source_url: https://github.com/mackyle/sqlite/tree/master/doc
  dev_url: https://github.com/mackyle/sqlite

extra:
  recipe-maintainers:
    - jakirkham
    - jjhelmus
    - msarahan
    - ocefpaf
    - snorfalorpagus
    - mingwandroid
