# This Makefile.am is in the public domain

plugindir = get_option('libdir') / 'anastasis'
sqldir = get_option('datadir') / 'anastasis' / 'sql'

install_data(
    'versioning.sql',
    'stasis-0001.sql',
    'stasis-0002.sql',
    'drop.sql',
    install_dir: sqldir,
)

# Stored procedures are not schema: they are dropped and recreated on every
# anastasis-dbinit, so fixing one does not need a new migration.  Keep the
# list in the same order as libanastasisdb_SOURCES.
procedures_sql = [
    'procedures-prelude.sql',
    'do_gc.sql',
    'do_insert_recdoc_payment.sql',
    'do_update_account_lifetime.sql',
    'commit.sql',
]

custom_target(
    'gen-stasis-procedures.sql',
    input: procedures_sql,
    output: 'procedures.sql',
    capture: true,
    command: ['./amalgamate-sql.sh', '@INPUT@'],
    install: true,
    install_dir: sqldir,
)

install_data('stasis-postgres.conf', install_dir: pkgcfgdir)

libanastasisdb_SOURCES = [
    'anastasis-db_pg.c',
    'preflight.c',
    'create_tables.c',
    'drop_tables.c',
    'do_gc.c',
    'get_challenge_payment.c',
    'get_recdoc_payment.c',
    'get_truth_payment.c',
    'insert_challenge_code.c',
    'get_truth.c',
    'get_truth_key_share.c',
    'get_last_auth_iban_in_row.c',
    'get_latest_recovery_document.c',
    'get_recovery_document.c',
    'iterate_recovery_meta_data.c',
    'get_account.c',
    'get_pending_challenge_payment.c',
    'update_to_challenge_code_satisfied.c',
    'update_totp_counter.c',
    'update_to_challenge_code_sent.c',
    'insert_auth_iban_in.c',
    'insert_challenge_payment.c',
    'update_to_challenge_payment_refunded.c',
    'do_insert_recdoc_payment.c',
    'insert_truth_payment.c',
    'do_insert_recovery_document.c',
    'insert_truth.c',
    'iterate_auth_iban_transfers.c',
    'get_exists_challenge_code_satisfied.c',
    'update_to_challenge_payment_paid.c',
    'do_update_account_lifetime.c',
    'do_verify_challenge_code.c',
]

libanastasisdb = library(
    'anastasisdb',
    libanastasisdb_SOURCES,
    soversion: solibversions['libanastasisdb']['soversion'],
    version: solibversions['libanastasisdb']['soversion'],
    install_rpath: rpath_option,
    dependencies: [
        gnunetutil_dep,
        gnunetpq_dep,
        pq_dep,
        talerpq_dep,
        talerutil_dep,
        libanastasisutil_dep,
    ],
    include_directories: [incdir, configuration_inc],
    install: true,
    install_dir: get_option('libdir'),
)

libanastasisdb_dep = declare_dependency(link_with: libanastasisdb)
pkg.generate(
    libanastasisdb,
    url: 'https://taler.net',
    description: 'GNU Taler anastasis DB utilities library',
)


executable(
    'anastasis-dbinit',
    ['anastasis-dbinit.c'],
    dependencies: [
        libanastasisutil_dep,
        libanastasisdb_dep,
        gnunetutil_dep,
        gcrypt_dep,
        talerutil_dep,
        talerpq_dep,
    ],
    include_directories: [incdir, configuration_inc],
    install: true,
)

test_anastasis_db_postgres = executable(
    'test_anastasis_db-postgres',
    ['test_anastasis_db.c'],
    dependencies: [
        libanastasisutil_dep,
        libanastasisdb_dep,
        gnunetutil_dep,
        talerutil_dep,
        talerpq_dep,
        gnunetpq_dep,
        json_dep,
    ],
    include_directories: [incdir, configuration_inc],
    build_by_default: false,
    install: false,
)
test(
    'test_anastasis_db_postgres',
    test_anastasis_db_postgres,
    workdir: meson.current_build_dir(),
    suite: ['stasis'],
    is_parallel: false,
)


configure_file(
    input: 'test_anastasis_db_postgres.conf',
    output: 'test_anastasis_db_postgres.conf',
    copy: true,
)


# Enforce the database layer naming convention (see contrib/check-db-naming.py
# and the rules it implements).  Needs no database, only the sources.
python3_bin = find_program('python3', required: false)
if python3_bin.found()
    test(
        'check_db_naming',
        python3_bin,
        args: [meson.project_source_root() / 'contrib' / 'check-db-naming.py'],
        workdir: meson.project_source_root(),
        suite: ['stasis'],
    )
endif
